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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.when;
31 import java.util.concurrent.CompletableFuture;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.aai.AaiConstants;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.coder.StandardCoderObject;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
39 import org.onap.policy.controlloop.policy.PolicyResult;
41 public class AaiGetOperatorTest extends BasicAaiOperator<Void> {
43 private static final String INPUT_FIELD = "input";
44 private static final String TEXT = "my-text";
46 private AaiGetOperation oper;
48 public AaiGetOperatorTest() {
49 super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT);
56 public void setUp() throws Exception {
58 oper = new AaiGetOperation(params, operator);
62 public void testGetRetry() {
63 // use default if null retry
64 assertEquals(AaiGetOperation.DEFAULT_RETRY, oper.getRetry(null));
66 // otherwise, use specified value
67 assertEquals(0, oper.getRetry(0));
68 assertEquals(10, oper.getRetry(10));
72 public void testStartOperationAsync_testStartQueryAsync_testPostProcessResponse() throws Exception {
74 // return a map in the reply
75 Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
76 when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
78 when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
80 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
81 assertFalse(future2.isDone());
83 assertEquals(PolicyResult.SUCCESS, future2.get(5, TimeUnit.SECONDS).getResult());
85 // data should have been cached within the context
86 StandardCoderObject data = context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY));
88 assertEquals(TEXT, data.getString(INPUT_FIELD));
92 * Tests startOperationAsync() when there's a failure.
95 public void testStartOperationAsyncFailure() throws Exception {
97 when(rawResponse.getStatus()).thenReturn(500);
98 when(rawResponse.readEntity(String.class)).thenReturn("");
100 when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
102 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
103 assertFalse(future2.isDone());
105 assertEquals(PolicyResult.FAILURE, future2.get(5, TimeUnit.SECONDS).getResult());
107 // data should NOT have been cached within the context
108 assertNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
112 public void testMakeHeaders() {
113 verifyHeaders(oper.makeHeaders());
117 public void testMakePath() {
118 assertEquals(PATH + "/" + TARGET_ENTITY, oper.makePath());
122 public void testAaiGetOperator() {
123 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
124 assertEquals(AaiGetOperation.TENANT, oper.getName());
128 public void testGetTenantKey() {
129 assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetOperation.getTenantKey(TARGET_ENTITY));