Merge "More actor clean-up"
[policy/models.git] / models-interactions / model-actors / actor.aai / src / test / java / org / onap / policy / controlloop / actor / aai / AaiGetOperationTest.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 org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.aai.AaiConstants;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.coder.StandardCoderObject;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
39 import org.onap.policy.controlloop.policy.PolicyResult;
40
41 public class AaiGetOperationTest extends BasicAaiOperation<Void> {
42
43     private static final String INPUT_FIELD = "input";
44     private static final String TEXT = "my-text";
45
46     private AaiGetOperation oper;
47
48     public AaiGetOperationTest() {
49         super(AaiConstants.ACTOR_NAME, AaiGetOperation.TENANT);
50     }
51
52     /**
53      * Sets up.
54      */
55     @Before
56     public void setUp() throws Exception {
57         super.setUpBasic();
58         oper = new AaiGetOperation(params, config);
59     }
60
61     @Test
62     public void testGetRetry() {
63         // use default if null retry
64         assertEquals(AaiGetOperation.DEFAULT_RETRY, oper.getRetry(null));
65
66         // otherwise, use specified value
67         assertEquals(0, oper.getRetry(0));
68         assertEquals(10, oper.getRetry(10));
69     }
70
71     @Test
72     public void testStartOperationAsync_testStartQueryAsync_testPostProcessResponse() throws Exception {
73
74         // return a map in the reply
75         Map<String, String> reply = Map.of(INPUT_FIELD, TEXT);
76         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(reply));
77
78         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
79
80         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
81         assertFalse(future2.isDone());
82
83         executor.runAll(100);
84         assertTrue(future2.isDone());
85
86         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
87
88         // data should have been cached within the context
89         StandardCoderObject data = context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY));
90         assertNotNull(data);
91         assertEquals(TEXT, data.getString(INPUT_FIELD));
92     }
93
94     /**
95      * Tests startOperationAsync() when there's a failure.
96      */
97     @Test
98     public void testStartOperationAsyncFailure() throws Exception {
99
100         when(rawResponse.getStatus()).thenReturn(500);
101         when(rawResponse.readEntity(String.class)).thenReturn("");
102
103         when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
104
105         CompletableFuture<OperationOutcome> future2 = oper.startOperationAsync(1, outcome);
106         assertFalse(future2.isDone());
107
108         executor.runAll(100);
109         assertTrue(future2.isDone());
110
111         assertEquals(PolicyResult.FAILURE, future2.get().getResult());
112
113         // data should NOT have been cached within the context
114         assertNull(context.getProperty(AaiGetOperation.getTenantKey(TARGET_ENTITY)));
115     }
116
117     @Test
118     public void testMakeHeaders() {
119         verifyHeaders(oper.makeHeaders());
120     }
121
122     @Test
123     public void testMakePath() {
124         assertEquals(PATH + "/" + TARGET_ENTITY, oper.makePath());
125     }
126
127     @Test
128     public void testAaiGetOperator() {
129         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
130         assertEquals(AaiGetOperation.TENANT, oper.getName());
131     }
132
133     @Test
134     public void testGetTenantKey() {
135         assertEquals("AAI.Tenant." + TARGET_ENTITY, AaiGetOperation.getTenantKey(TARGET_ENTITY));
136     }
137 }