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