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