diff options
Diffstat (limited to 'vid-app-common/src/test')
26 files changed, 2084 insertions, 1054 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/AAITreeConverterTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/AAITreeConverterTest.java index 8bcadd139..5bdfd1207 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/AAITreeConverterTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/AAITreeConverterTest.java @@ -18,7 +18,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.onap.vid.aai; import com.google.common.collect.ImmutableList; diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/OperationalEnvironmentTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/OperationalEnvironmentTest.java index 76cd21391..7f5e362ba 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/OperationalEnvironmentTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/OperationalEnvironmentTest.java @@ -3,13 +3,14 @@ * VID * ================================================================================ * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nokia. 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. @@ -20,190 +21,43 @@ package org.onap.vid.aai; -import org.junit.Test; -import org.onap.vid.aai.model.RelationshipList; -public class OperationalEnvironmentTest { - - private OperationalEnvironment createTestSubject() { - return new OperationalEnvironment(); - } - - @Test - public void testGetOperationalEnvironmentId() throws Exception { - OperationalEnvironment testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getOperationalEnvironmentId(); - } - - @Test - public void testSetOperationalEnvironmentId() throws Exception { - OperationalEnvironment testSubject; - String operationalEnvironmentId = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setJsonOperationalEnvironmentId(operationalEnvironmentId); - } - - @Test - public void testGetOperationalEnvironmentName() throws Exception { - OperationalEnvironment testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getOperationalEnvironmentName(); - } - - @Test - public void testSetOperationalEnvironmentName() throws Exception { - OperationalEnvironment testSubject; - String operationalEnvironmentName = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setJsonOperationalEnvironmentName(operationalEnvironmentName); - } - - @Test - public void testGetOperationalEnvironmentType() throws Exception { - OperationalEnvironment testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getOperationalEnvironmentType(); - } - - @Test - public void testSetOperationalEnvironmentType() throws Exception { - OperationalEnvironment testSubject; - String operationalEnvironmentType = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setJsonOperationalEnvironmentType(operationalEnvironmentType); - } - - @Test - public void testGetOperationalEnvironmentStatus() throws Exception { - OperationalEnvironment testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getOperationalEnvironmentStatus(); - } - - @Test - public void testSetOperationalEnvironmentStatus() throws Exception { - OperationalEnvironment testSubject; - String operationalEnvironmentStatus = ""; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.testng.annotations.Test; - // default test - testSubject = createTestSubject(); - testSubject.setJsonOperationalEnvironmentStatus(operationalEnvironmentStatus); - } +import java.io.IOException; - @Test - public void testGetTenantContext() throws Exception { - OperationalEnvironment testSubject; - String result; +import static org.assertj.core.api.Assertions.assertThat; - // default test - testSubject = createTestSubject(); - result = testSubject.getTenantContext(); - } - - @Test - public void testSetTenantContext() throws Exception { - OperationalEnvironment testSubject; - String tenantContext = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setJsonTenantContext(tenantContext); - } - - @Test - public void testGetWorkloadContext() throws Exception { - OperationalEnvironment testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getWorkloadContext(); - } - - @Test - public void testSetWorkloadContext() throws Exception { - OperationalEnvironment testSubject; - String workloadContext = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setJsonWorkloadContext(workloadContext); - } - - @Test - public void testGetResourceVersion() throws Exception { - OperationalEnvironment testSubject; - String result; - // default test - testSubject = createTestSubject(); - result = testSubject.getResourceVersion(); - } - - @Test - public void testSetResourceVersion() throws Exception { - OperationalEnvironment testSubject; - String resourceVersion = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setJsonResourceVersion(resourceVersion); - } - - @Test - public void testGetRelationshipList() throws Exception { - OperationalEnvironment testSubject; - RelationshipList result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getRelationshipList(); - } - - @Test - public void testSetRelationshipList() throws Exception { - OperationalEnvironment testSubject; - RelationshipList relationshipList = null; - - // default test - testSubject = createTestSubject(); - testSubject.setJsonRelationshipList(relationshipList); - } - - @Test - public void testOperationalEnvironment() throws Exception { - OperationalEnvironment testSubject; - String operationalEnvironmentId = ""; - String operationalEnvironmentName = ""; - String operationalEnvironmentType = ""; - String operationalEnvironmentStatus = ""; - String tenantContext = ""; - String workloadContext = ""; - String resourceVersion = ""; - RelationshipList relationshipList = null; +public class OperationalEnvironmentTest { - // default test - testSubject = new OperationalEnvironment(operationalEnvironmentId, operationalEnvironmentName, - operationalEnvironmentType, operationalEnvironmentStatus, tenantContext, workloadContext, - resourceVersion, relationshipList); + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + private static final String OPERATIONAL_ENVIRONMENT_TEST = "{\n" + + "\"operational-environment-id\": \"environmentId\",\n" + + "\"operational-environment-name\": \"environmentName\",\n" + + "\"operational-environment-type\": \"environmentType\",\n" + + "\"operational-environment-status\": \"environmentStatus\",\n" + + "\"tenant-context\": \"tenantContext\",\n" + + "\"workload-context\": \"workloadContext\",\n" + + "\"resource-version\": \"resourceVersion\",\n" + + "\"relationship-list\": {\n" + + "\"relationship\": []\n" + + "}\n" + + "}"; + + @Test + public void shouldProperlyConvertJsonToOperationalEnvironment() throws IOException { + OperationalEnvironment operationalEnvironment = OBJECT_MAPPER.readValue(OPERATIONAL_ENVIRONMENT_TEST, OperationalEnvironment.class); + + assertThat(operationalEnvironment.getOperationalEnvironmentId()).isEqualTo("environmentId"); + assertThat(operationalEnvironment.getWorkloadContext()).isEqualTo("workloadContext"); + assertThat(operationalEnvironment.getRelationshipList().getRelationship()).hasSize(0); + assertThat(operationalEnvironment.getResourceVersion()).isEqualTo("resourceVersion"); + assertThat(operationalEnvironment.getTenantContext()).isEqualTo("tenantContext"); + assertThat(operationalEnvironment.getOperationalEnvironmentType()).isEqualTo("environmentType"); + assertThat(operationalEnvironment.getOperationalEnvironmentStatus()).isEqualTo("environmentStatus"); + assertThat(operationalEnvironment.getOperationalEnvironmentName()).isEqualTo("environmentName"); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/PombaClientImplTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/PombaClientImplTest.java index 222bf1e5b..c41a479d9 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/PombaClientImplTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/PombaClientImplTest.java @@ -19,6 +19,7 @@ */ package org.onap.vid.aai; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; @@ -60,6 +61,20 @@ public class PombaClientImplTest { then(pombaRestInterface).should().RestPost("VidAaiController", expectedUrl, expectedPayload); } + @Test + public void should_handleException_withoutRethrowing() throws IOException { + //Given + String expectedUrl = "http://localhost/dummyUrl"; + String expectedPayload = readExpectedPombaJsonRequest(); + given(systemPropertiesWrapper.getProperty("pomba.server.url")).willReturn(expectedUrl); + given(pombaRestInterface.RestPost("VidAaiController", expectedUrl, expectedPayload)) + .willThrow(new NullPointerException()); + PombaRequest pombaRequest = createPombaRequest(); + + //When //Then + assertThatCode(() -> pombaClient.verify(pombaRequest)).doesNotThrowAnyException(); + } + private String readExpectedPombaJsonRequest() throws IOException { URL url = PombaClientImplTest.class.getClassLoader().getResource("pomba_request.json"); PombaRequest expectedPombaRequest = new ObjectMapper().readValue(url, PombaRequest.class); diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/ServicePropertiesTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/ServicePropertiesTest.java index 6ff766413..4c0875320 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/ServicePropertiesTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/ServicePropertiesTest.java @@ -7,9 +7,9 @@ * 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. @@ -20,35 +20,76 @@ package org.onap.vid.aai; -import java.util.Map; +import java.io.IOException; -import org.junit.Test; +import com.fasterxml.jackson.databind.ObjectMapper; import org.onap.vid.aai.model.ServiceProperties; +import org.testng.annotations.Test; + +import static org.assertj.core.api.Assertions.assertThat; public class ServicePropertiesTest { - private ServiceProperties createTestSubject() { - return new ServiceProperties(); - } - - @Test - public void testGetAdditionalProperties() throws Exception { - ServiceProperties testSubject; - Map<String, Object> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getAdditionalProperties(); - } - - @Test - public void testSetAdditionalProperty() throws Exception { - ServiceProperties testSubject; - String name = ""; - Object value = null; - - // default test - testSubject = createTestSubject(); - testSubject.setAdditionalProperty(name, value); - } + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + private static final String SERVICE_PROPERTIES_JSON = "{\n" + + "\"service-instance-id\": \"instanceId\",\n" + + "\"service-instance-name\": \"instanceName\",\n" + + "\"model-invariant-id\": \"invariantId\",\n" + + "\"model-version-id\": \"versionId\",\n" + + "\"resource-version\": \"version\",\n" + + "\"orchestration-status\": \"status\",\n" + + "\"global-customer-id\": \"customerId\",\n" + + "\"subscriber-name\": \"subscriberName\",\n" + + "\"subscriber-type\": \"subscriberType\",\n" + + "\"vnf-id\": \"vnfId\",\n" + + "\"vnf-name\": \"vnfName\",\n" + + "\"vnf-type\": \"vnfType\",\n" + + "\"service-id\": \"serviceId\",\n" + + "\"prov-status\": \"provStatus\",\n" + + "\"in-maint\": false,\n" + + "\"is-closed-loop-disabled\": false,\n" + + "\"model-customization-id\": \"customizationId\",\n" + + "\"nf-type\": \"nfType\",\n" + + "\"nf-function\": \"nfFunction\",\n" + + "\"nf-role\": \"nfRole\",\n" + + "\"nf-naming-code\": \"namingCode\",\n" + + "\"not-listed-property\":\"value\"}"+ + "}"; + + + @Test + public void shouldProperlyConvertJsonToServiceProperties() throws IOException { + ServiceProperties serviceProperties = OBJECT_MAPPER.readValue(SERVICE_PROPERTIES_JSON, ServiceProperties.class); + + + assertThat(serviceProperties.isClosedLoopDisabled).isFalse(); + assertThat(serviceProperties.globalCustomerId).isEqualTo("customerId"); + assertThat(serviceProperties.inMaint).isFalse(); + assertThat(serviceProperties.modelCustomizationId).isEqualTo("customizationId"); + assertThat(serviceProperties.modelInvariantId).isEqualTo("invariantId"); + assertThat(serviceProperties.modelVersionId).isEqualTo("versionId"); + assertThat(serviceProperties.nfRole).isEqualTo("nfRole"); + assertThat(serviceProperties.nfFunction).isEqualTo("nfFunction"); + assertThat(serviceProperties.nfType).isEqualTo("nfType"); + assertThat(serviceProperties.nfNamingCode).isEqualTo("namingCode"); + assertThat(serviceProperties.serviceId).isEqualTo("serviceId"); + assertThat(serviceProperties.serviceInstanceName).isEqualTo("instanceName"); + assertThat(serviceProperties.serviceInstanceId).isEqualTo("instanceId"); + assertThat(serviceProperties.provStatus).isEqualTo("provStatus"); + assertThat(serviceProperties.vnfId).isEqualTo("vnfId"); + assertThat(serviceProperties.vnfName).isEqualTo("vnfName"); + assertThat(serviceProperties.vnfType).isEqualTo("vnfType"); + } + + @Test + public void shouldProperlyAddAdditionalProperty() throws IOException { + ServiceProperties serviceProperties = OBJECT_MAPPER.readValue(SERVICE_PROPERTIES_JSON, ServiceProperties.class); + + + serviceProperties.setAdditionalProperty("additional", "property"); + + assertThat(serviceProperties.getAdditionalProperties()) + .containsOnlyKeys("not-listed-property","additional") + .containsValues("value","property"); + } } diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberWithFilterTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberWithFilterTest.java index 2842b08dc..c690b5fa0 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberWithFilterTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberWithFilterTest.java @@ -7,9 +7,9 @@ * 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. @@ -20,31 +20,15 @@ package org.onap.vid.aai; -import org.junit.Test; -public class SubscriberWithFilterTest { - - private SubscriberWithFilter createTestSubject() { - return new SubscriberWithFilter(); - } - - @Test - public void testGetIsPermitted() throws Exception { - SubscriberWithFilter testSubject; - boolean result; +import org.testng.annotations.Test; - // default test - testSubject = createTestSubject(); - result = testSubject.getIsPermitted(); - } +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static org.hamcrest.MatcherAssert.assertThat; - @Test - public void testSetIsPermitted() throws Exception { - SubscriberWithFilter testSubject; - boolean isPermitted = false; - - // default test - testSubject = createTestSubject(); - testSubject.setIsPermitted(isPermitted); - } +public class SubscriberWithFilterTest { + @Test + public void shouldHaveValidGettersAndSetters() { + assertThat(SubscriberWithFilter.class, hasValidGettersAndSetters()); + } } diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/VnfResultTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/VnfResultTest.java index 6f403d9da..7ee3b6152 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/VnfResultTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/VnfResultTest.java @@ -7,9 +7,9 @@ * 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. @@ -20,34 +20,64 @@ package org.onap.vid.aai; -import org.junit.Test; +import com.fasterxml.jackson.databind.ObjectMapper; import org.onap.vid.aai.model.VnfResult; +import org.testng.annotations.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; -import java.util.Map; public class VnfResultTest { - private VnfResult createTestSubject() { - return new VnfResult(); - } - - @Test - public void testGetAdditionalProperties() throws Exception { - VnfResult testSubject; - Map<String, Object> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getAdditionalProperties(); - } - - @Test - public void testSetAdditionalProperty() throws Exception { - VnfResult testSubject; - String name = ""; - Object value = null; - - // default test - testSubject = createTestSubject(); - } + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + private static final String VNF_RESULT_JSON = "{\n" + + "\"id\": \"sample\",\n" + + "\"node-type\": \"sampleNodeType\",\n" + + "\"url\": \"sample\",\n" + + "\"properties\": {\n" + + "\"service-instance-id\": \"sample\",\n" + + "\"service-instance-name\": \"sample\",\n" + + "\"model-invariant-id\": \"sample\",\n" + + "\"model-version-id\": \"sample\",\n" + + "\"resource-version\": \"sample\",\n" + + "\"orchestration-status\": \"sample\",\n" + + "\"global-customer-id\": \"sample\",\n" + + "\"subscriber-name\": \"sample\",\n" + + "\"subscriber-type\": \"sample\",\n" + + "\"vnf-id\": \"sample\",\n" + + "\"vnf-name\": \"sample\",\n" + + "\"vnf-type\": \"sample\",\n" + + "\"service-id\": \"sample\",\n" + + "\"prov-status\": \"sample\",\n" + + "\"in-maint\": false,\n" + + "\"is-closed-loop-disabled\": false,\n" + + "\"model-customization-id\": \"sample\",\n" + + "\"nf-type\": \"sample\",\n" + + "\"nf-function\": \"sample\",\n" + + "\"nf-role\": \"sample\",\n" + + "\"nf-naming-code\": \"sample\"\n" + + "},\n" + + "\"related-to\": [{\n" + + "\"id\": \"sample\",\n" + + "\"relationship-label\": \"sample\",\n" + + "\"node-type\": \"sample\",\n" + + "\"url\": \"sample\"\n" + + "\t}]\n" + + "}"; + + + @Test + public void shouldProperlyConvertJsonToVnfResult() throws IOException { + VnfResult vnfResult = OBJECT_MAPPER.readValue(VNF_RESULT_JSON, VnfResult.class); + + assertThat(vnfResult.nodeType).isEqualTo("sampleNodeType"); + assertThat(vnfResult.id).isEqualTo("sample"); + assertThat(vnfResult.url).isEqualTo("sample"); + assertThat(vnfResult.relatedTo).hasSize(1); + assertThat(vnfResult.properties.globalCustomerId).isEqualTo("sample"); + assertThat(vnfResult.getAdditionalProperties()).isEmpty(); + } } diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/model/RelationshipListTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/model/RelationshipListTest.java new file mode 100644 index 000000000..cc57171b8 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/aai/model/RelationshipListTest.java @@ -0,0 +1,35 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2019 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.vid.aai.model; + + +import org.testng.annotations.Test; + +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static org.hamcrest.MatcherAssert.assertThat; + +public class RelationshipListTest { + + @Test + public void shouldHaveValidGettersAndSetters() { + assertThat(RelationshipList.class, hasValidGettersAndSetters()); + } +} diff --git a/vid-app-common/src/test/java/org/onap/vid/job/command/InstanceGroupCommandTest.java b/vid-app-common/src/test/java/org/onap/vid/job/command/InstanceGroupCommandTest.java index 47ec44ca7..b4a5c64f5 100644 --- a/vid-app-common/src/test/java/org/onap/vid/job/command/InstanceGroupCommandTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/job/command/InstanceGroupCommandTest.java @@ -20,8 +20,14 @@ package org.onap.vid.job.command; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.same; +import static org.mockito.Mockito.verify; + import com.google.common.collect.ImmutableMap; -import org.apache.commons.beanutils.BeanUtils; +import java.util.Optional; import org.mockito.Answers; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -32,19 +38,11 @@ import org.onap.vid.model.serviceInstantiation.InstanceGroup; import org.onap.vid.mso.RestMsoImplementation; import org.onap.vid.mso.model.ModelInfo; import org.onap.vid.services.AsyncInstantiationBusinessLogic; +import org.onap.vid.testUtils.TestUtils; import org.springframework.http.HttpMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.Optional; -import java.util.Set; - -import static java.util.function.Function.identity; -import static java.util.stream.Collectors.toMap; -import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; - public class InstanceGroupCommandTest { @Mock(answer = Answers.RETURNS_MOCKS) @@ -73,7 +71,7 @@ public class InstanceGroupCommandTest { @Test public void createMyself_callsMso() { - final ModelInfo serviceModelInfo = setRandomStrings(new ModelInfo()); + final ModelInfo serviceModelInfo = setStrings(new ModelInfo()); final String serviceInstanceId = "service-instance-id"; final String userId = "ff3223"; @@ -92,14 +90,7 @@ public class InstanceGroupCommandTest { } - private ModelInfo setRandomStrings(ModelInfo object) { - try { - Set<String> fields = BeanUtils.describe(object).keySet(); - BeanUtils.populate(object, - fields.stream().collect(toMap(identity(), s -> randomAlphanumeric(4)))); - return object; - } catch (Exception e) { - throw new RuntimeException(e); - } + private ModelInfo setStrings(ModelInfo object) { + return TestUtils.setStringsInStringProperties(object); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/RestMsoImplementationTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/RestMsoImplementationTest.java new file mode 100644 index 000000000..4cba53785 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/mso/RestMsoImplementationTest.java @@ -0,0 +1,421 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2019 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.vid.mso; + +import io.joshworks.restclient.request.HttpRequest; +import org.glassfish.jersey.client.JerseyInvocation; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.aai.util.HttpsAuthClient; +import org.onap.vid.changeManagement.RequestDetailsWrapper; +import org.onap.vid.exceptions.GenericUncheckedException; +import org.onap.vid.mso.rest.RequestDetails; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; + +import java.util.Optional; + +import static org.assertj.core.api.Java6Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +public class RestMsoImplementationTest { + + @Mock + private HttpRequest httpRequest; + + @Mock + private Client mockClient; + + @Mock + private HttpsAuthClient mockHttpsAuthClient; + + @Mock + private WebTarget webTarget; + + @Mock + private javax.ws.rs.client.Invocation.Builder builder; + + @Mock + private Response response; + + @Mock + private JerseyInvocation jerseyInvocation; + + @InjectMocks + private RestMsoImplementation restMsoImplementation = new RestMsoImplementation(mockHttpsAuthClient); + + String path = "/test_path/"; + String rawData = "test-row-data"; + + @BeforeClass + public void setUp(){ + initMocks(this); + } + + @Test + public void shouldProperlyInitMsoClient() { + // when + MultivaluedHashMap<String, Object> result = restMsoImplementation.initMsoClient(); + + // then + assertThat(result).containsKeys("Authorization","X-ONAP-PartnerName"); + assertThat(result).doesNotContainKey("notExistingKey"); + } + + @Test + public void shouldProperlyGetRestObjectWithRequestInfo() { + // given + RestObject<HttpRequest> restObject = new RestObject<>(); + + prepareMocks(rawData, HttpStatus.ACCEPTED.value(),""); + + // when + RestObjectWithRequestInfo<HttpRequest> response = restMsoImplementation.Get(httpRequest, path, restObject,false); + + // then + assertThat(response.getRequestedUrl()).contains(path); + assertThat(response.getRawData()).isEqualTo(rawData); + assertThat(response.getHttpCode()).isEqualTo(HttpStatus.ACCEPTED.value()); + assertThat(response.getHttpMethod()).isEqualTo(HttpMethod.GET); + } + + @Test( expectedExceptions = GenericUncheckedException.class) + public void shouldThrowExceptionWhenGetRestObjectWithRequestInfoGetsWrongStatus() { + // given + RestObject<HttpRequest> restObject = new RestObject<>(); + + prepareMocks("",HttpStatus.BAD_REQUEST.value(),""); + + // when + restMsoImplementation.Get(httpRequest, "", restObject,false); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenGetRestObjectWithRequestInfoGetsWrongParameters() { + // given + RestObject<HttpRequest> restObject = new RestObject<>(); + + prepareMocks("",HttpStatus.ACCEPTED.value(),""); + when(mockClient.target(SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL))).thenThrow(new MsoTestException("test-target-exception")); + + // when + restMsoImplementation.Get(httpRequest, "", restObject,false); + } + + @Test() + public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndAcceptCode() { + // given + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),""); + + // when + RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class); + + // then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED.value()); + assertThat(response.getRaw()).isEqualTo(rawData); + } + + @Test() + public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndBadRequestCode() { + // given + prepareMocks(rawData,HttpStatus.BAD_REQUEST.value(),""); + + // when + RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class); + + // then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + assertThat(response.getRaw()).isEqualTo(rawData); + } + + @Test() + public void shouldProperlyDeleteRestObjectWithStatusHttpAccepted() { + // given + RestObject<HttpRequest> restObject = new RestObject<>(); + + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE"); + + // when + restMsoImplementation.Delete(httpRequest, "testObject", path, restObject); + + // then + assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED.value()); + } + + @Test() + public void shouldProperlyDeleteRestObjectWithStatusOK() { + // given + RestObject<HttpRequest> restObject = new RestObject<>(); + + prepareMocks(rawData,HttpStatus.OK.value(),"DELETE"); + + // when + restMsoImplementation.Delete(httpRequest, "testObject", path, restObject); + + // then + assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.OK.value()); + } + + @Test() + public void shouldProperlyReturnFromDeleteWithStatusBadRequest() { + // given + RestObject<HttpRequest> restObject = new RestObject<>(); + + prepareMocks(rawData,HttpStatus.BAD_REQUEST.value(),"DELETE"); + + // when + restMsoImplementation.Delete(httpRequest, "testObject", path, restObject); + + // then + assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + } + + @Test() + public void shouldProperlyReturnFromDeleteWithStatusOtherThenAbove() { + // given + RestObject<HttpRequest> restObject = new RestObject<>(); + prepareMocks(rawData,HttpStatus.NOT_EXTENDED.value(),"DELETE"); + + // when + restMsoImplementation.Delete(httpRequest, "testObject", path, restObject); + + // then + assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.NOT_EXTENDED.value()); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenCallsDeleteWithWrongParameters() { + // given + when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testDeleteException")); + + // when + restMsoImplementation.Delete(httpRequest, "testObject", "", null); + } + + @Test( expectedExceptions = NullPointerException.class) + public void shouldThrowExceptionWhenCallsDeleteWithWrongObjectType() { + // given + RestObject<HttpRequest> restObject = new RestObject<>(); + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE"); + + // when + restMsoImplementation.Delete(null, "testObject", path, restObject); + } + + @Test + public void shouldProperlyPostForObject() { + // given + RequestDetails requestDetails = new RequestDetails(); + + RestObject<HttpRequest> expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value()); + expectedResponse.setRaw(rawData); + + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"POST"); + + // when + RestObject<HttpRequest> response = restMsoImplementation.PostForObject(requestDetails, path, HttpRequest.class); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteForObject() { + // given + RequestDetails requestDetails = new RequestDetails(); + + RestObject<HttpRequest> expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value()); + expectedResponse.setRaw(rawData); + + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE"); + + // when + RestObject<HttpRequest> response = restMsoImplementation.DeleteForObject(requestDetails, path, HttpRequest.class); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyPost() { + // given + RequestDetails requestDetails = new RequestDetails(); + RestObject<String> response = new RestObject<>(); + + RestObject<String> expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value()); + expectedResponse.setRaw(rawData); + + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"POST"); + + // when + restMsoImplementation.Post(rawData,requestDetails, path, response); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyPrepareClient() { + // given + String method = "POST"; + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method); + + // when + javax.ws.rs.client.Invocation.Builder response = restMsoImplementation.prepareClient(path, method); + + // then + assertThat(response).isEqualTo(builder); + } + + @Test + public void shouldCreatRestObjectOnlyWithHttpMethod() { + // given + String method = "GET"; + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method); + + RestObject<String> expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value()); + expectedResponse.setRaw(rawData); + + // when + RestObject<String> response = restMsoImplementation.restCall(HttpMethod.GET, String.class, null, path, Optional.empty()); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenCreateRestObjectIsCalledWithoutDefinedClient() { + // given + when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testNoClientException")); + + // when + restMsoImplementation.restCall(HttpMethod.GET, String.class, null, "", Optional.empty()); + } + + @Test + public void shouldProperlyPutRestObjectWithProperParametersAndStatusAccepted() { + // given + String method = "PUT"; + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method); + + org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); + RestObject<String> response = new RestObject<>(); + + RestObject<String> expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value()); + expectedResponse.set(rawData); + + // when + restMsoImplementation.Put("testPutBody", requestDetailsWrapper , path, response); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyPutRestObjectWithProperParametersAndStatusMultipleChoices() { + // given + String method = "PUT"; + prepareMocks(rawData,HttpStatus.MULTIPLE_CHOICES.value(),method); + + org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); + RestObject<String> response = new RestObject<>(); + + RestObject<String> expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.MULTIPLE_CHOICES.value()); + expectedResponse.set(rawData); + + // when + restMsoImplementation.Put("testPutBody", requestDetailsWrapper , path, response); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenCallsPutWithWrongParameters() { + // given + when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testDeleteException")); + org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); + + // when + restMsoImplementation.Put(null, requestDetailsWrapper, "", null); + } + + @Test( expectedExceptions = NullPointerException.class) + public void shouldThrowExceptionWhenCallsPutWithWrongObjectType() { + // given + RestObject<HttpRequest> restObject = new RestObject<>(); + org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); + + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE"); + + // when + restMsoImplementation.Put(null, requestDetailsWrapper, path, restObject); + } + + + + private void prepareMocks(String rawData,int status,String httpMethod) { + + when(mockClient.target(any(String.class))).thenReturn(webTarget); + when(webTarget.request()).thenReturn(builder); + + + when(builder.accept(any(String.class))).thenReturn(builder); + when(builder.headers(any(MultivaluedMap.class))).thenReturn(builder); + when(builder.get()).thenReturn(response); + + when(builder.build( eq(httpMethod), any(Entity.class))).thenReturn(jerseyInvocation); + when(builder.build( eq(httpMethod))).thenReturn(jerseyInvocation); + + when(builder.put( any(Entity.class))).thenReturn(response); + when(jerseyInvocation.invoke()).thenReturn(response); + + + when(response.getStatus()).thenReturn(status); + when(response.readEntity(String.class)).thenReturn(rawData); + } + + private class MsoTestException extends RuntimeException{ + MsoTestException(String testException) { + super(testException); + } + } + +}
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/InstanceIdsTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/InstanceIdsTest.java new file mode 100644 index 000000000..e40bab6c1 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/InstanceIdsTest.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2019 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.vid.mso.rest; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEqualsExcluding; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + +public class InstanceIdsTest { + + private InstanceIds instanceIds; + + private String propertyName = "testProperty"; + private String additionalProperty = "testAdditionalProperty"; + private String networkInstanceId = "testNetworkId"; + private String serviceInstanceId = "testServiceId"; + + @BeforeMethod + public void setUp() { + instanceIds = new InstanceIds(); + } + + @Test + public void shouldHaveProperSettersAndGetters() { + assertThat(InstanceIds.class, hasValidGettersAndSettersExcluding("additionalProperties")); + } + + @Test + public void shouldHaveProperGetterAndSetterForAdditionalProperties() { + // when + instanceIds.setAdditionalProperty(propertyName,additionalProperty); + + // then + assertThat( instanceIds.getAdditionalProperties().get(propertyName) ).isEqualTo(additionalProperty); + } + + @Test + public void shouldProperlyConvertRelatedInstanceObjectToString() { + // given + instanceIds.setNetworkInstanceId(networkInstanceId); + instanceIds.setServiceInstanceId(serviceInstanceId); + instanceIds.setAdditionalProperty(propertyName,additionalProperty); + + // when + String response = instanceIds.toString(); + + // then + assertThat(response).contains( + "[networkInstanceId="+networkInstanceId + + ",serviceInstanceId="+serviceInstanceId + + ",vfModuleInstanceId=<null>," + + "vnfInstanceId=<null>" + + ",volumeGroupInstanceId=<null>" + + ",additionalProperties={"+propertyName+"="+additionalProperty+"}]" + ); + } + + @Test + public void shouldProperlyCheckIfObjectsAreEqual() { + assertThat(InstanceIds.class, hasValidBeanEqualsExcluding("additionalProperties")); + } +} diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java index 6114552e6..12fd4a519 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java @@ -20,8 +20,18 @@ */ package org.onap.vid.mso.rest; +import static org.onap.vid.controller.MsoController.SVC_INSTANCE_ID; +import static org.onap.vid.controller.MsoController.VNF_INSTANCE_ID; + import com.fasterxml.jackson.databind.ObjectMapper; import com.xebialabs.restito.server.StubServer; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Properties; +import java.util.UUID; import org.glassfish.grizzly.http.util.HttpStatus; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -37,17 +47,6 @@ import org.onap.vid.mso.MsoResponseWrapperInterface; import org.onap.vid.mso.RestObject; import org.springframework.test.context.ContextConfiguration; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Properties; -import java.util.UUID; - -import static org.onap.vid.controller.MsoController.SVC_INSTANCE_ID; -import static org.onap.vid.controller.MsoController.VNF_INSTANCE_ID; - @ContextConfiguration(classes = {SystemProperties.class}) public class MsoRestClientNewTest { @@ -280,16 +279,39 @@ public class MsoRestClientNewTest { } @Test - public void testGetManualTasks() { + public void testGetManualTasksByRequestId() { String p = props.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQ); String path = p + "/" + UUID.randomUUID(); + String validResponse = "" + + "{ " + + " \"taskList\": [ " + + " { " + + " \"taskId\": \"daf4dd84-b77a-42da-a051-3239b7a9392c\", " + + " \"type\": \"fallout\", " + + " \"nfRole\": \"vEsmeralda\", " + + " \"subscriptionServiceType\": \"Emanuel\", " + + " \"originalRequestId\": \"d352c70d-5ef8-4977-9ea8-4c8cbe860422\", " + + " \"originalRequestorId\": \"ss835w\", " + + " \"errorSource\": \"A&AI\", " + + " \"errorCode\": \"404\", " + + " \"errorMessage\": \"Failed in A&AI 404\", " + + " \"validResponses\": [ " + + " \"rollback\", " + + " \"abort\", " + + " \"skip\", " + + " \"resume\", " + + " \"retry\" " + + " ] " + + " } " + + " ] " + + "}"; try(MsoRestClientTestUtil closure = new MsoRestClientTestUtil( server, path, HttpStatus.OK_200, - CREATE_INSTANCE_RESPONSE_STR,CREATE_INSTANCE_RESPONSE_STR)) { - closure.executeGet(msoRestClient()::getManualTasks); + validResponse,validResponse)) { + closure.executeGet(endpoint -> msoRestClient().getManualTasksByRequestId(null, null, endpoint, null)); } } @@ -311,23 +333,6 @@ public class MsoRestClientNewTest { } @Test - public void testGetManualTasksByRequestId() throws Exception { - MsoRestClientNew testSubject; - String t = ""; - String sourceId = ""; - String endpoint = ""; - RestObject restObject = null; - MsoResponseWrapper result; - - // default test - try { - testSubject = createTestSubject(); - result = testSubject.getManualTasksByRequestId(t, sourceId, endpoint, restObject); - } catch (Exception e) { - } - } - - @Test public void testCompleteManualTask() throws Exception { MsoRestClientNew testSubject; RequestDetails requestDetails = null; @@ -468,10 +473,10 @@ public class MsoRestClientNewTest { private MsoRestClientNew msoRestClient() { final WebConfig webConfig = new WebConfig(); - return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl()); + return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl(),null); } private MsoRestClientNew createTestSubject() { - return new MsoRestClientNew(null, ""); + return new MsoRestClientNew(null, "",null); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java index 5a0f93b45..ff71d85fc 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java @@ -20,21 +20,33 @@ */ package org.onap.vid.mso.rest; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.json.JSONObject; -import org.junit.Assert; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import io.joshworks.restclient.http.HttpResponse; +import io.joshworks.restclient.http.JsonMapper; +import org.apache.http.ProtocolVersion; +import org.apache.http.StatusLine; +import org.apache.http.message.BasicHttpResponse; +import org.apache.http.message.BasicStatusLine; +import org.mockito.Mock; import org.onap.portalsdk.core.util.SystemProperties; -import org.onap.vid.changeManagement.RequestDetails; +import org.onap.vid.changeManagement.RequestDetailsWrapper; import org.onap.vid.client.SyncRestClient; import org.onap.vid.controller.LocalWebConfig; -import org.onap.vid.mso.MsoBusinessLogic; -import org.onap.vid.mso.MsoBusinessLogicImpl; -import org.onap.vid.mso.model.CloudConfiguration; -import org.onap.vid.mso.model.ModelInfo; -import org.onap.vid.mso.model.RequestInfo; -import org.onap.vid.mso.model.RequestParameters; +import org.onap.vid.model.RequestReferencesContainer; +import org.onap.vid.mso.MsoResponseWrapper; +import org.onap.vid.mso.MsoResponseWrapperInterface; +import org.onap.vid.mso.MsoUtil; +import org.onap.vid.mso.RestObject; +import org.onap.vid.mso.model.RequestReferences; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.web.WebAppConfiguration; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -43,51 +55,720 @@ import org.testng.annotations.Test; public class MsoRestClientTest { - private MsoBusinessLogic msoBusinessLogic = new MsoBusinessLogicImpl(new MsoRestClientNew(new SyncRestClient(), ""), null); - private ObjectMapper om = new ObjectMapper(); + private final String baseUrl = "http://testURL/"; + + @Mock + private SyncRestClient client; + + private MsoRestClientNew restClient; + + + @BeforeClass + private void setUp(){ + initMocks(this); + restClient = new MsoRestClientNew(client,baseUrl,null); + + } @Test - public void createInPlaceMsoRequest() { - String result = null; - try { - RequestDetails requestDetails = generateMockMsoRequest(); - result = om.writeValueAsString(msoBusinessLogic.generateInPlaceMsoRequest(requestDetails)); - } catch (Exception e) { - e.printStackTrace(); - } - if (result == null) { - Assert.fail("Failed to create mso request"); + public void shouldProperlyCreateServiceInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createSvcInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenCreateSvcInstanceGetsWrongParameters() { + // given + String endpoint = ""; + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(null),eq(String.class) ) ). + thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.createSvcInstance(null,endpoint); + } + + @Test + public void shouldProperlyCreateE2eSvcInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createE2eSvcInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyCreateVnf() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createVnf(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyCreateNwInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createNwInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyCreateVolumeGroupInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createVolumeGroupInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyCreateVfModuleInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createVfModuleInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyScaleOutVFModuleInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + RequestDetailsWrapper<RequestDetails> wrappedRequestDetails = new RequestDetailsWrapper<>(requestDetails); + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.scaleOutVFModuleInstance(wrappedRequestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyCreateConfigurationInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + org.onap.vid.mso.rest.RequestDetailsWrapper wrappedRequestDetails = new org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails); + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createConfigurationInstance(wrappedRequestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteE2eSvcInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + org.onap.vid.mso.rest.RequestDetailsWrapper wrappedRequestDetails = new org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails); + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteE2eSvcInstance(wrappedRequestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteSvcInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteSvcInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyUnassignSvcInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.unassignSvcInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteVnf() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteVnf(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteVfModule() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteVfModule(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteVolumeGroupInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteVolumeGroupInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteNwInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteNwInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyGetOrchestrationRequest() { + // given + RestObject restObject = generateMockMsoRestObject(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.getOrchestrationRequest(null,null,endpoint,restObject,true); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyGetOrchestrationRequestWithOnlyEndpoint() { + // given + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.getOrchestrationRequest(endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyGetManualTasksByRequestId() { + // given + RestObject restObject = generateMockMsoRestObject(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.getManualTasksByRequestId(null,null,endpoint,restObject); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test(expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenGetManualTasksByRequestIdGetsWrongParameter() { + // given + when( client.get( eq(baseUrl),anyMap(),anyMap(),eq(String.class) ) ).thenThrow(new MsoTestException("testsException")); + + // when + restClient.getManualTasksByRequestId(null,null,"",null); + } + + @Test + public void shouldProperlyCompleteManualTask() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + RestObject restObject = generateMockMsoRestObject(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint), anyMap(), eq(requestDetails), eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.completeManualTask(requestDetails,null,null,endpoint,restObject); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test(expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenCompleteManualTaskWrongParameter() { + // given + when( client.post( eq(baseUrl),anyMap(),eq(null), eq(String.class) ) ).thenThrow(new MsoTestException("testsException")); + + // when + restClient.completeManualTask(null, null,null,"",null); + } + + @Test + public void shouldProperlyReplaceVnf() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyReplaceVnfWithStatus202() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = createOkResponse(); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenReplaceVnfGetsWrongParameters() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest(); + + when( client.post( eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.replaceVnf(requestDetails, ""); + } + + @Test + public void shouldProperlyDeleteConfiguration() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest(); + org.onap.vid.mso.rest.RequestDetailsWrapper requestDetailsWrapper = new org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteConfiguration(requestDetailsWrapper,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test( expectedExceptions = MsoTestException.class ) + public void shouldThrowExceptionWhenProperlyDeleteConfigurationGetsWrongParameters() { + // given + when( client.delete( eq(baseUrl), anyMap(), eq(null), eq(String.class) ) ).thenThrow(new MsoTestException("test-delete-exception")); + + // when + restClient.deleteConfiguration(null,""); + } + + @Test + public void shouldProperlySetConfigurationActiveStatus() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.setConfigurationActiveStatus(requestDetails, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test(expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenSetConfigurationActiveStatusGetsWrongParameters() { + // given + + when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.setConfigurationActiveStatus(null, ""); + } + + @Test + public void shouldProperlySetPortOnConfigurationStatus() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.setPortOnConfigurationStatus(requestDetails, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test(expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenSetPortOnConfigurationStatusGetsWrongParameters() { + // given + String endpoint = ""; + + when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.setPortOnConfigurationStatus(null, endpoint); + } + + @Test + public void shouldProperlyChangeManagementUpdate() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails); + + String endpoint = "testEndpoint"; + HttpResponse<RequestReferencesContainer> httpResponse = HttpResponse.fallback( + new RequestReferencesContainer( + new RequestReferences())); + + MsoResponseWrapperInterface expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(RequestReferencesContainer.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapperInterface response = restClient.changeManagementUpdate(requestDetailsWrapper, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test + public void shouldProperlyUpdateVnfAndUpdateInstance() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapperInterface expectedResponse = MsoUtil.wrapResponse(httpResponse); + + + when(client.put(eq(baseUrl + endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapperInterface response = restClient.updateVnf(requestDetails, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test( expectedExceptions = MsoTestException.class ) + public void shouldThrowExceptionWhenUpdateVnfAndUpdateInstanceGetsWrongParameter() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = MsoRestClientTestUtil.generateChangeManagementMockMsoRequest(); + String endpoint = ""; + + when(client.put(eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.updateVnf(requestDetails, endpoint); + + // then + } + + @Test + public void shouldProperlySetServiceInstanceStatus() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + RestObject<String> restObject = generateMockMsoRestObject(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); + + // when + restClient.setServiceInstanceStatus(requestDetails,"", "", endpoint, restObject); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenSetServiceInstanceStatusGetsWrongParameter() { + // given + String endpoint = ""; + + when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.setServiceInstanceStatus(null,"", "", endpoint, null); + } + + @Test + public void shouldProperlyRemoveRelationshipFromServiceInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.removeRelationshipFromServiceInstance(requestDetails, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenRemoveRelationshipFromServiceInstanceGetsWrongParameter() { + // given + String endpoint = ""; + + when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.removeRelationshipFromServiceInstance(null,endpoint); + } + + @Test + public void shouldProperlyAddRelationshipToServiceInstance() { + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse<String> httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.addRelationshipToServiceInstance(requestDetails, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenAddRelationshipToServiceInstanceGetsWrongParameter() { + // given + String endpoint = ""; + + when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.addRelationshipToServiceInstance(null,endpoint); + } + + @Test + public void shouldProperlyPerformGetRequest() { + // given + String endpoint = "testEndpoint"; + HttpResponse<String> expectedResponse = HttpResponse.fallback("testOkResponse"); + + when(client.get(eq(baseUrl + endpoint), anyMap(), anyMap(), eq(String.class))).thenReturn(expectedResponse); + + // when + HttpResponse<String> response = restClient.get(endpoint, String.class); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test + public void shouldProperlyPerformPostRequest() { + // given + + RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(MsoRestClientTestUtil.generateMockMsoRequest()); + + String endpoint = "testEndpoint"; + HttpResponse<String> expectedResponse = HttpResponse.fallback("testOkResponse"); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class))).thenReturn(expectedResponse); + + // when + HttpResponse<String> response = restClient.post(endpoint,requestDetailsWrapper, String.class); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + private class MsoTestException extends RuntimeException{ + MsoTestException(String testException) { + super(testException); } - JSONObject jsonObj = new JSONObject(result); - Assert.assertNotNull(jsonObj.getJSONObject("requestDetails")); - } - - private RequestDetails generateMockMsoRequest() { - RequestDetails requestDetails = new RequestDetails(); - requestDetails.setVnfInstanceId("vnf-instance-id"); - requestDetails.setVnfName("vnf-name"); - CloudConfiguration cloudConfiguration = new CloudConfiguration(); - cloudConfiguration.setTenantId("tenant-id"); - cloudConfiguration.setLcpCloudRegionId("lcp-region"); - requestDetails.setCloudConfiguration(cloudConfiguration); - ModelInfo modelInfo = new ModelInfo(); - modelInfo.setModelInvariantId("model-invarient-id"); - modelInfo.setModelCustomizationName("modelCustomizationName"); - requestDetails.setModelInfo(modelInfo); - RequestInfo requestInfo = new RequestInfo(); - requestInfo.setRequestorId("ok883e"); - requestInfo.setSource("VID"); - requestDetails.setRequestInfo(requestInfo); - RequestParameters requestParameters = new RequestParameters(); - requestParameters.setSubscriptionServiceType("subscriber-service-type"); - requestParameters.setAdditionalProperty("a", 1); - requestParameters.setAdditionalProperty("b", 2); - requestParameters.setAdditionalProperty("c", 3); - requestParameters.setAdditionalProperty("d", 4); - String payload = "{\"existing_software_version\": \"3.1\",\"new_software_version\": \"3.2\", \"operations_timeout\": \"3600\"}"; - requestParameters.setAdditionalProperty("payload", payload); - - requestDetails.setRequestParameters(requestParameters); - return requestDetails; + } + + private HttpResponse<String> createOkResponse() { + StatusLine statusline = new BasicStatusLine( + new ProtocolVersion("http",1,1), 202, "acceptResponse"); + + org.apache.http.HttpResponse responseBase = new BasicHttpResponse(statusline); + + return new HttpResponse<>(responseBase ,String.class, new JsonMapper()); + } + + private RestObject<String> generateMockMsoRestObject() { + RestObject<String> restObject = new RestObject<>(); + + restObject.set("test-rest-object-body"); + restObject.setRaw("test-rest-object-raw-string"); + restObject.setStatusCode(202); + return restObject; } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTestUtil.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTestUtil.java index 2a607b511..f10a7f14f 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTestUtil.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTestUtil.java @@ -31,23 +31,30 @@ import static com.xebialabs.restito.semantics.Condition.method; import static com.xebialabs.restito.semantics.Condition.post; import static com.xebialabs.restito.semantics.Condition.uri; import static com.xebialabs.restito.semantics.Condition.withHeader; +import static net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals; import com.fasterxml.jackson.databind.ObjectMapper; import com.xebialabs.restito.semantics.Action; import com.xebialabs.restito.server.StubServer; import java.io.IOException; +import java.util.LinkedList; +import java.util.List; import java.util.function.BiFunction; import java.util.function.Function; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; - import org.glassfish.grizzly.http.Method; import org.glassfish.grizzly.http.util.HttpStatus; import org.json.JSONObject; import org.junit.Assert; import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.changeManagement.RelatedInstanceList; import org.onap.vid.changeManagement.RequestDetailsWrapper; import org.onap.vid.mso.MsoResponseWrapper; +import org.onap.vid.mso.model.CloudConfiguration; +import org.onap.vid.mso.model.ModelInfo; +import org.onap.vid.mso.model.RequestInfo; +import org.onap.vid.mso.model.RequestParameters; class MsoRestClientTestUtil implements AutoCloseable { private final StubServer server; @@ -109,6 +116,7 @@ class MsoRestClientTestUtil implements AutoCloseable { MsoResponseWrapper response = func.apply(sampleRequestDetails, endpoint); Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus()); + assertJsonEquals(expectedResponseStr, response.getEntity()); verifyServer(server, endpoint, Method.DELETE); } @@ -120,9 +128,78 @@ class MsoRestClientTestUtil implements AutoCloseable { MsoResponseWrapper response = func.apply(endpoint); Assert.assertEquals(expectedStatus.getStatusCode(), response.getStatus()); + assertJsonEquals(expectedResponseStr, response.getEntity()); verifyServer(server, endpoint, Method.GET); } + static org.onap.vid.changeManagement.RequestDetails generateMockMsoRequest() { + org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails(); + requestDetails.setVnfInstanceId("vnf-instance-id"); + requestDetails.setVnfName("vnf-name"); + CloudConfiguration cloudConfiguration = new CloudConfiguration(); + cloudConfiguration.setTenantId("tenant-id"); + cloudConfiguration.setLcpCloudRegionId("lcp-region"); + requestDetails.setCloudConfiguration(cloudConfiguration); + ModelInfo modelInfo = new ModelInfo(); + modelInfo.setModelInvariantId("model-invarient-id"); + modelInfo.setModelCustomizationName("modelCustomizationName"); + requestDetails.setModelInfo(modelInfo); + RequestInfo requestInfo = new RequestInfo(); + requestInfo.setRequestorId("ok883e"); + requestInfo.setSource("VID"); + requestDetails.setRequestInfo(requestInfo); + RequestParameters requestParameters = new RequestParameters(); + requestParameters.setSubscriptionServiceType("subscriber-service-type"); + requestParameters.setAdditionalProperty("a", 1); + requestParameters.setAdditionalProperty("b", 2); + requestParameters.setAdditionalProperty("c", 3); + requestParameters.setAdditionalProperty("d", 4); + String payload = "{\"existing_software_version\": \"3.1\",\"new_software_version\": \"3.2\", \"operations_timeout\": \"3600\"}"; + requestParameters.setAdditionalProperty("payload", payload); + + requestDetails.setRequestParameters(requestParameters); + return requestDetails; + } + + static org.onap.vid.changeManagement.RequestDetails generateChangeManagementMockMsoRequest() { + List<RelatedInstanceList> relatedInstances = new LinkedList<>(); + relatedInstances.add(new RelatedInstanceList()); + + org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails(); + + requestDetails.setVnfName("test-vnf-name"); + requestDetails.setVnfInstanceId("test-vnf-instance_id"); + requestDetails.setRelatedInstList(relatedInstances); + + CloudConfiguration cloudConfiguration = new CloudConfiguration(); + cloudConfiguration.setTenantId("tenant-id"); + cloudConfiguration.setLcpCloudRegionId("lcp-region"); + requestDetails.setCloudConfiguration(cloudConfiguration); + + ModelInfo modelInfo = new ModelInfo(); + modelInfo.setModelInvariantId("model-invarient-id"); + modelInfo.setModelCustomizationName("modelCustomizationName"); + modelInfo.setModelType("test-model-type"); + requestDetails.setModelInfo(modelInfo); + + RequestInfo requestInfo = new RequestInfo(); + requestInfo.setRequestorId("ok883e"); + requestInfo.setSource("VID"); + requestDetails.setRequestInfo(requestInfo); + + RequestParameters requestParameters = new RequestParameters(); + requestParameters.setSubscriptionServiceType("subscriber-service-type"); + requestParameters.setAdditionalProperty("a", 1); + requestParameters.setAdditionalProperty("b", 2); + requestParameters.setAdditionalProperty("c", 3); + requestParameters.setAdditionalProperty("d", 4); + String payload = "{\"existing_software_version\": \"3.1\",\"new_software_version\": \"3.2\", \"operations_timeout\": \"3600\"}"; + requestParameters.setAdditionalProperty("payload", payload); + + requestDetails.setRequestParameters(requestParameters); + return requestDetails; + } + private void verifyServer(StubServer server, String endpoint, Method httpMethod) { verifyHttp(server).once( method(httpMethod), diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RelatedInstanceTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RelatedInstanceTest.java index 1a8aa3b48..c76298442 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RelatedInstanceTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RelatedInstanceTest.java @@ -3,13 +3,14 @@ * VID * ================================================================================ * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nokia. 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. @@ -20,94 +21,66 @@ package org.onap.vid.mso.rest; -import org.junit.Test; -import org.onap.vid.mso.model.ModelInfo; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; -import java.util.Map; +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEqualsExcluding; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; public class RelatedInstanceTest { - private RelatedInstance createTestSubject() { - return new RelatedInstance(); - } + private RelatedInstance relatedInstance = new RelatedInstance(); - - @Test - public void testGetModelInfo() throws Exception { - RelatedInstance testSubject; - ModelInfo result; + private String instanceId = "testInstanceId"; + private String InstanceName = "testInstance"; + private String propertyName = "testProperty"; + private String additionalProperty = "testAdditionalProperty"; - // default test - testSubject = createTestSubject(); - result = testSubject.getModelInfo(); + @BeforeMethod + public void setUp() { + relatedInstance = new RelatedInstance(); } - @Test - public void testSetModelInfo() throws Exception { - RelatedInstance testSubject; - ModelInfo modelInfo = null; - - // default test - testSubject = createTestSubject(); - testSubject.setModelInfo(modelInfo); + public void shouldHaveProperGettersAndSetters() { + assertThat(RelatedInstance.class, hasValidGettersAndSettersExcluding("additionalProperties")); } - @Test - public void testToString() throws Exception { - RelatedInstance testSubject; - String result; + public void shouldHaveProperGetterAndSetterForAdditionalProperties() { + // when + relatedInstance.setAdditionalProperty(propertyName,additionalProperty); - // default test - testSubject = createTestSubject(); - result = testSubject.toString(); + // then + assertThat( relatedInstance.getAdditionalProperties().get(propertyName) ).isEqualTo(additionalProperty); } - @Test - public void testGetAdditionalProperties() throws Exception { - RelatedInstance testSubject; - Map<String, Object> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getAdditionalProperties(); + public void shouldProperlyConvertRelatedInstanceObjectToString() { + // given + relatedInstance.setInstanceId(instanceId); + relatedInstance.setInstanceName(InstanceName); + relatedInstance.setAdditionalProperty(propertyName,additionalProperty); + + // when + String response = relatedInstance.toString(); + + // then + assertThat(response).contains( + "instanceName="+InstanceName+"," + + "instanceId="+instanceId+"," + + "modelInfo=<null>," + + "additionalProperties={"+propertyName+"="+additionalProperty+"}]" + ); } - @Test - public void testSetAdditionalProperty() throws Exception { - RelatedInstance testSubject; - String name = ""; - Object value = null; - - // default test - testSubject = createTestSubject(); - testSubject.setAdditionalProperty(name, value); + public void shouldProperlyCheckIfObjectsAreEqual() { + assertThat(RelatedInstance.class, hasValidBeanEqualsExcluding("additionalProperties")); } - - @Test - public void testHashCode() throws Exception { - RelatedInstance testSubject; - int result; - // default test - testSubject = createTestSubject(); - result = testSubject.hashCode(); - } - - - @Test - public void testEquals() throws Exception { - RelatedInstance testSubject; - Object other = null; - boolean result; - - // default test - testSubject = createTestSubject(); - result = testSubject.equals(other); - } -} +}
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestDetailsTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestDetailsTest.java index 2ae7535dc..575ceab7c 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestDetailsTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestDetailsTest.java @@ -20,185 +20,114 @@ package org.onap.vid.mso.rest; -import org.junit.Test; -import org.onap.vid.mso.model.CloudConfiguration; -import org.onap.vid.mso.model.ModelInfo; -import org.onap.vid.mso.model.RequestInfo; - +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.assertj.core.api.AssertionsForClassTypes; +import org.onap.vid.exceptions.NotFoundException; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.util.Collections; import java.util.List; import java.util.Map; +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEqualsExcluding; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.testng.AssertJUnit.assertEquals; + public class RequestDetailsTest { - private RequestDetails createTestSubject() { - return new RequestDetails(); - } + private RequestDetails requestDetails; - - @Test - public void testGetCloudConfiguration() throws Exception { - RequestDetails testSubject; - CloudConfiguration result; + private String propertyName = "testProperty"; + private String additionalProperty = "testAdditionalProperty"; - // default test - testSubject = createTestSubject(); - result = testSubject.getCloudConfiguration(); - } + private static final ImmutableList<String> LCP_CLOUD_REGION_ID_PATH = + ImmutableList.of("requestDetails", "cloudConfiguration", "lcpCloudRegionId"); - - @Test - public void testSetCloudConfiguration() throws Exception { - RequestDetails testSubject; - CloudConfiguration cloudConfiguration = null; - // default test - testSubject = createTestSubject(); - testSubject.setCloudConfiguration(cloudConfiguration); + @BeforeMethod + public void setUp() { + requestDetails = new RequestDetails(); } - @Test - public void testGetModelInfo() throws Exception { - RequestDetails testSubject; - ModelInfo result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getModelInfo(); + public void shouldHaveProperSettersAndGetters() { + assertThat(RequestDetails.class, hasValidGettersAndSettersExcluding("additionalProperties")); } - @Test - public void testSetModelInfo() throws Exception { - RequestDetails testSubject; - ModelInfo modelInfo = null; + public void shouldHaveProperGetterAndSetterForAdditionalProperties() { + // when + requestDetails.setAdditionalProperty(propertyName,additionalProperty); - // default test - testSubject = createTestSubject(); - testSubject.setModelInfo(modelInfo); + // then + AssertionsForClassTypes.assertThat( requestDetails.getAdditionalProperties().get(propertyName) ).isEqualTo(additionalProperty); } - @Test - public void testGetRelatedInstanceList() throws Exception { - RequestDetails testSubject; - List<RelatedInstanceWrapper> result; + public void shouldProperlyConvertRelatedInstanceObjectToString() { + // given + requestDetails.setAdditionalProperty(propertyName,additionalProperty); - // default test - testSubject = createTestSubject(); - result = testSubject.getRelatedInstanceList(); - } - - - @Test - public void testSetRelatedInstanceList() throws Exception { - RequestDetails testSubject; - List<RelatedInstanceWrapper> relatedInstanceList = null; + // when + String response = requestDetails.toString(); - // default test - testSubject = createTestSubject(); - testSubject.setRelatedInstanceList(relatedInstanceList); + // then + AssertionsForClassTypes.assertThat(response).contains( + "additionalProperties={"+propertyName+"="+additionalProperty+"}]" + ); } - @Test - public void testGetRequestInfo() throws Exception { - RequestDetails testSubject; - RequestInfo result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getRequestInfo(); + public void shouldProperlyCheckIfObjectsAreEqual() { + assertThat(RequestDetails.class, hasValidBeanEqualsExcluding("additionalProperties")); } - - @Test - public void testSetRequestInfo() throws Exception { - RequestDetails testSubject; - RequestInfo requestInfo = null; + @DataProvider + public static Object[][] extractValueByPathDataProvider() { - // default test - testSubject = createTestSubject(); - testSubject.setRequestInfo(requestInfo); - } + RequestDetails requestDetails1 = new RequestDetails(); + Map cloudConfiguration = ImmutableMap.of("lcpCloudRegionId", "lcp1"); + requestDetails1.setAdditionalProperty("requestDetails", + ImmutableMap.of("cloudConfiguration", cloudConfiguration)); - - @Test - public void testGetSubscriberInfo() throws Exception { - RequestDetails testSubject; - SubscriberInfo result; - // default test - testSubject = createTestSubject(); - result = testSubject.getSubscriberInfo(); - } - - - @Test - public void testSetSubscriberInfo() throws Exception { - RequestDetails testSubject; - SubscriberInfo subscriberInfo = null; + return new Object[][] { + { requestDetails1, LCP_CLOUD_REGION_ID_PATH, String.class, "lcp1" }, + { requestDetails1, ImmutableList.of("requestDetails", "cloudConfiguration"), Map.class, cloudConfiguration }, - // default test - testSubject = createTestSubject(); - testSubject.setSubscriberInfo(subscriberInfo); + }; } - - @Test - public void testToString() throws Exception { - RequestDetails testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.toString(); + @Test(dataProvider = "extractValueByPathDataProvider") + public void testExtractValueByPath(RequestDetails requestDetails, List<String> keys, Class clz, Object expectedValue) { + assertEquals(expectedValue, requestDetails.extractValueByPathUsingAdditionalProperties(keys, clz)); } - - @Test - public void testGetAdditionalProperties() throws Exception { - RequestDetails testSubject; - Map<String, Object> result; + @DataProvider + public static Object[][] extractValueByPathDataProviderThrowException() { + RequestDetails requestDetails1 = new RequestDetails(); + requestDetails1.setAdditionalProperty("requestDetails", + ImmutableMap.of("cloudConfiguration", "notMap")); - // default test - testSubject = createTestSubject(); - result = testSubject.getAdditionalProperties(); - } + RequestDetails requestDetails2 = new RequestDetails(); + requestDetails2.setAdditionalProperty("requestDetails", + ImmutableMap.of("cloudConfiguration", Collections.EMPTY_MAP)); - - @Test - public void testSetAdditionalProperty() throws Exception { - RequestDetails testSubject; - String name = ""; - Object value = null; - - // default test - testSubject = createTestSubject(); - testSubject.setAdditionalProperty(name, value); - } - - - @Test - public void testHashCode() throws Exception { - RequestDetails testSubject; - int result; - - // default test - testSubject = createTestSubject(); - result = testSubject.hashCode(); + return new Object[][] { + { new RequestDetails(), LCP_CLOUD_REGION_ID_PATH, String.class}, + { requestDetails1, LCP_CLOUD_REGION_ID_PATH, String.class}, + { requestDetails1, ImmutableList.of("requestDetails", "abc"), String.class}, + { requestDetails2, LCP_CLOUD_REGION_ID_PATH, String.class}, + }; } - - @Test - public void testEquals() throws Exception { - RequestDetails testSubject; - Object other = null; - boolean result; - - // default test - testSubject = createTestSubject(); - result = testSubject.equals(other); + @Test(dataProvider = "extractValueByPathDataProviderThrowException", expectedExceptions = NotFoundException.class) + public void testExtractValueByPathThrowException(RequestDetails requestDetails, List<String> keys, Class clz) { + requestDetails.extractValueByPathUsingAdditionalProperties(keys, clz); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestDetailsWrapperTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestDetailsWrapperTest.java new file mode 100644 index 000000000..d30f0b671 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestDetailsWrapperTest.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2019 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.vid.mso.rest; + +import org.testng.annotations.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RequestDetailsWrapperTest { + + private RequestDetailsWrapper requestDetailsWrapper; + + @Test + public void shouldHaveProperConstructorAndGet(){ + // given + RequestDetails requestDetails = MsoRestClientTestUtil.generateMockMsoRequest(); + + // when + requestDetailsWrapper = new RequestDetailsWrapper(requestDetails); + + // then + assertThat(requestDetailsWrapper.getRequestDetails()).isEqualToComparingFieldByField(requestDetails); + } +} diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestListTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestListTest.java index 12c11e091..f7e6deea8 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestListTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestListTest.java @@ -7,9 +7,9 @@ * 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. @@ -20,86 +20,57 @@ package org.onap.vid.mso.rest; -import java.util.List; -import java.util.Map; +import org.assertj.core.api.AssertionsForClassTypes; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; -import org.junit.Test; +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEqualsExcluding; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding; +import static org.hamcrest.MatcherAssert.assertThat; public class RequestListTest { - private RequestList createTestSubject() { - return new RequestList(); - } + private RequestList requestList; - @Test - public void testGetRequestList() throws Exception { - RequestList testSubject; - List<RequestWrapper> result; + private String propertyName = "testProperty"; + private String additionalProperty = "testAdditionalProperty"; - // default test - testSubject = createTestSubject(); - result = testSubject.getRequestList(); + @BeforeMethod + public void setUp() { + requestList = new RequestList(); } @Test - public void testSetRequestList() throws Exception { - RequestList testSubject; - List<RequestWrapper> l = null; - - // default test - testSubject = createTestSubject(); - testSubject.setRequestList(l); + public void shouldHaveProperSettersAndGetters() { + assertThat(RequestList.class, hasValidGettersAndSettersExcluding("additionalProperties")); } @Test - public void testToString() throws Exception { - RequestList testSubject; - String result; + public void shouldHaveProperGetterAndSetterForAdditionalProperties() { + // when + requestList.setAdditionalProperty(propertyName,additionalProperty); - // default test - testSubject = createTestSubject(); - result = testSubject.toString(); + // then + AssertionsForClassTypes.assertThat( requestList.getAdditionalProperties().get(propertyName) ).isEqualTo(additionalProperty); } @Test - public void testGetAdditionalProperties() throws Exception { - RequestList testSubject; - Map<String, Object> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getAdditionalProperties(); + public void shouldProperlyCheckIfObjectsAreEqual() { + assertThat(RequestList.class, hasValidBeanEqualsExcluding("additionalProperties")); } @Test - public void testSetAdditionalProperty() throws Exception { - RequestList testSubject; - String name = ""; - Object value = null; - - // default test - testSubject = createTestSubject(); - testSubject.setAdditionalProperty(name, value); - } - - @Test - public void testHashCode() throws Exception { - RequestList testSubject; - int result; - - // default test - testSubject = createTestSubject(); - result = testSubject.hashCode(); - } - - @Test - public void testEquals() throws Exception { - RequestList testSubject; - Object other = null; - boolean result; - - // default test - testSubject = createTestSubject(); - result = testSubject.equals(other); + public void shouldProperlyConvertRelatedInstanceObjectToString() { + // given + requestList.setAdditionalProperty(propertyName,additionalProperty); + + // when + String response = requestList.toString(); + + // then + System.out.println(response); + AssertionsForClassTypes.assertThat(response).contains( + "additionalProperties={"+propertyName+"="+additionalProperty+"}]" + ); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestStatusTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestStatusTest.java new file mode 100644 index 000000000..ab1f4340d --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestStatusTest.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2019 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.vid.mso.rest; + +import org.assertj.core.api.AssertionsForClassTypes; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEqualsExcluding; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding; +import static org.hamcrest.MatcherAssert.assertThat; + +public class RequestStatusTest { + + private RequestStatus RequestStatus; + + private String propertyName = "testProperty"; + private String additionalProperty = "testAdditionalProperty"; + + @BeforeMethod + public void setUp() { + RequestStatus = new RequestStatus(); + } + + @Test + public void shouldHaveProperSettersAndGetters() { + assertThat(RequestStatus.class, hasValidGettersAndSettersExcluding("additionalProperties")); + } + + @Test + public void shouldHaveProperGetterAndSetterForAdditionalProperties() { + // when + RequestStatus.setAdditionalProperty(propertyName,additionalProperty); + + // then + AssertionsForClassTypes.assertThat( RequestStatus.getAdditionalProperties().get(propertyName) ).isEqualTo(additionalProperty); + } + + @Test + public void shouldProperlyCheckIfObjectsAreEqual() { + assertThat(RequestStatus.class, hasValidBeanEqualsExcluding("additionalProperties")); + } + + @Test + public void shouldProperlyConvertRelatedInstanceObjectToString() { + // given + RequestStatus.setAdditionalProperty(propertyName,additionalProperty); + RequestStatus.setRequestState("testState"); + RequestStatus.setTimestamp("100"); + + // when + String response = RequestStatus.toString(); + + // then + System.out.println(response); + AssertionsForClassTypes.assertThat(response).contains( + "percentProgress=<null>," + + "requestState=testState," + + "statusMessage=<null>," + + "timestamp=100," + + "wasRolledBack=<null>," + + "additionalProperties={"+propertyName+"="+additionalProperty+"}]" + ); + } + +} diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestTest.java index 1f8898e2a..0b5e288d7 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestTest.java @@ -7,9 +7,9 @@ * 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. @@ -20,102 +20,68 @@ package org.onap.vid.mso.rest; -import org.junit.Test; -public class RequestTest { - - private Request createTestSubject() { - return new Request(); - } - - @Test - public void testGetInstanceIds() throws Exception { - Request testSubject; - InstanceIds result; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; - // default test - testSubject = createTestSubject(); - result = testSubject.getInstanceIds(); - } - - @Test - public void testSetInstanceIds() throws Exception { - Request testSubject; - InstanceIds instanceIds = null; - - // default test - testSubject = createTestSubject(); - testSubject.setInstanceIds(instanceIds); - } +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEqualsExcluding; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; - @Test - public void testGetRequestDetails() throws Exception { - Request testSubject; - RequestDetails result; +public class RequestTest { - // default test - testSubject = createTestSubject(); - result = testSubject.getRequestDetails(); - } - @Test - public void testSetRequestDetails() throws Exception { - Request testSubject; - RequestDetails requestDetails = null; + private Request request; - // default test - testSubject = createTestSubject(); - testSubject.setRequestDetails(requestDetails); - } + private String propertyName = "testProperty"; + private String additionalProperty = "testAdditionalProperty"; - @Test - public void testGetRequestStatus() throws Exception { - Request testSubject; - RequestStatus result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getRequestStatus(); + @BeforeMethod + public void setUp() { + request = new Request(); } @Test - public void testSetRequestStatus() throws Exception { - Request testSubject; - RequestStatus requestStatus = null; - - // default test - testSubject = createTestSubject(); - testSubject.setRequestStatus(requestStatus); + public void shouldHaveProperSettersAndGetters() { + assertThat(Request.class, hasValidGettersAndSettersExcluding("additionalProperties")); } @Test - public void testToString() throws Exception { - Request testSubject; - String result; + public void shouldHaveProperGetterAndSetterForAdditionalProperties() { + // when + request.setAdditionalProperty(propertyName,additionalProperty); - // default test - testSubject = createTestSubject(); - result = testSubject.toString(); + // then + assertThat( request.getAdditionalProperties().get(propertyName) ).isEqualTo(additionalProperty); } @Test - public void testEquals() throws Exception { - Request testSubject; - Object other = null; - boolean result; - - // default test - testSubject = createTestSubject(); - result = testSubject.equals(other); + public void shouldProperlyConvertRelatedInstanceObjectToString() { + // given + request.setFinishTime("100"); + request.setRequestId("testRequest"); + request.setAdditionalProperty(propertyName,additionalProperty); + + // when + String response = request.toString(); + + // then + assertThat(response).contains( + "[instanceIds=<null>," + + "requestDetails=<null>," + + "requestStatus=<null>," + + "finishTime="+100+"," + + "requestId=testRequest," + + "requestScope=<null>," + + "requestType=<null>," + + "startTime=<null>," + + "additionalProperties={"+propertyName+"="+additionalProperty+"}]" + ); } @Test - public void testHashCode() throws Exception { - Request testSubject; - int result; - - // default test - testSubject = createTestSubject(); - result = testSubject.hashCode(); + public void shouldProperlyCheckIfObjectsAreEqual() { + assertThat(Request.class, hasValidBeanEqualsExcluding("additionalProperties")); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestWrapperTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestWrapperTest.java index 2a8e6d9b1..3e9b710c9 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestWrapperTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/RequestWrapperTest.java @@ -7,9 +7,9 @@ * 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. @@ -20,31 +20,15 @@ package org.onap.vid.mso.rest; -import org.junit.Test; +import org.testng.annotations.Test; -public class RequestWrapperTest { - - private RequestWrapper createTestSubject() { - return new RequestWrapper(); - } - - @Test - public void testGetRequest() throws Exception { - RequestWrapper testSubject; - Request result; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static org.hamcrest.MatcherAssert.assertThat; - // default test - testSubject = createTestSubject(); - result = testSubject.getRequest(); - } +public class RequestWrapperTest { @Test - public void testSetRequest() throws Exception { - RequestWrapper testSubject; - Request request = null; - - // default test - testSubject = createTestSubject(); - testSubject.setRequest(request); + public void shouldHaveProperSettersAndGetters() { + assertThat(RequestWrapper.class, hasValidGettersAndSetters()); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/ResponseTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/ResponseTest.java index 0a25e6d6a..a29e2a589 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/ResponseTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/ResponseTest.java @@ -20,51 +20,15 @@ package org.onap.vid.mso.rest; -import org.junit.Test; +import org.testng.annotations.Test; -public class ResponseTest { - - private Response createTestSubject() { - return new Response(); - } - - @Test - public void testGetStatus() throws Exception { - Response testSubject; - int result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getStatus(); - } - - @Test - public void testSetStatus() throws Exception { - Response testSubject; - int status = 0; - - // default test - testSubject = createTestSubject(); - testSubject.setStatus(status); - } - - @Test - public void testGetEntity() throws Exception { - Response testSubject; - RequestList result; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static org.hamcrest.MatcherAssert.assertThat; - // default test - testSubject = createTestSubject(); - result = testSubject.getEntity(); - } +public class ResponseTest { @Test - public void testSetEntity() throws Exception { - Response testSubject; - RequestList entity = null; - - // default test - testSubject = createTestSubject(); - testSubject.setEntity(entity); + public void shouldHaveProperSettersAndGetters() { + assertThat(Response.class, hasValidGettersAndSetters()); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/SubscriberInfoTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/SubscriberInfoTest.java new file mode 100644 index 000000000..5a7cd3b51 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/SubscriberInfoTest.java @@ -0,0 +1,81 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2019 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.vid.mso.rest; + +import org.assertj.core.api.AssertionsForClassTypes; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEqualsExcluding; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding; +import static org.hamcrest.MatcherAssert.assertThat; + +public class SubscriberInfoTest { + + private SubscriberInfo SubscriberInfo; + + private String propertyName = "testProperty"; + private String additionalProperty = "testAdditionalProperty"; + + @BeforeMethod + public void setUp() { + SubscriberInfo = new SubscriberInfo(); + } + + @Test + public void shouldHaveProperSettersAndGetters() { + assertThat(SubscriberInfo.class, hasValidGettersAndSettersExcluding("additionalProperties")); + } + + @Test + public void shouldHaveProperGetterAndSetterForAdditionalProperties() { + // when + SubscriberInfo.setAdditionalProperty(propertyName,additionalProperty); + + // then + AssertionsForClassTypes.assertThat( SubscriberInfo.getAdditionalProperties().get(propertyName) ).isEqualTo(additionalProperty); + } + + @Test + public void shouldProperlyCheckIfObjectsAreEqual() { + assertThat(SubscriberInfo.class, hasValidBeanEqualsExcluding("additionalProperties")); + } + + @Test + public void shouldProperlyConvertRelatedInstanceObjectToString() { + // given + SubscriberInfo.setAdditionalProperty(propertyName,additionalProperty); + SubscriberInfo.setGlobalSubscriberId("testSubscriberId"); + SubscriberInfo.setSubscriberName("testSubscriberName"); + + // when + String response = SubscriberInfo.toString(); + + // then + System.out.println(response); + AssertionsForClassTypes.assertThat(response).contains( + "globalSubscriberId=testSubscriberId," + + "subscriberCommonSiteId=<null>," + + "subscriberName=testSubscriberName," + + "additionalProperties={"+propertyName+"="+additionalProperty+"}]" + ); + } +} diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/TaskListTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/TaskListTest.java index 41ed6b9ca..4a8d40cbd 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/TaskListTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/TaskListTest.java @@ -20,33 +20,15 @@ package org.onap.vid.mso.rest; -import java.util.List; +import org.testng.annotations.Test; -import org.junit.Test; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static org.hamcrest.MatcherAssert.assertThat; public class TaskListTest { - private TaskList createTestSubject() { - return new TaskList(); - } - @Test - public void testGetTaskList() throws Exception { - TaskList testSubject; - List<Task> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getTaskList(); - } - - @Test - public void testSetTaskList() throws Exception { - TaskList testSubject; - List<Task> taskList = null; - - // default test - testSubject = createTestSubject(); - testSubject.setTaskList(taskList); + public void shouldHaveProperSettersAndGetters() { + assertThat(TaskList.class, hasValidGettersAndSetters()); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/TaskTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/TaskTest.java index 81ae19d81..f6d0c763f 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/TaskTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/TaskTest.java @@ -20,253 +20,64 @@ package org.onap.vid.mso.rest; -import java.util.List; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; -import org.junit.Test; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableList; +import java.io.IOException; +import org.onap.vid.testUtils.TestUtils; +import org.testng.annotations.Test; public class TaskTest { - private Task createTestSubject() { - return new Task(); - } - - @Test - public void testGetTaskId() throws Exception { - Task testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getTaskId(); - } - - @Test - public void testSetTaskId() throws Exception { - Task testSubject; - String taskId = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setTaskId(taskId); - } - - @Test - public void testGetType() throws Exception { - Task testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getType(); - } - - @Test - public void testSetType() throws Exception { - Task testSubject; - String type = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setType(type); - } - - @Test - public void testGetNfRole() throws Exception { - Task testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getNfRole(); - } - - @Test - public void testSetNfRole() throws Exception { - Task testSubject; - String nfRole = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setNfRole(nfRole); - } - - @Test - public void testGetSubscriptionServiceType() throws Exception { - Task testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getSubscriptionServiceType(); - } - - @Test - public void testSetSubscriptionServiceType() throws Exception { - Task testSubject; - String subscriptionServiceType = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setSubscriptionServiceType(subscriptionServiceType); - } - - @Test - public void testGetOriginalRequestId() throws Exception { - Task testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getOriginalRequestId(); - } - - @Test - public void testSetOriginalRequestId() throws Exception { - Task testSubject; - String originalRequestId = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setOriginalRequestId(originalRequestId); - } - - @Test - public void testGetOriginalRequestorId() throws Exception { - Task testSubject; - String result; + private final ObjectMapper mapper = new ObjectMapper(); + private final String TASK_JSON = "" + + "{ " + + " \"taskId\": \"taskId\", " + + " \"type\": \"type\", " + + " \"nfRole\": \"nfRole\", " + + " \"subscriptionServiceType\": \"subscriptionServiceType\", " + + " \"originalRequestId\": \"originalRequestId\", " + + " \"originalRequestorId\": \"originalRequestorId\", " + + " \"buildingBlockName\": \"buildingBlockName\", " + + " \"buildingBlockStep\": \"buildingBlockStep\", " + + " \"errorSource\": \"errorSource\", " + + " \"errorCode\": \"errorCode\", " + + " \"errorMessage\": \"errorMessage\", " + + " \"validResponses\": [ " + + " \"a\", " + + " \"b\", " + + " \"c\" " + + " ] " + + "} "; - // default test - testSubject = createTestSubject(); - result = testSubject.getOriginalRequestorId(); + private Task newTaskWithPopulatedFields() { + Task task = TestUtils.setStringsInStringProperties(new Task()); + task.setValidResponses(ImmutableList.of("a", "b", "c")); + return task; } @Test - public void testSetOriginalRequestorId() throws Exception { - Task testSubject; - String originalRequestorId = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setOriginalRequestorId(originalRequestorId); - } - - @Test - public void testGetErrorSource() throws Exception { - Task testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getErrorSource(); - } - - @Test - public void testSetErrorSource() throws Exception { - Task testSubject; - String errorSource = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setErrorSource(errorSource); - } - - @Test - public void testGetErrorCode() throws Exception { - Task testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getErrorCode(); - } - - @Test - public void testSetErrorCode() throws Exception { - Task testSubject; - String errorCode = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setErrorCode(errorCode); + public void shouldHaveProperSettersAndGetters() { + assertThat(Task.class, hasValidGettersAndSetters()); } @Test - public void testGetErrorMessage() throws Exception { - Task testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getErrorMessage(); - } - - @Test - public void testSetErrorMessage() throws Exception { - Task testSubject; - String errorMessage = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setErrorMessage(errorMessage); + public void serializeTask() throws IOException { + assertThat( + mapper.writeValueAsString(newTaskWithPopulatedFields()), + jsonEquals(TASK_JSON) + ); } @Test - public void testGetBuildingBlockName() throws Exception { - Task testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getBuildingBlockName(); - } - - @Test - public void testSetBuildingBlockName() throws Exception { - Task testSubject; - String buildingBlockName = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setBuildingBlockName(buildingBlockName); - } - - @Test - public void testGetBuildingBlockStep() throws Exception { - Task testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getBuildingBlockStep(); - } - - @Test - public void testSetBuildingBlockStep() throws Exception { - Task testSubject; - String buildingBlockStep = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setBuildingBlockStep(buildingBlockStep); - } - - @Test - public void testGetValidResponses() throws Exception { - Task testSubject; - List<String> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getValidResponses(); - } - - @Test - public void testSetValidResponses() throws Exception { - Task testSubject; - List<String> validResponses = null; - - // default test - testSubject = createTestSubject(); - testSubject.setValidResponses(validResponses); + public void deserializeTask() throws IOException { + assertThat( + mapper.readValue(TASK_JSON, Task.class), + is(newTaskWithPopulatedFields()) + ); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/services/ChangeManagementServiceUnitTest.java b/vid-app-common/src/test/java/org/onap/vid/services/ChangeManagementServiceUnitTest.java index b66899c31..e638605c1 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/ChangeManagementServiceUnitTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/ChangeManagementServiceUnitTest.java @@ -26,6 +26,7 @@ import org.apache.commons.io.IOUtils; import org.mockito.ArgumentCaptor; import org.onap.portalsdk.core.service.DataAccessService; import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.aai.util.HttpsAuthClient; import org.onap.vid.changeManagement.ChangeManagementRequest; import org.onap.vid.changeManagement.RequestDetailsWrapper; import org.onap.vid.client.SyncRestClient; @@ -116,11 +117,6 @@ public class ChangeManagementServiceUnitTest extends AbstractTestNGSpringContext @Configuration public static class TestMsoConfig extends MsoConfig { - public MsoRestClientNew getMsoClient() { - MsoRestClientNew spyClient = spy(new MsoRestClientNew(new SyncRestClient(), "")); - return spyClient; - } - @Bean public ChangeManagementService getChangeManagementService(DataAccessService dataAccessService, MsoBusinessLogic msoInterface, SchedulerRestInterfaceIfc schedulerRestInterface, CloudOwnerService cloudOwnerService) { return new ChangeManagementServiceImpl(dataAccessService, msoInterface, schedulerRestInterface, cloudOwnerService); diff --git a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java index 3a7d4690d..3d919d72d 100644 --- a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java +++ b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java @@ -20,6 +20,9 @@ package org.onap.vid.testUtils; +import static java.util.function.Function.identity; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toMap; import static org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptors; import static org.mockito.Matchers.any; import static org.mockito.Mockito.RETURNS_DEFAULTS; @@ -46,6 +49,7 @@ import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.GenericType; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.apache.commons.beanutils.BeanUtils; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.json.JSONArray; @@ -55,7 +59,6 @@ import org.mockito.MockSettings; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import org.onap.portalsdk.core.domain.support.DomainVo; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.asdc.beans.Service; import org.springframework.mock.env.MockEnvironment; @@ -131,12 +134,39 @@ public class TestUtils { valueType); } - public static String[] allPropertiesOf(Class<DomainVo> aClass) { + public static String[] allPropertiesOf(Class<?> aClass) { return Arrays.stream(getPropertyDescriptors(aClass)) .map(PropertyDescriptor::getDisplayName) .toArray(String[]::new); } + private static <T> List<String> allStringPropertiesOf(T object) { + return Arrays.stream(getPropertyDescriptors(object.getClass())) + .filter(descriptor -> descriptor.getPropertyType().isAssignableFrom(String.class)) + .map(PropertyDescriptor::getDisplayName) + .collect(toList()); + } + + /** + * Sets each String property with a value equal to the name of + * the property; e.g.: { name: "name", city: "city" } + * @param object + * @param <T> + * @return The modified object + */ + public static <T> T setStringsInStringProperties(T object) { + try { + final List<String> stringFields = allStringPropertiesOf(object); + + BeanUtils.populate(object, stringFields.stream() + .collect(toMap(identity(), identity()))); + + return object; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + public static class JavaxRsClientMocks { private final javax.ws.rs.client.Client fakeClient; |