2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.aai;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.xebialabs.restito.semantics.Action;
25 import io.joshworks.restclient.http.HttpResponse;
26 import io.joshworks.restclient.http.mapper.ObjectMapper;
27 import org.assertj.core.api.Assertions;
28 import org.glassfish.grizzly.http.util.HttpStatus;
29 import org.json.simple.parser.JSONParser;
30 import org.json.simple.parser.ParseException;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.vid.aai.model.AaiNodeQueryResponse;
38 import org.onap.vid.aai.model.ResourceType;
39 import org.onap.vid.client.SyncRestClient;
40 import org.onap.vid.model.SubscriberList;
41 import org.onap.vid.testUtils.StubServerUtil;
43 import java.io.IOException;
45 @RunWith(MockitoJUnitRunner.class)
46 public class AaiOverTLSClientServerTest {
49 private AaiOverTLSPropertySupplier propertySupplier;
51 private static StubServerUtil serverUtil;
53 private String searchNodesQueryResponsePayload =
55 + "\"result-data\": [\n"
57 + "\"resource-type\": \"generic-vnf\",\n"
58 + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/6eac8e69-c98d-4ac5-ab90-69fe0cabda76\"\n"
61 + "\"resource-type\": \"generic-vnf\",\n"
62 + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/e3766bc5-40a7-4dbe-9d4a-1d8c8f284913\"\n"
65 + "\"resource-type\": \"generic-vnf\",\n"
66 + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/6aa153ee-6637-4b49-beb5-a5e756e00393\"\n"
69 + "\"resource-type\": \"generic-vnf\",\n"
70 + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/5a981c30-de25-4ea9-98fa-ed398f13ea41\"\n"
73 + "\"resource-type\": \"generic-vnf\",\n"
74 + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/b0ef2271-8ac0-4268-b9a5-09cb50c20c85\"\n"
79 private String subscribersResponsePayload =
83 + "\"global-customer-id\": \"DemoCust_752df078-d8e9-4731-abf6-8ae7348075bb\",\n"
84 + "\"subscriber-name\": \"DemoCust_752df078-d8e9-4731-abf6-8ae7348075bb\",\n"
85 + "\"subscriber-type\": \"INFRA\",\n"
86 + "\"resource-version\": \"1536158347585\"\n"
89 + "\"global-customer-id\": \"DemoCust_62bf43a3-4888-4c82-ae98-3ebc3d782761\",\n"
90 + "\"subscriber-name\": \"DemoCust_62bf43a3-4888-4c82-ae98-3ebc3d782761\",\n"
91 + "\"subscriber-type\": \"INFRA\",\n"
92 + "\"resource-version\": \"1536240894581\"\n"
95 + "\"global-customer-id\": \"DemoCust_e84256d6-ef3e-4a28-9741-9987019c3a8f\",\n"
96 + "\"subscriber-name\": \"DemoCust_e84256d6-ef3e-4a28-9741-9987019c3a8f\",\n"
97 + "\"subscriber-type\": \"INFRA\",\n"
98 + "\"resource-version\": \"1536330956393\"\n"
101 + "\"global-customer-id\": \"ETE_Customer_377bb124-2638-4025-a315-cdae04f52bce\",\n"
102 + "\"subscriber-name\": \"ETE_Customer_377bb124-2638-4025-a315-cdae04f52bce\",\n"
103 + "\"subscriber-type\": \"INFRA\",\n"
104 + "\"resource-version\": \"1536088625538\"\n"
110 public static void setUpClass(){
111 serverUtil = new StubServerUtil();
112 serverUtil.runServer();
116 public static void tearDown(){
117 serverUtil.stopServer();
121 public void shouldSearchNodeTypeByName() throws IOException, ParseException {
122 ObjectMapper objectMapper = getFasterXmlObjectMapper();
123 AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper), propertySupplier, serverUtil.constructTargetUrl("http", ""));
125 serverUtil.prepareGetCall("/search/nodes-query", new JSONParser().parse(searchNodesQueryResponsePayload), Action.status(HttpStatus.OK_200));
127 HttpResponse<AaiNodeQueryResponse> aaiNodeQueryResponseHttpResponse = aaiOverTLSClient
128 .searchNodeTypeByName("any", ResourceType.GENERIC_VNF);
130 AaiNodeQueryResponse body = aaiNodeQueryResponseHttpResponse.getBody();
131 Assertions.assertThat(body.resultData.size()).isEqualTo(5);
132 Assertions.assertThat(aaiNodeQueryResponseHttpResponse.getStatus()).isEqualTo(200);
136 public void shouldGetSubscribers() throws ParseException, JsonProcessingException {
137 ObjectMapper objectMapper = getCodehausObjectMapper();
138 AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper), propertySupplier, serverUtil.constructTargetUrl("http", ""));
140 serverUtil.prepareGetCall("/business/customers", new JSONParser().parse(subscribersResponsePayload), Action.status(HttpStatus.OK_200));
142 HttpResponse<SubscriberList> allSubscribers = aaiOverTLSClient.getAllSubscribers();
144 SubscriberList subscriberList = allSubscribers.getBody();
145 Assertions.assertThat(subscriberList.customer.size()).isEqualTo(4);
146 Assertions.assertThat(allSubscribers.getStatus()).isEqualTo(200);
149 private ObjectMapper getCodehausObjectMapper() {
150 return new ObjectMapper() {
152 org.codehaus.jackson.map.ObjectMapper om = new org.codehaus.jackson.map.ObjectMapper();
155 public <T> T readValue(String s, Class<T> aClass) {
157 return om.readValue(s, aClass);
158 } catch (IOException e) {
159 throw new RuntimeException(e);
164 public String writeValue(Object o) {
166 return om.writeValueAsString(o);
167 } catch (IOException e) {
168 throw new RuntimeException(e);
174 private ObjectMapper getFasterXmlObjectMapper() {
175 return new ObjectMapper() {
177 com.fasterxml.jackson.databind.ObjectMapper om = new com.fasterxml.jackson.databind.ObjectMapper();
180 public <T> T readValue(String s, Class<T> aClass) {
182 return om.readValue(s, aClass);
183 } catch (IOException e) {
184 throw new RuntimeException(e);
189 public String writeValue(Object o) {
191 return om.writeValueAsString(o);
192 } catch (IOException e) {
193 throw new RuntimeException(e);