Set sub request ID before start callback
[policy/models.git] / models-interactions / model-actors / actor.aai / src / test / java / org / onap / policy / controlloop / actor / aai / AaiGetOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.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;
30
31 import java.util.Map;
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;
46
47 public class AaiGetOperationTest extends BasicAaiOperation<Void> {
48     private static final String INPUT_FIELD = "input";
49     private static final String TEXT = "my-text";
50
51     private AaiGetOperation oper;
52
53     public AaiGetOperationTest() {
54         super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT);
55     }
56
57     @BeforeClass
58     public static void setUpBeforeClass() throws Exception {
59         initBeforeClass();
60     }
61
62     @AfterClass
63     public static void tearDownAfterClass() {
64         destroyAfterClass();
65     }
66
67     /**
68      * Sets up.
69      */
70     @Before
71     public void setUp() throws Exception {
72         super.setUpBasic();
73         oper = new AaiGetOperation(params, config);
74     }
75
76     /**
77      * Tests "success" case with simulator.
78      */
79     @Test
80     public void testSuccess() throws Exception {
81         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
82         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
83
84         params = params.toBuilder().targetEntity("OzVServer").retry(0).timeoutSec(5).executor(blockingExecutor).build();
85         oper = new AaiGetOperation(params, config);
86
87         outcome = oper.start().get();
88         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
89     }
90
91     /**
92      * Tests "failure" case with simulator.
93      */
94     @Test
95     public void testFailure() throws Exception {
96         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
97         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
98
99         params = params.toBuilder().targetEntity("failedVserver").retry(0).timeoutSec(5).executor(blockingExecutor)
100                         .build();
101         oper = new AaiGetOperation(params, config);
102
103         outcome = oper.start().get();
104         assertEquals(PolicyResult.FAILURE, outcome.getResult());
105     }
106
107     @Test
108     public void testGetRetry() {
109         // use default if null retry
110         assertEquals(AaiGetOperation.DEFAULT_RETRY, oper.getRetry(null));
111
112         // otherwise, use specified value
113         assertEquals(0, oper.getRetry(0));
114         assertEquals(10, oper.getRetry(10));
115     }
116
117     @Test
118     public void testGenerateSubRequestId() {
119         oper.generateSubRequestId(3);
120         assertEquals("3", oper.getSubRequestId());
121     }
122
123     @Test
124     @SuppressWarnings("unchecked")
125     public void testStartOperationAsync_testStartQueryAsync_testPostProcessResponse() 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(PolicyResult.SUCCESS, future2.get().getResult());
143
144         // data should have been cached within the context
145         StandardCoderObject data = context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY));
146         assertNotNull(data);
147         assertEquals(TEXT, data.getString(INPUT_FIELD));
148
149         assertEquals("1", future2.get().getSubRequestId());
150     }
151
152     /**
153      * Tests startOperationAsync() when there's a failure.
154      */
155     @Test
156     @SuppressWarnings("unchecked")
157     public void testStartOperationAsyncFailure() throws Exception {
158
159         when(rawResponse.getStatus()).thenReturn(500);
160         when(rawResponse.readEntity(String.class)).thenReturn("");
161
162         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
163
164         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
165         assertFalse(future2.isDone());
166
167         executor.runAll(100);
168         assertTrue(future2.isDone());
169
170         assertEquals(PolicyResult.FAILURE, future2.get().getResult());
171
172         // data should NOT have been cached within the context
173         assertNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
174     }
175
176     @Test
177     public void testMakeHeaders() {
178         verifyHeaders(oper.makeHeaders());
179     }
180
181     @Test
182     public void testAaiGetOperator() {
183         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
184         assertEquals(AaiGetOperation.TENANT, oper.getName());
185     }
186
187     @Test
188     public void testGetTenantKey() {
189         assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetOperation.getTenantKey(TARGET_ENTITY));
190     }
191 }