ebfc04d24f50ffa2221f2a110cc001b44622bb55
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / test / java / org / onap / policy / controlloop / actor / sdnc / BandwidthOnDemandOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. 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.sdnc;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.List;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
34 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
35 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
36 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
38 import org.onap.policy.sdnc.SdncResponse;
39
40 public class BandwidthOnDemandOperationTest extends BasicSdncOperation {
41     private static final String MY_SERVICE = "my-service";
42     private static final String MY_VNF = "my-vnf";
43     private static final String MY_BANDWIDTH = "my-bandwidth";
44     private static final String MY_CHANGE_TIME = "my-change-time";
45
46     private BandwidthOnDemandOperation oper;
47
48     public BandwidthOnDemandOperationTest() {
49         super(DEFAULT_ACTOR, BandwidthOnDemandOperation.NAME);
50     }
51
52     @BeforeClass
53     public static void setUpBeforeClass() throws Exception {
54         initBeforeClass();
55     }
56
57     @AfterClass
58     public static void tearDownAfterClass() {
59         destroyAfterClass();
60     }
61
62     /**
63      * Set up.
64      */
65     @Override
66     @Before
67     public void setUp() throws Exception {
68         super.setUp();
69         oper = new BandwidthOnDemandOperation(params, config);
70     }
71
72     @Test
73     public void testConstructor() {
74         assertEquals(DEFAULT_ACTOR, oper.getActorName());
75         assertEquals(BandwidthOnDemandOperation.NAME, oper.getName());
76     }
77
78     @Test
79     public void testGetPropertyNames() {
80         // @formatter:off
81         assertThat(oper.getPropertyNames()).isEqualTo(
82                         List.of(
83                             OperationProperties.ENRICHMENT_SERVICE_ID,
84                             OperationProperties.ENRICHMENT_BANDWIDTH,
85                             OperationProperties.ENRICHMENT_BANDWIDTH_CHANGE_TIME,
86                             OperationProperties.ENRICHMENT_VNF_ID));
87
88         // @formatter:on
89     }
90
91     /**
92      * Tests "success" case with simulator.
93      */
94     @Test
95     public void testSuccess() throws Exception {
96         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT)
97                         .path("GENERIC-RESOURCE-API:vf-module-topology-operation").build();
98         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
99
100         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
101         oper = new BandwidthOnDemandOperation(params, config);
102
103         oper.setProperty(OperationProperties.ENRICHMENT_SERVICE_ID, MY_SERVICE);
104         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH, MY_BANDWIDTH);
105         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH_CHANGE_TIME, MY_CHANGE_TIME);
106         oper.setProperty(OperationProperties.ENRICHMENT_VNF_ID, MY_VNF);
107
108         outcome = oper.start().get();
109         assertEquals(OperationResult.SUCCESS, outcome.getResult());
110         assertTrue(outcome.getResponse() instanceof SdncResponse);
111     }
112
113     @Test
114     public void testMakeRequest() throws Exception {
115         oper.setProperty(OperationProperties.ENRICHMENT_SERVICE_ID, MY_SERVICE);
116         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH, MY_BANDWIDTH);
117         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH_CHANGE_TIME, MY_CHANGE_TIME);
118         oper.setProperty(OperationProperties.ENRICHMENT_VNF_ID, MY_VNF);
119
120         verifyRequest("bod.json", verifyOperation(oper), IGNORE_FIELDS);
121     }
122
123     /*
124      * Tests makeRequest() when a property is missing.
125      */
126
127     @Test
128     public void testMakeRequestMissingBandwidth() throws Exception {
129         oper = new BandwidthOnDemandOperation(params, config);
130         oper.setProperty(OperationProperties.ENRICHMENT_SERVICE_ID, MY_SERVICE);
131         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH_CHANGE_TIME, MY_CHANGE_TIME);
132         oper.setProperty(OperationProperties.ENRICHMENT_VNF_ID, MY_VNF);
133
134         oper.generateSubRequestId(1);
135         outcome.setSubRequestId(oper.getSubRequestId());
136
137         assertThatIllegalStateException().isThrownBy(() -> oper.makeRequest(1))
138                         .withMessageContaining("missing bandwidth from enrichment data");
139     }
140
141     @Test
142     public void testMakeRequestMissingBandwidthChangeTime() throws Exception {
143         oper = new BandwidthOnDemandOperation(params, config);
144         oper.setProperty(OperationProperties.ENRICHMENT_SERVICE_ID, MY_SERVICE);
145         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH, MY_BANDWIDTH);
146         oper.setProperty(OperationProperties.ENRICHMENT_VNF_ID, MY_VNF);
147
148         oper.generateSubRequestId(1);
149         outcome.setSubRequestId(oper.getSubRequestId());
150
151         assertThatIllegalStateException().isThrownBy(() -> oper.makeRequest(1))
152                         .withMessageContaining("missing bandwidth change time from enrichment data");
153     }
154
155     @Test
156     public void testMakeRequestMissingVnfId() throws Exception {
157         oper = new BandwidthOnDemandOperation(params, config);
158         oper.setProperty(OperationProperties.ENRICHMENT_SERVICE_ID, MY_SERVICE);
159         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH, MY_BANDWIDTH);
160         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH_CHANGE_TIME, MY_CHANGE_TIME);
161
162         oper.generateSubRequestId(1);
163         outcome.setSubRequestId(oper.getSubRequestId());
164
165         assertThatIllegalStateException().isThrownBy(() -> oper.makeRequest(1))
166                         .withMessageContaining("missing VNF id from enrichment data");
167     }
168 }