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