dd7b26c1278d11957a1bb7139b6eedc8ded01f8c
[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 - 2019 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.mockito.Mock;
32 import org.onap.vid.aai.model.ResourceType;
33 import org.onap.vid.client.SyncRestClient;
34 import org.onap.vid.model.SubscriberList;
35 import org.onap.vid.testUtils.StubServerUtil;
36 import org.testng.annotations.AfterClass;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.BeforeMethod;
39 import org.testng.annotations.Test;
40
41 import java.io.IOException;
42
43 import static org.mockito.MockitoAnnotations.initMocks;
44
45 public class AaiOverTLSClientServerTest {
46
47     @Mock
48     private AaiOverTLSPropertySupplier propertySupplier;
49
50     private static StubServerUtil serverUtil;
51
52     private String searchNodesQueryResponsePayload = "" +
53             "{" +
54             "\"result-data\": [" +
55             "    {" +
56             "      \"resource-type\": \"generic-vnf\"," +
57             "      \"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/6eac8e69-c98d-4ac5-ab90-69fe0cabda76\"" +
58             "    }" +
59             "  ]" +
60             "}";
61
62     private String subscribersResponsePayload =
63         "{\n"
64         + "\"customer\": [\n"
65         + "  {\n"
66         + "\"global-customer-id\": \"DemoCust_752df078-d8e9-4731-abf6-8ae7348075bb\",\n"
67         + "\"subscriber-name\": \"DemoCust_752df078-d8e9-4731-abf6-8ae7348075bb\",\n"
68         + "\"subscriber-type\": \"INFRA\",\n"
69         + "\"resource-version\": \"1536158347585\"\n"
70         + "},\n"
71         + "  {\n"
72         + "\"global-customer-id\": \"DemoCust_62bf43a3-4888-4c82-ae98-3ebc3d782761\",\n"
73         + "\"subscriber-name\": \"DemoCust_62bf43a3-4888-4c82-ae98-3ebc3d782761\",\n"
74         + "\"subscriber-type\": \"INFRA\",\n"
75         + "\"resource-version\": \"1536240894581\"\n"
76         + "},\n"
77         + "  {\n"
78         + "\"global-customer-id\": \"DemoCust_e84256d6-ef3e-4a28-9741-9987019c3a8f\",\n"
79         + "\"subscriber-name\": \"DemoCust_e84256d6-ef3e-4a28-9741-9987019c3a8f\",\n"
80         + "\"subscriber-type\": \"INFRA\",\n"
81         + "\"resource-version\": \"1536330956393\"\n"
82         + "},\n"
83         + "  {\n"
84         + "\"global-customer-id\": \"ETE_Customer_377bb124-2638-4025-a315-cdae04f52bce\",\n"
85         + "\"subscriber-name\": \"ETE_Customer_377bb124-2638-4025-a315-cdae04f52bce\",\n"
86         + "\"subscriber-type\": \"INFRA\",\n"
87         + "\"resource-version\": \"1536088625538\"\n"
88         + "}\n"
89         + "],\n"
90         + "}";
91
92     @BeforeClass
93     public static void setUpClass(){
94         serverUtil = new StubServerUtil();
95         serverUtil.runServer();
96     }
97
98     @AfterClass
99     public static void tearDown(){
100         serverUtil.stopServer();
101     }
102
103     @BeforeMethod
104     public void setUp(){
105         initMocks(this);
106     }
107
108     @Test
109     public void shouldSearchNodeTypeByName() throws IOException, ParseException {
110         ObjectMapper objectMapper = getFasterXmlObjectMapper();
111         AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper),  propertySupplier, serverUtil.constructTargetUrl("http", ""));
112
113         serverUtil.prepareGetCall("/nodes/generic-vnfs", new JSONParser().parse(searchNodesQueryResponsePayload), Action.status(HttpStatus.OK_200));
114
115         boolean aaiNodeQueryResponseHttpResponse = aaiOverTLSClient
116                 .isNodeTypeExistsByName("any", ResourceType.GENERIC_VNF);
117
118         Assertions.assertThat(aaiNodeQueryResponseHttpResponse).isEqualTo(true);
119     }
120
121     @Test
122     public void shouldGetSubscribers() throws ParseException, JsonProcessingException {
123         ObjectMapper objectMapper = getFasterXmlObjectMapper();
124         AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper),  propertySupplier, serverUtil.constructTargetUrl("http", ""));
125
126         serverUtil.prepareGetCall("/business/customers", new JSONParser().parse(subscribersResponsePayload), Action.status(HttpStatus.OK_200));
127
128         HttpResponse<SubscriberList> allSubscribers = aaiOverTLSClient.getAllSubscribers();
129
130         SubscriberList subscriberList = allSubscribers.getBody();
131         Assertions.assertThat(subscriberList.customer.size()).isEqualTo(4);
132         Assertions.assertThat(allSubscribers.getStatus()).isEqualTo(200);
133     }
134
135     private ObjectMapper getFasterXmlObjectMapper() {
136         return new ObjectMapper() {
137
138             com.fasterxml.jackson.databind.ObjectMapper om = new com.fasterxml.jackson.databind.ObjectMapper();
139
140             @Override
141             public <T> T readValue(String s, Class<T> aClass) {
142                 try {
143                     return om.readValue(s, aClass);
144                 } catch (IOException e) {
145                     throw new RuntimeException(e);
146                 }
147             }
148
149             @Override
150             public String writeValue(Object o) {
151                 try {
152                     return om.writeValueAsString(o);
153                 } catch (IOException e) {
154                     throw new RuntimeException(e);
155                 }
156             }
157         };
158     }
159
160 }