Include response in OperationOutcome
[policy/models.git] / models-interactions / model-actors / actor.vfc / src / test / java / org / onap / policy / controlloop / actor / vfc / VfcOperationTest.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.vfc;
22
23 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
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.assertSame;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.concurrent.CompletableFuture;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
36 import org.onap.policy.controlloop.policy.PolicyResult;
37 import org.onap.policy.vfc.VfcResponse;
38 import org.onap.policy.vfc.VfcResponseDescriptor;
39
40 public class VfcOperationTest extends BasicVfcOperation {
41
42     private VfcOperation oper;
43
44     /**
45      * setUp.
46      */
47     @Before
48     @Override
49     public void setUp() throws Exception {
50         super.setUp();
51
52         initConfig();
53
54         oper = new VfcOperation(params, config) {};
55     }
56
57     @Test
58     public void testConstructor_testGetWaitMsGet() {
59         assertEquals(DEFAULT_ACTOR, oper.getActorName());
60         assertEquals(DEFAULT_OPERATION, oper.getName());
61         assertSame(config, oper.getConfig());
62         assertEquals(1000 * WAIT_SEC_GETS, oper.getWaitMsGet());
63     }
64
65     @Test
66     public void testStartPreprocessorAsync() {
67         assertNotNull(oper.startPreprocessorAsync());
68     }
69
70     @Test
71     public void testResetGetCount() {
72         oper.resetGetCount();
73         assertEquals(0, oper.getGetCount());
74     }
75
76     @Test
77     public void testPostProcess() throws Exception {
78
79         assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
80             oper.postProcessResponse(outcome, PATH, rawResponse, response);
81         });
82
83         response.setResponseDescriptor(new VfcResponseDescriptor());
84         response.setJobId("sampleJobId");
85
86         // null status
87         CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
88         assertFalse(future2.isDone());
89
90         response.getResponseDescriptor().setStatus("FinisHeD");
91         future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
92         assertTrue(future2.isDone());
93         assertSame(outcome, future2.get());
94         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
95         assertSame(response, outcome.getResponse());
96
97         // failed
98         response.getResponseDescriptor().setStatus("eRRor");
99         future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
100         assertTrue(future2.isDone());
101         assertSame(outcome, future2.get());
102         assertEquals(PolicyResult.FAILURE, outcome.getResult());
103         assertSame(response, outcome.getResponse());
104
105         // unfinished
106         response.getResponseDescriptor().setStatus("anything but finished");
107         future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
108         assertFalse(future2.isDone());
109     }
110
111     @Test
112     public void testGetRequestState() {
113         VfcResponse mockResponse = Mockito.mock(VfcResponse.class);
114         Mockito.when(mockResponse.getResponseDescriptor()).thenReturn(null);
115         assertNull(oper.getRequestState(mockResponse));
116
117         VfcResponseDescriptor mockDescriptor = Mockito.mock(VfcResponseDescriptor.class);
118         Mockito.when(mockResponse.getResponseDescriptor()).thenReturn(mockDescriptor);
119
120         // TODO use actual request state value
121         Mockito.when(mockDescriptor.getStatus()).thenReturn("COMPLETE");
122         assertNotNull(oper.getRequestState(mockResponse));
123     }
124
125     @Test
126     public void testIsSuccess() {
127         assertTrue(oper.isSuccess(rawResponse, response));
128     }
129
130 }