2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.aai;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.mockito.ArgumentMatchers.anyMap;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.ArgumentMatchers.isNull;
29 import static org.mockito.ArgumentMatchers.startsWith;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
33 import java.util.HashMap;
35 import java.util.UUID;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.aai.util.Serialization;
40 import org.onap.policy.rest.RestManager;
41 import org.onap.policy.rest.RestManager.Pair;
43 public class AaiManagerTest {
44 RestManager restManagerMock;
45 UUID aaiNqRequestUuid = UUID.randomUUID();
46 Pair<Integer, String> httpResponseOk;
47 Pair<Integer, String> httpResponseErr0;
48 Pair<Integer, String> httpResponseErr1;
49 Pair<Integer, String> httpResponseWait;
55 public void beforeTestAaiManager() {
56 restManagerMock = mock(RestManager.class);
58 Map<String, String> expectedHeaders = new HashMap<>();
59 expectedHeaders.put("X-FromAppId", "POLICY");
60 expectedHeaders.put("X-TransactionId", aaiNqRequestUuid.toString());
61 expectedHeaders.put("Accept", "application/json");
63 AaiNqResponse aaiNqResponse = new AaiNqResponseTest().getAaiNqResponse();
64 httpResponseOk = restManagerMock.new Pair<>(200, Serialization.gsonPretty.toJson(aaiNqResponse));
65 httpResponseErr0 = restManagerMock.new Pair<>(200, null);
66 httpResponseErr1 = restManagerMock.new Pair<>(200, "{");
67 httpResponseWait = restManagerMock.new Pair<>(503, null);
71 public void testAaiManagerAaiNqRequest() {
73 AaiManager aaiManager = new AaiManager(restManagerMock);
74 assertNotNull(aaiManager);
76 UUID aaiNqUuid = UUID.randomUUID();
78 AaiNqQueryParameters aaiNqQueryParameters = new AaiNqQueryParameters();
79 AaiNqNamedQuery aaiNqNamedQuery = new AaiNqNamedQuery();
80 aaiNqNamedQuery.setNamedQueryUuid(aaiNqUuid);
81 aaiNqQueryParameters.setNamedQuery(aaiNqNamedQuery);
83 AaiNqRequest aaiNqRequest = new AaiNqRequest();
84 aaiNqRequest.setQueryParameters(aaiNqQueryParameters);
86 when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap(),
87 anyString(), anyString())).thenReturn(httpResponseOk);
89 AaiNqResponse aaiNqOkResponse = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Dorothy", "Gale",
90 aaiNqRequest, aaiNqRequestUuid);
91 assertNotNull(aaiNqOkResponse);
93 when(restManagerMock.post(isNull(), eq("Dorothy"), anyString(), anyMap(), anyString(), anyString()))
96 AaiNqResponse aaiNqNullResponse = aaiManager.postQuery(null, "Dorothy", "Gale", null, aaiNqRequestUuid);
97 assertNull(aaiNqNullResponse);
99 when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
100 anyString(), anyString())).thenReturn(httpResponseErr0);
102 AaiNqResponse aaiNqNotOkResponse0 = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Witch", "West",
103 aaiNqRequest, aaiNqRequestUuid);
104 assertNull(aaiNqNotOkResponse0);
106 when(restManagerMock.post(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
107 anyString(), anyString())).thenReturn(httpResponseErr1);
109 AaiNqResponse aaiNqNotOkResponse1 = aaiManager.postQuery("http://somewhere.under.the.rainbow", "Witch", "West",
110 aaiNqRequest, aaiNqRequestUuid);
111 assertNull(aaiNqNotOkResponse1);
115 public void testAaiManagerQueryByVserverName() {
116 AaiManager aaiManager = new AaiManager(restManagerMock);
117 assertNotNull(aaiManager);
119 UUID vserverNameRequestId = UUID.randomUUID();
121 when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
122 .thenReturn(httpResponseOk);
124 AaiGetVserverResponse vserverResponse = aaiManager.getQueryByVserverName("http://somewhere.over.the.rainbow",
125 "Dorothy", "Gale", vserverNameRequestId, "vserverName");
126 assertNotNull(vserverResponse);
128 AaiGetVserverResponse vserverNullResponse =
129 aaiManager.getQueryByVserverName(null, "Dorothy", "Gale", vserverNameRequestId, "vserverName");
130 assertNull(vserverNullResponse);
132 when(restManagerMock.get(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap()))
133 .thenReturn(httpResponseErr0);
135 AaiGetVserverResponse vserverNotOkResponse0 = aaiManager.getQueryByVserverName(
136 "http://somewhere.under.the.rainbow", "Witch", "West", vserverNameRequestId, "vserverName");
137 assertNull(vserverNotOkResponse0);
141 public void testAaiManagerQueryByVnfId() {
142 AaiManager aaiManager = new AaiManager(restManagerMock);
143 assertNotNull(aaiManager);
145 UUID vserverNameRequestId = UUID.randomUUID();
147 when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
148 .thenReturn(httpResponseOk);
150 AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
151 "Gale", vserverNameRequestId, "vnfID");
152 assertNotNull(vnfResponse);
156 public void testAaiManagerQueryByVnfName() {
157 AaiManager aaiManager = new AaiManager(restManagerMock);
158 assertNotNull(aaiManager);
160 UUID vserverNameRequestId = UUID.randomUUID();
162 when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
163 .thenReturn(httpResponseOk);
165 AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
166 "Gale", vserverNameRequestId, "vnfName");
167 assertNotNull(vnfResponse);