Delete preprocessed flag from actors
[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         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
76     }
77
78     @Test
79     public void testConstructor() {
80         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
81         assertEquals(AaiGetTenantOperation.NAME, oper.getName());
82     }
83
84     @Test
85     public void testGetPropertyNames() {
86         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
87     }
88
89     /**
90      * Tests "success" case with simulator.
91      */
92     @Test
93     public void testSuccess() throws Exception {
94         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
95         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
96
97         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
98         oper = new AaiGetTenantOperation(params, config);
99         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "OzVServer");
100
101         outcome = oper.start().get();
102         assertEquals(OperationResult.SUCCESS, outcome.getResult());
103         assertTrue(outcome.getResponse() instanceof StandardCoderObject);
104     }
105
106     /**
107      * Tests "failure" case with simulator.
108      */
109     @Test
110     public void testFailure() throws Exception {
111         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
112         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
113
114         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
115         oper = new AaiGetTenantOperation(params, config);
116         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "failedVserver");
117
118         outcome = oper.start().get();
119         assertEquals(OperationResult.FAILURE, outcome.getResult());
120     }
121
122     @Test
123     @SuppressWarnings("unchecked")
124     public void testStartOperationAsync_testStartQueryAsync() throws Exception {
125
126         // return a map in the reply
127         Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
128         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
129
130         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
131
132         oper.generateSubRequestId(1);
133         outcome.setSubRequestId(oper.getSubRequestId());
134
135         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
136         assertFalse(future2.isDone());
137
138         executor.runAll(100);
139         assertTrue(future2.isDone());
140
141         assertEquals(OperationResult.SUCCESS, future2.get().getResult());
142
143         assertEquals("1", future2.get().getSubRequestId());
144     }
145
146     /**
147      * Tests startOperationAsync() when there's a failure.
148      */
149     @Test
150     @SuppressWarnings("unchecked")
151     public void testStartOperationAsyncFailure() throws Exception {
152
153         when(rawResponse.getStatus()).thenReturn(500);
154         when(rawResponse.readEntity(String.class)).thenReturn("");
155
156         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
157
158         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
159         assertFalse(future2.isDone());
160
161         executor.runAll(100);
162         assertTrue(future2.isDone());
163
164         assertEquals(OperationResult.FAILURE, future2.get().getResult());
165     }
166
167     @Test
168     public void testGetKey() {
169         assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY));
170     }
171 }