Java 17 Upgrade
[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-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.aai;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.when;
31
32 import jakarta.ws.rs.client.InvocationCallback;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.concurrent.CompletableFuture;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.policy.aai.AaiConstants;
43 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.common.utils.coder.StandardCoderObject;
46 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
47 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
48 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
50 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class AaiGetTenantOperationTest extends BasicAaiOperation {
54     private static final String INPUT_FIELD = "input";
55     private static final String TEXT = "my-text";
56
57     private AaiGetTenantOperation oper;
58
59     public AaiGetTenantOperationTest() {
60         super(AaiConstants.ACTOR_NAME, AaiGetTenantOperation.NAME);
61     }
62
63     @BeforeClass
64     public static void setUpBeforeClass() throws Exception {
65         initBeforeClass();
66     }
67
68     @AfterClass
69     public static void tearDownAfterClass() {
70         destroyAfterClass();
71     }
72
73     /**
74      * Sets up.
75      */
76     @Before
77     public void setUp() throws Exception {
78         super.setUpBasic();
79         oper = new AaiGetTenantOperation(params, config);
80         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
81     }
82
83     @Test
84     public void testConstructor() {
85         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
86         assertEquals(AaiGetTenantOperation.NAME, oper.getName());
87     }
88
89     @Test
90     public void testGetPropertyNames() {
91         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
92     }
93
94     /**
95      * Tests "success" case with simulator.
96      */
97     @Test
98     public void testSuccess() throws Exception {
99         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
100         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
101
102         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
103         oper = new AaiGetTenantOperation(params, config);
104         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "OzVServer");
105
106         outcome = oper.start().get();
107         assertEquals(OperationResult.SUCCESS, outcome.getResult());
108         assertTrue(outcome.getResponse() instanceof StandardCoderObject);
109     }
110
111     /**
112      * Tests "failure" case with simulator.
113      */
114     @Test
115     public void testFailure() throws Exception {
116         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
117         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
118
119         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
120         oper = new AaiGetTenantOperation(params, config);
121         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "failedVserver");
122
123         outcome = oper.start().get();
124         assertEquals(OperationResult.FAILURE, outcome.getResult());
125     }
126
127     @Test
128     @SuppressWarnings("unchecked")
129     public void testStartOperationAsync_testStartQueryAsync() throws Exception {
130
131         // return a map in the reply
132         Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
133         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
134
135         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
136
137         oper.generateSubRequestId(1);
138         outcome.setSubRequestId(oper.getSubRequestId());
139
140         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
141         assertFalse(future2.isDone());
142
143         executor.runAll(100);
144         assertTrue(future2.isDone());
145
146         assertEquals(OperationResult.SUCCESS, future2.get().getResult());
147
148         assertEquals("1", future2.get().getSubRequestId());
149     }
150
151     /**
152      * Tests startOperationAsync() when there's a failure.
153      */
154     @Test
155     @SuppressWarnings("unchecked")
156     public void testStartOperationAsyncFailure() throws Exception {
157
158         when(rawResponse.getStatus()).thenReturn(500);
159         when(rawResponse.readEntity(String.class)).thenReturn("");
160
161         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
162
163         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
164         assertFalse(future2.isDone());
165
166         executor.runAll(100);
167         assertTrue(future2.isDone());
168
169         assertEquals(OperationResult.FAILURE, future2.get().getResult());
170     }
171
172     /**
173      * Tests startOperationAsync() when a property is missing.
174      */
175     @Test
176     public void testStartOperationAsyncMissingProperty() throws Exception {
177         oper = new AaiGetTenantOperation(params, config);
178
179         oper.generateSubRequestId(1);
180         outcome.setSubRequestId(oper.getSubRequestId());
181
182         assertThatIllegalStateException().isThrownBy(() -> oper.startOperationAsync(1, outcome))
183                         .withMessageContaining("missing target entity");
184     }
185
186     @Test
187     public void testGetKey() {
188         assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY));
189     }
190 }