80ec3476e2ba5be3965eff1b38869e7198ac294b
[policy/models.git] / models-interactions / model-actors / actor.vfc / src / test / java / org / onap / policy / controlloop / actor / vfc / VfcActorServiceProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Policy Drools Applications
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. All rights reserved.
6  * Modifications Copyright (C) 2018-2019 AT&T Corp. All rights reserved.
7  * Modifications Copyright (C) 2019 Nordix Foundation.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.controlloop.actor.vfc;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.fail;
29
30 import java.util.Objects;
31 import java.util.UUID;
32
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.aai.AaiGetVnfResponse;
37 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
38 import org.onap.policy.controlloop.ControlLoopOperation;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.policy.Policy;
41 import org.onap.policy.simulators.Util;
42 import org.onap.policy.vfc.VfcRequest;
43
44 public class VfcActorServiceProviderTest {
45
46     /**
47      * Set up for test class.
48      */
49     @BeforeClass
50     public static void setUpSimulator() {
51         try {
52             Util.buildAaiSim();
53         } catch (Exception e) {
54             fail(e.getMessage());
55         }
56     }
57
58     @AfterClass
59     public static void tearDownSimulator() {
60         HttpServletServer.factory.destroy();
61     }
62
63     @Test
64     public void testConstructRequest() {
65         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
66         ControlLoopOperation operation = new ControlLoopOperation();
67
68         Policy policy = new Policy();
69         policy.setRecipe("GoToOz");
70
71         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, null, null, null));
72
73         onset.getAai().put("generic-vnf.vnf-id", "dorothy.gale.1939");
74         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, null, null, null));
75
76         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666", "AAI", "AAI"));
77
78         UUID requestId = UUID.randomUUID();
79         onset.setRequestId(requestId);
80         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666", "AAI", "AAI"));
81
82         onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
83         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666", "AAI", null));
84
85         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666", "AAI", "AAI"));
86
87         onset.getAai().put("service-instance.service-instance-id", "");
88         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666", "AAI", "AAI"));
89
90         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(), "http://localhost:6666", "AAI", "AAI"));
91
92         policy.setRecipe("Restart");
93         assertNotNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(), "http://localhost:6666", "AAI", "AAI"));
94
95         VfcRequest request =
96                 VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(), "http://localhost:6666", "AAI", "AAI");
97
98         assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
99         assertEquals("dorothy.gale.1939", request.getHealRequest().getVnfInstanceId());
100         assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
101     }
102
103     @Test
104     public void testMethods() {
105         VfcActorServiceProvider sp = new VfcActorServiceProvider();
106
107         assertEquals("VFC", sp.actor());
108         assertEquals(1, sp.recipes().size());
109         assertEquals("Restart", sp.recipes().get(0));
110         assertEquals("VM", sp.recipeTargets("Restart").get(0));
111         assertEquals(0, sp.recipePayloads("Restart").size());
112     }
113 }