65d3fe831f84aae06a09dc4c918deba74cc4ae52
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.when;
32
33 import jakarta.ws.rs.client.InvocationCallback;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.concurrent.CompletableFuture;
37 import org.junit.jupiter.api.AfterAll;
38 import org.junit.jupiter.api.BeforeAll;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.mockito.junit.jupiter.MockitoExtension;
43 import org.onap.policy.aai.AaiConstants;
44 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.common.utils.coder.StandardCoderObject;
47 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
48 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
49 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
50 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
51 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
52
53 @ExtendWith(MockitoExtension.class)
54 class AaiGetTenantOperationTest extends BasicAaiOperation {
55     private static final String INPUT_FIELD = "input";
56     private static final String TEXT = "my-text";
57
58     private AaiGetTenantOperation oper;
59
60     AaiGetTenantOperationTest() {
61         super(AaiConstants.ACTOR_NAME, AaiGetTenantOperation.NAME);
62     }
63
64     @BeforeAll
65     static void setUpBeforeClass() throws Exception {
66         initBeforeClass();
67     }
68
69     @AfterAll
70     static void tearDownAfterClass() {
71         destroyAfterClass();
72     }
73
74     /**
75      * Sets up.
76      */
77     @BeforeEach
78     void setUp() throws Exception {
79         super.setUpBasic();
80         oper = new AaiGetTenantOperation(params, config);
81         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
82     }
83
84     @Test
85     void testConstructor() {
86         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
87         assertEquals(AaiGetTenantOperation.NAME, oper.getName());
88     }
89
90     @Test
91     void testGetPropertyNames() {
92         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
93     }
94
95     /**
96      * Tests "success" case with simulator.
97      */
98     @Test
99     void testSuccess() throws Exception {
100         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
101         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
102
103         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
104         oper = new AaiGetTenantOperation(params, config);
105         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "OzVServer");
106
107         outcome = oper.start().get();
108         assertEquals(OperationResult.SUCCESS, outcome.getResult());
109         assertInstanceOf(StandardCoderObject.class, outcome.getResponse());
110     }
111
112     /**
113      * Tests "failure" case with simulator.
114      */
115     @Test
116     void testFailure() throws Exception {
117         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
118         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
119
120         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
121         oper = new AaiGetTenantOperation(params, config);
122         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "failedVserver");
123
124         outcome = oper.start().get();
125         assertEquals(OperationResult.FAILURE, outcome.getResult());
126     }
127
128     @Test
129     @SuppressWarnings("unchecked")
130     void testStartOperationAsync_testStartQueryAsync() throws Exception {
131
132         // return a map in the reply
133         Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
134         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
135
136         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
137
138         oper.generateSubRequestId(1);
139         outcome.setSubRequestId(oper.getSubRequestId());
140
141         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
142         assertFalse(future2.isDone());
143
144         executor.runAll(100);
145         assertTrue(future2.isDone());
146
147         assertEquals(OperationResult.SUCCESS, future2.get().getResult());
148
149         assertEquals("1", future2.get().getSubRequestId());
150     }
151
152     /**
153      * Tests startOperationAsync() when there's a failure.
154      */
155     @Test
156     @SuppressWarnings("unchecked")
157     void testStartOperationAsyncFailure() throws Exception {
158
159         when(rawResponse.getStatus()).thenReturn(500);
160         when(rawResponse.readEntity(String.class)).thenReturn("");
161
162         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
163
164         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
165         assertFalse(future2.isDone());
166
167         executor.runAll(100);
168         assertTrue(future2.isDone());
169
170         assertEquals(OperationResult.FAILURE, future2.get().getResult());
171     }
172
173     /**
174      * Tests startOperationAsync() when a property is missing.
175      */
176     @Test
177     void testStartOperationAsyncMissingProperty() throws Exception {
178         oper = new AaiGetTenantOperation(params, config);
179
180         oper.generateSubRequestId(1);
181         outcome.setSubRequestId(oper.getSubRequestId());
182
183         assertThatIllegalStateException().isThrownBy(() -> oper.startOperationAsync(1, outcome))
184                         .withMessageContaining("missing target entity");
185     }
186
187     @Test
188     void testGetKey() {
189         assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY));
190     }
191 }