move actors code in drools-applications to policy/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 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.util.HashMap;
35 import java.util.Map;
36 import java.util.UUID;
37
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;
43
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;
51
52     /**
53      * Set up test cases.
54      */
55     @Before
56     public void beforeTestAaiManager() {
57         restManagerMock = mock(RestManager.class);
58
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");
63
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);
69     }
70
71     @Test
72     public void testAaiManagerAaiNqRequest() {
73
74         AaiManager aaiManager = new AaiManager(restManagerMock);
75         assertNotNull(aaiManager);
76
77         UUID aaiNqUuid = UUID.randomUUID();
78
79         AaiNqQueryParameters aaiNqQueryParameters = new AaiNqQueryParameters();
80         AaiNqNamedQuery aaiNqNamedQuery = new AaiNqNamedQuery();
81         aaiNqNamedQuery.setNamedQueryUuid(aaiNqUuid);
82         aaiNqQueryParameters.setNamedQuery(aaiNqNamedQuery);
83
84         AaiNqRequest aaiNqRequest = new AaiNqRequest();
85         aaiNqRequest.setQueryParameters(aaiNqQueryParameters);
86
87         when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap(),
88                 anyString(), anyString())).thenReturn(httpResponseOk);
89
90         AaiNqResponse aaiNqOkResponse = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Dorothy", "Gale",
91                 aaiNqRequest, aaiNqRequestUuid);
92         assertNotNull(aaiNqOkResponse);
93
94         when(restManagerMock.post(isNull(), eq("Dorothy"), anyString(), anyMap(), anyString(), anyString()))
95                 .thenReturn(null);
96
97         AaiNqResponse aaiNqNullResponse = aaiManager.postQuery(null, "Dorothy", "Gale", null, aaiNqRequestUuid);
98         assertNull(aaiNqNullResponse);
99
100         when(restManagerMock.post(startsWith("http://somewhere.over.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
101                 anyString(), anyString())).thenReturn(httpResponseErr0);
102
103         AaiNqResponse aaiNqNotOkResponse0 = aaiManager.postQuery("http://somewhere.over.the.rainbow", "Witch", "West",
104                 aaiNqRequest, aaiNqRequestUuid);
105         assertNull(aaiNqNotOkResponse0);
106
107         when(restManagerMock.post(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap(),
108                 anyString(), anyString())).thenReturn(httpResponseErr1);
109
110         AaiNqResponse aaiNqNotOkResponse1 = aaiManager.postQuery("http://somewhere.under.the.rainbow", "Witch", "West",
111                 aaiNqRequest, aaiNqRequestUuid);
112         assertNull(aaiNqNotOkResponse1);
113     }
114
115     @Test
116     public void testAaiManagerQueryByVserverName() {
117         AaiManager aaiManager = new AaiManager(restManagerMock);
118         assertNotNull(aaiManager);
119
120         UUID vserverNameRequestId = UUID.randomUUID();
121
122         when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
123                 .thenReturn(httpResponseOk);
124
125         AaiGetVserverResponse vserverResponse = aaiManager.getQueryByVserverName("http://somewhere.over.the.rainbow",
126                 "Dorothy", "Gale", vserverNameRequestId, "vserverName");
127         assertNotNull(vserverResponse);
128
129         AaiGetVserverResponse vserverNullResponse =
130                 aaiManager.getQueryByVserverName(null, "Dorothy", "Gale", vserverNameRequestId, "vserverName");
131         assertNull(vserverNullResponse);
132
133         when(restManagerMock.get(startsWith("http://somewhere.under.the.rainbow"), eq("Witch"), eq("West"), anyMap()))
134                 .thenReturn(httpResponseErr0);
135
136         AaiGetVserverResponse vserverNotOkResponse0 = aaiManager.getQueryByVserverName(
137                 "http://somewhere.under.the.rainbow", "Witch", "West", vserverNameRequestId, "vserverName");
138         assertNull(vserverNotOkResponse0);
139     }
140
141     @Test
142     public void testAaiManagerQueryByVnfId() {
143         AaiManager aaiManager = new AaiManager(restManagerMock);
144         assertNotNull(aaiManager);
145
146         UUID vserverNameRequestId = UUID.randomUUID();
147
148         when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
149                 .thenReturn(httpResponseOk);
150
151         AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
152                 "Gale", vserverNameRequestId, "vnfID");
153         assertNotNull(vnfResponse);
154     }
155
156     @Test
157     public void testAaiManagerQueryByVnfName() {
158         AaiManager aaiManager = new AaiManager(restManagerMock);
159         assertNotNull(aaiManager);
160
161         UUID vserverNameRequestId = UUID.randomUUID();
162
163         when(restManagerMock.get(startsWith("http://somewhere.over.the.rainbow"), eq("Dorothy"), eq("Gale"), anyMap()))
164                 .thenReturn(httpResponseOk);
165
166         AaiGetVnfResponse vnfResponse = aaiManager.getQueryByVnfId("http://somewhere.over.the.rainbow", "Dorothy",
167                 "Gale", vserverNameRequestId, "vnfName");
168         assertNotNull(vnfResponse);
169     }
170 }