From d3076db84866ee94c3532699714b9e8c2e86593d Mon Sep 17 00:00:00 2001 From: "michal.banka" Date: Wed, 3 Apr 2019 12:29:10 +0200 Subject: Increase test coverage for vid/aii - third part Change-Id: Ib4da72c1da212cf009f70694ffcb0be4049cb67e Issue-ID: VID-385 Signed-off-by: michal.banka --- .../org/onap/vid/aai/AaiGetVnfResponseTest.java | 88 ++++-- .../onap/vid/aai/OperationalEnvironmentTest.java | 28 +- .../org/onap/vid/aai/ServicePropertiesTest.java | 95 ------- .../onap/vid/aai/SubscriberAaiResponseTest.java | 36 ++- .../vid/aai/SubscriberFilteredResultsTest.java | 88 ++++-- .../test/java/org/onap/vid/aai/VnfResultTest.java | 83 ------ .../onap/vid/aai/model/ServicePropertiesTest.java | 93 ++++++ .../java/org/onap/vid/aai/model/VnfResultTest.java | 84 ++++++ .../onap/vid/aai/util/AAIRestInterfaceTest.java | 302 ++++++++++++++++---- .../vid/aai/util/SingleAAIRestInterfaceTest.java | 315 --------------------- 10 files changed, 617 insertions(+), 595 deletions(-) delete mode 100644 vid-app-common/src/test/java/org/onap/vid/aai/ServicePropertiesTest.java delete mode 100644 vid-app-common/src/test/java/org/onap/vid/aai/VnfResultTest.java create mode 100644 vid-app-common/src/test/java/org/onap/vid/aai/model/ServicePropertiesTest.java create mode 100644 vid-app-common/src/test/java/org/onap/vid/aai/model/VnfResultTest.java delete mode 100644 vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java (limited to 'vid-app-common/src/test') diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/AaiGetVnfResponseTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/AaiGetVnfResponseTest.java index 5eb47a1ff..9d8734b39 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/AaiGetVnfResponseTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/AaiGetVnfResponseTest.java @@ -20,34 +20,88 @@ package org.onap.vid.aai; -import java.util.Map; +import java.io.IOException; +import java.util.*; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.junit.Before; import org.junit.Test; +import org.onap.vid.aai.model.VnfResult; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; public class AaiGetVnfResponseTest { - private AaiGetVnfResponse createTestSubject() { - return new AaiGetVnfResponse(); + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + private static final String VNF_RESULT = + "{\n" + + " \"id\": \"1\",\n" + + " \"nodetype\": \"testNodeType\",\n" + + " \"url\": \"test/url\",\n" + + " \"serviceProperties\": {},\n" + + " \"relatedTo\": []\n" + + "}\n"; + + private static final String AAI_GET_VNF_RESPONSE_TEST = "{ \n" + + " \"results\": \n" + + " [\n" + + VNF_RESULT + + " ]\n" + + "}"; + + + private AaiGetVnfResponse aaiGetVnfResponse; + private VnfResult expectedVnfResult; + private List expectedList; + + @Before + public void setUp() throws IOException { + expectedVnfResult = OBJECT_MAPPER.readValue(VNF_RESULT, VnfResult.class); + expectedList = Collections.singletonList(expectedVnfResult); + } @Test - public void testGetAdditionalProperties() throws Exception { - AaiGetVnfResponse testSubject; - Map result; + public void shouldHaveProperSetters() { + aaiGetVnfResponse = new AaiGetVnfResponse(); + aaiGetVnfResponse.setAdditionalProperty("key","value"); + aaiGetVnfResponse.setResults(expectedList); + + assertEquals(aaiGetVnfResponse.getAdditionalProperties().size(), 1); + assertTrue(aaiGetVnfResponse.getAdditionalProperties().containsKey("key")); + assertTrue(aaiGetVnfResponse.getAdditionalProperties().containsValue("value")); + assertEquals(aaiGetVnfResponse.getResults().size(), 1); + assertEquals(aaiGetVnfResponse.getResults().get(0), expectedVnfResult); + } + + @Test + public void shouldProperlyConvertJsonToAiiGetVnfResponse() throws IOException { + aaiGetVnfResponse = OBJECT_MAPPER.readValue(AAI_GET_VNF_RESPONSE_TEST, AaiGetVnfResponse.class); + assertThat(aaiGetVnfResponse.getResults(), is(expectedList)); + } + + @Test + public void shouldReturnProperToString(){ + aaiGetVnfResponse = new AaiGetVnfResponse(); + aaiGetVnfResponse.setAdditionalProperty("testKey","testValue"); + aaiGetVnfResponse.setResults(expectedList); + Map expectedMap = new HashMap<>(); + expectedMap.put("testKey", "testValue"); + + String expectedOutput = "AaiGetVnfResponse{" + + "results=" + expectedList + + ", additionalProperties="+expectedMap+"}"; - // default test - testSubject = createTestSubject(); - result = testSubject.getAdditionalProperties(); + assertEquals(aaiGetVnfResponse.toString(), expectedOutput); } @Test - public void testSetAdditionalProperty() throws Exception { - AaiGetVnfResponse testSubject; - String name = ""; - Object value = null; - - // default test - testSubject = createTestSubject(); - testSubject.setAdditionalProperty(name, value); + public void shouldAddAdditionalProperty(){ + aaiGetVnfResponse = new AaiGetVnfResponse(); + aaiGetVnfResponse.setAdditionalProperty("key", "value"); + assertTrue(aaiGetVnfResponse.getAdditionalProperties().containsKey("key")); } } 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 7f5e362ba..c331b3252 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 @@ -23,10 +23,14 @@ package org.onap.vid.aai; import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Assert; +import org.onap.vid.aai.model.RelationshipList; import org.testng.annotations.Test; import java.io.IOException; +import java.util.ArrayList; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; import static org.assertj.core.api.Assertions.assertThat; @@ -47,9 +51,30 @@ public class OperationalEnvironmentTest { "}\n" + "}"; + @Test + public void shouldCreateProperOperationalEnvironmentWithConstructor(){ + RelationshipList relationshipList = new RelationshipList(); + relationshipList.relationship = new ArrayList<>(); + + OperationalEnvironment operationalEnvironment = new OperationalEnvironment("testId", + "testEnvName", "testEnvType", + "testEnvStatus", "testTenant", "testWorkload", + "testResource", relationshipList); + + assertThat(operationalEnvironment.getOperationalEnvironmentId()).isEqualTo("testId"); + assertThat(operationalEnvironment.getWorkloadContext()).isEqualTo("testWorkload"); + assertThat(operationalEnvironment.getRelationshipList().getRelationship()).hasSize(0); + assertThat(operationalEnvironment.getResourceVersion()).isEqualTo("testResource"); + assertThat(operationalEnvironment.getTenantContext()).isEqualTo("testTenant"); + assertThat(operationalEnvironment.getOperationalEnvironmentType()).isEqualTo("testEnvType"); + assertThat(operationalEnvironment.getOperationalEnvironmentStatus()).isEqualTo("testEnvStatus"); + assertThat(operationalEnvironment.getOperationalEnvironmentName()).isEqualTo("testEnvName"); + } + @Test public void shouldProperlyConvertJsonToOperationalEnvironment() throws IOException { - OperationalEnvironment operationalEnvironment = OBJECT_MAPPER.readValue(OPERATIONAL_ENVIRONMENT_TEST, OperationalEnvironment.class); + OperationalEnvironment operationalEnvironment = + OBJECT_MAPPER.readValue(OPERATIONAL_ENVIRONMENT_TEST, OperationalEnvironment.class); assertThat(operationalEnvironment.getOperationalEnvironmentId()).isEqualTo("environmentId"); assertThat(operationalEnvironment.getWorkloadContext()).isEqualTo("workloadContext"); @@ -60,4 +85,5 @@ public class OperationalEnvironmentTest { assertThat(operationalEnvironment.getOperationalEnvironmentStatus()).isEqualTo("environmentStatus"); assertThat(operationalEnvironment.getOperationalEnvironmentName()).isEqualTo("environmentName"); } + } 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 deleted file mode 100644 index 4c0875320..000000000 --- a/vid-app-common/src/test/java/org/onap/vid/aai/ServicePropertiesTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * VID - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.vid.aai; - -import java.io.IOException; - -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 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/SubscriberAaiResponseTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberAaiResponseTest.java index ef2220c2d..b8dd2cfa7 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberAaiResponseTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberAaiResponseTest.java @@ -20,23 +20,39 @@ package org.onap.vid.aai; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; +import org.onap.vid.model.Subscriber; import org.onap.vid.model.SubscriberList; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + public class SubscriberAaiResponseTest { - private SubscriberAaiResponse createTestSubject() { - return new SubscriberAaiResponse(new SubscriberList(), "", 0); - } + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + private static final String SUBSCRIBER_JSON_EXAMPLE = "{\n" + + " \"global-customer-id\": \"id\",\n" + + " \"subscriber-name\": \"name\",\n" + + " \"subscriber-type\": \"type\",\n" + + " \"resource-version\": \"version\"\n" + + "}"; - @Test - public void testGetSubscriberList() throws Exception { - SubscriberAaiResponse testSubject; - SubscriberList result; + public void shouldGetSubscriberList() throws IOException { + Subscriber sampleSubscriber = + OBJECT_MAPPER.readValue(SUBSCRIBER_JSON_EXAMPLE, Subscriber.class); + List expectedListOfSubscribers = Arrays.asList(sampleSubscriber); + SubscriberList expectedSubscriberList = new SubscriberList(expectedListOfSubscribers); + SubscriberAaiResponse subscriberAaiResponse = new SubscriberAaiResponse(expectedSubscriberList, "msg", 200); + + assertEquals(subscriberAaiResponse.getSubscriberList(), expectedSubscriberList); + assertEquals(subscriberAaiResponse.getSubscriberList().customer.size(), 1); + assertEquals(subscriberAaiResponse.getSubscriberList().customer.get(0), sampleSubscriber); - // default test - testSubject = createTestSubject(); - result = testSubject.getSubscriberList(); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberFilteredResultsTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberFilteredResultsTest.java index 98b35aea4..f9668c960 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberFilteredResultsTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberFilteredResultsTest.java @@ -20,43 +20,89 @@ package org.onap.vid.aai; +import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Before; import org.junit.Test; +import org.onap.vid.model.Subscriber; import org.onap.vid.model.SubscriberList; import org.onap.vid.roles.EcompRole; import org.onap.vid.roles.Role; import org.onap.vid.roles.RoleValidator; +import static org.junit.Assert.assertEquals; + public class SubscriberFilteredResultsTest { - private SubscriberFilteredResults createTestSubject() { - ArrayList list = new ArrayList(); - list.add(new Role(EcompRole.READ, "a", "a", "a")); - RoleValidator rl=RoleValidator.by(list); - SubscriberList sl = new SubscriberList(); - sl.customer = new ArrayList(); - sl.customer.add(new org.onap.vid.model.Subscriber()); - return new SubscriberFilteredResults(rl, sl, "OK", 200); + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + private SubscriberFilteredResults subscriberFilteredResults; + private RoleValidator roleValidator; + private SubscriberList subscriberList; + private SubscriberListWithFilterData subscriberListWithFilterData; + + private static final String SUBSCRIBER_JSON_EXAMPLE = "{\n" + + " \"global-customer-id\": \"id\",\n" + + " \"subscriber-name\": \"name\",\n" + + " \"subscriber-type\": \"type\",\n" + + " \"resource-version\": \"version\"\n" + + "}"; + + @Before + public void setUp() throws IOException { + createTestSubject(); } @Test - public void testGetSubscriberList() throws Exception { - SubscriberFilteredResults testSubject; - SubscriberListWithFilterData result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getSubscriberList(); + public void testGetSubscriberList(){ + assertEquals(subscriberFilteredResults.getSubscriberList(), subscriberListWithFilterData); } @Test public void testSetSubscriberList() throws Exception { - SubscriberFilteredResults testSubject; - SubscriberListWithFilterData subscriberList = null; + subscriberList.customer = new ArrayList<>(); + subscriberList.customer.add(new Subscriber()); + SubscriberListWithFilterData expectedList = createSubscriberList(subscriberList,roleValidator); + subscriberFilteredResults.setSubscriberList(expectedList); + + assertEquals(subscriberFilteredResults.getSubscriberList(), expectedList); + } + + private void createTestSubject() throws IOException { + prepareRoleValidator(); + prepareSubscriberList(); + prepareSubscriberListWithFilterData(); + createSubscriberFilteredResults(); + } + + private void createSubscriberFilteredResults() { + subscriberFilteredResults = + new SubscriberFilteredResults(roleValidator, subscriberList, "OK", 200); + subscriberFilteredResults.setSubscriberList(subscriberListWithFilterData); + } + + private void prepareSubscriberListWithFilterData() { + subscriberListWithFilterData = createSubscriberList(subscriberList, roleValidator); + } + + private void prepareRoleValidator() { + ArrayList list = new ArrayList<>(); + list.add(new Role(EcompRole.READ, "a", "a", "a")); + roleValidator = RoleValidator.by(list); + } + + private void prepareSubscriberList() throws IOException { + Subscriber sampleSubscriber = + OBJECT_MAPPER.readValue(SUBSCRIBER_JSON_EXAMPLE, Subscriber.class); + List expectedListOfSubscribers = Collections.singletonList(sampleSubscriber); + subscriberList = new SubscriberList(expectedListOfSubscribers); + } - // default test - testSubject = createTestSubject(); - //testSubject.setSubscriberList(subscriberList); - testSubject.getSubscriberList(); + private SubscriberListWithFilterData createSubscriberList(SubscriberList subscriberList, + RoleValidator roleValidator){ + return new SubscriberListWithFilterData(subscriberList,roleValidator); } } 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 deleted file mode 100644 index 7ee3b6152..000000000 --- a/vid-app-common/src/test/java/org/onap/vid/aai/VnfResultTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * VID - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.vid.aai; - -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; - - -public class VnfResultTest { - - 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/ServicePropertiesTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/model/ServicePropertiesTest.java new file mode 100644 index 000000000..3eccba9b6 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/aai/model/ServicePropertiesTest.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.aai.model; + +import java.io.IOException; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.testng.annotations.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ServicePropertiesTest { + + 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 shouldAddAdditionalProperty() 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/model/VnfResultTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/model/VnfResultTest.java new file mode 100644 index 000000000..075d29f28 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/aai/model/VnfResultTest.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.aai.model; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Assert; +import org.testng.annotations.Test; + +import java.io.IOException; + +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEquals; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static org.assertj.core.api.Assertions.assertThat; + + +public class VnfResultTest { + + 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/util/AAIRestInterfaceTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java index 3ed59ed38..e64b2ac55 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * VID * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018 - 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. @@ -20,80 +20,276 @@ package org.onap.vid.aai.util; -import org.junit.Test; + +import org.mockito.Mock; +import org.mockito.Mockito; + +import org.onap.vid.aai.ExceptionWithRequestInfo; +import org.onap.vid.aai.exceptions.InvalidPropertyException; +import org.onap.vid.exceptions.GenericUncheckedException; +import org.onap.vid.utils.Unchecked; +import org.springframework.http.HttpMethod; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.util.Optional; +import java.util.UUID; + +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanConstructor; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static javax.ws.rs.core.Response.Status.*; +import static junit.framework.TestCase.assertSame; +import static junit.framework.TestCase.fail; +import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + public class AAIRestInterfaceTest { - /* - TO BE IMPLEMENTED - + private static final String PATH = "path"; + private static final String HTTP_LOCALHOST = "http://localhost/"; + @Mock + private Client client; + @Mock + private WebTarget webTarget; + @Mock + private Invocation.Builder builder; + @Mock + private Invocation invocation; + @Mock + private ServletRequestHelper servletRequestHelper; + @Mock + private HttpsAuthClient httpsAuthClient; + @Mock + private HttpServletRequest httpServletRequest; + @Mock + private Response response; + @Mock + private SystemPropertyHelper systemPropertyHelper; + + private AAIRestInterface testSubject; + + @BeforeMethod + public void setUp() throws Exception { + initMocks(this); + mockSystemProperties(); + testSubject = createTestSubject(); + when(client.target(HTTP_LOCALHOST+PATH)).thenReturn(webTarget); + when(webTarget.request()).thenReturn(builder); + when(builder.accept(Mockito.anyString())).thenReturn(builder); + when(builder.header(Mockito.anyString(), Mockito.anyString())).thenReturn(builder); + when(builder.build(Mockito.anyString())).thenReturn(invocation); + when(builder.build(Mockito.anyString(), any(Entity.class))).thenReturn(invocation); + when(invocation.invoke()).thenReturn(response); + when(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString()); + } + private AAIRestInterface createTestSubject() { - return new AAIRestInterface(""); + return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper); } @Test - public void testEncodeURL() throws Exception { - AAIRestInterface testSubject; - String nodeKey = ""; - String result; + public void shouldEncodeURL() throws Exception { + String nodeKey = "some unusual uri"; + String nodeKey2 = "some/usual/uri"; + Assert.assertEquals(testSubject.encodeURL(nodeKey), "some%20unusual%20uri"); + Assert.assertEquals(testSubject.encodeURL(nodeKey2), "some%2Fusual%2Furi"); + } - // default test - testSubject = createTestSubject(); - result = testSubject.encodeURL(nodeKey); + @Test + public void shouldSetRestSrvrBaseURL() { + String baseUrl = "anything"; + testSubject.setRestSrvrBaseURL(baseUrl); + Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl); } @Test - public void testSetRestSrvrBaseURL() throws Exception { - AAIRestInterface testSubject; - String baseURL = ""; + public void shouldExecuteRestJsonPutMethodWithResponse200() { + // given + String payload = "{\"id\": 1}"; + Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); - // test 1 - testSubject = createTestSubject(); - baseURL = null; - testSubject.SetRestSrvrBaseURL(baseURL); + // when + when(response.getStatusInfo()).thenReturn(OK); + Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse(); - // test 2 - testSubject = createTestSubject(); - baseURL = ""; - testSubject.SetRestSrvrBaseURL(baseURL); + // then + verify(builder).build(HttpMethod.PUT.name(), entity); + Assert.assertEquals(response, finalResponse); + } + + @Test(expectedExceptions = {ExceptionWithRequestInfo.class}) + public void shouldFailWhenRestJsonPutMethodExecuted() { + // given + String payload = "{\"id\": 1}"; + + // when + when(builder.build(eq(HttpMethod.PUT.name()), any(Entity.class))).thenThrow(new GenericUncheckedException("msg")); + testSubject.RestPut("", PATH, payload, false, true); + + //then } @Test - public void testGetRestSrvrBaseURL() throws Exception { - AAIRestInterface testSubject; - String result; + public void shouldExecuteRestJsonPutMethodWithResponse400() { + // given + String payload = "{\"id\": 1}"; + Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); - // default test - testSubject = createTestSubject(); - result = testSubject.getRestSrvrBaseURL(); + // when + when(response.getStatusInfo()).thenReturn(BAD_REQUEST); + when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode()); + Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse(); + + // then + verify(builder).build(HttpMethod.PUT.name(), entity); + Assert.assertEquals(response, finalResponse); } + @Test + public void shouldExecuteRestPostMethodWithResponse200() { + // given + String payload = "{\"id\": 1}"; + Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); + + // when + when(builder.post(Mockito.any(Entity.class))).thenReturn(response); + when(response.getStatusInfo()).thenReturn(OK); + Response finalResponse = testSubject.RestPost("", PATH, payload, false); + + // then + verify(builder).post(entity); + Assert.assertEquals(response, finalResponse); + } @Test - public void testRestPut() throws Exception { - AAIRestInterface testSubject; - String fromAppId = ""; - String transId = ""; - String path = ""; - String payload = ""; - boolean xml = false; - - // default test - testSubject = createTestSubject(); - testSubject.RestPut(fromAppId, transId, path, payload, xml); + public void shouldExecuteRestPostMethodWithResponse400() { + // given + String payload = "{\"id\": 1}"; + Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); + + // when + when(builder.post(Mockito.any(Entity.class))).thenReturn(response); + when(response.getStatusInfo()).thenReturn(BAD_REQUEST); + when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode()); + Response finalResponse = testSubject.RestPost("", PATH, payload, false); + + // then + verify(builder).post(entity); + Assert.assertEquals(response, finalResponse); } @Test - public void testRestPost() throws Exception { - AAIRestInterface testSubject; - String fromAppId = ""; - String transId = ""; - String path = ""; - String payload = ""; - boolean xml = false; - - // default test - testSubject = createTestSubject(); - testSubject.RestPost(fromAppId, transId, path, payload, xml); - }*/ + public void shouldFailWhenRestPostMethodExecuted() { + // given + String payload = "{\"id\": 1}"; + Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); + + // when + when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException()); + Response finalResponse = testSubject.RestPost("", PATH, payload, false); + + // then + verify(builder).post(entity); + Assert.assertNull(finalResponse); + } + + @Test + public void shouldExecuteRestDeleteMethodWithResponse400() { + // given + // when + when(builder.delete()).thenReturn(response); + when(response.getStatusInfo()).thenReturn(BAD_REQUEST); + String reason = "Any reason"; + when(response.readEntity(String.class)).thenReturn(reason); + when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode()); + boolean finalResponse = testSubject.Delete("", "", PATH); + + // then + verify(builder).delete(); + Assert.assertFalse(finalResponse); + } + + @Test + public void shouldExecuteRestDeleteMethodWithResponse404() { + // given + // when + when(builder.delete()).thenReturn(response); + when(response.getStatusInfo()).thenReturn(NOT_FOUND); + String reason = "Any reason"; + when(response.readEntity(String.class)).thenReturn(reason); + when(response.getStatus()).thenReturn(NOT_FOUND.getStatusCode()); + boolean finalResponse = testSubject.Delete("", "", PATH); + + // then + verify(builder).delete(); + Assert.assertFalse(finalResponse); + } + + @Test + public void shouldFailWhenRestDeleteExecuted() { + // given + // when + when(builder.delete()).thenThrow(new RuntimeException()); + boolean finalResponse = testSubject.Delete("", "", PATH); + // then + verify(builder).delete(); + Assert.assertFalse(finalResponse); + } + + @Test + public void shouldExecuteRestGetMethodWithResponse200() { + // given + // when + when(response.getStatusInfo()).thenReturn(OK); + Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse(); + + // then + Assert.assertEquals(response, finalResponse); + } + + @Test + public void shouldExecuteRestGetMethodWithResponse400() { + // given + // when + when(response.getStatusInfo()).thenReturn(BAD_REQUEST); + when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode()); + Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse(); + + // then + Assert.assertEquals(response, finalResponse); + } + + @Test + public void shouldFailWhenRestGetMethodExecuted() { + // given + // when + when(builder.build(HttpMethod.GET.name())).thenThrow(new RuntimeException()); + Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse(); + + // then + Assert.assertNull(finalResponse); + } + + private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException { + when(systemPropertyHelper.getEncodedCredentials()).thenReturn("someCredentials"); + when(systemPropertyHelper.getFullServicePath(Mockito.anyString())).thenReturn("http://localhost/path"); + when(systemPropertyHelper.getFullServicePath(Mockito.any(URI.class))).thenReturn("http://localhost/path"); + when(systemPropertyHelper.getServiceBasePath(Mockito.anyString())).thenReturn("http://localhost/path"); + } + } diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java deleted file mode 100644 index 3393aa7c1..000000000 --- a/vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java +++ /dev/null @@ -1,315 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * VID - * ================================================================================ - * Copyright (C) 2018 - 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.vid.aai.util; - - -import org.mockito.Mock; -import org.mockito.Mockito; - -import org.onap.vid.aai.ExceptionWithRequestInfo; -import org.onap.vid.aai.exceptions.InvalidPropertyException; -import org.onap.vid.exceptions.GenericUncheckedException; -import org.onap.vid.utils.Unchecked; -import org.springframework.http.HttpMethod; -import org.testng.Assert; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.util.Optional; -import java.util.UUID; - -import static javax.ws.rs.core.Response.Status.*; -import static junit.framework.TestCase.fail; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.initMocks; - - -public class SingleAAIRestInterfaceTest { - - private static final String PATH = "path"; - private static final String HTTP_LOCALHOST = "http://localhost/"; - @Mock - private Client client; - @Mock - private WebTarget webTarget; - @Mock - private Invocation.Builder builder; - @Mock - private Invocation invocation; - @Mock - private ServletRequestHelper servletRequestHelper; - @Mock - private HttpsAuthClient httpsAuthClient; - @Mock - private HttpServletRequest httpServletRequest; - @Mock - private Response response; - @Mock - private SystemPropertyHelper systemPropertyHelper; - - private AAIRestInterface testSubject; - - @BeforeMethod - public void setUp() throws Exception { - initMocks(this); - mockSystemProperties(); - testSubject = createTestSubject(); - when(client.target(HTTP_LOCALHOST+PATH)).thenReturn(webTarget); - when(webTarget.request()).thenReturn(builder); - when(builder.accept(Mockito.anyString())).thenReturn(builder); - when(builder.header(Mockito.anyString(), Mockito.anyString())).thenReturn(builder); - when(builder.build(Mockito.anyString())).thenReturn(invocation); - when(builder.build(Mockito.anyString(), any(Entity.class))).thenReturn(invocation); - when(invocation.invoke()).thenReturn(response); - when(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString()); - } - - private AAIRestInterface createTestSubject() { - return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper); - } - - @Test - public void testEncodeURL() throws Exception { - String nodeKey = "some unusual uri"; - Assert.assertEquals(testSubject.encodeURL(nodeKey), "some%20unusual%20uri"); - } - - @Test - public void testSetRestSrvrBaseURLWithNullValue() { - testSubject.SetRestSrvrBaseURL(null); - } - - @Test - public void testSetRestSrvrBaseURL() { - String baseUrl = "anything"; - testSubject.SetRestSrvrBaseURL(baseUrl); - Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl); - } - - @Test - public void testRestJsonPutWithResponse200() { - // given - String methodName = "RestPut"; - String payload = "{\"id\": 1}"; - Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); - - // when - when(response.getStatusInfo()).thenReturn(OK); - Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse(); - - // then - verify(builder).build(HttpMethod.PUT.name(), entity); - Assert.assertEquals(response, finalResponse); - } - - @Test(expectedExceptions = {ExceptionWithRequestInfo.class}) - public void testFailedRestJsonPut() { - // given - String methodName = "RestPut"; - String payload = "{\"id\": 1}"; - Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); - - // when - when(builder.build(eq(HttpMethod.PUT.name()), any(Entity.class))).thenThrow(new GenericUncheckedException("msg")); - Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse(); - - // then - fail("expected unreachable: exception to be thrown"); - } - - @Test - public void testRestJsonPutWithResponse400() { - // given - String methodName = "RestPut"; - String payload = "{\"id\": 1}"; - Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); - - // when - when(response.getStatusInfo()).thenReturn(BAD_REQUEST); - when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode()); - Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse(); - - // then - verify(builder).build(HttpMethod.PUT.name(), entity); - Assert.assertEquals(response, finalResponse); - } - - @Test - public void testRestPostWithResponse200() { - // given - String methodName = "RestPost"; - String payload = "{\"id\": 1}"; - Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); - - // when - when(builder.post(Mockito.any(Entity.class))).thenReturn(response); - when(response.getStatusInfo()).thenReturn(OK); - Response finalResponse = testSubject.RestPost("", PATH, payload, false); - - // then - verify(builder).post(entity); - Assert.assertEquals(response, finalResponse); - } - - @Test - public void testRestPostWithResponse400() { - // given - String methodName = "RestPost"; - String payload = "{\"id\": 1}"; - Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); - - // when - when(builder.post(Mockito.any(Entity.class))).thenReturn(response); - when(response.getStatusInfo()).thenReturn(BAD_REQUEST); - when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode()); - Response finalResponse = testSubject.RestPost("", PATH, payload, false); - - // then - verify(builder).post(entity); - Assert.assertEquals(response, finalResponse); - } - - @Test - public void testFailedRestPost() { - // given - String methodName = "RestPost"; - String payload = "{\"id\": 1}"; - Entity entity = Entity.entity(payload, MediaType.APPLICATION_JSON); - - // when - when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException()); - Response finalResponse = testSubject.RestPost("", PATH, payload, false); - - // then - verify(builder).post(entity); - Assert.assertEquals(finalResponse, null); - } - - @Test - public void testRestDeleteWithResponse400() { - // given - String methodName = "Delete"; - - // when - when(builder.delete()).thenReturn(response); - when(response.getStatusInfo()).thenReturn(BAD_REQUEST); - String reason = "Any reason"; - when(response.readEntity(String.class)).thenReturn(reason); - when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode()); - boolean finalResponse = testSubject.Delete("", "", PATH); - - // then - verify(builder).delete(); - Assert.assertFalse(finalResponse); - } - - @Test - public void testRestDeleteWithResponse404() { - // given - String methodName = "Delete"; - - // when - when(builder.delete()).thenReturn(response); - when(response.getStatusInfo()).thenReturn(NOT_FOUND); - String reason = "Any reason"; - when(response.readEntity(String.class)).thenReturn(reason); - when(response.getStatus()).thenReturn(NOT_FOUND.getStatusCode()); - boolean finalResponse = testSubject.Delete("", "", PATH); - - // then - verify(builder).delete(); - Assert.assertFalse(finalResponse); - } - - @Test - public void testFailedRestDelete() { - // given - String methodName = "Delete"; - - // when - when(builder.delete()).thenThrow(new RuntimeException()); - boolean finalResponse = testSubject.Delete("", "", PATH); - - // then - verify(builder).delete(); - Assert.assertFalse(finalResponse); - } - - @Test - public void testRestJsonGetWithResponse200() { - // given - String methodName = "RestGet"; - - // when - when(response.getStatusInfo()).thenReturn(OK); - Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse(); - - // then - Assert.assertEquals(response, finalResponse); - } - - @Test - public void testRestJsonGetWithResponse400() { - // given - String methodName = "RestGet"; - - // when - when(response.getStatusInfo()).thenReturn(BAD_REQUEST); - when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode()); - Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse(); - - // then - Assert.assertEquals(response, finalResponse); - } - - @Test - public void testFailedRestGet() { - // given - String methodName = "RestGet"; - - // when - when(builder.build(HttpMethod.GET.name())).thenThrow(new RuntimeException()); - Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse(); - - // then - Assert.assertEquals(finalResponse, null); - } - - private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException { - when(systemPropertyHelper.getEncodedCredentials()).thenReturn("someCredentials"); - when(systemPropertyHelper.getFullServicePath(Mockito.anyString())).thenReturn("http://localhost/path"); - when(systemPropertyHelper.getFullServicePath(Mockito.any(URI.class))).thenReturn("http://localhost/path"); - when(systemPropertyHelper.getServiceBasePath(Mockito.anyString())).thenReturn("http://localhost/path"); - } - -} -- cgit 1.2.3-korg