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