Merge "Remove commons-io dependency from models"
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / test / java / org / onap / policy / controlloop / actor / sdnc / RerouteOperationTest.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 RerouteOperationTest extends BasicSdncOperation {
43     private static final String MY_SERVICE = "my-service";
44     private static final String MY_NETWORK = "my-network";
45
46     private RerouteOperation oper;
47
48     public RerouteOperationTest() {
49         super(DEFAULT_ACTOR, RerouteOperation.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 RerouteOperation(params, config);
70     }
71
72     /**
73      * Tests "success" case with simulator.
74      */
75     @Test
76     public void testSuccess() throws Exception {
77         HttpParams opParams = HttpParams.builder().clientName(MY_CLIENT)
78                         .path("GENERIC-RESOURCE-API:network-topology-operation").build();
79         config = new HttpConfig(blockingExecutor, opParams, HttpClientFactoryInstance.getClientFactory());
80
81         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).preprocessed(true).build();
82         oper = new RerouteOperation(params, config);
83
84         oper.setProperty(OperationProperties.ENRICHMENT_SERVICE_ID, MY_SERVICE);
85         oper.setProperty(OperationProperties.ENRICHMENT_NETWORK_ID, MY_NETWORK);
86
87         outcome = oper.start().get();
88         assertEquals(OperationResult.SUCCESS, outcome.getResult());
89         assertTrue(outcome.getResponse() instanceof SdncResponse);
90     }
91
92     @Test
93     public void testConstructor() {
94         assertEquals(DEFAULT_ACTOR, oper.getActorName());
95         assertEquals(RerouteOperation.NAME, oper.getName());
96     }
97
98     @Test
99     public void testGetPropertyNames() {
100         // @formatter:off
101         assertThat(oper.getPropertyNames()).isEqualTo(
102                         List.of(
103                             OperationProperties.ENRICHMENT_SERVICE_ID,
104                             OperationProperties.ENRICHMENT_NETWORK_ID));
105         // @formatter:on
106     }
107
108     @Test
109     public void testMakeRequest() throws Exception {
110         oper.generateSubRequestId(1);
111         SdncRequest request = oper.makeRequest(1);
112         assertEquals(MY_SERVICE, request.getNsInstanceId());
113         assertEquals(REQ_ID, request.getRequestId());
114         assertEquals("/my-path/", request.getUrl());
115         assertEquals(oper.getSubRequestId(), request.getHealRequest().getRequestHeaderInfo().getSvcRequestId());
116
117         verifyRequest("reroute.json", request, IGNORE_FIELDS);
118
119         verifyMissing(RerouteOperation.SERVICE_ID_KEY, "service", RerouteOperation::new);
120         verifyMissing(RerouteOperation.NETWORK_ID_KEY, "network", RerouteOperation::new);
121
122         // perform the operation
123         makeContext();
124         verifyRequest("reroute.json", verifyOperation(oper), IGNORE_FIELDS);
125     }
126
127     @Test
128     public void testMakeRequestViaProperties() throws Exception {
129         // clear the enrichment data and remake the operation
130         event.setAai(null);
131         context = new ControlLoopEventContext(event);
132         params = params.toBuilder().context(context).build();
133         oper = new RerouteOperation(params, config);
134
135         oper.setProperty(OperationProperties.ENRICHMENT_SERVICE_ID, MY_SERVICE);
136         oper.setProperty(OperationProperties.ENRICHMENT_NETWORK_ID, MY_NETWORK);
137
138         verifyRequest("reroute.json", verifyOperation(oper), IGNORE_FIELDS);
139     }
140
141     @Override
142     protected Map<String, String> makeEnrichment() {
143         return Map.of(RerouteOperation.SERVICE_ID_KEY, MY_SERVICE, RerouteOperation.NETWORK_ID_KEY, MY_NETWORK);
144     }
145 }