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;
34 import java.util.HashMap;
36 import java.util.UUID;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.aai.util.Serialization;
41 import org.onap.policy.rest.RestManager;
42 import org.onap.policy.rest.RestManager.Pair;
44 public class AaiManagerTest {
45 RestManager restManagerMock;
46 UUID aaiNqRequestUuid = UUID.randomUUID();
47 Pair<Integer, String> httpResponseOk;
48 Pair<Integer, String> httpResponseErr0;
49 Pair<Integer, String> httpResponseErr1;
50 Pair<Integer, String> httpResponseWait;
56 public void beforeTestAaiManager() {
57 restManagerMock = mock(RestManager.class);
59 Map<String, String> expectedHeaders = new HashMap<>();
60 expectedHeaders.put("X-FromAppId", "POLICY");
61 expectedHeaders.put("X-TransactionId", aaiNqRequestUuid.toString());
62 expectedHeaders.put("Accept", "application/json");
64 AaiNqResponse aaiNqResponse = new AaiNqResponseTest().getAaiNqResponse();
65 httpResponseOk = restManagerMock.new Pair<>(200, Serialization.gsonPretty.toJson(aaiNqResponse));
66 httpResponseErr0 = restManagerMock.new Pair<>(200, null);
67 httpResponseErr1 = restManagerMock.new Pair<>(200, "{");
68 httpResponseWait = restManagerMock.new Pair<>(503, null);
72 public void testAaiManagerAaiNqRequest() {
74 AaiManager aaiManager = new AaiManager(restManagerMock);
75 assertNotNull(aaiManager);
77 UUID aaiNqUuid = UUID.randomUUID();
79 AaiNqQueryParameters aaiNqQueryParameters = new AaiNqQueryParameters();
80 AaiNqNamedQuery aaiNqNamedQuery = new AaiNqNamedQuery();
81 aaiNqNamedQuery.setNamedQueryUuid(aaiNqUuid);
82 aaiNqQueryParameters.setNamedQuery(aaiNqNamedQuery);
84 AaiNqRequest aaiNqRequest = new AaiNqRequest();
85 aaiNqRequest.setQueryParameters(aaiNqQueryParameters);
87 when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap(),
88 anyString(), anyString())).thenReturn(httpResponseOk);
90 AaiNqResponse aaiNqOkResponse = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Dorothy", "Gale",
91 aaiNqRequest, aaiNqRequestUuid);
92 assertNotNull(aaiNqOkResponse);
94 when(restManagerMock.post(isNull(), eq("Dorothy"), anyString(), anyMap(), anyString(), anyString()))
97 AaiNqResponse aaiNqNullResponse = aaiManager.postQuery(null, "Dorothy", "Gale", null, aaiNqRequestUuid);
98 assertNull(aaiNqNullResponse);
100 when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
101 anyString(), anyString())).thenReturn(httpResponseErr0);
103 AaiNqResponse aaiNqNotOkResponse0 = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Witch", "West",
104 aaiNqRequest, aaiNqRequestUuid);
105 assertNull(aaiNqNotOkResponse0);
107 when(restManagerMock.post(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
108 anyString(), anyString())).thenReturn(httpResponseErr1);
110 AaiNqResponse aaiNqNotOkResponse1 = aaiManager.postQuery("http://somewhere.under.the.rainbow", "Witch", "West",
111 aaiNqRequest, aaiNqRequestUuid);
112 assertNull(aaiNqNotOkResponse1);
116 public void testAaiManagerQueryByVserverName() {
117 AaiManager aaiManager = new AaiManager(restManagerMock);
118 assertNotNull(aaiManager);
120 UUID vserverNameRequestId = UUID.randomUUID();
122 when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
123 .thenReturn(httpResponseOk);
125 AaiGetVserverResponse vserverResponse = aaiManager.getQueryByVserverName("http://somewhere.over.the.rainbow",
126 "Dorothy", "Gale", vserverNameRequestId, "vserverName");
127 assertNotNull(vserverResponse);
129 AaiGetVserverResponse vserverNullResponse =
130 aaiManager.getQueryByVserverName(null, "Dorothy", "Gale", vserverNameRequestId, "vserverName");
131 assertNull(vserverNullResponse);
133 when(restManagerMock.get(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap()))
134 .thenReturn(httpResponseErr0);
136 AaiGetVserverResponse vserverNotOkResponse0 = aaiManager.getQueryByVserverName(
137 "http://somewhere.under.the.rainbow", "Witch", "West", vserverNameRequestId, "vserverName");
138 assertNull(vserverNotOkResponse0);
142 public void testAaiManagerQueryByVnfId() {
143 AaiManager aaiManager = new AaiManager(restManagerMock);
144 assertNotNull(aaiManager);
146 UUID vserverNameRequestId = UUID.randomUUID();
148 when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
149 .thenReturn(httpResponseOk);
151 AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
152 "Gale", vserverNameRequestId, "vnfID");
153 assertNotNull(vnfResponse);
157 public void testAaiManagerQueryByVnfName() {
158 AaiManager aaiManager = new AaiManager(restManagerMock);
159 assertNotNull(aaiManager);
161 UUID vserverNameRequestId = UUID.randomUUID();
163 when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
164 .thenReturn(httpResponseOk);
166 AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
167 "Gale", vserverNameRequestId, "vnfName");
168 assertNotNull(vnfResponse);