Remove Target and TargetType
[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 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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.List;
28 import java.util.Map;
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.controlloop.ControlLoopEventContext;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
39 import org.onap.policy.sdnc.SdncRequest;
40 import org.onap.policy.sdnc.SdncResponse;
41
42 public class BandwidthOnDemandOperationTest extends BasicSdncOperation {
43     private static final String MY_SERVICE = "my-service";
44     private static final String MY_VNF = "my-vnf";
45     private static final String MY_BANDWIDTH = "my-bandwidth";
46     private static final String MY_CHANGE_TIME = "my-change-time";
47
48     private BandwidthOnDemandOperation oper;
49
50     public BandwidthOnDemandOperationTest() {
51         super(DEFAULT_ACTOR, BandwidthOnDemandOperation.NAME);
52     }
53
54     @BeforeClass
55     public static void setUpBeforeClass() throws Exception {
56         initBeforeClass();
57     }
58
59     @AfterClass
60     public static void tearDownAfterClass() {
61         destroyAfterClass();
62     }
63
64     /**
65      * Set up.
66      */
67     @Override
68     @Before
69     public void setUp() throws Exception {
70         super.setUp();
71         oper = new BandwidthOnDemandOperation(params, config);
72     }
73
74     @Test
75     public void testConstructor() {
76         assertEquals(DEFAULT_ACTOR, oper.getActorName());
77         assertEquals(BandwidthOnDemandOperation.NAME, oper.getName());
78     }
79
80     @Test
81     public void testGetPropertyNames() {
82         // @formatter:off
83         assertThat(oper.getPropertyNames()).isEqualTo(
84                         List.of(
85                             OperationProperties.ENRICHMENT_SERVICE_ID,
86                             OperationProperties.ENRICHMENT_BANDWIDTH,
87                             OperationProperties.ENRICHMENT_BANDWIDTH_CHANGE_TIME,
88                             OperationProperties.ENRICHMENT_VNF_ID));
89
90         // @formatter:on
91     }
92
93     /**
94      * Tests "success" case with simulator.
95      */
96     @Test
97     public void testSuccess() throws Exception {
98         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT)
99                         .path("GENERIC-RESOURCE-API:vf-module-topology-operation").build();
100         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
101
102         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).preprocessed(true).build();
103         oper = new BandwidthOnDemandOperation(params, config);
104
105         oper.setProperty(OperationProperties.ENRICHMENT_SERVICE_ID, MY_SERVICE);
106         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH, MY_BANDWIDTH);
107         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH_CHANGE_TIME, MY_CHANGE_TIME);
108         oper.setProperty(OperationProperties.ENRICHMENT_VNF_ID, MY_VNF);
109
110         outcome = oper.start().get();
111         assertEquals(OperationResult.SUCCESS, outcome.getResult());
112         assertTrue(outcome.getResponse() instanceof SdncResponse);
113     }
114
115     @Test
116     public void testMakeRequest() throws Exception {
117         oper.generateSubRequestId(1);
118         SdncRequest request = oper.makeRequest(1);
119         assertEquals(MY_SERVICE, request.getNsInstanceId());
120         assertEquals(REQ_ID, request.getRequestId());
121         assertEquals("/my-path/", request.getUrl());
122         assertEquals(oper.getSubRequestId(), request.getHealRequest().getRequestHeaderInfo().getSvcRequestId());
123
124         verifyRequest("bod.json", request, IGNORE_FIELDS);
125
126         verifyMissing(BandwidthOnDemandOperation.SERVICE_ID_KEY, "service", BandwidthOnDemandOperation::new);
127
128         // perform the operation
129         makeContext();
130         verifyRequest("bod.json", verifyOperation(oper), IGNORE_FIELDS);
131     }
132
133     @Test
134     public void testMakeRequestViaProperties() throws Exception {
135         // clear the enrichment data and remake the operation
136         event.setAai(null);
137         context = new ControlLoopEventContext(event);
138         params = params.toBuilder().context(context).build();
139         oper = new BandwidthOnDemandOperation(params, config);
140
141         oper.setProperty(OperationProperties.ENRICHMENT_SERVICE_ID, MY_SERVICE);
142         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH, MY_BANDWIDTH);
143         oper.setProperty(OperationProperties.ENRICHMENT_BANDWIDTH_CHANGE_TIME, MY_CHANGE_TIME);
144         oper.setProperty(OperationProperties.ENRICHMENT_VNF_ID, MY_VNF);
145
146         verifyRequest("bod.json", verifyOperation(oper), IGNORE_FIELDS);
147     }
148
149     @Override
150     protected Map<String, String> makeEnrichment() {
151         return Map.of(BandwidthOnDemandOperation.SERVICE_ID_KEY, MY_SERVICE, BandwidthOnDemandOperation.BANDWIDTH,
152                         MY_BANDWIDTH, BandwidthOnDemandOperation.BANDWIDTH_CHANGE_TIME, MY_CHANGE_TIME,
153                         BandwidthOnDemandOperation.VNF_ID, MY_VNF);
154     }
155 }