diff options
author | michal.banka <michal.banka@nokia.com> | 2019-04-03 12:29:10 +0200 |
---|---|---|
committer | michal.banka <michal.banka@nokia.com> | 2019-04-04 10:22:43 +0200 |
commit | d3076db84866ee94c3532699714b9e8c2e86593d (patch) | |
tree | 66076fcbe6d2d8d47c1401190c32d34fdcd0d992 /vid-app-common/src/test/java/org/onap | |
parent | 13d5d23cf1e7fcd52244ef284019a0eb280bb14a (diff) |
Increase test coverage for vid/aii - third part
Change-Id: Ib4da72c1da212cf009f70694ffcb0be4049cb67e
Issue-ID: VID-385
Signed-off-by: michal.banka <michal.banka@nokia.com>
Diffstat (limited to 'vid-app-common/src/test/java/org/onap')
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/aai/AaiGetVnfResponseTest.java | 88 | ||||
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/aai/OperationalEnvironmentTest.java | 28 | ||||
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/aai/SubscriberAaiResponseTest.java | 36 | ||||
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/aai/SubscriberFilteredResultsTest.java | 88 | ||||
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/aai/model/ServicePropertiesTest.java (renamed from vid-app-common/src/test/java/org/onap/vid/aai/ServicePropertiesTest.java) | 6 | ||||
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/aai/model/VnfResultTest.java (renamed from vid-app-common/src/test/java/org/onap/vid/aai/VnfResultTest.java) | 7 | ||||
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java | 302 | ||||
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java | 315 |
8 files changed, 446 insertions, 424 deletions
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<VnfResult> 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<String, Object> 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<String,Object> 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; @@ -48,8 +52,29 @@ public class OperationalEnvironmentTest { "}"; @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/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<Subscriber> 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<Role> list = new ArrayList<Role>(); - list.add(new Role(EcompRole.READ, "a", "a", "a")); - RoleValidator rl=RoleValidator.by(list); - SubscriberList sl = new SubscriberList(); - sl.customer = new ArrayList<org.onap.vid.model.Subscriber>(); - 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<Role> 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<Subscriber> 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/ServicePropertiesTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/model/ServicePropertiesTest.java index 4c0875320..3eccba9b6 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/model/ServicePropertiesTest.java @@ -18,12 +18,11 @@ * ============LICENSE_END========================================================= */ -package org.onap.vid.aai; +package org.onap.vid.aai.model; 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; @@ -82,10 +81,9 @@ public class ServicePropertiesTest { } @Test - public void shouldProperlyAddAdditionalProperty() throws IOException { + public void shouldAddAdditionalProperty() throws IOException { ServiceProperties serviceProperties = OBJECT_MAPPER.readValue(SERVICE_PROPERTIES_JSON, ServiceProperties.class); - serviceProperties.setAdditionalProperty("additional", "property"); assertThat(serviceProperties.getAdditionalProperties()) 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/model/VnfResultTest.java index 7ee3b6152..075d29f28 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/model/VnfResultTest.java @@ -18,14 +18,16 @@ * ============LICENSE_END========================================================= */ -package org.onap.vid.aai; +package org.onap.vid.aai.model; import com.fasterxml.jackson.databind.ObjectMapper; -import org.onap.vid.aai.model.VnfResult; +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; @@ -68,7 +70,6 @@ public class VnfResultTest { "\t}]\n" + "}"; - @Test public void shouldProperlyConvertJsonToVnfResult() throws IOException { VnfResult vnfResult = OBJECT_MAPPER.readValue(VNF_RESULT_JSON, VnfResult.class); 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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"); - } - -} |