Merge "Modify Actors to use properties when provided"
[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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.Map;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
34 import org.onap.policy.vfc.VfcResponse;
35 import org.onap.policy.vfc.VfcResponseDescriptor;
36
37 public class VfcOperationTest extends BasicVfcOperation {
38
39     private VfcOperation oper;
40
41     /**
42      * setUp.
43      */
44     @Before
45     @Override
46     public void setUp() throws Exception {
47         super.setUp();
48
49         initConfig();
50
51         oper = new VfcOperation(params, config) {};
52     }
53
54     @Test
55     public void testConstructor() {
56         assertEquals(DEFAULT_ACTOR, oper.getActorName());
57         assertEquals(DEFAULT_OPERATION, oper.getName());
58         assertSame(config, oper.getConfig());
59         assertTrue(oper.isUsePolling());
60     }
61
62     @Test
63     public void testStartPreprocessorAsync() {
64         assertNotNull(oper.startPreprocessorAsync());
65     }
66
67     @Test
68     public void testResetPollCount() {
69         oper.resetPollCount();
70         assertEquals(0, oper.getPollCount());
71     }
72
73     @Test
74     public void testGetRequestState() {
75         VfcResponse mockResponse = Mockito.mock(VfcResponse.class);
76         Mockito.when(mockResponse.getResponseDescriptor()).thenReturn(null);
77         assertNull(oper.getRequestState(mockResponse));
78
79         VfcResponseDescriptor mockDescriptor = Mockito.mock(VfcResponseDescriptor.class);
80         Mockito.when(mockResponse.getResponseDescriptor()).thenReturn(mockDescriptor);
81
82         // TODO use actual request state value
83         Mockito.when(mockDescriptor.getStatus()).thenReturn("COMPLETE");
84         assertNotNull(oper.getRequestState(mockResponse));
85     }
86
87     @Test
88     public void testIsSuccess() {
89         assertTrue(oper.isSuccess(rawResponse, response));
90     }
91
92     @Test
93     public void testGetOptProperty() {
94         // in neither property nor enrichment
95         assertNull(oper.getOptProperty("propA", "propA2"));
96
97         // both - should choose the property
98         remakeOper(Map.of("propB2", "valueB2"));
99         oper.setProperty("propB", "valueB");
100         assertEquals("valueB", oper.getOptProperty("propB", "propB2"));
101
102         // both - should choose the property, even if it's null
103         remakeOper(Map.of("propC2", "valueC2"));
104         oper.setProperty("propC", null);
105         assertNull(oper.getOptProperty("propC", "propC2"));
106
107         // only in enrichment data
108         remakeOper(Map.of("propD2", "valueD2"));
109         assertEquals("valueD2", oper.getOptProperty("propD", "propD2"));
110     }
111
112     /**
113      * Remakes the operation, with the specified A&AI enrichment data.
114      *
115      * @param aai A&AI enrichment data
116      */
117     private void remakeOper(Map<String, String> aai) {
118         event.setAai(aai);
119         context = new ControlLoopEventContext(event);
120         params = params.toBuilder().context(context).build();
121
122         oper = new VfcOperation(params, config) {};
123     }
124 }