Fix simple sonar issues in models
[policy/models.git] / models-interactions / model-actors / actor.appc / src / test / java / org / onap / policy / controlloop / actor / appc / AppcServiceProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * AppcServiceProviderTest
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.appc;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.time.Instant;
29 import java.util.HashMap;
30 import java.util.UUID;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.appc.Request;
35 import org.onap.policy.appc.Response;
36 import org.onap.policy.appc.ResponseCode;
37 import org.onap.policy.appc.util.Serialization;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
39 import org.onap.policy.controlloop.ControlLoopEventStatus;
40 import org.onap.policy.controlloop.ControlLoopOperation;
41 import org.onap.policy.controlloop.ControlLoopTargetType;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.policy.Policy;
44 import org.onap.policy.controlloop.policy.Target;
45 import org.onap.policy.controlloop.policy.TargetType;
46 import org.onap.policy.simulators.Util;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class AppcServiceProviderTest {
51
52     private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
53
54     private static final String MODIFY_CONFIG = "ModifyConfig";
55
56     private static final String JSON_OUTPUT = "JSON Output: \n";
57
58     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
59
60     private static final VirtualControlLoopEvent onsetEvent;
61     private static final ControlLoopOperation operation;
62     private static final Policy policy;
63
64     private static final String KEY1 = "my-keyA";
65     private static final String KEY2 = "my-keyB";
66     private static final String SUBKEY = "sub-key";
67
68     private static final String VALUE1 = "'my-value'".replace('\'', '"');
69     private static final String VALUE2 = "{'sub-key':20}".replace('\'', '"');
70     private static final String SUBVALUE = "20";
71
72     static {
73         /*
74          * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
75          * VM.
76          */
77         onsetEvent = new VirtualControlLoopEvent();
78         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
79         onsetEvent.setRequestId(UUID.randomUUID());
80         onsetEvent.setClosedLoopEventClient("tca.instance00001");
81         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
82         onsetEvent.setTarget("generic-vnf.vnf-name");
83         onsetEvent.setFrom("DCAE");
84         onsetEvent.setClosedLoopAlarmStart(Instant.now());
85         onsetEvent.setAai(new HashMap<>());
86         onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
87         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
88
89         /* Construct an operation with an APPC actor and ModifyConfig operation. */
90         operation = new ControlLoopOperation();
91         operation.setActor("APPC");
92         operation.setOperation(MODIFY_CONFIG);
93         operation.setTarget("VNF");
94         operation.setEnd(Instant.now());
95         operation.setSubRequestId("1");
96
97         /* Construct a policy specifying to modify configuration. */
98         policy = new Policy();
99         policy.setName("Modify Packet Generation Config");
100         policy.setDescription("Upon getting the trigger event, modify packet gen config");
101         policy.setActor("APPC");
102         policy.setTarget(new Target(TargetType.VNF));
103         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
104         policy.setRecipe(MODIFY_CONFIG);
105         policy.setPayload(null);
106         policy.setRetry(2);
107         policy.setTimeout(300);
108
109     }
110
111     /**
112      * Set up before test class.
113      * @throws Exception if the A&AI simulator cannot be started
114      */
115     @BeforeClass
116     public static void setUpSimulator() throws Exception {
117         Util.buildAaiSim();
118     }
119
120     /**
121      * Tear down after test class.
122      */
123     @AfterClass
124     public static void tearDownSimulator() {
125         HttpServletServer.factory.destroy();
126     }
127
128     @Test
129     public void constructModifyConfigRequestTest() {
130         policy.setPayload(new HashMap<>());
131         policy.getPayload().put(KEY1, VALUE1);
132         policy.getPayload().put(KEY2, VALUE2);
133
134         Request appcRequest;
135         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
136
137         /* The service provider must return a non null APPC request */
138         assertNotNull(appcRequest);
139
140         /* A common header is required and cannot be null */
141         assertNotNull(appcRequest.getCommonHeader());
142         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
143
144         /* An action is required and cannot be null */
145         assertNotNull(appcRequest.getAction());
146         assertEquals(MODIFY_CONFIG, appcRequest.getAction());
147
148         /* A payload is required and cannot be null */
149         assertNotNull(appcRequest.getPayload());
150         assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
151         assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
152         assertTrue(appcRequest.getPayload().containsKey(KEY1));
153         assertTrue(appcRequest.getPayload().containsKey(KEY2));
154
155         logger.debug("APPC Request: \n" + appcRequest.toString());
156
157         /* Print out request as json to make sure serialization works */
158         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
159         logger.debug(JSON_OUTPUT + jsonRequest);
160
161         /* The JSON string must contain the following fields */
162         assertTrue(jsonRequest.contains("CommonHeader"));
163         assertTrue(jsonRequest.contains("Action"));
164         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
165         assertTrue(jsonRequest.contains("Payload"));
166         assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
167         assertTrue(jsonRequest.contains(KEY1));
168         assertTrue(jsonRequest.contains(KEY2));
169         assertTrue(jsonRequest.contains(SUBKEY));
170         assertTrue(jsonRequest.contains(SUBVALUE));
171
172         Response appcResponse = new Response(appcRequest);
173         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
174         appcResponse.getStatus().setDescription("AppC success");
175         /* Print out request as json to make sure serialization works */
176         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
177         logger.debug(JSON_OUTPUT + jsonResponse);
178     }
179
180     @Test
181     public void constructModifyConfigRequestTest_NullPayload() {
182
183         Request appcRequest;
184         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
185
186         /* The service provider must return a non null APPC request */
187         assertNotNull(appcRequest);
188
189         /* A common header is required and cannot be null */
190         assertNotNull(appcRequest.getCommonHeader());
191         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
192
193         /* An action is required and cannot be null */
194         assertNotNull(appcRequest.getAction());
195         assertEquals(MODIFY_CONFIG, appcRequest.getAction());
196
197         /* A payload is required and cannot be null */
198         assertNotNull(appcRequest.getPayload());
199         assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
200         assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
201
202         logger.debug("APPC Request: \n" + appcRequest.toString());
203
204         /* Print out request as json to make sure serialization works */
205         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
206         logger.debug(JSON_OUTPUT + jsonRequest);
207
208         /* The JSON string must contain the following fields */
209         assertTrue(jsonRequest.contains("CommonHeader"));
210         assertTrue(jsonRequest.contains("Action"));
211         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
212         assertTrue(jsonRequest.contains("Payload"));
213         assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
214
215         Response appcResponse = new Response(appcRequest);
216         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
217         appcResponse.getStatus().setDescription("AppC success");
218         /* Print out request as json to make sure serialization works */
219         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
220         logger.debug(JSON_OUTPUT + jsonResponse);
221     }
222
223     @Test
224     public void testMethods() {
225         AppcActorServiceProvider sp = new AppcActorServiceProvider();
226
227         assertEquals("APPC", sp.actor());
228         assertEquals(4, sp.recipes().size());
229         assertEquals("VM", sp.recipeTargets("Restart").get(0));
230         assertEquals(0, sp.recipePayloads("Restart").size());
231     }
232 }