Clean up of Pair classes - models
[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-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
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.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;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.nio.file.Files;
38 import java.util.Map;
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;
44
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";
51
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;
57
58     /**
59      * Set up test cases.
60      *
61      * @throws Exception if error occurs
62      */
63     @Before
64     public void beforeTestAaiManager() throws Exception {
65         restManagerMock = mock(RestManager.class);
66
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, "{");
73
74     }
75
76
77     @Test
78     public void testAaiCqResponse() {
79         AaiManager aaiManager = new AaiManager(restManagerMock);
80         assertNotNull(aaiManager);
81
82         UUID vserverNameRequestId = UUID.randomUUID();
83
84         when(restManagerMock.put(startsWith(CQ_QUERY_URL), eq("Foo"), eq("Bar"), anyMap(), anyString(),
85                 anyString())).thenReturn(httpCqResponseOk);
86
87         when(restManagerMock.get(startsWith(CQ_QUERY_URL), eq("Foo"), eq("Bar"), anyMap()))
88                 .thenReturn(httpTenantResponseOk);
89
90         AaiCqResponse aaiCqResponse =
91                 aaiManager.getCustomQueryResponse(CQ_QUERY_URL, "Foo", "Bar", vserverNameRequestId, "Foo");
92         assertNotNull(aaiCqResponse);
93
94         when(restManagerMock.put(eq(""), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
95                 .thenReturn(httpResponseErr0);
96
97         when(restManagerMock.get(eq("/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap()))
98                 .thenReturn(httpResponseErr0);
99
100         AaiCqResponse aaiCqResponseNull =
101                 aaiManager.getCustomQueryResponse("", "Foo", "Bar", vserverNameRequestId, "Foo");
102         assertNull(aaiCqResponseNull);
103
104
105         when(restManagerMock.put(eq("Error"), eq("Foo"), eq("Bar"), anyMap(), anyString(), anyString()))
106                 .thenReturn(httpResponseErr1);
107
108         when(restManagerMock.get(eq("Error/aai/v11/query?format=resource"), eq("Foo"), eq("Bar"), anyMap()))
109                 .thenReturn(httpResponseErr1);
110
111         AaiCqResponse aaiCqResponseErr =
112                 aaiManager.getCustomQueryResponse("Error", "Foo", "Bar", vserverNameRequestId, "Foo");
113         assertNull(aaiCqResponseErr);
114     }
115
116     private String getTenantQueryResponse() throws IOException {
117         String responseString = "";
118         responseString = new String(Files.readAllBytes(new File(TENANT_RESPONSE_SAMPLE).toPath()));
119         return responseString;
120     }
121
122
123     @Test
124     public void testAaiManagerGetPnf() {
125         AaiManager aaiManager = new AaiManager(restManagerMock);
126         assertNotNull(aaiManager);
127         String pnfName = "test-pnf";
128         String pnfResponse = "{\"pnf-name\":" + pnfName
129                                      + ",\"pnf-id\":\"123456\",\"in-maint\":false,\"ipaddress-v4-oam\":\"1.1.1.1\"}";
130
131         Pair<Integer, String> pnfHttpResponse = Pair.of(200, pnfResponse);
132         when(restManagerMock.get(contains(pnfName), eq(DOROTHY), eq("Gale"), anyMap()))
133                 .thenReturn(pnfHttpResponse);
134
135         Map<String, String> pnfParams = aaiManager.getPnf(SOME_URL, DOROTHY, "Gale", UUID.randomUUID(), pnfName);
136         assertNotNull(pnfParams);
137         assertEquals(pnfName, pnfParams.get("pnf.pnf-name"));
138     }
139 }