Modify Actors to use properties when provided
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / test / java / org / onap / policy / controlloop / actor / sdnc / SdncOperationTest.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.sdnc;
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
29 import java.util.Collections;
30 import java.util.Map;
31 import java.util.TreeMap;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
35 import org.onap.policy.sdnc.SdncHealRequest;
36 import org.onap.policy.sdnc.SdncHealRequestHeaderInfo;
37 import org.onap.policy.sdnc.SdncRequest;
38
39 public class SdncOperationTest extends BasicSdncOperation {
40
41     private static final String MY_URI = "my-uri";
42
43     private SdncRequest request;
44     private SdncOperation oper;
45
46     /**
47      * Sets up.
48      */
49     @Before
50     public void setUp() throws Exception {
51         super.setUp();
52
53         request = new SdncRequest();
54         request.setUrl(MY_URI);
55
56         SdncHealRequest healRequest = new SdncHealRequest();
57         request.setHealRequest(healRequest);
58
59         SdncHealRequestHeaderInfo headerInfo = new SdncHealRequestHeaderInfo();
60         healRequest.setRequestHeaderInfo(headerInfo);
61         headerInfo.setSvcRequestId(SUB_REQ_ID);
62
63         oper = new SdncOperation(params, config, Collections.emptyList()) {
64             @Override
65             protected SdncRequest makeRequest(int attempt) {
66                 return request;
67             }
68         };
69     }
70
71     @Test
72     public void testSdncOperator() {
73         assertEquals(DEFAULT_ACTOR, oper.getActorName());
74         assertEquals(DEFAULT_OPERATION, oper.getName());
75     }
76
77     @Test
78     public void testStartPreprocessorAsync() {
79         assertNotNull(oper.startPreprocessorAsync());
80     }
81
82     @Test
83     public void testStartOperationAsync_testStartRequestAsync() throws Exception {
84         verifyOperation(oper);
85     }
86
87     @Test
88     public void testIsSuccess() {
89         // success case
90         response.getResponseOutput().setResponseCode("200");
91         assertTrue(oper.isSuccess(null, response));
92
93         // failure code
94         response.getResponseOutput().setResponseCode("555");
95         assertFalse(oper.isSuccess(null, response));
96
97         // null code
98         response.getResponseOutput().setResponseCode(null);
99         assertFalse(oper.isSuccess(null, response));
100
101         // null output
102         response.setResponseOutput(null);
103         assertFalse(oper.isSuccess(null, response));
104     }
105
106     @Test
107     public void testGetOptProperty() {
108         // in neither property nor enrichment
109         assertNull(oper.getOptProperty("propA", "propA2"));
110
111         // both - should choose the property
112         remakeOper(Map.of("propB2", "valueB2"));
113         oper.setProperty("propB", "valueB");
114         assertEquals("valueB", oper.getOptProperty("propB", "propB2"));
115
116         // both - should choose the property, even if it's null
117         remakeOper(Map.of("propC2", "valueC2"));
118         oper.setProperty("propC", null);
119         assertNull(oper.getOptProperty("propC", "propC2"));
120
121         // only in enrichment data
122         remakeOper(Map.of("propD2", "valueD2"));
123         assertEquals("valueD2", oper.getOptProperty("propD", "propD2"));
124     }
125
126     /**
127      * Remakes the operation, with the specified A&AI enrichment data.
128      *
129      * @param aai A&AI enrichment data
130      */
131     private void remakeOper(Map<String, String> aai) {
132         event.setAai(aai);
133         context = new ControlLoopEventContext(event);
134         params = params.toBuilder().context(context).build();
135
136         oper = new SdncOperation(params, config, Collections.emptyList()) {
137             @Override
138             protected SdncRequest makeRequest(int attempt) {
139                 return request;
140             }
141         };
142     }
143
144     @Override
145     protected Map<String, String> makeEnrichment() {
146         return new TreeMap<>();
147     }
148 }