Clean up and enhancement of Actor re-design
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / test / java / org / onap / policy / controlloop / actor / sdnc / SdncActorServiceProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * TestSdncActorServiceProvider
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.policy.Policy;
40 import org.onap.policy.sdnc.SdncRequest;
41
42 public class SdncActorServiceProviderTest {
43
44     private static final String REROUTE = RerouteOperator.NAME;
45
46     /**
47      * Set up before test class.
48      *
49      * @throws Exception if the A&AI simulator cannot be started
50      */
51     @BeforeClass
52     public static void setUpSimulator() throws Exception {
53         org.onap.policy.simulators.Util.buildAaiSim();
54     }
55
56     @AfterClass
57     public static void tearDownSimulator() {
58         HttpServletServerFactoryInstance.getServerFactory().destroy();
59     }
60
61     @Test
62     public void testSdncActorServiceProvider() {
63         final SdncActorServiceProvider prov = new SdncActorServiceProvider();
64
65         // verify that it has the operators we expect
66         var expected = Arrays.asList(BandwidthOnDemandOperator.NAME, RerouteOperator.NAME).stream().sorted()
67                         .collect(Collectors.toList());
68         var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
69
70         assertEquals(expected.toString(), actual.toString());
71     }
72
73     @Test
74     public void testConstructRequest() {
75         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
76         ControlLoopOperation operation = new ControlLoopOperation();
77
78         Policy policy = new Policy();
79         policy.setRecipe(REROUTE);
80
81         SdncActorServiceProvider provider = new SdncActorServiceProvider();
82         assertNull(provider.constructRequest(onset, operation, policy));
83
84         onset.getAai().put("network-information.network-id", "network-5555");
85         assertNull(provider.constructRequest(onset, operation, policy));
86
87         assertNull(provider.constructRequest(onset, operation, policy));
88
89         UUID requestId = UUID.randomUUID();
90         onset.setRequestId(requestId);
91         assertNull(provider.constructRequest(onset, operation, policy));
92
93         assertNull(provider.constructRequest(onset, operation, policy));
94
95         onset.getAai().put("service-instance.service-instance-id", "service-instance-01");
96         assertNotNull(provider.constructRequest(onset, operation, policy));
97
98         policy.setRecipe(REROUTE);
99         assertNotNull(provider.constructRequest(onset, operation, policy));
100
101         SdncRequest request = provider.constructRequest(onset, operation, policy);
102
103         assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
104         assertEquals("reoptimize", request.getHealRequest().getRequestHeaderInfo().getSvcAction());
105         assertEquals("ReoptimizeSOTNInstance", request.getHealRequest().getRequestInfo().getRequestAction());
106         assertEquals("network-5555", request.getHealRequest().getNetworkInfo().getNetworkId());
107         assertEquals("service-instance-01", request.getHealRequest().getServiceInfo().getServiceInstanceId());
108     }
109
110     @Test
111     public void testMethods() {
112         SdncActorServiceProvider sp = new SdncActorServiceProvider();
113
114         assertEquals("SDNC", sp.actor());
115         assertEquals(1, sp.recipes().size());
116         assertEquals(REROUTE, sp.recipes().get(0));
117         assertEquals("VM", sp.recipeTargets(REROUTE).get(0));
118         assertEquals(0, sp.recipePayloads(REROUTE).size());
119     }
120 }