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