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