c8179992cb407bcb3bb4b4d665a98048b9f09748
[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.Test;
30 import org.onap.policy.appc.Request;
31 import org.onap.policy.appc.util.Serialization;
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.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class AppcServiceProviderTest {
43
44     private static final Logger logger = LoggerFactory.getLogger(AppcServiceProviderTest.class);
45     
46     private static VirtualControlLoopEvent onsetEvent;
47     private static ControlLoopOperation operation;
48     private static Policy policy;
49
50     static {
51         /* 
52          * Construct an onset with an AAI subtag containing
53          * generic-vnf.vnf-id and a target type of VM.
54          */
55         onsetEvent = new VirtualControlLoopEvent();
56         onsetEvent.closedLoopControlName = "closedLoopControlName-Test";
57         onsetEvent.requestID = UUID.randomUUID();
58         onsetEvent.closedLoopEventClient = "tca.instance00001";
59         onsetEvent.target_type = ControlLoopTargetType.VF;
60         onsetEvent.target = "generic-vnf.vnf-id";
61         onsetEvent.from = "DCAE";
62         onsetEvent.closedLoopAlarmStart = Instant.now();
63         onsetEvent.AAI = new HashMap<>();
64         onsetEvent.AAI.put("generic-vnf.vnf-id", "fw0001vm001fw001");
65         onsetEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
66
67         /* Construct an operation with an APPC actor and ModifyConfig operation. */
68         operation = new ControlLoopOperation();
69         operation.actor = "APPC";
70         operation.operation = "ModifyConfig";
71         operation.target = "VM";
72         operation.end = Instant.now();
73         operation.subRequestId = "1";
74
75         /* Construct a policy specifying to modify configuration. */
76         policy = new Policy();
77         policy.setName("Modify Packet Generation Config");
78         policy.setDescription("Upon getting the trigger event, modify packet gen config");
79         policy.setActor("APPC");
80         policy.setTarget(new Target(TargetType.VM));
81         policy.setRecipe("ModifyConfig");
82         policy.setPayload(null);
83         policy.setRetry(2);
84         policy.setTimeout(300);
85
86     }
87
88     @Test
89     public void constructModifyConfigRequestTest() {
90         
91         Request appcRequest = APPCActorServiceProvider.constructRequest(onsetEvent, operation, policy);
92         
93         /* The service provider must return a non null APPC request */
94         assertNotNull(appcRequest);
95
96         /* A common header is required and cannot be null */
97         assertNotNull(appcRequest.getCommonHeader());
98         assertEquals(appcRequest.getCommonHeader().RequestID, onsetEvent.requestID);
99
100         /* An action is required and cannot be null */
101         assertNotNull(appcRequest.Action);
102         assertEquals(appcRequest.Action, "ModifyConfig");
103
104         /* A payload is required and cannot be null */
105         assertNotNull(appcRequest.getPayload());
106         assertTrue(appcRequest.getPayload().containsKey("generic-vnf.vnf-id"));
107         assertTrue(appcRequest.getPayload().containsKey("pg-streams"));
108
109         logger.debug("APPC Request: \n" + appcRequest.toString());
110         
111         /* Print out request as json to make sure serialization works */
112         String jsonRequest = Serialization.gsonPretty.toJson(appcRequest);
113         logger.debug("JSON Output: \n" + jsonRequest);
114         
115         /* The JSON string must contain the following fields */
116         assertTrue(jsonRequest.contains("CommonHeader"));
117         assertTrue(jsonRequest.contains("Action"));
118         assertTrue(jsonRequest.contains("ModifyConfig"));
119         assertTrue(jsonRequest.contains("Payload"));
120         assertTrue(jsonRequest.contains("generic-vnf.vnf-id"));
121         assertTrue(jsonRequest.contains("pg-streams"));
122     }
123
124 }