Merge "Custom Query Code"
[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      * Set up test cases.
61      * @throws Exception  if error occurs
62      */
63     @Before
64     public void beforeTestAaiManager() throws Exception {
65         restManagerMock = mock(RestManager.class);
66
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");
71
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);
81     }
82
83     @Test
84     public void testAaiCqResponse() {
85         AaiManager aaiManager = new AaiManager(restManagerMock);
86         assertNotNull(aaiManager);
87
88         UUID vserverNameRequestId = UUID.randomUUID();
89
90         when(restManagerMock.put(startsWith("http://testing.cq.query"), eq("Foo"), eq("Bar"), anyMap(), anyString(),
91                 anyString())).thenReturn(httpCqResponseOk);
92
93         when(restManagerMock.get(startsWith("http://testing.cq.query"), eq("Foo"), eq("Bar"), anyMap()))
94                 .thenReturn(httpTenantResponseOk);
95
96         AaiCqResponse aaiCqResponse =
97                 aaiManager.getCustomQueryResponse("http://testing.cq.query", "Foo", "Bar", vserverNameRequestId, "Foo");
98         assertNotNull(aaiCqResponse);
99
100         when(restManagerMock.put(eq(""), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
101                 .thenReturn(httpResponseErr0);
102
103         when(restManagerMock.get(eq("/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap()))
104                 .thenReturn(httpResponseErr0);
105
106         AaiCqResponse aaiCqResponseNull =
107                 aaiManager.getCustomQueryResponse("", "Foo", "Bar", vserverNameRequestId, "Foo");
108         assertNull(aaiCqResponseNull);
109
110
111         when(restManagerMock.put(eq("Error"), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
112                 .thenReturn(httpResponseErr1);
113
114         when(restManagerMock.get(eq("Error/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap()))
115                 .thenReturn(httpResponseErr1);
116
117         AaiCqResponse aaiCqResponseErr =
118                 aaiManager.getCustomQueryResponse("Error", "Foo", "Bar", vserverNameRequestId, "Foo");
119         assertNull(aaiCqResponseErr);
120     }
121
122     private String getTenantQueryResponse() throws IOException {
123         String responseString = "";
124         responseString = new String(Files.readAllBytes(new File(TENANT_RESPONSE_SAMPLE).toPath()));
125         return responseString;
126     }
127
128     @Test
129     public void testAaiManagerAaiNqRequest() {
130
131         AaiManager aaiManager = new AaiManager(restManagerMock);
132         assertNotNull(aaiManager);
133
134         UUID aaiNqUuid = UUID.randomUUID();
135
136         AaiNqQueryParameters aaiNqQueryParameters = new AaiNqQueryParameters();
137         AaiNqNamedQuery aaiNqNamedQuery = new AaiNqNamedQuery();
138         aaiNqNamedQuery.setNamedQueryUuid(aaiNqUuid);
139         aaiNqQueryParameters.setNamedQuery(aaiNqNamedQuery);
140
141         AaiNqRequest aaiNqRequest = new AaiNqRequest();
142         aaiNqRequest.setQueryParameters(aaiNqQueryParameters);
143
144         when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap(),
145                 anyString(), anyString())).thenReturn(httpResponseOk);
146
147         AaiNqResponse aaiNqOkResponse = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Dorothy", "Gale",
148                 aaiNqRequest, aaiNqRequestUuid);
149         assertNotNull(aaiNqOkResponse);
150
151         when(restManagerMock.post(isNull(), eq("Dorothy"), anyString(), anyMap(), anyString(), anyString()))
152                 .thenReturn(null);
153
154         AaiNqResponse aaiNqNullResponse = aaiManager.postQuery(null, "Dorothy", "Gale", null, aaiNqRequestUuid);
155         assertNull(aaiNqNullResponse);
156
157         when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
158                 anyString(), anyString())).thenReturn(httpResponseErr0);
159
160         AaiNqResponse aaiNqNotOkResponse0 = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Witch", "West",
161                 aaiNqRequest, aaiNqRequestUuid);
162         assertNull(aaiNqNotOkResponse0);
163
164         when(restManagerMock.post(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
165                 anyString(), anyString())).thenReturn(httpResponseErr1);
166
167         AaiNqResponse aaiNqNotOkResponse1 = aaiManager.postQuery("http://somewhere.under.the.rainbow", "Witch", "West",
168                 aaiNqRequest, aaiNqRequestUuid);
169         assertNull(aaiNqNotOkResponse1);
170     }
171
172     @Test
173     public void testAaiManagerQueryByVserverName() {
174         AaiManager aaiManager = new AaiManager(restManagerMock);
175         assertNotNull(aaiManager);
176
177         UUID vserverNameRequestId = UUID.randomUUID();
178
179         when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
180                 .thenReturn(httpResponseOk);
181
182         AaiGetVserverResponse vserverResponse = aaiManager.getQueryByVserverName("http://somewhere.over.the.rainbow",
183                 "Dorothy", "Gale", vserverNameRequestId, "vserverName");
184         assertNotNull(vserverResponse);
185
186         AaiGetVserverResponse vserverNullResponse =
187                 aaiManager.getQueryByVserverName(null, "Dorothy", "Gale", vserverNameRequestId, "vserverName");
188         assertNull(vserverNullResponse);
189
190         when(restManagerMock.get(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap()))
191                 .thenReturn(httpResponseErr0);
192
193         AaiGetVserverResponse vserverNotOkResponse0 = aaiManager.getQueryByVserverName(
194                 "http://somewhere.under.the.rainbow", "Witch", "West", vserverNameRequestId, "vserverName");
195         assertNull(vserverNotOkResponse0);
196     }
197
198     @Test
199     public void testAaiManagerQueryByVnfId() {
200         AaiManager aaiManager = new AaiManager(restManagerMock);
201         assertNotNull(aaiManager);
202
203         UUID vserverNameRequestId = UUID.randomUUID();
204
205         when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
206                 .thenReturn(httpResponseOk);
207
208         AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
209                 "Gale", vserverNameRequestId, "vnfID");
210         assertNotNull(vnfResponse);
211     }
212
213     @Test
214     public void testAaiManagerQueryByVnfName() {
215         AaiManager aaiManager = new AaiManager(restManagerMock);
216         assertNotNull(aaiManager);
217
218         UUID vserverNameRequestId = UUID.randomUUID();
219
220         when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
221                 .thenReturn(httpResponseOk);
222
223         AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
224                 "Gale", vserverNameRequestId, "vnfName");
225         assertNotNull(vnfResponse);
226     }
227 }