diff options
Diffstat (limited to 'vid-app-common/src/test')
16 files changed, 581 insertions, 476 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 4655292c6..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,45 +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=new RoleValidator(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"); - } - -} diff --git a/vid-app-common/src/test/java/org/onap/vid/bl/AaiServiceTest.java b/vid-app-common/src/test/java/org/onap/vid/bl/AaiServiceTest.java index 9f1dc84c0..1b50681fc 100644 --- a/vid-app-common/src/test/java/org/onap/vid/bl/AaiServiceTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/bl/AaiServiceTest.java @@ -20,15 +20,29 @@ package org.onap.vid.bl; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.arrayWithSize; +import static org.hamcrest.Matchers.equalTo; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.onap.vid.aai.AaiClientInterface; import org.onap.vid.aai.AaiResponse; -import org.onap.vid.aai.model.*; +import org.onap.vid.aai.model.AaiGetPnfResponse; import org.onap.vid.aai.model.AaiGetPnfs.Pnf; import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse; +import org.onap.vid.aai.model.LogicalLinkResponse; +import org.onap.vid.aai.model.Relationship; +import org.onap.vid.aai.model.RelationshipData; +import org.onap.vid.aai.model.RelationshipList; +import org.onap.vid.aai.model.ServiceRelationships; import org.onap.vid.roles.Role; import org.onap.vid.roles.RoleValidator; import org.onap.vid.services.AaiServiceImpl; @@ -36,16 +50,6 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.arrayWithSize; -import static org.hamcrest.Matchers.equalTo; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; - public class AaiServiceTest { @InjectMocks @@ -164,7 +168,7 @@ public class AaiServiceTest { AaiResponse<GetTenantsResponse[]> aaiResponse = new AaiResponse<>(getTenantsResponses, null, 200); Mockito.doReturn(aaiResponse).when(aaiClientInterface).getTenants(serviceGlobalCustomerId, serviceServiceType); Role role = new Role(null, userGlobalCustomerId, userServiceType, userTenantName); - RoleValidator roleValidator = new RoleValidator(Collections.singletonList(role)); + RoleValidator roleValidator = RoleValidator.by(Collections.singletonList(role)); AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidator); assertThat(actualTenants.getT(), arrayWithSize(1)); diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java index 6d4508dbf..0abf6cd57 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java @@ -3,13 +3,14 @@ * VID * ================================================================================ * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Nokia. * ================================================================================ * 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,33 +21,41 @@ package org.onap.vid.controller; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import org.mockito.InjectMocks; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; import org.onap.vid.aai.AaiResponseTranslator; +import org.onap.vid.aai.util.AAIRestInterface; +import org.onap.vid.roles.RoleProvider; import org.onap.vid.services.AaiService; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import java.util.Map; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; +import org.onap.vid.utils.SystemPropertiesWrapper; +@RunWith(MockitoJUnitRunner.class) public class AaiControllerTest { - @InjectMocks - AaiController aaiController = new AaiController(); - @Mock - AaiService aaiService; + private AaiService aaiService; + @Mock + private AAIRestInterface aaiRestInterface; + @Mock + private RoleProvider roleProvider; + @Mock + private SystemPropertiesWrapper systemPropertiesWrapper; - @BeforeMethod - public void initMocks(){ - MockitoAnnotations.initMocks(this); + private AaiController aaiController; + + @Before + public void setUp(){ + aaiController = new AaiController(aaiService, aaiRestInterface, roleProvider, systemPropertiesWrapper); } @Test @@ -70,7 +79,4 @@ public class AaiControllerTest { "c", toBeReturnedForC ))); } - - - } diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/ControllersUtilsTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/ControllersUtilsTest.java index f084b3dec..80836d155 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/ControllersUtilsTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/ControllersUtilsTest.java @@ -21,16 +21,25 @@ package org.onap.vid.controller; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; +import org.apache.commons.lang.exception.ExceptionUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Answers; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.model.ExceptionResponse; import org.onap.vid.utils.SystemPropertiesWrapper; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; @RunWith(MockitoJUnitRunner.class) public class ControllersUtilsTest { @@ -110,4 +119,26 @@ public class ControllersUtilsTest { // THEN assertThat(loginId).isEmpty(); } + + @Test + public void shouldCreateResponseEntity_fromGivenException() { + // GIVEN + EELFLoggerDelegate eelfLoggerDelegate = mock(EELFLoggerDelegate.class); + Response response = mock(Response.class); + given(response.getStatus()).willReturn(HttpStatus.OK.value()); + WebApplicationException webApplicationException = new WebApplicationException("ErrorMessage", response); + + // WHEN + ResponseEntity responseEntity = ControllersUtils + .handleWebApplicationException(webApplicationException, eelfLoggerDelegate); + + // THEN + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(responseEntity.getBody()) + .isInstanceOfSatisfying(ExceptionResponse.class, + er -> assertThat(er.getMessage()).isEqualTo("ErrorMessage (Request id: null)")); + then(eelfLoggerDelegate).should() + .error(EELFLoggerDelegate.errorLogger, "{}: {}", "handleWebApplicationException", + ExceptionUtils.getMessage(webApplicationException), webApplicationException); + } }
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/ServicePermissionsTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/ServicePermissionsTest.java index abdf31572..36af92c0c 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/ServicePermissionsTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/ServicePermissionsTest.java @@ -20,14 +20,6 @@ package org.onap.vid.controller; -import org.jetbrains.annotations.NotNull; -import org.onap.vid.aai.model.Permissions; -import org.onap.vid.roles.RoleProvider; -import org.onap.vid.roles.RoleValidator; -import org.springframework.mock.web.MockHttpServletRequest; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - import static java.lang.Boolean.FALSE; import static java.lang.Boolean.TRUE; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; @@ -37,6 +29,14 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import org.jetbrains.annotations.NotNull; +import org.onap.vid.aai.model.Permissions; +import org.onap.vid.roles.RoleProvider; +import org.onap.vid.roles.RoleValidator; +import org.springframework.mock.web.MockHttpServletRequest; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + public class ServicePermissionsTest { @DataProvider diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/AlwaysValidRoleValidatorTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/AlwaysValidRoleValidatorTest.java new file mode 100644 index 000000000..363c6ff76 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/roles/AlwaysValidRoleValidatorTest.java @@ -0,0 +1,43 @@ +/*- + * ============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.roles; + +import static org.testng.Assert.assertTrue; + +import org.testng.annotations.Test; + +public class AlwaysValidRoleValidatorTest { + + @Test + public void testIsSubscriberPermitted() { + assertTrue(new AlwaysValidRoleValidator().isSubscriberPermitted("any")); + } + + @Test + public void testIsServicePermitted() { + assertTrue(new AlwaysValidRoleValidator().isServicePermitted("any", "any")); + } + + @Test + public void testIsTenantPermitted() { + assertTrue(new AlwaysValidRoleValidator().isTenantPermitted("any", "any", "any")); + } +}
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorByRolesTest.java index 69ec3458e..9362ec9d7 100644 --- a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorByRolesTest.java @@ -21,18 +21,17 @@ package org.onap.vid.roles; +import static org.assertj.core.api.Assertions.assertThat; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import java.util.List; +import java.util.Map; import org.onap.vid.mso.rest.RequestDetails; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.List; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; - -public class RoleValidatorTest { +public class RoleValidatorByRolesTest { private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber"; private static final String NOT_MATCHING_SUBSCRIBER = "notMatchingSubscriber"; @@ -47,12 +46,11 @@ public class RoleValidatorTest { private Map<String, Object> requestParameters = ImmutableMap.of("subscriptionServiceType", SAMPLE_SERVICE_TYPE); private Map<String, Object> requestDetailsProperties = ImmutableMap.of("subscriberInfo", subscriberInfo, "requestParameters", requestParameters); private RequestDetails requestDetails; - private RoleValidator roleValidator; + private RoleValidatorByRoles roleValidator; @BeforeMethod public void setUp() { - roleValidator = new RoleValidator(roles); - roleValidator.enableRoles(); + roleValidator = new RoleValidatorByRoles(roles); requestDetails = new RequestDetails(); } diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceImplTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceImplTest.java index 59cee89b2..e9f94ca0e 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceImplTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceImplTest.java @@ -44,6 +44,7 @@ import org.onap.vid.aai.model.AaiGetServicesRequestModel.GetServicesAAIRespone; import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse; import org.onap.vid.aai.model.VnfResult; import org.onap.vid.roles.RoleValidator; +import org.onap.vid.roles.RoleValidatorByRoles; public class AaiServiceImplTest { @@ -137,7 +138,7 @@ public class AaiServiceImplTest { when(response.getT()).thenReturn(new GetTenantsResponse[]{ permittedTenant, unpermittedTenant }); when(aaiClient.getTenants(globalCustomerId, serviceType)).thenReturn(response); - RoleValidator roleValidator = mock(RoleValidator.class); + RoleValidator roleValidator = mock(RoleValidatorByRoles.class); when(roleValidator.isTenantPermitted(globalCustomerId, serviceType, "permitted_tenant")).thenReturn(true); when(roleValidator.isTenantPermitted(globalCustomerId, serviceType, "unpermitted_tenant")).thenReturn(false); @@ -202,7 +203,7 @@ public class AaiServiceImplTest { @SuppressWarnings("unchecked") public void getServicesShouldMarkAllServicesAsPermitted() { // given - RoleValidator roleValidator = modelGenerator.nextObject(RoleValidator.class); + RoleValidator roleValidator = modelGenerator.nextObject(RoleValidatorByRoles.class); GetServicesAAIRespone inputPayload = modelGenerator.nextObject(GetServicesAAIRespone.class); assertThat(inputPayload.service.stream().allMatch(service -> service.isPermitted)).isFalse(); diff --git a/vid-app-common/src/test/resources/WEB-INF/conf/system.properties b/vid-app-common/src/test/resources/WEB-INF/conf/system.properties index fce0d1aeb..a0f0dbe19 100644 --- a/vid-app-common/src/test/resources/WEB-INF/conf/system.properties +++ b/vid-app-common/src/test/resources/WEB-INF/conf/system.properties @@ -71,6 +71,8 @@ application_name = Virtual Infrastructure Deployment element_map_file_path = app/fusionapp/files/ element_map_icon_path = app/fusionapp/icons/ +role_management_activated = false + #aai related properties #dev server #ist servers |