Copy policy-endpoints from drools-pdp to common
[policy/drools-applications.git] / controlloop / common / actors / actor.vfc / src / test / java / org / onap / policy / controlloop / actor / vfc / TestVFCActorServiceProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * TestVFCActorServiceProvider
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. 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.vfc;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27
28 import java.util.UUID;
29
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.aai.AaiGetVnfResponse;
34 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
35 import org.onap.policy.controlloop.ControlLoopOperation;
36 import org.onap.policy.controlloop.VirtualControlLoopEvent;
37 import org.onap.policy.controlloop.policy.Policy;
38 import org.onap.policy.drools.system.PolicyEngine;
39 import org.onap.policy.simulators.Util;
40 import org.onap.policy.vfc.VFCRequest;
41
42 public class TestVFCActorServiceProvider {
43
44     /**
45      * Set up for test class.
46      */
47     @BeforeClass
48     public static void setUpSimulator() {
49         try {
50             Util.buildAaiSim();
51         } catch (Exception e) {
52             fail(e.getMessage());
53         }
54     }
55
56     @AfterClass
57     public static void tearDownSimulator() {
58         HttpServletServer.factory.destroy();
59     }
60
61     @Test
62     public void testConstructRequest() {
63         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
64         ControlLoopOperation operation = new ControlLoopOperation();
65
66         Policy policy = new Policy();
67         policy.setRecipe("GoToOz");
68
69         assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null));
70
71         onset.getAai().put("generic-vnf.vnf-id", "dorothy.gale.1939");
72         assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null));
73
74         PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
75         PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
76         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
77         assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null));
78
79         UUID requestId = UUID.randomUUID();
80         onset.setRequestId(requestId);
81         assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null));
82
83         onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
84         PolicyEngine.manager.getEnvironment().remove("aai.password");
85         assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null));
86
87         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
88         assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null));
89
90         onset.getAai().put("service-instance.service-instance-id", "");
91         assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, null));
92
93         assertNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse()));
94
95         policy.setRecipe("Restart");
96         assertNotNull(VFCActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse()));
97
98         VFCRequest request =
99                 VFCActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse());
100
101         assertEquals(requestId, request.getRequestId());
102         assertEquals("dorothy.gale.1939", request.getHealRequest().getVnfInstanceId());
103         assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
104     }
105
106     @Test
107     public void testMethods() {
108         VFCActorServiceProvider sp = new VFCActorServiceProvider();
109
110         assertEquals("VFC", sp.actor());
111         assertEquals(1, sp.recipes().size());
112         assertEquals("Restart", sp.recipes().get(0));
113         assertEquals("VM", sp.recipeTargets("Restart").get(0));
114         assertEquals(0, sp.recipePayloads("Restart").size());
115     }
116 }