create JoshworksJacksonObjectMapper and use it everywhere needed
[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  * Modifications Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.aai;
23
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.MockitoAnnotations.initMocks;
26 import static org.onap.vid.utils.KotlinUtilsKt.JOSHWORKS_JACKSON_OBJECT_MAPPER;
27
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.xebialabs.restito.semantics.Action;
30 import io.joshworks.restclient.http.HttpResponse;
31 import java.io.IOException;
32 import org.assertj.core.api.Assertions;
33 import org.glassfish.grizzly.http.util.HttpStatus;
34 import org.jetbrains.annotations.NotNull;
35 import org.json.simple.parser.JSONParser;
36 import org.json.simple.parser.ParseException;
37 import org.mockito.Mock;
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 import org.onap.vid.utils.Logging;
43 import org.testng.annotations.AfterClass;
44 import org.testng.annotations.BeforeClass;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
47
48 public class AaiOverTLSClientServerTest {
49
50     @Mock
51     private AaiOverTLSPropertySupplier propertySupplier;
52
53     private static StubServerUtil serverUtil;
54
55     private String searchNodesQueryResponsePayload = "" +
56             "{" +
57             "\"result-data\": [" +
58             "    {" +
59             "      \"resource-type\": \"generic-vnf\"," +
60             "      \"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/6eac8e69-c98d-4ac5-ab90-69fe0cabda76\"" +
61             "    }" +
62             "  ]" +
63             "}";
64
65     private String subscribersResponsePayload =
66         "{\n"
67         + "\"customer\": [\n"
68         + "  {\n"
69         + "\"global-customer-id\": \"DemoCust_752df078-d8e9-4731-abf6-8ae7348075bb\",\n"
70         + "\"subscriber-name\": \"DemoCust_752df078-d8e9-4731-abf6-8ae7348075bb\",\n"
71         + "\"subscriber-type\": \"INFRA\",\n"
72         + "\"resource-version\": \"1536158347585\"\n"
73         + "},\n"
74         + "  {\n"
75         + "\"global-customer-id\": \"DemoCust_62bf43a3-4888-4c82-ae98-3ebc3d782761\",\n"
76         + "\"subscriber-name\": \"DemoCust_62bf43a3-4888-4c82-ae98-3ebc3d782761\",\n"
77         + "\"subscriber-type\": \"INFRA\",\n"
78         + "\"resource-version\": \"1536240894581\"\n"
79         + "},\n"
80         + "  {\n"
81         + "\"global-customer-id\": \"DemoCust_e84256d6-ef3e-4a28-9741-9987019c3a8f\",\n"
82         + "\"subscriber-name\": \"DemoCust_e84256d6-ef3e-4a28-9741-9987019c3a8f\",\n"
83         + "\"subscriber-type\": \"INFRA\",\n"
84         + "\"resource-version\": \"1536330956393\"\n"
85         + "},\n"
86         + "  {\n"
87         + "\"global-customer-id\": \"ETE_Customer_377bb124-2638-4025-a315-cdae04f52bce\",\n"
88         + "\"subscriber-name\": \"ETE_Customer_377bb124-2638-4025-a315-cdae04f52bce\",\n"
89         + "\"subscriber-type\": \"INFRA\",\n"
90         + "\"resource-version\": \"1536088625538\"\n"
91         + "}\n"
92         + "],\n"
93         + "}";
94
95     @BeforeClass
96     public static void setUpClass(){
97         serverUtil = new StubServerUtil();
98         serverUtil.runServer();
99     }
100
101     @AfterClass
102     public static void tearDown(){
103         serverUtil.stopServer();
104     }
105
106     @BeforeMethod
107     public void setUp(){
108         initMocks(this);
109     }
110
111     @Test
112     public void shouldSearchNodeTypeByName() throws IOException, ParseException {
113         AaiOverTLSClient aaiOverTLSClient = createAaiOverTLSClient();
114
115         serverUtil.prepareGetCall("/nodes/generic-vnfs", new JSONParser().parse(searchNodesQueryResponsePayload), Action.status(HttpStatus.OK_200));
116
117         boolean aaiNodeQueryResponseHttpResponse = aaiOverTLSClient
118                 .isNodeTypeExistsByName("any", ResourceType.GENERIC_VNF);
119
120         Assertions.assertThat(aaiNodeQueryResponseHttpResponse).isEqualTo(true);
121     }
122
123     @NotNull
124     private AaiOverTLSClient createAaiOverTLSClient() {
125         return new AaiOverTLSClient(
126             new SyncRestClient(JOSHWORKS_JACKSON_OBJECT_MAPPER, mock(Logging.class)),
127             propertySupplier,
128             serverUtil.constructTargetUrl("http", "")
129         );
130     }
131
132     @Test
133     public void shouldGetSubscribers() throws ParseException, JsonProcessingException {
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 }