diff options
author | Gabriel <adam.krysiak@nokia.com> | 2018-06-25 08:25:06 +0200 |
---|---|---|
committer | Gabriel <adam.krysiak@nokia.com> | 2018-07-03 07:53:03 +0200 |
commit | 2dc06bd93935107a84934d3ff4f3d4209160e68b (patch) | |
tree | 590ede87091641076e013ad84724a9c8750aa20a /src/test | |
parent | 21ebe86d82cb7e7700e45df9873ef5ca1834fc76 (diff) |
cleaned up clds service code
Change-Id: Id1688515986d96b35f4cc654a38d662b3d491240
Issue-ID: CLAMP-178
Signed-off-by: Gabriel <adam.krysiak@nokia.com>
Diffstat (limited to 'src/test')
6 files changed, 194 insertions, 117 deletions
diff --git a/src/test/java/org/onap/clamp/clds/client/req/OperationalPolicyReqTest.java b/src/test/java/org/onap/clamp/clds/client/req/OperationalPolicyReqTest.java deleted file mode 100644 index dac8705e8..000000000 --- a/src/test/java/org/onap/clamp/clds/client/req/OperationalPolicyReqTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END============================================ - * =================================================================== - * - */ - -package org.onap.clamp.clds.client.req; - -import static org.junit.Assert.assertTrue; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -import org.junit.Test; -import org.onap.clamp.clds.client.req.policy.OperationalPolicyReq; -import org.onap.policy.controlloop.policy.PolicyResult; -import org.onap.policy.sdc.Resource; -import org.onap.policy.sdc.ResourceType; - -public class OperationalPolicyReqTest { - - @Test - public void convertToResourceTest() throws NoSuchMethodException, SecurityException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException { - Method method = OperationalPolicyReq.class.getDeclaredMethod("convertToResource", List.class, - ResourceType.class); - method.setAccessible(true); - // return method.invoke(targetObject, argObjects); - List<String> stringList = new ArrayList<>(); - stringList.add("test1"); - stringList.add("test2"); - stringList.add("test3"); - stringList.add("test4"); - Resource[] resources = (Resource[]) method.invoke(null, stringList, ResourceType.VF); - - assertTrue(resources.length == 4); - assertTrue("test1".equals(resources[0].getResourceName())); - assertTrue("test2".equals(resources[1].getResourceName())); - assertTrue("test3".equals(resources[2].getResourceName())); - assertTrue("test4".equals(resources[3].getResourceName())); - } - - @Test - public void convertToPolicyResultTest() throws NoSuchMethodException, SecurityException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException { - Method method = OperationalPolicyReq.class.getDeclaredMethod("convertToPolicyResult", List.class); - method.setAccessible(true); - // return method.invoke(targetObject, argObjects); - List<String> stringList = new ArrayList<>(); - stringList.add("FAILURE"); - stringList.add("SUCCESS"); - stringList.add("FAILURE_GUARD"); - stringList.add("FAILURE_TIMEOUT"); - PolicyResult[] policyResult = (PolicyResult[]) method.invoke(null, stringList); - - assertTrue(policyResult.length == 4); - assertTrue(policyResult[0].equals(PolicyResult.FAILURE)); - assertTrue(policyResult[1].equals(PolicyResult.SUCCESS)); - assertTrue(policyResult[2].equals(PolicyResult.FAILURE_GUARD)); - assertTrue(policyResult[3].equals(PolicyResult.FAILURE_TIMEOUT)); - } -} diff --git a/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReqTest.java b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReqTest.java new file mode 100644 index 000000000..d5e3069b2 --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReqTest.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.client.req.policy; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.sdc.Resource; +import org.onap.policy.sdc.ResourceType; + +public class OperationalPolicyReqTest { + + @Test + public void shouldConvertGivenStringsToResourceObjects() + throws NoSuchMethodException, SecurityException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException { + + //given + List<String> stringList = Arrays.asList("test1", "test2", "test3", "test4"); + + //when + Resource[] resources = OperationalPolicyReq.convertToResource(stringList, ResourceType.VF); + + //then + assertThat(resources).extracting(Resource::getResourceName) + .containsExactly("test1", "test2", "test3", "test4"); + } + + @Test + public void shouldConvertGivenStringsToPolicyResults() + throws NoSuchMethodException, SecurityException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException { + //given + List<String> stringList = Arrays.asList("FAILURE", "SUCCESS", "FAILURE_GUARD", "FAILURE_TIMEOUT"); + + //when + PolicyResult[] policyResults = OperationalPolicyReq.convertToPolicyResult(stringList); + + //then + assertThat(policyResults) + .containsExactly(PolicyResult.FAILURE, PolicyResult.SUCCESS, + PolicyResult.FAILURE_GUARD, PolicyResult.FAILURE_TIMEOUT); + } +} diff --git a/src/test/java/org/onap/clamp/clds/config/CldsUserJsonDecoderTest.java b/src/test/java/org/onap/clamp/clds/config/CldsUserJsonDecoderTest.java index ec5c196b9..c04357ded 100644 --- a/src/test/java/org/onap/clamp/clds/config/CldsUserJsonDecoderTest.java +++ b/src/test/java/org/onap/clamp/clds/config/CldsUserJsonDecoderTest.java @@ -17,15 +17,18 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END============================================ + * Modifications copyright (c) 2018 Nokia * =================================================================== * */ package org.onap.clamp.clds.config; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import java.util.Collections; import org.junit.Test; import org.onap.clamp.clds.service.CldsUser; @@ -47,34 +50,49 @@ public class CldsUserJsonDecoderTest { @Test public void testDecodingDoubleUsers() { + + //when CldsUser[] usersArray = CldsUserJsonDecoder - .decodeJson(CldsUserJsonDecoderTest.class.getResourceAsStream("/clds/clds-users-two-users.json")); - assertEquals(usersArray.length, 2); - assertEquals(usersArray[0].getUser(), user1); - assertEquals(usersArray[1].getUser(), user2); - assertEquals(usersArray[0].getPassword(), password); - assertEquals(usersArray[1].getPassword(), password); - assertArrayEquals(usersArray[0].getPermissionsString(), normalPermissionsArray); - assertArrayEquals(usersArray[1].getPermissionsString(), normalPermissionsArray); + .decodeJson(CldsUserJsonDecoderTest.class.getResourceAsStream("/clds/clds-users-two-users.json")); + + //then + assertThat(usersArray).hasSize(2); + assertThat(usersArray[0]) + .extracting(CldsUser::getUser, CldsUser::getPassword, CldsUser::getPermissionsString) + .containsExactly(user1, password, normalPermissionsArray); + + assertThat(usersArray[1]) + .extracting(CldsUser::getUser, CldsUser::getPassword, CldsUser::getPermissionsString) + .containsExactly(user2, password, normalPermissionsArray); + } @Test public void testDecodingNoPermission() { + // when CldsUser[] usersArray = CldsUserJsonDecoder .decodeJson(this.getClass().getResourceAsStream("/clds/clds-users-no-permission.json")); - assertEquals(usersArray.length, 1); - assertEquals(usersArray[0].getUser(), user1); - assertEquals(usersArray[0].getPassword(), null); - assertArrayEquals(usersArray[0].getPermissionsString(), new String[0]); + + //then + assertThat(usersArray).hasSize(1); + CldsUser user = usersArray[0]; + assertThat(user.getUser()).isEqualTo(user1); + assertThat(user.getPassword()).isEqualTo(null); + assertThat(user.getPermissionsString()).isEmpty(); } @Test public void testDecodingIncompletePermissions() { + + //when CldsUser[] usersArray = CldsUserJsonDecoder .decodeJson(this.getClass().getResourceAsStream("/clds/clds-users-incomplete-permissions.json")); - assertEquals(usersArray.length, 1); - assertEquals(usersArray[0].getUser(), user1); - assertEquals(usersArray[0].getPassword(), password); - assertArrayEquals(usersArray[0].getPermissionsString(), incompletePermissionsArray); + + //then + assertThat(usersArray).hasSize(1); + CldsUser user = usersArray[0]; + assertThat(user.getUser()).isEqualTo(user1); + assertThat(user.getPassword()).isEqualTo(password); + assertThat(user.getPermissionsString()).isEqualTo(incompletePermissionsArray); } } diff --git a/src/test/java/org/onap/clamp/clds/config/sdc/SdcSingleControllerConfigurationTest.java b/src/test/java/org/onap/clamp/clds/config/sdc/SdcSingleControllerConfigurationTest.java index 00d5dffbb..1a1621271 100644 --- a/src/test/java/org/onap/clamp/clds/config/sdc/SdcSingleControllerConfigurationTest.java +++ b/src/test/java/org/onap/clamp/clds/config/sdc/SdcSingleControllerConfigurationTest.java @@ -16,10 +16,14 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * Modifications copyright (c) 2018 Nokia + * ================================================================================ + * */ package org.onap.clamp.clds.config.sdc; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -43,7 +47,7 @@ import org.onap.clamp.clds.util.ResourceFileUtil; public class SdcSingleControllerConfigurationTest { private SdcSingleControllerConfiguration loadControllerConfiguration(String fileName, String sdcControllerName) - throws JsonParseException, JsonMappingException, IOException { + throws IOException { JsonNode jsonNode = new ObjectMapper().readValue(ResourceFileUtil.getResourceAsStream(fileName), JsonNode.class); SdcSingleControllerConfiguration sdcSingleControllerConfiguration = new SdcSingleControllerConfiguration( @@ -63,17 +67,16 @@ public class SdcSingleControllerConfigurationTest { assertEquals("hostname:8080", sdcConfig.getAsdcAddress()); assertEquals(10, sdcConfig.getPollingInterval()); assertEquals(30, sdcConfig.getPollingTimeout()); - assertEquals(SdcSingleControllerConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST.size(), - sdcConfig.getRelevantArtifactTypes().size()); - assertTrue(sdcConfig.activateServerTLSAuth()); + + assertThat(SdcSingleControllerConfiguration.SUPPORTED_ARTIFACT_TYPES_LIST) + .hasSameSizeAs(sdcConfig.getRelevantArtifactTypes()); assertEquals("ThePassword", sdcConfig.getKeyStorePassword()); - assertArrayEquals(new String[] { - "localhost" - }, sdcConfig.getMsgBusAddress().toArray()); + assertTrue(sdcConfig.activateServerTLSAuth()); + assertThat(sdcConfig.getMsgBusAddress()).contains("localhost"); } @Test(expected = SdcParametersException.class) - public final void testAllRequiredParameters() throws JsonParseException, JsonMappingException, IOException { + public final void testAllRequiredParameters() throws IOException { SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration("clds/sdc-controller-config-TLS.json", "sdc-controller1"); // No exception should be raised @@ -84,7 +87,7 @@ public class SdcSingleControllerConfigurationTest { @Test public final void testAllRequiredParametersEmptyEncrypted() - throws JsonParseException, JsonMappingException, IOException { + throws IOException { SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration( "clds/sdc-controller-config-empty-encrypted.json", "sdc-controller1"); sdcConfig.testAllRequiredParameters(); @@ -92,7 +95,7 @@ public class SdcSingleControllerConfigurationTest { } @Test - public final void testConsumerGroupWithNull() throws JsonParseException, JsonMappingException, IOException { + public final void testConsumerGroupWithNull() throws IOException { SdcSingleControllerConfiguration sdcConfig = loadControllerConfiguration("clds/sdc-controller-config-NULL.json", "sdc-controller1"); assertTrue(sdcConfig.getConsumerGroup() == null); diff --git a/src/test/java/org/onap/clamp/clds/model/CldsDbServiceCacheTest.java b/src/test/java/org/onap/clamp/clds/model/CldsDbServiceCacheTest.java index 36563a25a..3fe29dc0f 100644 --- a/src/test/java/org/onap/clamp/clds/model/CldsDbServiceCacheTest.java +++ b/src/test/java/org/onap/clamp/clds/model/CldsDbServiceCacheTest.java @@ -17,12 +17,14 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END============================================ + * Modifications copyright (c) 2018 Nokia * =================================================================== * */ package org.onap.clamp.clds.model; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -30,38 +32,46 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.util.ArrayList; import java.util.List; - import org.junit.Test; public class CldsDbServiceCacheTest { @Test public void testConstructor() throws IOException, ClassNotFoundException { + // given CldsServiceData cldsServiceData = new CldsServiceData(); cldsServiceData.setServiceUUID("testUUID"); - cldsServiceData.setAgeOfRecord(Long.valueOf(100)); + cldsServiceData.setAgeOfRecord(100L); cldsServiceData.setServiceInvariantUUID("testInvariantUUID"); + CldsVfData cldsVfData = new CldsVfData(); cldsVfData.setVfName("vf"); + CldsVfKPIData cldsVfKpiData = new CldsVfKPIData(); cldsVfKpiData.setFieldPath("fieldPath"); cldsVfKpiData.setFieldPathValue("fieldValue"); + List<CldsVfKPIData> cldsKpiList = new ArrayList<>(); cldsKpiList.add(cldsVfKpiData); cldsVfData.setCldsKPIList(cldsKpiList); + List<CldsVfData> cldsVfs = new ArrayList<>(); cldsVfs.add(cldsVfData); cldsServiceData.setCldsVfs(cldsVfs); + CldsDbServiceCache cldsDbServiceCache = new CldsDbServiceCache(cldsServiceData); + + // when ObjectInputStream reader = new ObjectInputStream(cldsDbServiceCache.getCldsDataInstream()); CldsServiceData cldsServiceDataResult = (CldsServiceData) reader.readObject(); - assertNotNull(cldsServiceDataResult); - assertNotNull(cldsServiceDataResult.getCldsVfs()); - assertEquals(cldsServiceDataResult.getCldsVfs().size(), 1); - assertNotNull(cldsServiceDataResult.getCldsVfs().get(0).getCldsKPIList()); - assertEquals(cldsServiceDataResult.getCldsVfs().get(0).getCldsKPIList().size(), 1); - assertEquals(cldsServiceDataResult.getServiceInvariantUUID(), "testInvariantUUID"); - assertEquals(cldsServiceDataResult.getServiceUUID(), "testUUID"); - assertEquals(cldsServiceDataResult.getAgeOfRecord(), Long.valueOf(100L)); + + // then + assertThat(cldsServiceDataResult).isNotNull(); + assertThat(cldsServiceDataResult.getCldsVfs()).hasSize(1); + assertThat(cldsServiceDataResult.getCldsVfs().get(0).getCldsKPIList()).hasSize(1); + + assertThat(cldsServiceDataResult.getServiceInvariantUUID()).isEqualTo("testInvariantUUID"); + assertThat(cldsServiceDataResult.getServiceUUID()).isEqualTo("testUUID"); + assertThat(cldsServiceDataResult.getAgeOfRecord()).isEqualTo(100L); } } diff --git a/src/test/java/org/onap/clamp/clds/service/CldsInfoProviderTest.java b/src/test/java/org/onap/clamp/clds/service/CldsInfoProviderTest.java new file mode 100644 index 000000000..d5f7c15b6 --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/service/CldsInfoProviderTest.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2018 Nokia Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.onap.clamp.clds.model.CldsInfo; + + +public class CldsInfoProviderTest { + + private static final String TEST_USERNAME = "TEST_USERNAME"; + + @Test + public void shouldProvideCldsInfoFromContext() throws Exception { + + // given + CldsService serviceBase = mock(CldsService.class); + when(serviceBase.getUserName()).thenReturn(TEST_USERNAME); + when(serviceBase.isAuthorizedNoException(any())).thenReturn(true); + CldsInfoProvider cldsInfoProvider = new CldsInfoProvider(serviceBase); + + // when + CldsInfo cldsInfo = cldsInfoProvider.getCldsInfo(); + + // then + assertThat(cldsInfo.getUserName()).isEqualTo(TEST_USERNAME); + assertThat(cldsInfo.isPermissionReadCl()).isTrue(); + assertThat(cldsInfo.isPermissionReadTemplate()).isTrue(); + assertThat(cldsInfo.isPermissionUpdateCl()).isTrue(); + assertThat(cldsInfo.isPermissionUpdateTemplate()).isTrue(); + } +}
\ No newline at end of file |