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