2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controlloop.actor.aai;
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;
33 import jakarta.ws.rs.client.InvocationCallback;
34 import java.util.List;
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;
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";
58 private AaiGetTenantOperation oper;
60 AaiGetTenantOperationTest() {
61 super(AaiConstants.ACTOR_NAME, AaiGetTenantOperation.NAME);
65 static void setUpBeforeClass() throws Exception {
70 static void tearDownAfterClass() {
78 void setUp() throws Exception {
80 oper = new AaiGetTenantOperation(params, config);
81 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
85 void testConstructor() {
86 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
87 assertEquals(AaiGetTenantOperation.NAME, oper.getName());
91 void testGetPropertyNames() {
92 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
96 * Tests "success" case with simulator.
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());
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");
107 outcome = oper.start().get();
108 assertEquals(OperationResult.SUCCESS, outcome.getResult());
109 assertInstanceOf(StandardCoderObject.class, outcome.getResponse());
113 * Tests "failure" case with simulator.
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());
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");
124 outcome = oper.start().get();
125 assertEquals(OperationResult.FAILURE, outcome.getResult());
129 @SuppressWarnings("unchecked")
130 void testStartOperationAsync_testStartQueryAsync() throws Exception {
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));
136 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
138 oper.generateSubRequestId(1);
139 outcome.setSubRequestId(oper.getSubRequestId());
141 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
142 assertFalse(future2.isDone());
144 executor.runAll(100);
145 assertTrue(future2.isDone());
147 assertEquals(OperationResult.SUCCESS, future2.get().getResult());
149 assertEquals("1", future2.get().getSubRequestId());
153 * Tests startOperationAsync() when there's a failure.
156 @SuppressWarnings("unchecked")
157 void testStartOperationAsyncFailure() throws Exception {
159 when(rawResponse.getStatus()).thenReturn(500);
160 when(rawResponse.readEntity(String.class)).thenReturn("");
162 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
164 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
165 assertFalse(future2.isDone());
167 executor.runAll(100);
168 assertTrue(future2.isDone());
170 assertEquals(OperationResult.FAILURE, future2.get().getResult());
174 * Tests startOperationAsync() when a property is missing.
177 void testStartOperationAsyncMissingProperty() throws Exception {
178 oper = new AaiGetTenantOperation(params, config);
180 oper.generateSubRequestId(1);
181 outcome.setSubRequestId(oper.getSubRequestId());
183 assertThatIllegalStateException().isThrownBy(() -> oper.startOperationAsync(1, outcome))
184 .withMessageContaining("missing target entity");
189 assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY));