dd6d0ec18f32ca2f9422735a5b82c443d3c6fd38
[policy/models.git] / models-interactions / model-actors / actor.aai / src / test / java / org / onap / policy / controlloop / actor / aai / AaiCustomQueryOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 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.controlloop.actor.aai;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.lenient;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.TreeMap;
38 import java.util.concurrent.CompletableFuture;
39 import java.util.concurrent.ExecutionException;
40 import java.util.concurrent.TimeoutException;
41 import javax.ws.rs.client.InvocationCallback;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.junit.MockitoJUnitRunner;
48 import org.onap.policy.aai.AaiConstants;
49 import org.onap.policy.aai.AaiCqResponse;
50 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
51 import org.onap.policy.common.utils.coder.StandardCoder;
52 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
53 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
54 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
55 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
56 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
57
58 @RunWith(MockitoJUnitRunner.class)
59 public class AaiCustomQueryOperationTest extends BasicAaiOperation {
60     private static final StandardCoder coder = new StandardCoder();
61
62     private static final String MY_LINK = "my-link";
63
64     private AaiCustomQueryOperation oper;
65
66     public AaiCustomQueryOperationTest() {
67         super(AaiConstants.ACTOR_NAME, AaiCustomQueryOperation.NAME);
68     }
69
70     @BeforeClass
71     public static void setUpBeforeClass() throws Exception {
72         initBeforeClass();
73     }
74
75     @AfterClass
76     public static void tearDownAfterClass() {
77         destroyAfterClass();
78     }
79
80     /**
81      * Sets up.
82      */
83     @Before
84     public void setUp() throws Exception {
85         super.setUpBasic();
86
87         oper = new AaiCustomQueryOperation(params, config);
88         oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
89     }
90
91     /**
92      * Tests "success" case with simulator.
93      */
94     @Test
95     public void testSuccess() throws Exception {
96         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/query").build();
97         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
98
99         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
100         oper = new AaiCustomQueryOperation(params, config);
101
102         oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
103
104         outcome = oper.start().get();
105         assertEquals(OperationResult.SUCCESS, outcome.getResult());
106
107         assertNotNull(outcome.getResponse());
108     }
109
110     @Test
111     public void testConstructor() {
112         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
113         assertEquals(AaiCustomQueryOperation.NAME, oper.getName());
114     }
115
116     @Test
117     public void testGetPropertyNames() {
118         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_VSERVER_LINK));
119     }
120
121     @Test
122     public void testGenerateSubRequestId() {
123         oper.generateSubRequestId(3);
124         assertEquals("3", oper.getSubRequestId());
125     }
126
127     @Test
128     @SuppressWarnings("unchecked")
129     public void testStartOperationAsync_testMakeRequest() throws Exception {
130         // need two responses
131         when(rawResponse.readEntity(String.class)).thenReturn(makeTenantReply()).thenReturn(makeCqReply());
132         lenient().when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
133         when(webAsync.put(any(), any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse, 1));
134
135         CompletableFuture<OperationOutcome> future2 = oper.start();
136
137         assertEquals(OperationResult.SUCCESS, getResult(future2));
138
139         assertEquals("1", future2.get().getSubRequestId());
140     }
141
142     @Test
143     public void testMakeHeaders() {
144         verifyHeaders(oper.makeHeaders());
145     }
146
147     @Test
148     @SuppressWarnings("unchecked")
149     public void testMakeRequest_testGetVserverLink() throws Exception {
150         when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply());
151         when(webAsync.put(any(), any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse, 1));
152
153         oper.start();
154         executor.runAll(100);
155
156         verify(webAsync).put(requestCaptor.capture(), any(InvocationCallback.class));
157
158         String reqText = requestCaptor.getValue().getEntity();
159         Map<String, String> reqMap = coder.decode(reqText, Map.class);
160
161         // sort the request fields so they match the order in cq.json
162         Map<String, String> request = new TreeMap<>(reqMap);
163
164         verifyRequest("cq.json", request);
165     }
166
167     @Test
168     public void testGetVserverLink() throws Exception {
169         oper.setProperty(OperationProperties.AAI_VSERVER_LINK, MY_LINK);
170         assertEquals(MY_LINK, oper.getVserverLink());
171     }
172
173     @Test
174     public void testSetOutcome() {
175         outcome = oper.setOutcome(params.makeOutcome(), OperationResult.SUCCESS, null, null);
176         assertNull(outcome.getResponse());
177
178         outcome = oper.setOutcome(params.makeOutcome(), OperationResult.SUCCESS, null, "{}");
179         assertTrue(outcome.getResponse() instanceof AaiCqResponse);
180     }
181
182     private String makeTenantReply() throws Exception {
183         Map<String, String> links = Map.of(AaiCustomQueryOperation.RESOURCE_LINK, MY_LINK);
184         List<Map<String, String>> data = Arrays.asList(links);
185
186         Map<String, Object> reply = Map.of(AaiCustomQueryOperation.RESULT_DATA, data);
187         return coder.encode(reply);
188     }
189
190     private String makeCqReply() {
191         return "{}";
192     }
193
194
195     private OperationResult getResult(CompletableFuture<OperationOutcome> future2)
196                     throws InterruptedException, ExecutionException, TimeoutException {
197
198         executor.runAll(100);
199         assertTrue(future2.isDone());
200
201         return future2.get().getResult();
202     }
203 }