Create a Junit test for ServiceTopologyOperation
[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 org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.onap.sdnc.northbound.util.PropBuilder;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationInput;
32 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationOutput;
33 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformation;
34 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformation.RequestAction;
35 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader;
36 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader.SvcAction;
37 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.information.ServiceInformation;
38 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.Service;
39 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatus;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41
42 import static org.junit.Assert.assertEquals;
43 import static org.onap.sdnc.northbound.util.MDSALUtil.build;
44 import static org.onap.sdnc.northbound.util.MDSALUtil.read;
45 import static org.onap.sdnc.northbound.util.MDSALUtil.requestInformation;
46 import static org.onap.sdnc.northbound.util.MDSALUtil.rpc;
47 import static org.onap.sdnc.northbound.util.MDSALUtil.sdncRequestHeader;
48 import static org.onap.sdnc.northbound.util.MDSALUtil.service;
49 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceData;
50 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceInformationBuilder;
51 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceResponseInformation;
52 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceStatus;
53 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationInput;
54 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationOutput;
55
56
57 /**
58  * This class test the ServiceTopologyOperation mdsal RPC.
59  */
60 @RunWith(MockitoJUnitRunner.class)
61 public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderTest {
62
63
64     final String SVC_OPERATION = "service-topology-operation";
65
66
67     @Before
68     public void setUp() throws Exception {
69         super.setUp();
70         svcClient.setScvOperation(SVC_OPERATION);
71     }
72
73
74     /**
75      * Verify  ServiceTopologyOperation RPC executes a DG then produces the expected
76      * {@link ServiceTopologyOperationOutput} and persisted the expected {@link Service} in the {@link DataBroker}
77      */
78     @Test
79     public void testServiceTopologyOperationRPC_ExecuteDG_Success() throws Exception {
80
81
82         //mock svcClient to perform a successful execution with the expected parameters
83         svcClient.mockHasGraph(true);
84         PropBuilder svcResultProp = svcClient.createExecuteOKResult();
85         svcClient.mockExecute(svcResultProp);
86
87         // create the ServiceTopologyOperationInput from the template
88         ServiceTopologyOperationInput serviceTopologyOperationInput = createSTOI(RequestAction.CreateServiceInstance);
89
90         //execute the mdsal rpc
91         ServiceTopologyOperationOutput actualServiceTopologyOperationOutput = rpc(
92                 genericResourceApiProvider::serviceTopologyOperation
93                 , RpcResult::getResult
94                 , serviceTopologyOperationInput
95         );
96
97
98         //verify the returned ServiceTopologyOperationOutput
99         ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput = createExpectedSTOO(svcResultProp,serviceTopologyOperationInput);
100         assertEquals(expectedServiceTopologyOperationOutput,actualServiceTopologyOperationOutput);
101
102
103         //verify the persisted Service
104         Service actualService = read(dataBroker,serviceTopologyOperationInput.getServiceInformation().getServiceInstanceId(), LogicalDatastoreType.CONFIGURATION);
105         Service expectedService = createExpectedService(
106                 expectedServiceTopologyOperationOutput,
107                 serviceTopologyOperationInput,
108                 actualService);
109         assertEquals(expectedService,actualService);
110
111         LOG.debug("done");
112     }
113
114
115
116     private ServiceTopologyOperationInput createSTOI(RequestAction requestAction)
117     {
118
119         return 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                         .setServiceInformation(build(serviceInformationBuilder()
130                                 .setServiceInstanceId("service-instance-id: xyz")
131                         ))
132         );
133     }
134
135
136     private ServiceTopologyOperationOutput createExpectedSTOO(PropBuilder expectedSvcResultProp,ServiceTopologyOperationInput expectedServiceTopologyOperationInput){
137         return build(
138                 serviceTopologyOperationOutput()
139                         .setSvcRequestId(expectedServiceTopologyOperationInput.getSdncRequestHeader().getSvcRequestId())
140                         .setResponseCode(expectedSvcResultProp.get(svcClient.errorCode))
141                         .setAckFinalIndicator(expectedSvcResultProp.get(svcClient.ackFinal))
142                         .setResponseMessage(expectedSvcResultProp.get(svcClient.errorMessage))
143                         .setServiceResponseInformation(build(serviceResponseInformation()
144                                 .setInstanceId(expectedServiceTopologyOperationInput.getServiceInformation().getServiceInstanceId())
145                                 .setObjectPath(expectedSvcResultProp.get(svcClient.serviceObjectPath))
146                         ))
147         );
148     }
149
150     private Service createExpectedService(
151             ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput,
152             ServiceTopologyOperationInput expectedServiceTopologyOperationInput,
153             Service actualService
154     ){
155
156
157         //We cannot predict the timeStamp value so just steal it from the actual
158         //we need this to prevent the equals method from returning false as a result of the timestamp
159         String responseTimeStamp = actualService == null || actualService.getServiceStatus() == null?
160                 null : actualService.getServiceStatus().getResponseTimestamp();
161
162         SdncRequestHeader expectedSdncRequestHeader = expectedServiceTopologyOperationInput.getSdncRequestHeader();
163         ServiceInformation expectedServiceInformation = expectedServiceTopologyOperationInput.getServiceInformation();
164         RequestInformation expectedRequestInformation = expectedServiceTopologyOperationInput.getRequestInformation();
165
166         return build(
167                service()
168                 .setServiceInstanceId(expectedServiceInformation.getServiceInstanceId())
169                 .setServiceData(build(serviceData()))
170                 .setServiceStatus(
171                         build(
172                              serviceStatus()
173                                 .setAction(expectedRequestInformation.getRequestAction().name())
174                                 .setFinalIndicator(expectedServiceTopologyOperationOutput.getAckFinalIndicator())
175                                 .setResponseCode(expectedServiceTopologyOperationOutput.getResponseCode())
176                                 .setResponseMessage(expectedServiceTopologyOperationOutput.getResponseMessage())
177                                 .setRpcAction(toRpcAction(expectedSdncRequestHeader.getSvcAction()))
178                                 .setRpcName(SVC_OPERATION)
179                                 .setRequestStatus(ServiceStatus.RequestStatus.Synccomplete)
180                                 .setResponseTimestamp(responseTimeStamp)
181                         )
182                 )
183         );
184
185     }
186
187     public ServiceStatus.RpcAction toRpcAction(SvcAction fromEnum){
188         return fromEnum == null? null : ServiceStatus.RpcAction.valueOf(fromEnum.name());
189     }
190
191
192 }