Add property lists to 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.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.when;
31
32 import java.util.Map;
33 import java.util.concurrent.CompletableFuture;
34 import javax.ws.rs.client.InvocationCallback;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.aai.AaiConstants;
40 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.coder.StandardCoderObject;
43 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
45 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
46 import org.onap.policy.controlloop.policy.PolicyResult;
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()).isEmpty();
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().targetEntity("OzVServer").retry(0).timeoutSec(5).executor(blockingExecutor).build();
97         oper = new AaiGetTenantOperation(params, config);
98
99         outcome = oper.start().get();
100         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
101         assertTrue(outcome.getResponse() instanceof StandardCoderObject);
102     }
103
104     /**
105      * Tests "failure" case with simulator.
106      */
107     @Test
108     public void testFailure() throws Exception {
109         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT).path("v16/search/nodes-query").build();
110         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
111
112         params = params.toBuilder().targetEntity("failedVserver").retry(0).timeoutSec(5).executor(blockingExecutor)
113                         .build();
114         oper = new AaiGetTenantOperation(params, config);
115
116         outcome = oper.start().get();
117         assertEquals(PolicyResult.FAILURE, outcome.getResult());
118     }
119
120     @Test
121     @SuppressWarnings("unchecked")
122     public void testStartOperationAsync_testStartQueryAsync() throws Exception {
123
124         // return a map in the reply
125         Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
126         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
127
128         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
129
130         oper.generateSubRequestId(1);
131         outcome.setSubRequestId(oper.getSubRequestId());
132
133         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
134         assertFalse(future2.isDone());
135
136         executor.runAll(100);
137         assertTrue(future2.isDone());
138
139         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
140
141         // data should have been cached within the context
142         StandardCoderObject data = context.getProperty(AaiGetTenantOperation.getKey(TARGET_ENTITY));
143         assertNotNull(data);
144         assertEquals(TEXT, data.getString(INPUT_FIELD));
145
146         assertEquals("1", future2.get().getSubRequestId());
147     }
148
149     /**
150      * Tests startOperationAsync() when there's a failure.
151      */
152     @Test
153     @SuppressWarnings("unchecked")
154     public void testStartOperationAsyncFailure() throws Exception {
155
156         when(rawResponse.getStatus()).thenReturn(500);
157         when(rawResponse.readEntity(String.class)).thenReturn("");
158
159         when(webAsync.get(any(InvocationCallback.class))).thenAnswer(provideResponse(rawResponse));
160
161         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
162         assertFalse(future2.isDone());
163
164         executor.runAll(100);
165         assertTrue(future2.isDone());
166
167         assertEquals(PolicyResult.FAILURE, future2.get().getResult());
168
169         // data should NOT have been cached within the context
170         assertNull(context.getProperty(AaiGetTenantOperation.getKey(TARGET_ENTITY)));
171     }
172
173     @Test
174     public void testGetKey() {
175         assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetTenantOperation.getKey(TARGET_ENTITY));
176     }
177 }