dcdf59a56f998f2cd08a87924939e053d32a440d
[policy/drools-applications.git] /
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.util.Serialization;
34 import org.onap.policy.controlloop.ControlLoopEventStatus;
35 import org.onap.policy.controlloop.ControlLoopOperation;
36 import org.onap.policy.controlloop.ControlLoopTargetType;
37 import org.onap.policy.controlloop.VirtualControlLoopEvent;
38 import org.onap.policy.controlloop.policy.Policy;
39 import org.onap.policy.controlloop.policy.Target;
40 import org.onap.policy.controlloop.policy.TargetType;
41 import org.onap.policy.drools.http.server.HttpServletServer;
42 import org.onap.policy.simulators.Util;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class AppcServiceProviderTest {
47
48     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
49     
50     private static VirtualControlLoopEvent onsetEvent;
51     private static ControlLoopOperation operation;
52     private static Policy policy;
53
54     static {
55         /* 
56          * Construct an onset with an AAI subtag containing
57          * generic-vnf.vnf-id and a target type of VM.
58          */
59         onsetEvent = new VirtualControlLoopEvent();
60         onsetEvent.closedLoopControlName = "closedLoopControlName-Test";
61         onsetEvent.requestID = UUID.randomUUID();
62         onsetEvent.closedLoopEventClient = "tca.instance00001";
63         onsetEvent.target_type = ControlLoopTargetType.VNF;
64         onsetEvent.target = "generic-vnf.vnf-id";
65         onsetEvent.from = "DCAE";
66         onsetEvent.closedLoopAlarmStart = Instant.now();
67         onsetEvent.AAI = new HashMap<>();
68         onsetEvent.AAI.put("generic-vnf.vnf-id", "fw0001vm001fw001");
69         onsetEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
70
71         /* Construct an operation with an APPC actor and ModifyConfig operation. */
72         operation = new ControlLoopOperation();
73         operation.actor = "APPC";
74         operation.operation = "ModifyConfig";
75         operation.target = "VNF";
76         operation.end = Instant.now();
77         operation.subRequestId = "1";
78
79         /* Construct a policy specifying to modify configuration. */
80         policy = new Policy();
81         policy.setName("Modify Packet Generation Config");
82         policy.setDescription("Upon getting the trigger event, modify packet gen config");
83         policy.setActor("APPC");
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
93     @BeforeClass
94     public static void setUpSimulator() {
95         try {
96             Util.buildAaiSim();
97         } catch (Exception e) {
98             fail(e.getMessage());
99         }
100     }
101
102     @AfterClass
103     public static void tearDownSimulator() {
104         HttpServletServer.factory.destroy();
105     }
106     
107     @Test
108     public void constructModifyConfigRequestTest() {
109         
110         Request appcRequest = APPCActorServiceProvider.constructRequest(onsetEvent, operation, policy);
111         
112         /* The service provider must return a non null APPC request */
113         assertNotNull(appcRequest);
114
115         /* A common header is required and cannot be null */
116         assertNotNull(appcRequest.getCommonHeader());
117         assertEquals(appcRequest.getCommonHeader().RequestID, onsetEvent.requestID);
118
119         /* An action is required and cannot be null */
120         assertNotNull(appcRequest.Action);
121         assertEquals(appcRequest.Action, "ModifyConfig");
122
123         /* A payload is required and cannot be null */
124         assertNotNull(appcRequest.getPayload());
125         assertTrue(appcRequest.getPayload().containsKey("generic-vnf.vnf-id"));
126         assertNotNull(appcRequest.getPayload().get("generic-vnf.vnf-id"));
127         assertTrue(appcRequest.getPayload().containsKey("pg-streams"));
128
129         logger.debug("APPC Request: \n" + appcRequest.toString());
130         
131         /* Print out request as json to make sure serialization works */
132         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
133         logger.debug("JSON Output: \n" + jsonRequest);
134         
135         /* The JSON string must contain the following fields */
136         assertTrue(jsonRequest.contains("CommonHeader"));
137         assertTrue(jsonRequest.contains("Action"));
138         assertTrue(jsonRequest.contains("ModifyConfig"));
139         assertTrue(jsonRequest.contains("Payload"));
140         assertTrue(jsonRequest.contains("generic-vnf.vnf-id"));
141         assertTrue(jsonRequest.contains("pg-streams"));
142     }
143
144 }