Merge "add unit test for pdpstatistics provider"
[policy/models.git] / models-interactions / model-actors / actor.aai / src / test / java / org / onap / policy / controlloop / actor / aai / AaiCustomQueryOperationTest.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.assertNotNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.when;
29
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.concurrent.CompletableFuture;
34 import java.util.concurrent.ExecutionException;
35 import java.util.concurrent.TimeoutException;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.onap.policy.aai.AaiConstants;
40 import org.onap.policy.aai.AaiCqResponse;
41 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
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.Operation;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
46 import org.onap.policy.controlloop.actorserviceprovider.Util;
47 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
48 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
50 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
51 import org.onap.policy.controlloop.policy.PolicyResult;
52
53 public class AaiCustomQueryOperationTest extends BasicAaiOperator<Map<String, String>> {
54     private static final StandardCoder coder = new StandardCoder();
55
56     private static final String MY_LINK = "my-link";
57
58     @Mock
59     private Actor tenantActor;
60
61     private AaiCustomQueryOperation oper;
62
63     public AaiCustomQueryOperationTest() {
64         super(AaiConstants.ACTOR_NAME, AaiCustomQueryOperation.NAME);
65     }
66
67     /**
68      * Sets up.
69      */
70     @Before
71     public void setUp() throws Exception {
72         super.setUp();
73
74         MyTenantOperator tenantOperator = new MyTenantOperator();
75
76         when(service.getActor(AaiConstants.ACTOR_NAME)).thenReturn(tenantActor);
77         when(tenantActor.getOperator(AaiGetOperation.TENANT)).thenReturn(tenantOperator);
78
79         oper = new AaiCustomQueryOperation(params, operator);
80     }
81
82     @Test
83     public void testAaiCustomQueryOperation() {
84         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
85         assertEquals(AaiCustomQueryOperation.NAME, oper.getName());
86     }
87
88     @Test
89     public void testStartOperationAsync_testStartPreprocessorAsync_testMakeRequest_testPostProcess() throws Exception {
90         // need two responses
91         when(rawResponse.readEntity(String.class)).thenReturn(makeTenantReply()).thenReturn(makeCqReply());
92         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
93         when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
94
95         CompletableFuture<OperationOutcome> future2 = oper.start();
96
97         assertEquals(PolicyResult.SUCCESS, getResult(future2));
98
99         // tenant response should have been cached within the context
100         assertNotNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
101
102         // custom query response should have been cached within the context
103         AaiCqResponse cqData = context.getProperty(AaiCqResponse.CONTEXT_KEY);
104         assertNotNull(cqData);
105     }
106
107     /**
108      * Tests when preprocessor step is not needed.
109      */
110     @Test
111     public void testStartOperationAsync_testStartPreprocessorAsyncNotNeeded() throws Exception {
112         // pre-load the tenant data
113         final StandardCoderObject data = preloadTenantData();
114
115         // only need one response
116         when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply());
117         when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
118
119         CompletableFuture<OperationOutcome> future2 = oper.start();
120
121         assertEquals(PolicyResult.SUCCESS, getResult(future2));
122
123         // should not have replaced tenant response
124         assertSame(data, context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
125
126         // custom query response should have been cached within the context
127         AaiCqResponse cqData = context.getProperty(AaiCqResponse.CONTEXT_KEY);
128         assertNotNull(cqData);
129     }
130
131     @Test
132     public void testMakeHeaders() {
133         verifyHeaders(oper.makeHeaders());
134     }
135
136     @Test
137     public void testMakeRequestNoResourceLink() throws Exception {
138         // pre-load EMPTY tenant data
139         preloadTenantData(new StandardCoderObject());
140
141         when(rawResponse.readEntity(String.class)).thenReturn(makeCqReply());
142         when(client.put(any(), any(), any(), any())).thenAnswer(provideResponse(rawResponse));
143
144         CompletableFuture<OperationOutcome> future2 = oper.start();
145
146         assertEquals(PolicyResult.FAILURE_EXCEPTION, getResult(future2));
147     }
148
149     private String makeTenantReply() throws Exception {
150         Map<String, String> links = Map.of(AaiCustomQueryOperation.RESOURCE_LINK, MY_LINK);
151         List<Map<String, String>> data = Arrays.asList(links);
152
153         Map<String, Object> reply = Map.of(AaiCustomQueryOperation.RESULT_DATA, data);
154         return coder.encode(reply);
155     }
156
157     private String makeCqReply() {
158         return "{}";
159     }
160
161     private StandardCoderObject preloadTenantData() throws Exception {
162         StandardCoderObject data = coder.decode(makeTenantReply(), StandardCoderObject.class);
163         preloadTenantData(data);
164         return data;
165     }
166
167     private void preloadTenantData(StandardCoderObject data) {
168         context.setProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY), data);
169     }
170
171     private PolicyResult getResult(CompletableFuture<OperationOutcome> future2)
172                     throws InterruptedException, ExecutionException, TimeoutException {
173
174         executor.runAll(100);
175         assertTrue(future2.isDone());
176
177         return future2.get().getResult();
178     }
179
180     protected class MyTenantOperator extends HttpOperator {
181         public MyTenantOperator() {
182             super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT);
183
184             HttpParams http = HttpParams.builder().clientName(MY_CLIENT).path(PATH).timeoutSec(1).build();
185
186             configure(Util.translateToMap(AaiGetOperation.TENANT, http));
187             start();
188         }
189
190         @Override
191         public Operation buildOperation(ControlLoopOperationParams params) {
192             return new AaiGetOperation(params, this);
193         }
194
195         @Override
196         protected HttpClientFactory getClientFactory() {
197             return factory;
198         }
199     }
200 }