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.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.when;
32 import java.util.concurrent.CompletableFuture;
33 import javax.ws.rs.client.InvocationCallback;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.aai.AaiConstants;
39 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.coder.StandardCoderObject;
42 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
43 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
45 import org.onap.policy.controlloop.policy.PolicyResult;
47 public class AaiGetPnfOperationTest extends BasicAaiOperation<Void> {
48 private static final String INPUT_FIELD = "input";
49 private static final String TEXT = "my-text";
51 private AaiGetPnfOperation oper;
53 public AaiGetPnfOperationTest() {
54 super(AaiConstants.ACTOR_NAME, AaiGetPnfOperation.NAME);
58 public static void setUpBeforeClass() throws Exception {
63 public static void tearDownAfterClass() {
71 public void setUp() throws Exception {
73 oper = new AaiGetPnfOperation(params, config);
77 public void testConstructor() {
78 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
79 assertEquals(AaiGetPnfOperation.NAME, oper.getName());
83 * Tests "success" case with simulator.
86 public void testSuccess() throws Exception {
87 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/network/pnfs/pnf").build();
88 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
90 params = params.toBuilder().targetEntity("OzVServer").retry(0).timeoutSec(5).executor(blockingExecutor).build();
91 oper = new AaiGetPnfOperation(params, config);
93 outcome = oper.start().get();
94 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
98 * Tests "failure" case with simulator.
101 public void testFailure() throws Exception {
102 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/network/pnfs/pnf").build();
103 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
105 params = params.toBuilder().targetEntity("getFail").retry(0).timeoutSec(5).executor(blockingExecutor).build();
106 oper = new AaiGetPnfOperation(params, config);
108 outcome = oper.start().get();
109 assertEquals(PolicyResult.FAILURE, outcome.getResult());
113 @SuppressWarnings("unchecked")
114 public void testStartOperationAsync_testStartQueryAsync() throws Exception {
116 // return a map in the reply
117 Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
118 when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
120 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
122 oper.generateSubRequestId(1);
123 outcome.setSubRequestId(oper.getSubRequestId());
125 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
126 assertFalse(future2.isDone());
128 executor.runAll(100);
129 assertTrue(future2.isDone());
131 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
133 // data should have been cached within the context
134 StandardCoderObject data = context.getProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY));
136 assertEquals(TEXT, data.getString(INPUT_FIELD));
138 assertEquals("1", future2.get().getSubRequestId());
142 * Tests startOperationAsync() when there's a failure.
145 @SuppressWarnings("unchecked")
146 public void testStartOperationAsyncFailure() throws Exception {
148 when(rawResponse.getStatus()).thenReturn(500);
149 when(rawResponse.readEntity(String.class)).thenReturn("");
151 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
153 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
154 assertFalse(future2.isDone());
156 executor.runAll(100);
157 assertTrue(future2.isDone());
159 assertEquals(PolicyResult.FAILURE, future2.get().getResult());
161 // data should NOT have been cached within the context
162 assertNull(context.getProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY)));
166 public void testGetKey() {
167 assertEquals("AAI.Pnf." + TARGET_ENTITY, AaiGetPnfOperation.getKey(TARGET_ENTITY));