Merge "Add debugging of REST call"
[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-2018 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.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.appc.Request;
36 import org.onap.policy.appc.Response;
37 import org.onap.policy.appc.ResponseCode;
38 import org.onap.policy.appc.util.Serialization;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
40 import org.onap.policy.controlloop.ControlLoopEventStatus;
41 import org.onap.policy.controlloop.ControlLoopOperation;
42 import org.onap.policy.controlloop.ControlLoopTargetType;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.policy.Policy;
45 import org.onap.policy.controlloop.policy.Target;
46 import org.onap.policy.controlloop.policy.TargetType;
47 import org.onap.policy.drools.system.PolicyEngine;
48 import org.onap.policy.simulators.Util;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class AppcServiceProviderTest {
53
54     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
55
56     private static final VirtualControlLoopEvent onsetEvent;
57     private static final ControlLoopOperation operation;
58     private static final Policy policy;
59
60     static {
61         /*
62          * Construct an onset with an AAI subtag containing generic-vnf.vnf-id and a target type of
63          * VM.
64          */
65         onsetEvent = new VirtualControlLoopEvent();
66         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
67         onsetEvent.setRequestId(UUID.randomUUID());
68         onsetEvent.setClosedLoopEventClient("tca.instance00001");
69         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
70         onsetEvent.setTarget("generic-vnf.vnf-name");
71         onsetEvent.setFrom("DCAE");
72         onsetEvent.setClosedLoopAlarmStart(Instant.now());
73         onsetEvent.setAai(new HashMap<>());
74         onsetEvent.getAai().put("generic-vnf.vnf-name", "fw0001vm001fw001");
75         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
76
77         /* Construct an operation with an APPC actor and ModifyConfig operation. */
78         operation = new ControlLoopOperation();
79         operation.setActor("APPC");
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 Packet Generation Config");
88         policy.setDescription("Upon getting the trigger event, modify packet gen config");
89         policy.setActor("APPC");
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         /* Set environment properties */
98         PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
99         PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
100         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
101
102     }
103
104     /**
105      * Set up before test class.
106      */
107     @BeforeClass
108     public static void setUpSimulator() {
109         try {
110             Util.buildAaiSim();
111         } catch (Exception e) {
112             fail(e.getMessage());
113         }
114     }
115
116     /**
117      * Tear down after test class.
118      */
119     @AfterClass
120     public static void tearDownSimulator() {
121         HttpServletServer.factory.destroy();
122     }
123
124     @Test
125     public void constructModifyConfigRequestTest() {
126
127         Request appcRequest;
128         appcRequest = AppcActorServiceProvider.constructRequest(onsetEvent, operation, policy, "vnf01");
129
130         /* The service provider must return a non null APPC request */
131         assertNotNull(appcRequest);
132
133         /* A common header is required and cannot be null */
134         assertNotNull(appcRequest.getCommonHeader());
135         assertEquals(appcRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
136
137         /* An action is required and cannot be null */
138         assertNotNull(appcRequest.getAction());
139         assertEquals("ModifyConfig", appcRequest.getAction());
140
141         /* A payload is required and cannot be null */
142         assertNotNull(appcRequest.getPayload());
143         assertTrue(appcRequest.getPayload().containsKey("generic-vnf.vnf-id"));
144         assertNotNull(appcRequest.getPayload().get("generic-vnf.vnf-id"));
145         assertTrue(appcRequest.getPayload().containsKey("pg-streams"));
146
147         logger.debug("APPC Request: \n" + appcRequest.toString());
148
149         /* Print out request as json to make sure serialization works */
150         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
151         logger.debug("JSON Output: \n" + jsonRequest);
152
153         /* The JSON string must contain the following fields */
154         assertTrue(jsonRequest.contains("CommonHeader"));
155         assertTrue(jsonRequest.contains("Action"));
156         assertTrue(jsonRequest.contains("ModifyConfig"));
157         assertTrue(jsonRequest.contains("Payload"));
158         assertTrue(jsonRequest.contains("generic-vnf.vnf-id"));
159         assertTrue(jsonRequest.contains("pg-streams"));
160
161         Response appcResponse = new Response(appcRequest);
162         appcResponse.getStatus().setCode(ResponseCode.SUCCESS.getValue());
163         appcResponse.getStatus().setDescription("AppC success");
164         /* Print out request as json to make sure serialization works */
165         String jsonResponse = Serialization.gsonPretty.toJson(appcResponse);
166         logger.debug("JSON Output: \n" + jsonResponse);
167     }
168
169     @Test
170     public void testMethods() {
171         AppcActorServiceProvider sp = new AppcActorServiceProvider();
172
173         assertEquals("APPC", sp.actor());
174         assertEquals(4, sp.recipes().size());
175         assertEquals("VM", sp.recipeTargets("Restart").get(0));
176         assertEquals(0, sp.recipePayloads("Restart").size());
177     }
178 }