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.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.when;
30 import java.util.List;
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.OperationProperties;
44 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
45 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
46 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
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()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
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().retry(0).timeoutSec(5).executor(blockingExecutor).build();
97 oper = new AaiGetPnfOperation(params, config);
98 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "OzVServer");
100 outcome = oper.start().get();
101 assertEquals(OperationResult.SUCCESS, outcome.getResult());
102 assertTrue(outcome.getResponse() instanceof StandardCoderObject);
106 * Tests "failure" case with simulator.
109 public void testFailure() throws Exception {
110 HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/network/pnfs/pnf").build();
111 config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
113 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
114 oper = new AaiGetPnfOperation(params, config);
115 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "getFail");
117 outcome = oper.start().get();
118 assertEquals(OperationResult.FAILURE, outcome.getResult());
122 @SuppressWarnings("unchecked")
123 public void testStartOperationAsync_testStartQueryAsync() throws Exception {
125 // return a map in the reply
126 Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
127 when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
129 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
131 oper.generateSubRequestId(1);
132 outcome.setSubRequestId(oper.getSubRequestId());
134 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
135 assertFalse(future2.isDone());
137 executor.runAll(100);
138 assertTrue(future2.isDone());
140 assertEquals(OperationResult.SUCCESS, future2.get().getResult());
142 assertEquals("1", future2.get().getSubRequestId());
146 * Tests startOperationAsync() when there's a failure.
149 @SuppressWarnings("unchecked")
150 public void testStartOperationAsyncFailure() throws Exception {
152 when(rawResponse.getStatus()).thenReturn(500);
153 when(rawResponse.readEntity(String.class)).thenReturn("");
155 when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
157 CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
158 assertFalse(future2.isDone());
160 executor.runAll(100);
161 assertTrue(future2.isDone());
163 assertEquals(OperationResult.FAILURE, future2.get().getResult());
167 public void testGetKey() {
168 assertEquals("AAI.Pnf." + TARGET_ENTITY, AaiGetPnfOperation.getKey(TARGET_ENTITY));