d4f59e7da2c3285083051cc0f13163b44783ea43
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / AaiOverTLSClientServerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.vid.aai;
22
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;
42
43 import java.io.IOException;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class AaiOverTLSClientServerTest {
47
48     @Mock
49     private AaiOverTLSPropertySupplier propertySupplier;
50
51     private static StubServerUtil serverUtil;
52
53     private String searchNodesQueryResponsePayload =
54         "{\n"
55             + "\"result-data\": [\n"
56             + "  {\n"
57             + "\"resource-type\": \"generic-vnf\",\n"
58             + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/6eac8e69-c98d-4ac5-ab90-69fe0cabda76\"\n"
59             + "},\n"
60             + "  {\n"
61             + "\"resource-type\": \"generic-vnf\",\n"
62             + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/e3766bc5-40a7-4dbe-9d4a-1d8c8f284913\"\n"
63             + "},\n"
64             + "  {\n"
65             + "\"resource-type\": \"generic-vnf\",\n"
66             + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/6aa153ee-6637-4b49-beb5-a5e756e00393\"\n"
67             + "},\n"
68             + "  {\n"
69             + "\"resource-type\": \"generic-vnf\",\n"
70             + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/5a981c30-de25-4ea9-98fa-ed398f13ea41\"\n"
71             + "},\n"
72             + "  {\n"
73             + "\"resource-type\": \"generic-vnf\",\n"
74             + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/b0ef2271-8ac0-4268-b9a5-09cb50c20c85\"\n"
75             + "}\n"
76             + "],\n"
77             + "}";
78
79     private String subscribersResponsePayload =
80         "{\n"
81         + "\"customer\": [\n"
82         + "  {\n"
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"
87         + "},\n"
88         + "  {\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"
93         + "},\n"
94         + "  {\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"
99         + "},\n"
100         + "  {\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"
105         + "}\n"
106         + "],\n"
107         + "}";
108
109     @BeforeClass
110     public static void setUpClass(){
111         serverUtil = new StubServerUtil();
112         serverUtil.runServer();
113     }
114
115     @AfterClass
116     public static void tearDown(){
117         serverUtil.stopServer();
118     }
119
120     @Test
121     public void shouldSearchNodeTypeByName() throws IOException, ParseException {
122         ObjectMapper objectMapper = getFasterXmlObjectMapper();
123         AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper),  propertySupplier, serverUtil.constructTargetUrl("http", ""));
124
125         serverUtil.prepareGetCall("/search/nodes-query", new JSONParser().parse(searchNodesQueryResponsePayload), Action.status(HttpStatus.OK_200));
126
127         HttpResponse<AaiNodeQueryResponse> aaiNodeQueryResponseHttpResponse = aaiOverTLSClient
128             .searchNodeTypeByName("any", ResourceType.GENERIC_VNF);
129
130         AaiNodeQueryResponse body = aaiNodeQueryResponseHttpResponse.getBody();
131         Assertions.assertThat(body.resultData.size()).isEqualTo(5);
132         Assertions.assertThat(aaiNodeQueryResponseHttpResponse.getStatus()).isEqualTo(200);
133     }
134
135     @Test
136     public void shouldGetSubscribers() throws ParseException, JsonProcessingException {
137         ObjectMapper objectMapper = getCodehausObjectMapper();
138         AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper),  propertySupplier, serverUtil.constructTargetUrl("http", ""));
139
140         serverUtil.prepareGetCall("/business/customers", new JSONParser().parse(subscribersResponsePayload), Action.status(HttpStatus.OK_200));
141
142         HttpResponse<SubscriberList> allSubscribers = aaiOverTLSClient.getAllSubscribers();
143
144         SubscriberList subscriberList = allSubscribers.getBody();
145         Assertions.assertThat(subscriberList.customer.size()).isEqualTo(4);
146         Assertions.assertThat(allSubscribers.getStatus()).isEqualTo(200);
147     }
148
149     private ObjectMapper getCodehausObjectMapper() {
150         return new ObjectMapper() {
151
152             org.codehaus.jackson.map.ObjectMapper om = new org.codehaus.jackson.map.ObjectMapper();
153
154             @Override
155             public <T> T readValue(String s, Class<T> aClass) {
156                 try {
157                     return om.readValue(s, aClass);
158                 } catch (IOException e) {
159                     throw new RuntimeException(e);
160                 }
161             }
162
163             @Override
164             public String writeValue(Object o) {
165                 try {
166                     return om.writeValueAsString(o);
167                 } catch (IOException e) {
168                     throw new RuntimeException(e);
169                 }
170             }
171         };
172     }
173
174     private ObjectMapper getFasterXmlObjectMapper() {
175         return new ObjectMapper() {
176
177             com.fasterxml.jackson.databind.ObjectMapper om = new com.fasterxml.jackson.databind.ObjectMapper();
178
179             @Override
180             public <T> T readValue(String s, Class<T> aClass) {
181                 try {
182                     return om.readValue(s, aClass);
183                 } catch (IOException e) {
184                     throw new RuntimeException(e);
185                 }
186             }
187
188             @Override
189             public String writeValue(Object o) {
190                 try {
191                     return om.writeValueAsString(o);
192                 } catch (IOException e) {
193                     throw new RuntimeException(e);
194                 }
195             }
196         };
197     }
198
199 }