Supports new aai changes.
[policy/models.git] / models-interactions / model-impl / aai / src / test / java / org / onap / policy / aai / AaiManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * aai
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
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.policy.aai;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.mockito.ArgumentMatchers.anyMap;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.ArgumentMatchers.isNull;
30 import static org.mockito.ArgumentMatchers.startsWith;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.util.HashMap;
38 import java.util.Map;
39 import java.util.UUID;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.onap.policy.aai.util.Serialization;
43 import org.onap.policy.rest.RestManager;
44 import org.onap.policy.rest.RestManager.Pair;
45
46 public class AaiManagerTest {
47     RestManager restManagerMock;
48     UUID aaiNqRequestUuid = UUID.randomUUID();
49     Pair<Integer, String> httpResponseOk;
50     Pair<Integer, String> httpResponseErr0;
51     Pair<Integer, String> httpResponseErr1;
52     Pair<Integer, String> httpResponseWait;
53     Pair<Integer, String> httpTenantResponseOk;
54     Pair<Integer, String> httpCqResponseOk;
55
56     private static final String TENANT_RESPONSE_SAMPLE =
57             "src/test/resources/org/onap/policy/aai/AaiTenantResponse.json";
58
59
60
61     /**
62      * Set up test cases.
63      *
64      * @throws Exception if error occurs
65      */
66     @Before
67     public void beforeTestAaiManager() throws Exception {
68         restManagerMock = mock(RestManager.class);
69
70         Map<String, String> expectedHeaders = new HashMap<>();
71         expectedHeaders.put("X-FromAppId", "POLICY");
72         expectedHeaders.put("X-TransactionId", aaiNqRequestUuid.toString());
73         expectedHeaders.put("Accept", "application/json");
74
75         String aaiCqResponse = new AaiCqResponseTest().getAaiCqResponse();
76         String tenantResponse = this.getTenantQueryResponse();
77         httpCqResponseOk = restManagerMock.new Pair<>(200, aaiCqResponse);
78         httpTenantResponseOk = restManagerMock.new Pair<>(200, tenantResponse);
79         AaiNqResponse aaiNqResponse = new AaiNqResponseTest().getAaiNqResponse();
80         httpResponseOk = restManagerMock.new Pair<>(200, Serialization.gsonPretty.toJson(aaiNqResponse));
81         httpResponseErr0 = restManagerMock.new Pair<>(200, null);
82         httpResponseErr1 = restManagerMock.new Pair<>(200, "{");
83         httpResponseWait = restManagerMock.new Pair<>(503, null);
84
85     }
86
87
88     @Test
89     public void testAaiCqResponse() {
90         AaiManager aaiManager = new AaiManager(restManagerMock);
91         assertNotNull(aaiManager);
92
93         UUID vserverNameRequestId = UUID.randomUUID();
94
95         when(restManagerMock.put(startsWith("http://testing.cq.query"), eq("Foo"), eq("Bar"), anyMap(), anyString(),
96                 anyString())).thenReturn(httpCqResponseOk);
97
98         when(restManagerMock.get(startsWith("http://testing.cq.query"), eq("Foo"), eq("Bar"), anyMap()))
99                 .thenReturn(httpTenantResponseOk);
100
101         AaiCqResponse aaiCqResponse =
102                 aaiManager.getCustomQueryResponse("http://testing.cq.query", "Foo", "Bar", vserverNameRequestId, "Foo");
103         assertNotNull(aaiCqResponse);
104
105         when(restManagerMock.put(eq(""), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
106                 .thenReturn(httpResponseErr0);
107
108         when(restManagerMock.get(eq("/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap()))
109                 .thenReturn(httpResponseErr0);
110
111         AaiCqResponse aaiCqResponseNull =
112                 aaiManager.getCustomQueryResponse("", "Foo", "Bar", vserverNameRequestId, "Foo");
113         assertNull(aaiCqResponseNull);
114
115
116         when(restManagerMock.put(eq("Error"), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
117                 .thenReturn(httpResponseErr1);
118
119         when(restManagerMock.get(eq("Error/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap()))
120                 .thenReturn(httpResponseErr1);
121
122         AaiCqResponse aaiCqResponseErr =
123                 aaiManager.getCustomQueryResponse("Error", "Foo", "Bar", vserverNameRequestId, "Foo");
124         assertNull(aaiCqResponseErr);
125     }
126
127     private String getTenantQueryResponse() throws IOException {
128         String responseString = "";
129         responseString = new String(Files.readAllBytes(new File(TENANT_RESPONSE_SAMPLE).toPath()));
130         return responseString;
131     }
132
133     @Test
134     public void testAaiManagerAaiNqRequest() {
135
136         AaiManager aaiManager = new AaiManager(restManagerMock);
137         assertNotNull(aaiManager);
138
139         UUID aaiNqUuid = UUID.randomUUID();
140
141         AaiNqQueryParameters aaiNqQueryParameters = new AaiNqQueryParameters();
142         AaiNqNamedQuery aaiNqNamedQuery = new AaiNqNamedQuery();
143         aaiNqNamedQuery.setNamedQueryUuid(aaiNqUuid);
144         aaiNqQueryParameters.setNamedQuery(aaiNqNamedQuery);
145
146         AaiNqRequest aaiNqRequest = new AaiNqRequest();
147         aaiNqRequest.setQueryParameters(aaiNqQueryParameters);
148
149         when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap(),
150                 anyString(), anyString())).thenReturn(httpResponseOk);
151
152         AaiNqResponse aaiNqOkResponse = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Dorothy", "Gale",
153                 aaiNqRequest, aaiNqRequestUuid);
154         assertNotNull(aaiNqOkResponse);
155
156         when(restManagerMock.post(isNull(), eq("Dorothy"), anyString(), anyMap(), anyString(), anyString()))
157                 .thenReturn(null);
158
159         AaiNqResponse aaiNqNullResponse = aaiManager.postQuery(null, "Dorothy", "Gale", null, aaiNqRequestUuid);
160         assertNull(aaiNqNullResponse);
161
162         when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
163                 anyString(), anyString())).thenReturn(httpResponseErr0);
164
165         AaiNqResponse aaiNqNotOkResponse0 = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Witch", "West",
166                 aaiNqRequest, aaiNqRequestUuid);
167         assertNull(aaiNqNotOkResponse0);
168
169         when(restManagerMock.post(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
170                 anyString(), anyString())).thenReturn(httpResponseErr1);
171
172         AaiNqResponse aaiNqNotOkResponse1 = aaiManager.postQuery("http://somewhere.under.the.rainbow", "Witch", "West",
173                 aaiNqRequest, aaiNqRequestUuid);
174         assertNull(aaiNqNotOkResponse1);
175     }
176
177     @Test
178     public void testAaiManagerQueryByVserverName() {
179         AaiManager aaiManager = new AaiManager(restManagerMock);
180         assertNotNull(aaiManager);
181
182         UUID vserverNameRequestId = UUID.randomUUID();
183
184         when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
185                 .thenReturn(httpResponseOk);
186
187         AaiGetVserverResponse vserverResponse = aaiManager.getQueryByVserverName("http://somewhere.over.the.rainbow",
188                 "Dorothy", "Gale", vserverNameRequestId, "vserverName");
189         assertNotNull(vserverResponse);
190
191         AaiGetVserverResponse vserverNullResponse =
192                 aaiManager.getQueryByVserverName(null, "Dorothy", "Gale", vserverNameRequestId, "vserverName");
193         assertNull(vserverNullResponse);
194
195         when(restManagerMock.get(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap()))
196                 .thenReturn(httpResponseErr0);
197
198         AaiGetVserverResponse vserverNotOkResponse0 = aaiManager.getQueryByVserverName(
199                 "http://somewhere.under.the.rainbow", "Witch", "West", vserverNameRequestId, "vserverName");
200         assertNull(vserverNotOkResponse0);
201     }
202
203     @Test
204     public void testAaiManagerQueryByVnfId() {
205         AaiManager aaiManager = new AaiManager(restManagerMock);
206         assertNotNull(aaiManager);
207
208         UUID vserverNameRequestId = UUID.randomUUID();
209
210         when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
211                 .thenReturn(httpResponseOk);
212
213         AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
214                 "Gale", vserverNameRequestId, "vnfID");
215         assertNotNull(vnfResponse);
216     }
217
218     @Test
219     public void testAaiManagerQueryByVnfName() {
220         AaiManager aaiManager = new AaiManager(restManagerMock);
221         assertNotNull(aaiManager);
222
223         UUID vserverNameRequestId = UUID.randomUUID();
224
225         when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
226                 .thenReturn(httpResponseOk);
227
228         AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
229                 "Gale", vserverNameRequestId, "vnfName");
230         assertNotNull(vnfResponse);
231     }
232 }