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