Change payload to Map<String,Object> so it's more versatile
[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.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.time.Instant;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.UUID;
33 import java.util.stream.Collectors;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.appc.Request;
38 import org.onap.policy.appc.Response;
39 import org.onap.policy.appc.ResponseCode;
40 import org.onap.policy.appc.util.Serialization;
41 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
42 import org.onap.policy.controlloop.ControlLoopEventStatus;
43 import org.onap.policy.controlloop.ControlLoopOperation;
44 import org.onap.policy.controlloop.ControlLoopTargetType;
45 import org.onap.policy.controlloop.VirtualControlLoopEvent;
46 import org.onap.policy.controlloop.actor.test.BasicActor;
47 import org.onap.policy.controlloop.policy.Policy;
48 import org.onap.policy.controlloop.policy.Target;
49 import org.onap.policy.controlloop.policy.TargetType;
50 import org.onap.policy.simulators.Util;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class AppcServiceProviderTest extends BasicActor {
55
56     private static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
57
58     private static final String MODIFY_CONFIG = "ModifyConfig";
59
60     private static final String JSON_OUTPUT = "JSON Output: \n";
61
62     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
63
64     private static final VirtualControlLoopEvent onsetEvent;
65     private static final ControlLoopOperation operation;
66     private static final Policy policy;
67
68     private static final String KEY1 = "my-keyA";
69     private static final String KEY2 = "my-keyB";
70     private static final String SUBKEY = "sub-key";
71
72     private static final String VALUE1 = "'my-value'".replace('\'', '"');
73     private static final String VALUE2 = "{'sub-key':20}".replace('\'', '"');
74     private static final String SUBVALUE = "20";
75
76     static {
77         /*
78          * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a
79          * target type of VM.
80          */
81         onsetEvent = new VirtualControlLoopEvent();
82         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
83         onsetEvent.setRequestId(UUID.randomUUID());
84         onsetEvent.setClosedLoopEventClient("tca.instance00001");
85         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
86         onsetEvent.setTarget("generic-vnf.vnf-name");
87         onsetEvent.setFrom("DCAE");
88         onsetEvent.setClosedLoopAlarmStart(Instant.now());
89         onsetEvent.setAai(new HashMap<>());
90         onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
91         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
92
93         /* Construct an operation with an APPC actor and ModifyConfig operation. */
94         operation = new ControlLoopOperation();
95         operation.setActor("APPC");
96         operation.setOperation(MODIFY_CONFIG);
97         operation.setTarget("VNF");
98         operation.setEnd(Instant.now());
99         operation.setSubRequestId("1");
100
101         /* Construct a policy specifying to modify configuration. */
102         policy = new Policy();
103         policy.setName("Modify Packet Generation Config");
104         policy.setDescription("Upon getting the trigger event, modify packet gen config");
105         policy.setActor("APPC");
106         policy.setTarget(new Target(TargetType.VNF));
107         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
108         policy.setRecipe(MODIFY_CONFIG);
109         policy.setPayload(null);
110         policy.setRetry(2);
111         policy.setTimeout(300);
112
113     }
114
115     /**
116      * Set up before test class.
117      *
118      * @throws Exception if the A&AI simulator cannot be started
119      */
120     @BeforeClass
121     public static void setUpSimulator() throws Exception {
122         Util.buildAaiSim();
123     }
124
125     /**
126      * Tear down after test class.
127      */
128     @AfterClass
129     public static void tearDownSimulator() {
130         HttpServletServerFactoryInstance.getServerFactory().destroy();
131     }
132
133     @Test
134     public void testConstructor() {
135         AppcActorServiceProvider prov = new AppcActorServiceProvider();
136
137         // verify that it has the operators we expect
138         var expected = Arrays.asList(ModifyConfigOperation.NAME).stream().sorted().collect(Collectors.toList());
139         var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
140
141         assertEquals(expected.toString(), actual.toString());
142     }
143
144     @Test
145     public void testActorService() {
146         // verify that it all plugs into the ActorService
147         verifyActorService(AppcActorServiceProvider.NAME, "service.yaml");
148     }
149
150     @Test
151     public void testConstructModifyConfigRequest() {
152         policy.setPayload(new HashMap<>());
153         policy.getPayload().put(KEY1, VALUE1);
154         policy.getPayload().put(KEY2, VALUE2);
155
156         Request appcRequest;
157         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
158
159         /* The service provider must return a non null APPC request */
160         assertNotNull(appcRequest);
161
162         /* A common header is required and cannot be null */
163         assertNotNull(appcRequest.getCommonHeader());
164         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
165
166         /* An action is required and cannot be null */
167         assertNotNull(appcRequest.getAction());
168         assertEquals(MODIFY_CONFIG, appcRequest.getAction());
169
170         /* A payload is required and cannot be null */
171         assertNotNull(appcRequest.getPayload());
172         assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
173         assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
174         assertTrue(appcRequest.getPayload().containsKey(KEY1));
175         assertTrue(appcRequest.getPayload().containsKey(KEY2));
176
177         logger.debug("APPC Request: \n" + appcRequest.toString());
178
179         /* Print out request as json to make sure serialization works */
180         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
181         logger.debug(JSON_OUTPUT + jsonRequest);
182
183         /* The JSON string must contain the following fields */
184         assertTrue(jsonRequest.contains("CommonHeader"));
185         assertTrue(jsonRequest.contains("Action"));
186         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
187         assertTrue(jsonRequest.contains("Payload"));
188         assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
189         assertTrue(jsonRequest.contains(KEY1));
190         assertTrue(jsonRequest.contains(KEY2));
191         assertTrue(jsonRequest.contains(SUBKEY));
192         assertTrue(jsonRequest.contains(SUBVALUE));
193         assertFalse(jsonRequest.contains(SUBVALUE + ".0"));
194
195         Response appcResponse = new Response(appcRequest);
196         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
197         appcResponse.getStatus().setDescription("AppC success");
198         /* Print out request as json to make sure serialization works */
199         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
200         logger.debug(JSON_OUTPUT + jsonResponse);
201     }
202
203     @Test
204     public void testConstructModifyConfigRequest_NullPayload() {
205
206         Request appcRequest;
207         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
208
209         /* The service provider must return a non null APPC request */
210         assertNotNull(appcRequest);
211
212         /* A common header is required and cannot be null */
213         assertNotNull(appcRequest.getCommonHeader());
214         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
215
216         /* An action is required and cannot be null */
217         assertNotNull(appcRequest.getAction());
218         assertEquals(MODIFY_CONFIG, appcRequest.getAction());
219
220         /* A payload is required and cannot be null */
221         assertNotNull(appcRequest.getPayload());
222         assertTrue(appcRequest.getPayload().containsKey(GENERIC_VNF_ID));
223         assertNotNull(appcRequest.getPayload().get(GENERIC_VNF_ID));
224
225         logger.debug("APPC Request: \n" + appcRequest.toString());
226
227         /* Print out request as json to make sure serialization works */
228         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
229         logger.debug(JSON_OUTPUT + jsonRequest);
230
231         /* The JSON string must contain the following fields */
232         assertTrue(jsonRequest.contains("CommonHeader"));
233         assertTrue(jsonRequest.contains("Action"));
234         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
235         assertTrue(jsonRequest.contains("Payload"));
236         assertTrue(jsonRequest.contains(GENERIC_VNF_ID));
237
238         Response appcResponse = new Response(appcRequest);
239         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
240         appcResponse.getStatus().setDescription("AppC success");
241         /* Print out request as json to make sure serialization works */
242         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
243         logger.debug(JSON_OUTPUT + jsonResponse);
244     }
245
246     @Test
247     public void testMethods() {
248         AppcActorServiceProvider sp = new AppcActorServiceProvider();
249
250         assertEquals("APPC", sp.actor());
251         assertEquals(4, sp.recipes().size());
252         assertEquals("VM", sp.recipeTargets("Restart").get(0));
253         assertEquals(0, sp.recipePayloads("Restart").size());
254     }
255 }