Fix Tech Debt/JUnit on control loop event POJOs
[policy/drools-applications.git] / controlloop / common / 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 AT&T Intellectual Property. All rights reserved.
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.appc;
22
23 import static org.junit.Assert.*;
24
25 import java.time.Instant;
26 import java.util.HashMap;
27 import java.util.UUID;
28
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.policy.appc.Request;
33 import org.onap.policy.appc.Response;
34 import org.onap.policy.appc.ResponseCode;
35 import org.onap.policy.appc.util.Serialization;
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.http.server.HttpServletServer;
44 import org.onap.policy.drools.system.PolicyEngine;
45 import org.onap.policy.simulators.Util;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class AppcServiceProviderTest {
50
51     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
52     
53     private static VirtualControlLoopEvent onsetEvent;
54     private static ControlLoopOperation operation;
55     private static Policy policy;
56
57     static {
58         /* 
59          * Construct an onset with an AAI subtag containing
60          * generic-vnf.vnf-id and a target type of VM.
61          */
62         onsetEvent = new VirtualControlLoopEvent();
63         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
64         onsetEvent.setRequestID(UUID.randomUUID());
65         onsetEvent.setClosedLoopEventClient("tca.instance00001");
66         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
67         onsetEvent.setTarget("generic-vnf.vnf-name");
68         onsetEvent.setFrom("DCAE");
69         onsetEvent.setClosedLoopAlarmStart(Instant.now());
70         onsetEvent.setAAI(new HashMap<>());
71         onsetEvent.getAAI().put("generic-vnf.vnf-name", "fw0001vm001fw001");
72         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
73
74         /* Construct an operation with an APPC actor and ModifyConfig operation. */
75         operation = new ControlLoopOperation();
76         operation.setActor("APPC");
77         operation.setOperation("ModifyConfig");
78         operation.setTarget("VNF");
79         operation.setEnd(Instant.now());
80         operation.setSubRequestId("1");
81
82         /* Construct a policy specifying to modify configuration. */
83         policy = new Policy();
84         policy.setName("Modify Packet Generation Config");
85         policy.setDescription("Upon getting the trigger event, modify packet gen config");
86         policy.setActor("APPC");
87         policy.setTarget(new Target(TargetType.VNF));
88         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
89         policy.setRecipe("ModifyConfig");
90         policy.setPayload(null);
91         policy.setRetry(2);
92         policy.setTimeout(300);
93         
94         /* Set environment properties */
95         PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
96         PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
97         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
98         
99     }
100
101     @BeforeClass
102     public static void setUpSimulator() {
103         try {
104             Util.buildAaiSim();
105         } catch (Exception e) {
106             fail(e.getMessage());
107         }
108     }
109
110     @AfterClass
111     public static void tearDownSimulator() {
112         HttpServletServer.factory.destroy();
113     }
114     
115     @Test
116     public void constructModifyConfigRequestTest() {
117         
118         Request appcRequest = null;
119         appcRequest = APPCActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
120         
121         /* The service provider must return a non null APPC request */
122         assertNotNull(appcRequest);
123
124         /* A common header is required and cannot be null */
125         assertNotNull(appcRequest.getCommonHeader());
126         assertEquals(appcRequest.getCommonHeader().getRequestID(), onsetEvent.getRequestID());
127
128         /* An action is required and cannot be null */
129         assertNotNull(appcRequest.getAction());
130         assertEquals(appcRequest.getAction(), "ModifyConfig");
131
132         /* A payload is required and cannot be null */
133         assertNotNull(appcRequest.getPayload());
134         assertTrue(appcRequest.getPayload().containsKey("generic-vnf.vnf-id"));
135         assertNotNull(appcRequest.getPayload().get("generic-vnf.vnf-id"));
136         assertTrue(appcRequest.getPayload().containsKey("pg-streams"));
137
138         logger.debug("APPC Request: \n" + appcRequest.toString());
139         
140         /* Print out request as json to make sure serialization works */
141         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
142         logger.debug("JSON Output: \n" + jsonRequest);
143         
144         /* The JSON string must contain the following fields */
145         assertTrue(jsonRequest.contains("commonHeader"));
146         assertTrue(jsonRequest.contains("action"));
147         assertTrue(jsonRequest.contains("ModifyConfig"));
148         assertTrue(jsonRequest.contains("payload"));
149         assertTrue(jsonRequest.contains("generic-vnf.vnf-id"));
150         assertTrue(jsonRequest.contains("pg-streams"));
151         
152         Response appcResponse = new Response(appcRequest);
153         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
154         appcResponse.getStatus().setDescription("AppC success");
155         /* Print out request as json to make sure serialization works */
156         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
157         logger.debug("JSON Output: \n" + jsonResponse);
158     }
159
160 }