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