020b09e8c93bccf387aade425586bad0669e9b95
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / test / java / org / onap / policy / controlloop / actor / sdnc / SdncActorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2019 Huawei. All rights reserved.
6  * Modifications Copyright (C) 2018-2020 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.sdnc;
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.util.Arrays;
30 import java.util.Objects;
31 import java.util.UUID;
32 import java.util.stream.Collectors;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
37 import org.onap.policy.controlloop.ControlLoopOperation;
38 import org.onap.policy.controlloop.VirtualControlLoopEvent;
39 import org.onap.policy.controlloop.actor.test.BasicActor;
40 import org.onap.policy.controlloop.policy.Policy;
41 import org.onap.policy.sdnc.SdncRequest;
42
43 public class SdncActorTest extends BasicActor {
44
45     private static final String REROUTE = RerouteOperation.NAME;
46
47     /**
48      * Set up before test class.
49      *
50      * @throws Exception if the A&AI simulator cannot be started
51      */
52     @BeforeClass
53     public static void setUpSimulator() throws Exception {
54         org.onap.policy.simulators.Util.buildAaiSim();
55     }
56
57     @AfterClass
58     public static void tearDownSimulator() {
59         HttpServletServerFactoryInstance.getServerFactory().destroy();
60     }
61
62     @Test
63     public void testSdncActorServiceProvider() {
64         final SdncActor prov = new SdncActor();
65
66         // verify that it has the operators we expect
67         var expected = Arrays.asList(BandwidthOnDemandOperation.NAME, RerouteOperation.NAME).stream().sorted()
68                         .collect(Collectors.toList());
69         var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
70
71         assertEquals(expected.toString(), actual.toString());
72
73         // verify that it all plugs into the ActorService
74         verifyActorService(SdncActor.NAME, "service.yaml");
75     }
76
77     @Test
78     public void testConstructRequest() {
79         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
80         ControlLoopOperation operation = new ControlLoopOperation();
81
82         Policy policy = new Policy();
83         policy.setRecipe(REROUTE);
84
85         SdncActor provider = new SdncActor();
86         assertNull(provider.constructRequest(onset, operation, policy));
87
88         onset.getAai().put("network-information.network-id", "network-5555");
89         assertNull(provider.constructRequest(onset, operation, policy));
90
91         assertNull(provider.constructRequest(onset, operation, policy));
92
93         UUID requestId = UUID.randomUUID();
94         onset.setRequestId(requestId);
95         assertNull(provider.constructRequest(onset, operation, policy));
96
97         assertNull(provider.constructRequest(onset, operation, policy));
98
99         onset.getAai().put("service-instance.service-instance-id", "service-instance-01");
100         assertNotNull(provider.constructRequest(onset, operation, policy));
101
102         policy.setRecipe(REROUTE);
103         assertNotNull(provider.constructRequest(onset, operation, policy));
104
105         SdncRequest request = provider.constructRequest(onset, operation, policy);
106
107         assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
108         assertEquals("reoptimize", request.getHealRequest().getRequestHeaderInfo().getSvcAction());
109         assertEquals("ReoptimizeSOTNInstance", request.getHealRequest().getRequestInfo().getRequestAction());
110         assertEquals("network-5555", request.getHealRequest().getNetworkInfo().getNetworkId());
111         assertEquals("service-instance-01", request.getHealRequest().getServiceInfo().getServiceInstanceId());
112     }
113
114     @Test
115     public void testMethods() {
116         SdncActor sp = new SdncActor();
117
118         assertEquals("SDNC", sp.actor());
119         assertEquals(1, sp.recipes().size());
120         assertEquals(REROUTE, sp.recipes().get(0));
121         assertEquals("VM", sp.recipeTargets(REROUTE).get(0));
122         assertEquals(0, sp.recipePayloads(REROUTE).size());
123     }
124 }