82d575199d2fecc83f9c941c1d778e7cd57b8b52
[policy/models.git] / models-interactions / model-actors / actor.aai / src / test / java / org / onap / policy / controlloop / actor / aai / AaiGetPnfOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.aai;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.when;
30
31 import java.util.List;
32 import java.util.Map;
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.OperationProperties;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
46 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
47 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
48
49 public class AaiGetPnfOperationTest extends BasicAaiOperation {
50     private static final String INPUT_FIELD = "input";
51     private static final String TEXT = "my-text";
52
53     private AaiGetPnfOperation oper;
54
55     public AaiGetPnfOperationTest() {
56         super(AaiConstants.ACTOR_NAME, AaiGetPnfOperation.NAME);
57     }
58
59     @BeforeClass
60     public static void setUpBeforeClass() throws Exception {
61         initBeforeClass();
62     }
63
64     @AfterClass
65     public static void tearDownAfterClass() {
66         destroyAfterClass();
67     }
68
69     /**
70      * Sets up.
71      */
72     @Before
73     public void setUp() throws Exception {
74         super.setUpBasic();
75         oper = new AaiGetPnfOperation(params, config);
76         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
77     }
78
79     @Test
80     public void testConstructor() {
81         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
82         assertEquals(AaiGetPnfOperation.NAME, oper.getName());
83     }
84
85     @Test
86     public void testGetPropertyNames() {
87         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
88     }
89
90     /**
91      * Tests "success" case with simulator.
92      */
93     @Test
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());
97
98         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
99         oper = new AaiGetPnfOperation(params, config);
100         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "demo-pnf");
101
102         outcome = oper.start().get();
103         assertEquals(OperationResult.SUCCESS, outcome.getResult());
104         assertTrue(outcome.getResponse() instanceof StandardCoderObject);
105     }
106
107     /**
108      * Tests "failure" case with simulator.
109      */
110     @Test
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());
114
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");
118
119         outcome = oper.start().get();
120         assertEquals(OperationResult.FAILURE, outcome.getResult());
121     }
122
123     @Test
124     @SuppressWarnings("unchecked")
125     public void testStartOperationAsync_testStartQueryAsync() throws Exception {
126
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));
130
131         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
132
133         oper.generateSubRequestId(1);
134         outcome.setSubRequestId(oper.getSubRequestId());
135
136         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
137         assertFalse(future2.isDone());
138
139         executor.runAll(100);
140         assertTrue(future2.isDone());
141
142         assertEquals(OperationResult.SUCCESS, future2.get().getResult());
143
144         assertEquals("1", future2.get().getSubRequestId());
145     }
146
147     /**
148      * Tests startOperationAsync() when there's a failure.
149      */
150     @Test
151     @SuppressWarnings("unchecked")
152     public void testStartOperationAsyncFailure() throws Exception {
153
154         when(rawResponse.getStatus()).thenReturn(500);
155         when(rawResponse.readEntity(String.class)).thenReturn("");
156
157         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
158
159         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
160         assertFalse(future2.isDone());
161
162         executor.runAll(100);
163         assertTrue(future2.isDone());
164
165         assertEquals(OperationResult.FAILURE, future2.get().getResult());
166     }
167
168     /**
169      * Tests startOperationAsync() when a property is missing.
170      */
171     @Test
172     public void testStartOperationAsyncMissingProperty() throws Exception {
173         oper = new AaiGetPnfOperation(params, config);
174
175         oper.generateSubRequestId(1);
176         outcome.setSubRequestId(oper.getSubRequestId());
177
178         assertThatIllegalStateException().isThrownBy(() -> oper.startOperationAsync(1, outcome))
179                         .withMessageContaining("missing target entity");
180     }
181
182     @Test
183     public void testGetKey() {
184         assertEquals("AAI.Pnf." + TARGET_ENTITY, AaiGetPnfOperation.getKey(TARGET_ENTITY));
185     }
186 }