Merge from ECOMP's repository
[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.ResourceType;
38 import org.onap.vid.client.SyncRestClient;
39 import org.onap.vid.model.SubscriberList;
40 import org.onap.vid.testUtils.StubServerUtil;
41
42 import java.io.IOException;
43
44 @RunWith(MockitoJUnitRunner.class)
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     @Test
104     public void shouldSearchNodeTypeByName() throws IOException, ParseException {
105         ObjectMapper objectMapper = getFasterXmlObjectMapper();
106         AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper),  propertySupplier, serverUtil.constructTargetUrl("http", ""));
107
108         serverUtil.prepareGetCall("/nodes/generic-vnfs", new JSONParser().parse(searchNodesQueryResponsePayload), Action.status(HttpStatus.OK_200));
109
110         boolean aaiNodeQueryResponseHttpResponse = aaiOverTLSClient
111                 .isNodeTypeExistsByName("any", ResourceType.GENERIC_VNF);
112
113         Assertions.assertThat(aaiNodeQueryResponseHttpResponse).isEqualTo(true);
114     }
115
116     @Test
117     public void shouldGetSubscribers() throws ParseException, JsonProcessingException {
118         ObjectMapper objectMapper = getFasterXmlObjectMapper();
119         AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper),  propertySupplier, serverUtil.constructTargetUrl("http", ""));
120
121         serverUtil.prepareGetCall("/business/customers", new JSONParser().parse(subscribersResponsePayload), Action.status(HttpStatus.OK_200));
122
123         HttpResponse<SubscriberList> allSubscribers = aaiOverTLSClient.getAllSubscribers();
124
125         SubscriberList subscriberList = allSubscribers.getBody();
126         Assertions.assertThat(subscriberList.customer.size()).isEqualTo(4);
127         Assertions.assertThat(allSubscribers.getStatus()).isEqualTo(200);
128     }
129
130     private ObjectMapper getFasterXmlObjectMapper() {
131         return new ObjectMapper() {
132
133             com.fasterxml.jackson.databind.ObjectMapper om = new com.fasterxml.jackson.databind.ObjectMapper();
134
135             @Override
136             public <T> T readValue(String s, Class<T> aClass) {
137                 try {
138                     return om.readValue(s, aClass);
139                 } catch (IOException e) {
140                     throw new RuntimeException(e);
141                 }
142             }
143
144             @Override
145             public String writeValue(Object o) {
146                 try {
147                     return om.writeValueAsString(o);
148                 } catch (IOException e) {
149                     throw new RuntimeException(e);
150                 }
151             }
152         };
153     }
154
155 }