From 8e7db0db55f8910b19898d2b401e6ea15e115b33 Mon Sep 17 00:00:00 2001 From: golabek Date: Wed, 29 Aug 2018 15:29:56 +0200 Subject: Nodes query rewritten to generic rest client Added AAI Rest Client based on new generic client Change-Id: I561f8a5d95ec35fdddc648b24965dd9dac03c9a4 Issue-ID: VID-269 Signed-off-by: golabek --- .../onap/vid/aai/AaiOverTLSClientServerTest.java | 210 +++++++++++++++++++++ .../org/onap/vid/aai/AaiOverTLSClientTest.java | 75 ++++++++ .../vid/config/JobCommandsConfigWithMockedMso.java | 40 +++- 3 files changed, 323 insertions(+), 2 deletions(-) create mode 100644 vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientServerTest.java create mode 100644 vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientTest.java (limited to 'vid-app-common/src/test') diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientServerTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientServerTest.java new file mode 100644 index 000000000..f2b26a801 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientServerTest.java @@ -0,0 +1,210 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2018 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.aai; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.xebialabs.restito.semantics.Action; +import io.joshworks.restclient.http.HttpResponse; +import io.joshworks.restclient.http.mapper.ObjectMapper; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import org.apache.commons.io.IOUtils; +import org.assertj.core.api.Assertions; +import org.glassfish.grizzly.http.util.HttpStatus; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.vid.aai.model.AaiNodeQueryResponse; +import org.onap.vid.aai.model.ResourceType; +import org.onap.vid.client.SyncRestClient; +import org.onap.vid.testUtils.StubServerUtil; + +@RunWith(MockitoJUnitRunner.class) +public class AaiOverTLSClientServerTest { + + @Mock + private AaiOverTLSPropertySupplier propertySupplier; + + private static StubServerUtil serverUtil; + + private String responsePayload = + "{\n" + + "\"result-data\": [\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/6eac8e69-c98d-4ac5-ab90-69fe0cabda76\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/35305ca7-ad59-4b9b-9d21-1dd2b5103968\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/1ed7a7ef-e9b8-4fad-8e10-92d0f714acc6\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/5054bf7f-7913-4307-8bcd-aecce8b7539c\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/7b484da9-8ac1-406c-9c3f-ffcf0437047d\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/8e4ac9cb-c1d3-4a4b-9b1b-3bc60dc4c22d\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/b04f78e2-2f09-4dd5-bd9d-c6045036966f\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/e1e44c20-8803-416d-a4ba-c665de36f1aa\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/823f40cc-683a-4591-b82a-d6457a18e1bb\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/76f8282a-6099-4d2c-9f8b-b636ba486c23\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/c6039a4b-54e8-40a5-817d-84d8e87387e8\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/5ba68684-1c61-48b9-872f-de483fdc5cdb\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/b0400a1f-4dad-434a-bb36-ac13ef6afbc3\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/1142a64e-dd13-425e-a218-bf562365dfc9\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/98c34cbf-da33-4659-9797-4729d2a481df\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/b043e61c-a73a-446b-83ee-4751cac700e3\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/82ae1189-c4b9-45e4-870b-95160be90dae\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/75ddc2c9-d61b-49d9-9d57-22473fd0b7fe\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/b4443ed1-cfb5-4cc0-aa78-598942354285\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/c72519e2-3b76-494f-b742-524abd82b6d0\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/f6561cb3-7a23-44cc-98e3-3a6275e5f340\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/e3766bc5-40a7-4dbe-9d4a-1d8c8f284913\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/6aa153ee-6637-4b49-beb5-a5e756e00393\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/5a981c30-de25-4ea9-98fa-ed398f13ea41\"\n" + + "},\n" + + " {\n" + + "\"resource-type\": \"generic-vnf\",\n" + + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/b0ef2271-8ac0-4268-b9a5-09cb50c20c85\"\n" + + "}\n" + + "],\n" + + "}"; + + + @BeforeClass + public static void setUpClass(){ + serverUtil = new StubServerUtil(); + serverUtil.runServer(); + } + + @AfterClass + public static void tearDown(){ + serverUtil.stopServer(); + } + + @Test + public void shouldSearchNodeTypeByName() throws IOException, ParseException { + ObjectMapper objectMapper = new ObjectMapper() { + + com.fasterxml.jackson.databind.ObjectMapper om = new com.fasterxml.jackson.databind.ObjectMapper(); + + @Override + public T readValue(String s, Class aClass) { + try { + return om.readValue(s, aClass); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public String writeValue(Object o) { + try { + return om.writeValueAsString(o); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + }; + AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper), propertySupplier, serverUtil.constructTargetUrl("http", "")); + + serverUtil.prepareGetCall("/search/nodes-query", new JSONParser().parse(responsePayload), Action.status(HttpStatus.OK_200)); + + HttpResponse aaiNodeQueryResponseHttpResponse = aaiOverTLSClient + .searchNodeTypeByName("any", ResourceType.GENERIC_VNF); + + AaiNodeQueryResponse body = aaiNodeQueryResponseHttpResponse.getBody(); + Assertions.assertThat(body.resultData.size()).isEqualTo(25); + Assertions.assertThat(aaiNodeQueryResponseHttpResponse.getStatus()).isEqualTo(200); + } + + + +} diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientTest.java new file mode 100644 index 000000000..5b631a643 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientTest.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2018 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.aai; + +import com.google.common.collect.ImmutableMap; +import java.util.Collections; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.vid.aai.model.AaiNodeQueryResponse; +import org.onap.vid.aai.model.ResourceType; +import org.onap.vid.client.SyncRestClient; + +@RunWith(MockitoJUnitRunner.class) +public class AaiOverTLSClientTest { + + public static final String SEARCH_NODES_QUERY_SEARCH_NODE_TYPE = "search/nodes-query?search-node-type=generic-vnf&filter=vnf-name:EQUALS:name"; + private AaiOverTLSClient aaiRestClient; + + @Mock + private SyncRestClient syncRestClient; + @Mock + private AaiOverTLSPropertySupplier propertySupplier; + + @Before + public void setUp() { + aaiRestClient = new AaiOverTLSClient(syncRestClient, propertySupplier); + } + + @Test + public void searchNodeTypeByName() { + mockPropertyReader(); + + aaiRestClient.searchNodeTypeByName("name", ResourceType.GENERIC_VNF); + Mockito.verify(syncRestClient).get(Matchers.contains(SEARCH_NODES_QUERY_SEARCH_NODE_TYPE), + Matchers.eq(getHeaders()), Matchers.eq(Collections.emptyMap()), Matchers.eq(AaiNodeQueryResponse.class)); + } + + private void mockPropertyReader() { + Mockito.when(propertySupplier.getPassword()).thenReturn("Pass"); + Mockito.when(propertySupplier.getUsername()).thenReturn("User"); + Mockito.when(propertySupplier.getRequestId()).thenReturn("1"); + Mockito.when(propertySupplier.getRandomUUID()).thenReturn("2"); + } + + private Map getHeaders(){ + return ImmutableMap.builder().put("Authorization", "Basic VXNlcjpQYXNz"). + put("X-FromAppId", "VidAaiController").put("Accept", "application/json").put("X-ECOMP-RequestID", "1"). + put("X-TransactionId", "2").put("Content-Type", "application/json").build(); + } + +} \ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/config/JobCommandsConfigWithMockedMso.java b/vid-app-common/src/test/java/org/onap/vid/config/JobCommandsConfigWithMockedMso.java index 56ac28d2e..e5b3c365a 100644 --- a/vid-app-common/src/test/java/org/onap/vid/config/JobCommandsConfigWithMockedMso.java +++ b/vid-app-common/src/test/java/org/onap/vid/config/JobCommandsConfigWithMockedMso.java @@ -21,13 +21,20 @@ package org.onap.vid.config; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; import org.hibernate.SessionFactory; import org.mockito.Mockito; import org.onap.portalsdk.core.service.DataAccessService; import org.onap.vid.aai.AaiClientInterface; +import org.onap.vid.aai.AaiOverTLSClient; +import org.onap.vid.aai.AaiOverTLSClientInterface; +import org.onap.vid.aai.AaiOverTLSPropertySupplier; import org.onap.vid.aai.util.HttpsAuthClient; import org.onap.vid.aai.util.SSLContextProvider; import org.onap.vid.aai.util.SystemPropertyHelper; +import org.onap.vid.client.SyncRestClient; import org.onap.vid.job.JobAdapter; import org.onap.vid.job.JobsBrokerService; import org.onap.vid.job.command.InProgressStatusCommand; @@ -83,13 +90,42 @@ public class JobCommandsConfigWithMockedMso { return jobWorker; } + @Bean + public AaiOverTLSClientInterface AaiOverTLSClient(){ + io.joshworks.restclient.http.mapper.ObjectMapper objectMapper = new io.joshworks.restclient.http.mapper.ObjectMapper() { + + ObjectMapper om = new ObjectMapper(); + + @Override + public T readValue(String s, Class aClass) { + try { + return om.readValue(s, aClass); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public String writeValue(Object o) { + try { + return om.writeValueAsString(o); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + }; + + return new AaiOverTLSClient(new SyncRestClient(objectMapper), new AaiOverTLSPropertySupplier()); + } + @Bean public AsyncInstantiationBusinessLogic asyncInstantiationBusinessLogic(DataAccessService dataAccessService, JobAdapter jobAdapter, JobsBrokerService jobsBrokerService, SessionFactory sessionFactory, - AaiClientInterface aaiClient) { - return new AsyncInstantiationBusinessLogicImpl(dataAccessService, jobAdapter, jobsBrokerService, sessionFactory, aaiClient); + AaiClientInterface aaiClient, + AaiOverTLSClientInterface aaiOverTLSClientInterface) { + return new AsyncInstantiationBusinessLogicImpl(dataAccessService, jobAdapter, jobsBrokerService, sessionFactory, aaiClient, aaiOverTLSClientInterface); } @Bean -- cgit 1.2.3-korg