2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.aai;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.when;
33 import java.util.concurrent.CompletableFuture;
34 import javax.ws.rs.client.InvocationCallback;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.aai.AaiConstants;
40 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.coder.StandardCoderObject;
43 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
45 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
46 import org.onap.policy.controlloop.policy.PolicyResult;
48 public class AaiGetTenantOperationTest extends BasicAaiOperation {
49 private static final String INPUT_FIELD = "input";
50 private static final String TEXT = "my-text";
52 private AaiGetTenantOperation oper;
54 public AaiGetTenantOperationTest() {
55 super(AaiConstants.ACTOR_NAME, AaiGetTenantOperation.NAME);
59 public static void setUpBeforeClass() throws Exception {
64 public static void tearDownAfterClass() {
72 public void setUp() throws Exception {
74 oper = new AaiGetTenantOperation(params, config);
78 public void testConstructor() {
79 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
80 assertEquals(AaiGetTenantOperation.NAME, oper.getName());
84 public void testGetPropertyNames() {
85 assertThat(oper.getPropertyNames()).isEmpty();
89 * Tests "success" case with simulator.
92 public void testSuccess() throws Exception {
93 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
94 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
96 params = params.toBuilder().targetEntity("OzVServer").retry(0).timeoutSec(5).executor(blockingExecutor).build();
97 oper = new AaiGetTenantOperation(params, config);
99 outcome = oper.start().get();
100 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
101 assertTrue(outcome.getResponse() instanceof StandardCoderObject);
105 * Tests "failure" case with simulator.
108 public void testFailure() throws Exception {
109 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
110 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
112 params = params.toBuilder().targetEntity("failedVserver").retry(0).timeoutSec(5).executor(blockingExecutor)
114 oper = new AaiGetTenantOperation(params, config);
116 outcome = oper.start().get();
117 assertEquals(PolicyResult.FAILURE, outcome.getResult());
121 @SuppressWarnings("unchecked")
122 public void testStartOperationAsync_testStartQueryAsync() throws Exception {
124 // return a map in the reply
125 Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
126 when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
128 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
130 oper.generateSubRequestId(1);
131 outcome.setSubRequestId(oper.getSubRequestId());
133 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
134 assertFalse(future2.isDone());
136 executor.runAll(100);
137 assertTrue(future2.isDone());
139 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
141 // data should have been cached within the context
142 StandardCoderObject data = context.getProperty(AaiGetTenantOperation.getKey(TARGET_ENTITY));
144 assertEquals(TEXT, data.getString(INPUT_FIELD));
146 assertEquals("1", future2.get().getSubRequestId());
150 * Tests startOperationAsync() when there's a failure.
153 @SuppressWarnings("unchecked")
154 public void testStartOperationAsyncFailure() throws Exception {
156 when(rawResponse.getStatus()).thenReturn(500);
157 when(rawResponse.readEntity(String.class)).thenReturn("");
159 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
161 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
162 assertFalse(future2.isDone());
164 executor.runAll(100);
165 assertTrue(future2.isDone());
167 assertEquals(PolicyResult.FAILURE, future2.get().getResult());
169 // data should NOT have been cached within the context
170 assertNull(context.getProperty(AaiGetTenantOperation.getKey(TARGET_ENTITY)));
174 public void testGetKey() {
175 assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY));