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 AaiGetPnfOperationTest extends BasicAaiOperation {
49 private static final String INPUT_FIELD = "input";
50 private static final String TEXT = "my-text";
52 private AaiGetPnfOperation oper;
54 public AaiGetPnfOperationTest() {
55 super(AaiConstants.ACTOR_NAME, AaiGetPnfOperation.NAME);
59 public static void setUpBeforeClass() throws Exception {
64 public static void tearDownAfterClass() {
72 public void setUp() throws Exception {
74 oper = new AaiGetPnfOperation(params, config);
78 public void testConstructor() {
79 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
80 assertEquals(AaiGetPnfOperation.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/network/pnfs/pnf").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 AaiGetPnfOperation(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/network/pnfs/pnf").build();
110 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
112 params = params.toBuilder().targetEntity("getFail").retry(0).timeoutSec(5).executor(blockingExecutor).build();
113 oper = new AaiGetPnfOperation(params, config);
115 outcome = oper.start().get();
116 assertEquals(PolicyResult.FAILURE, outcome.getResult());
120 @SuppressWarnings("unchecked")
121 public void testStartOperationAsync_testStartQueryAsync() throws Exception {
123 // return a map in the reply
124 Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
125 when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
127 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
129 oper.generateSubRequestId(1);
130 outcome.setSubRequestId(oper.getSubRequestId());
132 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
133 assertFalse(future2.isDone());
135 executor.runAll(100);
136 assertTrue(future2.isDone());
138 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
140 // data should have been cached within the context
141 StandardCoderObject data = context.getProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY));
143 assertEquals(TEXT, data.getString(INPUT_FIELD));
145 assertEquals("1", future2.get().getSubRequestId());
149 * Tests startOperationAsync() when there's a failure.
152 @SuppressWarnings("unchecked")
153 public void testStartOperationAsyncFailure() throws Exception {
155 when(rawResponse.getStatus()).thenReturn(500);
156 when(rawResponse.readEntity(String.class)).thenReturn("");
158 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
160 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
161 assertFalse(future2.isDone());
163 executor.runAll(100);
164 assertTrue(future2.isDone());
166 assertEquals(PolicyResult.FAILURE, future2.get().getResult());
168 // data should NOT have been cached within the context
169 assertNull(context.getProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY)));
173 public void testGetKey() {
174 assertEquals("AAI.Pnf." + TARGET_ENTITY, AaiGetPnfOperation.getKey(TARGET_ENTITY));