030bb9ec4c45dcaad96e61f1a2acd25b8164490b
[policy/drools-applications.git] /
1 /*-
2  * SdnrActorServiceProviderTest
3  * ================================================================================
4  * Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  * 
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.controlloop.actor.sdnr;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.time.Instant;
28 import java.util.HashMap;
29 import java.util.UUID;
30
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
35 import org.onap.policy.controlloop.ControlLoopEventStatus;
36 import org.onap.policy.controlloop.ControlLoopOperation;
37 import org.onap.policy.controlloop.ControlLoopTargetType;
38 import org.onap.policy.controlloop.VirtualControlLoopEvent;
39 import org.onap.policy.controlloop.policy.Policy;
40 import org.onap.policy.controlloop.policy.Target;
41 import org.onap.policy.controlloop.policy.TargetType;
42 import org.onap.policy.drools.system.PolicyEngine;
43 import org.onap.policy.sdnr.PciRequest;
44 import org.onap.policy.sdnr.PciResponse;
45 import org.onap.policy.sdnr.util.Serialization;
46 import org.onap.policy.simulators.Util;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class SdnrActorServiceProviderTest {
51
52     private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProviderTest.class);
53
54     private static final VirtualControlLoopEvent onsetEvent;
55     private static final ControlLoopOperation operation;
56     private static final Policy policy;
57
58     static {
59         /*
60          * Construct an onset. Using dummy AAI details since the code mandates AAI
61          * details.
62          */
63         onsetEvent = new VirtualControlLoopEvent();
64         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
65         onsetEvent.setRequestId(UUID.randomUUID());
66         onsetEvent.setClosedLoopEventClient("tca.instance00001");
67         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
68         onsetEvent.setTarget("generic-vnf.vnf-name");
69         onsetEvent.setFrom("DCAE");
70         onsetEvent.setClosedLoopAlarmStart(Instant.now());
71         onsetEvent.setAai(new HashMap<>());
72         onsetEvent.getAai().put("generic-vnf.vnf-name", "notused");
73         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
74         onsetEvent.setPayload("some payload");
75
76         /* Construct an operation with an SDNR actor and ModifyConfig operation. */
77         operation = new ControlLoopOperation();
78         operation.setActor("SDNR");
79         operation.setOperation("ModifyConfig");
80         operation.setTarget("VNF");
81         operation.setEnd(Instant.now());
82         operation.setSubRequestId("1");
83
84         /* Construct a policy specifying to modify configuration. */
85         policy = new Policy();
86         policy.setName("Modify PCI Config");
87         policy.setDescription("Upon getting the trigger event, modify pci config");
88         policy.setActor("SDNR");
89         policy.setTarget(new Target(TargetType.VNF));
90         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
91         policy.setRecipe("ModifyConfig");
92         policy.setPayload(null);
93         policy.setRetry(2);
94         policy.setTimeout(300);
95     }
96
97     @Test
98     public void constructModifyConfigRequestTest() {
99
100         PciRequest sdnrRequest;
101         sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
102
103         /* The service provider must return a non null SDNR request */
104         assertNotNull(sdnrRequest);
105
106         /* A common header is required and cannot be null */
107         assertNotNull(sdnrRequest.getCommonHeader());
108         assertEquals(sdnrRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
109
110         /* An action is required and cannot be null */
111         assertNotNull(sdnrRequest.getAction());
112         assertEquals("ModifyConfig", sdnrRequest.getAction());
113
114         /* A payload is required and cannot be null */
115         assertNotNull(sdnrRequest.getPayload());
116         assertEquals("some payload", sdnrRequest.getPayload());
117
118         logger.debug("SDNR Request: \n" + sdnrRequest.toString());
119
120         /* Print out request as json to make sure serialization works */
121         String jsonRequest = Serialization.gsonPretty.toJson(sdnrRequest);
122         logger.debug("JSON Output: \n" + jsonRequest);
123
124         /* The JSON string must contain the following fields */
125         assertTrue(jsonRequest.contains("CommonHeader"));
126         assertTrue(jsonRequest.contains("Action"));
127         assertTrue(jsonRequest.contains("ModifyConfig"));
128         assertTrue(jsonRequest.contains("payload"));
129
130         PciResponse sdnrResponse = new PciResponse(sdnrRequest);
131         sdnrResponse.getStatus().setCode(200);
132         sdnrResponse.getStatus().setValue("SDNR success");
133         /* Print out request as json to make sure serialization works */
134         String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
135         logger.debug("JSON Output: \n" + jsonResponse);
136     }
137
138     @Test
139     public void testMethods() {
140         SdnrActorServiceProvider sp = new SdnrActorServiceProvider();
141
142         assertEquals("SDNR", sp.actor());
143         assertEquals(1, sp.recipes().size());
144         assertEquals("VNF", sp.recipeTargets("ModifyConfig").get(0));
145         assertEquals(2, sp.recipePayloads("ModifyConfig").size());
146     }
147 }