2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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
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.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.when;
32 import jakarta.ws.rs.client.InvocationCallback;
33 import java.util.List;
35 import java.util.concurrent.CompletableFuture;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.policy.aai.AaiConstants;
43 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.common.utils.coder.StandardCoderObject;
46 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
47 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
48 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
50 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
52 @RunWith(MockitoJUnitRunner.class)
53 public class AaiGetTenantOperationTest extends BasicAaiOperation {
54 private static final String INPUT_FIELD = "input";
55 private static final String TEXT = "my-text";
57 private AaiGetTenantOperation oper;
59 public AaiGetTenantOperationTest() {
60 super(AaiConstants.ACTOR_NAME, AaiGetTenantOperation.NAME);
64 public static void setUpBeforeClass() throws Exception {
69 public static void tearDownAfterClass() {
77 public void setUp() throws Exception {
79 oper = new AaiGetTenantOperation(params, config);
80 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
84 public void testConstructor() {
85 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
86 assertEquals(AaiGetTenantOperation.NAME, oper.getName());
90 public void testGetPropertyNames() {
91 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
95 * Tests "success" case with simulator.
98 public void testSuccess() throws Exception {
99 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
100 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
102 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
103 oper = new AaiGetTenantOperation(params, config);
104 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "OzVServer");
106 outcome = oper.start().get();
107 assertEquals(OperationResult.SUCCESS, outcome.getResult());
108 assertTrue(outcome.getResponse() instanceof StandardCoderObject);
112 * Tests "failure" case with simulator.
115 public void testFailure() throws Exception {
116 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
117 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
119 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
120 oper = new AaiGetTenantOperation(params, config);
121 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "failedVserver");
123 outcome = oper.start().get();
124 assertEquals(OperationResult.FAILURE, outcome.getResult());
128 @SuppressWarnings("unchecked")
129 public void testStartOperationAsync_testStartQueryAsync() throws Exception {
131 // return a map in the reply
132 Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
133 when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
135 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
137 oper.generateSubRequestId(1);
138 outcome.setSubRequestId(oper.getSubRequestId());
140 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
141 assertFalse(future2.isDone());
143 executor.runAll(100);
144 assertTrue(future2.isDone());
146 assertEquals(OperationResult.SUCCESS, future2.get().getResult());
148 assertEquals("1", future2.get().getSubRequestId());
152 * Tests startOperationAsync() when there's a failure.
155 @SuppressWarnings("unchecked")
156 public void testStartOperationAsyncFailure() throws Exception {
158 when(rawResponse.getStatus()).thenReturn(500);
159 when(rawResponse.readEntity(String.class)).thenReturn("");
161 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
163 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
164 assertFalse(future2.isDone());
166 executor.runAll(100);
167 assertTrue(future2.isDone());
169 assertEquals(OperationResult.FAILURE, future2.get().getResult());
173 * Tests startOperationAsync() when a property is missing.
176 public void testStartOperationAsyncMissingProperty() throws Exception {
177 oper = new AaiGetTenantOperation(params, config);
179 oper.generateSubRequestId(1);
180 outcome.setSubRequestId(oper.getSubRequestId());
182 assertThatIllegalStateException().isThrownBy(() -> oper.startOperationAsync(1, outcome))
183 .withMessageContaining("missing target entity");
187 public void testGetKey() {
188 assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY));