GenericResourceApiProvider unit tests part 2.
[sdnc/northbound.git] / generic-resource-api / provider / src / test / java / org / onap / sdnc / northbound / ServiceTopologyOperationRPCTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.sdnc.northbound;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 import static org.onap.sdnc.northbound.GenericResourceApiProvider.APP_NAME;
28 import static org.onap.sdnc.northbound.GenericResourceApiProvider.NO_SERVICE_LOGIC_ACTIVE;
29 import static org.onap.sdnc.northbound.GenericResourceApiProvider.NULL_OR_EMPTY_ERROR_PARAM;
30 import static org.onap.sdnc.northbound.util.MDSALUtil.build;
31 import static org.onap.sdnc.northbound.util.MDSALUtil.exec;
32 import static org.onap.sdnc.northbound.util.MDSALUtil.requestInformation;
33 import static org.onap.sdnc.northbound.util.MDSALUtil.sdncRequestHeader;
34 import static org.onap.sdnc.northbound.util.MDSALUtil.service;
35 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceData;
36 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceInformationBuilder;
37 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceResponseInformation;
38 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceStatus;
39 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationInput;
40 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationOutput;
41
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mockito;
46 import org.mockito.runners.MockitoJUnitRunner;
47 import org.onap.sdnc.northbound.util.PropBuilder;
48 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
49 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
50 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
51 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
52 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationInput;
53 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationOutput;
54 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformation;
55 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader;
56 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader.SvcAction;
57 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.information.ServiceInformation;
58 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.Service;
59 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatus;
60 import org.opendaylight.yangtools.yang.common.RpcResult;
61
62
63 /**
64  * This class test the ServiceTopologyOperation mdsal RPC.
65  */
66 @RunWith(MockitoJUnitRunner.class)
67 public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderTest {
68
69     final String SVC_OPERATION = "service-topology-operation";
70
71     @Before
72     public void setUp() throws Exception {
73         super.setUp();
74         svcClient.setScvOperation(SVC_OPERATION);
75     }
76
77     /**
78      * Verify  ServiceTopologyOperation RPC executes a DG then produces the expected {@link
79      * ServiceTopologyOperationOutput} and persisted the expected {@link Service} in the {@link DataBroker}
80      */
81     @Test
82     public void testServiceTopologyOperationRPC_ExecuteDG_Success() throws Exception {
83
84         //mock svcClient to perform a successful execution with the expected parameters
85         svcClient.mockHasGraph(true);
86         PropBuilder svcResultProp = svcClient.createExecuteOKResult();
87         svcClient.mockExecute(svcResultProp);
88
89         // create the ServiceTopologyOperationInput from the template
90         ServiceTopologyOperationInput serviceTopologyOperationInput = createSTOI();
91
92         //execute the mdsal exec
93         ServiceTopologyOperationOutput actualServiceTopologyOperationOutput = exec(
94             genericResourceApiProvider::serviceTopologyOperation
95             , serviceTopologyOperationInput
96             , RpcResult::getResult
97         );
98
99         //verify the returned ServiceTopologyOperationOutput
100         ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput = createExpectedSTOO(svcResultProp,
101             serviceTopologyOperationInput);
102         assertEquals(expectedServiceTopologyOperationOutput, actualServiceTopologyOperationOutput);
103
104         //verify the persisted Service
105         Service actualService = db.read(serviceTopologyOperationInput.getServiceInformation().getServiceInstanceId(),
106             LogicalDatastoreType.CONFIGURATION);
107         Service expectedService = createExpectedService(
108             expectedServiceTopologyOperationOutput,
109             serviceTopologyOperationInput,
110             actualService);
111         assertEquals(expectedService, actualService);
112
113         LOG.debug("done");
114     }
115
116     @Test
117     public void should_fail_when_service_info_not_present() throws Exception {
118         // create the ServiceTopologyOperationInput from the template
119         ServiceTopologyOperationInput input = build(
120             serviceTopologyOperationInput()
121                 .setSdncRequestHeader(build(sdncRequestHeader()
122                     .setSvcRequestId("svc-request-id: xyz")
123                     .setSvcAction(SvcAction.Assign)
124                 ))
125                 .setRequestInformation(build(requestInformation()
126                     .setRequestId("request-id: xyz")
127                     .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
128                 )));
129
130         //execute the mdsal exec
131         ServiceTopologyOperationOutput output = exec(
132             genericResourceApiProvider::serviceTopologyOperation
133             , input
134             , RpcResult::getResult
135         );
136
137         assertEquals("404", output.getResponseCode());
138         assertEquals(NULL_OR_EMPTY_ERROR_PARAM, output.getResponseMessage());
139         assertEquals("Y", output.getAckFinalIndicator());
140     }
141
142
143     @Test
144     public void should_fail_when_client_execution_failed() throws Exception {
145         svcClient.mockHasGraph(true);
146         svcClient.mockExecute(new RuntimeException("test exception"));
147
148         ServiceTopologyOperationInput input = createSTOI();
149
150         //execute the mdsal exec
151         ServiceTopologyOperationOutput output = exec(
152             genericResourceApiProvider::serviceTopologyOperation
153             , input
154             , RpcResult::getResult
155         );
156
157         assertEquals("500", output.getResponseCode());
158         assertEquals("test exception", output.getResponseMessage());
159         assertEquals("Y", output.getAckFinalIndicator());
160     }
161
162     @Test
163     public void should_fail_when_client_has_no_graph() throws Exception {
164         svcClient.mockHasGraph(false);
165
166         ServiceTopologyOperationInput input = createSTOI();
167
168         //execute the mdsal exec
169         ServiceTopologyOperationOutput output = exec(
170             genericResourceApiProvider::serviceTopologyOperation
171             , input
172             , RpcResult::getResult
173         );
174
175         assertEquals("503", output.getResponseCode());
176         assertEquals(NO_SERVICE_LOGIC_ACTIVE + APP_NAME + ": '" + SVC_OPERATION + "'", output.getResponseMessage());
177         assertEquals("Y", output.getAckFinalIndicator());
178     }
179
180
181     @Test
182     public void should_fail_when_failed_to_update_mdsal() throws Exception {
183
184         svcClient.mockHasGraph(true);
185         WriteTransaction mockWriteTransaction = mock(WriteTransaction.class);
186         when(mockWriteTransaction.submit()).thenThrow(new TransactionChainClosedException("test exception"));
187
188         DataBroker spyDataBroker = Mockito.spy(dataBroker);
189         when(spyDataBroker.newWriteOnlyTransaction()).thenReturn(mockWriteTransaction);
190         genericResourceApiProvider.setDataBroker(spyDataBroker);
191
192         ServiceTopologyOperationInput input = createSTOI();
193
194         //execute the mdsal exec
195         ServiceTopologyOperationOutput output = exec(
196             genericResourceApiProvider::serviceTopologyOperation
197             , input
198             , RpcResult::getResult
199         );
200
201         assertEquals("500", output.getResponseCode());
202         assertEquals("test exception", output.getResponseMessage());
203         assertEquals("Y", output.getAckFinalIndicator());
204     }
205
206     private ServiceTopologyOperationInput createSTOI() {
207
208         return build(
209             serviceTopologyOperationInput()
210                 .setSdncRequestHeader(build(sdncRequestHeader()
211                     .setSvcRequestId("svc-request-id: xyz")
212                     .setSvcAction(SvcAction.Assign)
213                 ))
214                 .setRequestInformation(build(requestInformation()
215                     .setRequestId("request-id: xyz")
216                     .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
217                 ))
218                 .setServiceInformation(build(serviceInformationBuilder()
219                     .setServiceInstanceId("service-instance-id: xyz")
220                 ))
221         );
222     }
223
224
225     private ServiceTopologyOperationOutput createExpectedSTOO(PropBuilder expectedSvcResultProp,
226         ServiceTopologyOperationInput expectedServiceTopologyOperationInput) {
227         return build(
228             serviceTopologyOperationOutput()
229                 .setSvcRequestId(expectedServiceTopologyOperationInput.getSdncRequestHeader().getSvcRequestId())
230                 .setResponseCode(expectedSvcResultProp.get(svcClient.errorCode))
231                 .setAckFinalIndicator(expectedSvcResultProp.get(svcClient.ackFinal))
232                 .setResponseMessage(expectedSvcResultProp.get(svcClient.errorMessage))
233                 .setServiceResponseInformation(build(serviceResponseInformation()
234                     .setInstanceId(expectedServiceTopologyOperationInput.getServiceInformation().getServiceInstanceId())
235                     .setObjectPath(expectedSvcResultProp.get(svcClient.serviceObjectPath))
236                 ))
237         );
238     }
239
240     private Service createExpectedService(
241         ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput,
242         ServiceTopologyOperationInput expectedServiceTopologyOperationInput,
243         Service actualService
244     ) {
245
246         //We cannot predict the timeStamp value so just steal it from the actual
247         //we need this to prevent the equals method from returning false as a result of the timestamp
248         String responseTimeStamp = actualService == null || actualService.getServiceStatus() == null ?
249             null : actualService.getServiceStatus().getResponseTimestamp();
250
251         SdncRequestHeader expectedSdncRequestHeader = expectedServiceTopologyOperationInput.getSdncRequestHeader();
252         ServiceInformation expectedServiceInformation = expectedServiceTopologyOperationInput.getServiceInformation();
253         RequestInformation expectedRequestInformation = expectedServiceTopologyOperationInput.getRequestInformation();
254
255         return build(
256             service()
257                 .setServiceInstanceId(expectedServiceInformation.getServiceInstanceId())
258                 .setServiceData(build(serviceData()))
259                 .setServiceStatus(
260                     build(
261                         serviceStatus()
262                             .setAction(expectedRequestInformation.getRequestAction().name())
263                             .setFinalIndicator(expectedServiceTopologyOperationOutput.getAckFinalIndicator())
264                             .setResponseCode(expectedServiceTopologyOperationOutput.getResponseCode())
265                             .setResponseMessage(expectedServiceTopologyOperationOutput.getResponseMessage())
266                             .setRpcAction(toRpcAction(expectedSdncRequestHeader.getSvcAction()))
267                             .setRpcName(SVC_OPERATION)
268                             .setRequestStatus(ServiceStatus.RequestStatus.Synccomplete)
269                             .setResponseTimestamp(responseTimeStamp)
270                     )
271                 )
272         );
273
274     }
275
276     public ServiceStatus.RpcAction toRpcAction(SvcAction fromEnum) {
277         return fromEnum == null ? null : ServiceStatus.RpcAction.valueOf(fromEnum.name());
278     }
279
280
281 }