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