2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.aai;
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;
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.util.HashMap;
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;
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;
56 private static final String TENANT_RESPONSE_SAMPLE =
57 "src/test/resources/org/onap/policy/aai/AaiTenantResponse.json";
61 * @throws Exception if error occurs
64 public void beforeTestAaiManager() throws Exception {
65 restManagerMock = mock(RestManager.class);
67 Map<String, String> expectedHeaders = new HashMap<>();
68 expectedHeaders.put("X-FromAppId", "POLICY");
69 expectedHeaders.put("X-TransactionId", aaiNqRequestUuid.toString());
70 expectedHeaders.put("Accept", "application/json");
72 String aaiCqResponse = new AaiCqResponseTest().getAaiCqResponse();
73 String tenantResponse = this.getTenantQueryResponse();
74 httpCqResponseOk = restManagerMock.new Pair<>(200, aaiCqResponse);
75 httpTenantResponseOk = restManagerMock.new Pair<>(200, tenantResponse);
76 AaiNqResponse aaiNqResponse = new AaiNqResponseTest().getAaiNqResponse();
77 httpResponseOk = restManagerMock.new Pair<>(200, Serialization.gsonPretty.toJson(aaiNqResponse));
78 httpResponseErr0 = restManagerMock.new Pair<>(200, null);
79 httpResponseErr1 = restManagerMock.new Pair<>(200, "{");
80 httpResponseWait = restManagerMock.new Pair<>(503, null);
84 public void testAaiCqResponse() {
85 AaiManager aaiManager = new AaiManager(restManagerMock);
86 assertNotNull(aaiManager);
88 UUID vserverNameRequestId = UUID.randomUUID();
90 when(restManagerMock.put(startsWith("http://testing.cq.query"), eq("Foo"), eq("Bar"), anyMap(), anyString(),
91 anyString())).thenReturn(httpCqResponseOk);
93 when(restManagerMock.get(startsWith("http://testing.cq.query"), eq("Foo"), eq("Bar"), anyMap()))
94 .thenReturn(httpTenantResponseOk);
96 AaiCqResponse aaiCqResponse =
97 aaiManager.getCustomQueryResponse("http://testing.cq.query", "Foo", "Bar", vserverNameRequestId, "Foo");
98 assertNotNull(aaiCqResponse);
100 when(restManagerMock.put(eq(""), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
101 .thenReturn(httpResponseErr0);
103 when(restManagerMock.get(eq("/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap()))
104 .thenReturn(httpResponseErr0);
106 AaiCqResponse aaiCqResponseNull =
107 aaiManager.getCustomQueryResponse("", "Foo", "Bar", vserverNameRequestId, "Foo");
108 assertNull(aaiCqResponseNull);
111 when(restManagerMock.put(eq("Error"), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
112 .thenReturn(httpResponseErr1);
114 when(restManagerMock.get(eq("Error/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap()))
115 .thenReturn(httpResponseErr1);
117 AaiCqResponse aaiCqResponseErr =
118 aaiManager.getCustomQueryResponse("Error", "Foo", "Bar", vserverNameRequestId, "Foo");
119 assertNull(aaiCqResponseErr);
122 private String getTenantQueryResponse() throws IOException {
123 String responseString = "";
124 responseString = new String(Files.readAllBytes(new File(TENANT_RESPONSE_SAMPLE).toPath()));
125 return responseString;
129 public void testAaiManagerAaiNqRequest() {
131 AaiManager aaiManager = new AaiManager(restManagerMock);
132 assertNotNull(aaiManager);
134 UUID aaiNqUuid = UUID.randomUUID();
136 AaiNqQueryParameters aaiNqQueryParameters = new AaiNqQueryParameters();
137 AaiNqNamedQuery aaiNqNamedQuery = new AaiNqNamedQuery();
138 aaiNqNamedQuery.setNamedQueryUuid(aaiNqUuid);
139 aaiNqQueryParameters.setNamedQuery(aaiNqNamedQuery);
141 AaiNqRequest aaiNqRequest = new AaiNqRequest();
142 aaiNqRequest.setQueryParameters(aaiNqQueryParameters);
144 when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap(),
145 anyString(), anyString())).thenReturn(httpResponseOk);
147 AaiNqResponse aaiNqOkResponse = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Dorothy", "Gale",
148 aaiNqRequest, aaiNqRequestUuid);
149 assertNotNull(aaiNqOkResponse);
151 when(restManagerMock.post(isNull(), eq("Dorothy"), anyString(), anyMap(), anyString(), anyString()))
154 AaiNqResponse aaiNqNullResponse = aaiManager.postQuery(null, "Dorothy", "Gale", null, aaiNqRequestUuid);
155 assertNull(aaiNqNullResponse);
157 when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
158 anyString(), anyString())).thenReturn(httpResponseErr0);
160 AaiNqResponse aaiNqNotOkResponse0 = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Witch", "West",
161 aaiNqRequest, aaiNqRequestUuid);
162 assertNull(aaiNqNotOkResponse0);
164 when(restManagerMock.post(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
165 anyString(), anyString())).thenReturn(httpResponseErr1);
167 AaiNqResponse aaiNqNotOkResponse1 = aaiManager.postQuery("http://somewhere.under.the.rainbow", "Witch", "West",
168 aaiNqRequest, aaiNqRequestUuid);
169 assertNull(aaiNqNotOkResponse1);
173 public void testAaiManagerQueryByVserverName() {
174 AaiManager aaiManager = new AaiManager(restManagerMock);
175 assertNotNull(aaiManager);
177 UUID vserverNameRequestId = UUID.randomUUID();
179 when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
180 .thenReturn(httpResponseOk);
182 AaiGetVserverResponse vserverResponse = aaiManager.getQueryByVserverName("http://somewhere.over.the.rainbow",
183 "Dorothy", "Gale", vserverNameRequestId, "vserverName");
184 assertNotNull(vserverResponse);
186 AaiGetVserverResponse vserverNullResponse =
187 aaiManager.getQueryByVserverName(null, "Dorothy", "Gale", vserverNameRequestId, "vserverName");
188 assertNull(vserverNullResponse);
190 when(restManagerMock.get(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap()))
191 .thenReturn(httpResponseErr0);
193 AaiGetVserverResponse vserverNotOkResponse0 = aaiManager.getQueryByVserverName(
194 "http://somewhere.under.the.rainbow", "Witch", "West", vserverNameRequestId, "vserverName");
195 assertNull(vserverNotOkResponse0);
199 public void testAaiManagerQueryByVnfId() {
200 AaiManager aaiManager = new AaiManager(restManagerMock);
201 assertNotNull(aaiManager);
203 UUID vserverNameRequestId = UUID.randomUUID();
205 when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
206 .thenReturn(httpResponseOk);
208 AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
209 "Gale", vserverNameRequestId, "vnfID");
210 assertNotNull(vnfResponse);
214 public void testAaiManagerQueryByVnfName() {
215 AaiManager aaiManager = new AaiManager(restManagerMock);
216 assertNotNull(aaiManager);
218 UUID vserverNameRequestId = UUID.randomUUID();
220 when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
221 .thenReturn(httpResponseOk);
223 AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
224 "Gale", vserverNameRequestId, "vnfName");
225 assertNotNull(vnfResponse);