Make Actors event-agnostic
[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.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
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.List;
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.OperationProperties;
44 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
45 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
46 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
47
48 public class AaiGetTenantOperationTest extends BasicAaiOperation {
49     private static final String INPUT_FIELD = "input";
50     private static final String TEXT = "my-text";
51
52     private AaiGetTenantOperation oper;
53
54     public AaiGetTenantOperationTest() {
55         super(AaiConstants.ACTOR_NAME, AaiGetTenantOperation.NAME);
56     }
57
58     @BeforeClass
59     public static void setUpBeforeClass() throws Exception {
60         initBeforeClass();
61     }
62
63     @AfterClass
64     public static void tearDownAfterClass() {
65         destroyAfterClass();
66     }
67
68     /**
69      * Sets up.
70      */
71     @Before
72     public void setUp() throws Exception {
73         super.setUpBasic();
74         oper = new AaiGetTenantOperation(params, config);
75     }
76
77     @Test
78     public void testConstructor() {
79         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
80         assertEquals(AaiGetTenantOperation.NAME, oper.getName());
81     }
82
83     @Test
84     public void testGetPropertyNames() {
85         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
86     }
87
88     /**
89      * Tests "success" case with simulator.
90      */
91     @Test
92     public void testSuccess() throws Exception {
93         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
94         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
95
96         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
97         oper = new AaiGetTenantOperation(params, config);
98         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "OzVServer");
99
100         outcome = oper.start().get();
101         assertEquals(OperationResult.SUCCESS, outcome.getResult());
102         assertTrue(outcome.getResponse() instanceof StandardCoderObject);
103     }
104
105     /**
106      * Tests "failure" case with simulator.
107      */
108     @Test
109     public void testFailure() throws Exception {
110         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
111         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
112
113         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
114         oper = new AaiGetTenantOperation(params, config);
115         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "failedVserver");
116
117         outcome = oper.start().get();
118         assertEquals(OperationResult.FAILURE, outcome.getResult());
119     }
120
121     @Test
122     @SuppressWarnings("unchecked")
123     public void testStartOperationAsync_testStartQueryAsync() throws Exception {
124
125         // return a map in the reply
126         Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
127         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
128
129         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
130
131         oper.generateSubRequestId(1);
132         outcome.setSubRequestId(oper.getSubRequestId());
133
134         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
135         assertFalse(future2.isDone());
136
137         executor.runAll(100);
138         assertTrue(future2.isDone());
139
140         assertEquals(OperationResult.SUCCESS, future2.get().getResult());
141
142         assertEquals("1", future2.get().getSubRequestId());
143     }
144
145     /**
146      * Tests startOperationAsync() when there's a failure.
147      */
148     @Test
149     @SuppressWarnings("unchecked")
150     public void testStartOperationAsyncFailure() throws Exception {
151
152         when(rawResponse.getStatus()).thenReturn(500);
153         when(rawResponse.readEntity(String.class)).thenReturn("");
154
155         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
156
157         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
158         assertFalse(future2.isDone());
159
160         executor.runAll(100);
161         assertTrue(future2.isDone());
162
163         assertEquals(OperationResult.FAILURE, future2.get().getResult());
164     }
165
166     @Test
167     public void testGetKey() {
168         assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY));
169     }
170 }