db224b06590ac0eb937d08138d6213ca784648a0
[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
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.util.Objects;
32 import java.util.UUID;
33 import org.apache.commons.io.IOUtils;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.aai.AaiCqResponse;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.controlloop.ControlLoopOperation;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.policy.Policy;
42 import org.onap.policy.simulators.Util;
43 import org.onap.policy.vfc.VfcRequest;
44
45 public class VfcActorServiceProviderTest {
46
47     private static final String DOROTHY_GALE_1939 = "dorothy.gale.1939";
48     private static final String CQ_RESPONSE_JSON = "aai/AaiCqResponse.json";
49     private static final String RESTART = "Restart";
50
51     /**
52      * Set up before test class.
53      * @throws Exception if the A&AI simulator cannot be started
54      */
55     @BeforeClass
56     public static void setUpSimulator() throws Exception {
57         Util.buildAaiSim();
58     }
59
60     @AfterClass
61     public static void tearDownSimulator() {
62         HttpServletServerFactoryInstance.getServerFactory().destroy();
63     }
64
65     @Test
66     public void testMethods() {
67         VfcActorServiceProvider sp = new VfcActorServiceProvider();
68
69         assertEquals("VFC", sp.actor());
70         assertEquals(1, sp.recipes().size());
71         assertEquals(RESTART, sp.recipes().get(0));
72         assertEquals("VM", sp.recipeTargets(RESTART).get(0));
73         assertEquals(0, sp.recipePayloads(RESTART).size());
74     }
75
76     @Test
77     public void testConstructRequestCq() throws IOException {
78         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
79         ControlLoopOperation operation = new ControlLoopOperation();
80
81         Policy policy = new Policy();
82         policy.setRecipe("GoToOz");
83
84         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
85
86         onset.getAai().put("generic-vnf.vnf-id", DOROTHY_GALE_1939);
87         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
88
89
90         UUID requestId = UUID.randomUUID();
91         onset.setRequestId(requestId);
92         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
93
94         onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
95         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
96
97
98         onset.getAai().put("service-instance.service-instance-id", "");
99         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
100
101         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
102                 loadAaiResponse(CQ_RESPONSE_JSON)));
103
104         policy.setRecipe(RESTART);
105         assertNotNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
106                 loadAaiResponse(CQ_RESPONSE_JSON)));
107
108         VfcRequest request = VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
109                 loadAaiResponse(CQ_RESPONSE_JSON));
110
111         assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
112         assertEquals(DOROTHY_GALE_1939, request.getHealRequest().getVnfInstanceId());
113         assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
114     }
115
116     /**
117      * Reads an AAI vserver named-query response from a file.
118      *
119      * @param fileName name of the file containing the JSON response
120      * @return output from the AAI vserver named-query
121      * @throws IOException if the file cannot be read
122      */
123     private AaiCqResponse loadAaiResponse(String fileName) throws IOException {
124         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
125         return new AaiCqResponse(resp);
126     }
127
128 }