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