2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 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.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.ArgumentMatchers.anyMap;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.contains;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.ArgumentMatchers.startsWith;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
36 import java.io.IOException;
37 import java.nio.file.Files;
39 import java.util.UUID;
40 import org.apache.commons.lang3.tuple.Pair;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.policy.rest.RestManager;
45 public class AaiManagerTest {
46 private static final String CQ_QUERY_URL = "http://testing.cq.query";
47 private static final String DOROTHY = "Dorothy";
48 private static final String SOME_URL = "http://somewhere.over.the.rainbow";
49 private static final String TENANT_RESPONSE_SAMPLE =
50 "src/test/resources/org/onap/policy/aai/AaiTenantResponse.json";
52 private RestManager restManagerMock;
53 private Pair<Integer, String> httpResponseErr0;
54 private Pair<Integer, String> httpResponseErr1;
55 private Pair<Integer, String> httpTenantResponseOk;
56 private Pair<Integer, String> httpCqResponseOk;
61 * @throws Exception if error occurs
64 public void beforeTestAaiManager() throws Exception {
65 restManagerMock = mock(RestManager.class);
67 String aaiCqResponse = new AaiCqResponseTest().getAaiCqResponse();
68 String tenantResponse = this.getTenantQueryResponse();
69 httpCqResponseOk = Pair.of(200, aaiCqResponse);
70 httpTenantResponseOk = Pair.of(200, tenantResponse);
71 httpResponseErr0 = Pair.of(200, null);
72 httpResponseErr1 = Pair.of(200, "{");
78 public void testAaiCqResponse() {
79 AaiManager aaiManager = new AaiManager(restManagerMock);
80 assertNotNull(aaiManager);
82 UUID vserverNameRequestId = UUID.randomUUID();
84 when(restManagerMock.put(startsWith(CQ_QUERY_URL), eq("Foo"), eq("Bar"), anyMap(), anyString(),
85 anyString())).thenReturn(httpCqResponseOk);
87 when(restManagerMock.get(startsWith(CQ_QUERY_URL), eq("Foo"), eq("Bar"), anyMap()))
88 .thenReturn(httpTenantResponseOk);
90 AaiCqResponse aaiCqResponse =
91 aaiManager.getCustomQueryResponse(CQ_QUERY_URL, "Foo", "Bar", vserverNameRequestId, "Foo");
92 assertNotNull(aaiCqResponse);
94 when(restManagerMock.put(eq(""), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
95 .thenReturn(httpResponseErr0);
97 AaiCqResponse aaiCqResponseNull =
98 aaiManager.getCustomQueryResponse("", "Foo", "Bar", vserverNameRequestId, "Foo");
99 assertNull(aaiCqResponseNull);
102 when(restManagerMock.put(eq("Error"), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
103 .thenReturn(httpResponseErr1);
105 AaiCqResponse aaiCqResponseErr =
106 aaiManager.getCustomQueryResponse("Error", "Foo", "Bar", vserverNameRequestId, "Foo");
107 assertNull(aaiCqResponseErr);
110 private String getTenantQueryResponse() throws IOException {
111 String responseString = "";
112 responseString = new String(Files.readAllBytes(new File(TENANT_RESPONSE_SAMPLE).toPath()));
113 return responseString;
118 public void testAaiManagerGetPnf() {
119 AaiManager aaiManager = new AaiManager(restManagerMock);
120 assertNotNull(aaiManager);
121 String pnfName = "test-pnf";
122 String pnfResponse = "{\"pnf-name\":" + pnfName
123 + ",\"pnf-id\":\"123456\",\"in-maint\":false,\"ipaddress-v4-oam\":\"1.1.1.1\"}";
125 Pair<Integer, String> pnfHttpResponse = Pair.of(200, pnfResponse);
126 when(restManagerMock.get(contains(pnfName), eq(DOROTHY), eq("Gale"), anyMap()))
127 .thenReturn(pnfHttpResponse);
129 Map<String, String> pnfParams = aaiManager.getPnf(SOME_URL, DOROTHY, "Gale", UUID.randomUUID(), pnfName);
130 assertNotNull(pnfParams);
131 assertEquals(pnfName, pnfParams.get("pnf.pnf-name"));