Fix some sonars in policy-models
[policy/models.git] / models-interactions / model-actors / actor.aai / src / test / java / org / onap / policy / controlloop / actor / aai / AaiGetTenantOperationTest.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 AaiGetTenantOperationTest extends BasicAaiOperation {
48     private static final String INPUT_FIELD = "input";
49     private static final String TEXT = "my-text";
50
51     private AaiGetTenantOperation oper;
52
53     public AaiGetTenantOperationTest() {
54         super(AaiConstants.ACTOR_NAME, AaiGetTenantOperation.NAME);
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 AaiGetTenantOperation(params, config);
74     }
75
76     @Test
77     public void testConstructor() {
78         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
79         assertEquals(AaiGetTenantOperation.NAME, oper.getName());
80     }
81
82     /**
83      * Tests "success" case with simulator.
84      */
85     @Test
86     public void testSuccess() throws Exception {
87         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
88         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
89
90         params = params.toBuilder().targetEntity("OzVServer").retry(0).timeoutSec(5).executor(blockingExecutor).build();
91         oper = new AaiGetTenantOperation(params, config);
92
93         outcome = oper.start().get();
94         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
95         assertTrue(outcome.getResponse() instanceof StandardCoderObject);
96     }
97
98     /**
99      * Tests "failure" case with simulator.
100      */
101     @Test
102     public void testFailure() throws Exception {
103         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
104         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
105
106         params = params.toBuilder().targetEntity("failedVserver").retry(0).timeoutSec(5).executor(blockingExecutor)
107                         .build();
108         oper = new AaiGetTenantOperation(params, config);
109
110         outcome = oper.start().get();
111         assertEquals(PolicyResult.FAILURE, outcome.getResult());
112     }
113
114     @Test
115     @SuppressWarnings("unchecked")
116     public void testStartOperationAsync_testStartQueryAsync() throws Exception {
117
118         // return a map in the reply
119         Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
120         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
121
122         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
123
124         oper.generateSubRequestId(1);
125         outcome.setSubRequestId(oper.getSubRequestId());
126
127         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
128         assertFalse(future2.isDone());
129
130         executor.runAll(100);
131         assertTrue(future2.isDone());
132
133         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
134
135         // data should have been cached within the context
136         StandardCoderObject data = context.getProperty(AaiGetTenantOperation.getKey(TARGET_ENTITY));
137         assertNotNull(data);
138         assertEquals(TEXT, data.getString(INPUT_FIELD));
139
140         assertEquals("1", future2.get().getSubRequestId());
141     }
142
143     /**
144      * Tests startOperationAsync() when there's a failure.
145      */
146     @Test
147     @SuppressWarnings("unchecked")
148     public void testStartOperationAsyncFailure() throws Exception {
149
150         when(rawResponse.getStatus()).thenReturn(500);
151         when(rawResponse.readEntity(String.class)).thenReturn("");
152
153         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
154
155         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
156         assertFalse(future2.isDone());
157
158         executor.runAll(100);
159         assertTrue(future2.isDone());
160
161         assertEquals(PolicyResult.FAILURE, future2.get().getResult());
162
163         // data should NOT have been cached within the context
164         assertNull(context.getProperty(AaiGetTenantOperation.getKey(TARGET_ENTITY)));
165     }
166
167     @Test
168     public void testGetKey() {
169         assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY));
170     }
171 }