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;
32 import java.util.List;
34 import java.util.concurrent.CompletableFuture;
35 import javax.ws.rs.client.InvocationCallback;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.policy.aai.AaiConstants;
41 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.coder.StandardCoderObject;
44 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
46 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
47 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
48 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
50 public class AaiGetPnfOperationTest extends BasicAaiOperation {
51 private static final String INPUT_FIELD = "input";
52 private static final String TEXT = "my-text";
54 private AaiGetPnfOperation oper;
56 public AaiGetPnfOperationTest() {
57 super(AaiConstants.ACTOR_NAME, AaiGetPnfOperation.NAME);
61 public static void setUpBeforeClass() throws Exception {
66 public static void tearDownAfterClass() {
74 public void setUp() throws Exception {
76 oper = new AaiGetPnfOperation(params, config);
80 public void testConstructor() {
81 assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
82 assertEquals(AaiGetPnfOperation.NAME, oper.getName());
86 public void testGetPropertyNames() {
87 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
91 * Tests "success" case with simulator.
94 public void testSuccess() throws Exception {
95 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/network/pnfs/pnf").build();
96 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
98 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
99 oper = new AaiGetPnfOperation(params, config);
100 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "OzVServer");
102 outcome = oper.start().get();
103 assertEquals(OperationResult.SUCCESS, outcome.getResult());
104 assertTrue(outcome.getResponse() instanceof StandardCoderObject);
108 * Tests "failure" case with simulator.
111 public void testFailure() throws Exception {
112 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/network/pnfs/pnf").build();
113 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
115 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
116 oper = new AaiGetPnfOperation(params, config);
117 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "getFail");
119 outcome = oper.start().get();
120 assertEquals(OperationResult.FAILURE, outcome.getResult());
124 @SuppressWarnings("unchecked")
125 public void testStartOperationAsync_testStartQueryAsync() throws Exception {
127 // return a map in the reply
128 Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
129 when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
131 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
133 oper.generateSubRequestId(1);
134 outcome.setSubRequestId(oper.getSubRequestId());
136 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
137 assertFalse(future2.isDone());
139 executor.runAll(100);
140 assertTrue(future2.isDone());
142 assertEquals(OperationResult.SUCCESS, future2.get().getResult());
144 // data should have been cached within the context
145 StandardCoderObject data = context.getProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY));
147 assertEquals(TEXT, data.getString(INPUT_FIELD));
149 assertEquals("1", future2.get().getSubRequestId());
153 * Tests startOperationAsync() when there's a failure.
156 @SuppressWarnings("unchecked")
157 public void testStartOperationAsyncFailure() throws Exception {
159 when(rawResponse.getStatus()).thenReturn(500);
160 when(rawResponse.readEntity(String.class)).thenReturn("");
162 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
164 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
165 assertFalse(future2.isDone());
167 executor.runAll(100);
168 assertTrue(future2.isDone());
170 assertEquals(OperationResult.FAILURE, future2.get().getResult());
172 // data should NOT have been cached within the context
173 assertNull(context.getProperty(AaiGetPnfOperation.getKey(TARGET_ENTITY)));
177 public void testGetKey() {
178 assertEquals("AAI.Pnf." + TARGET_ENTITY, AaiGetPnfOperation.getKey(TARGET_ENTITY));