Release version 2.1.1 and roll versions
[sdnc/northbound.git] / vnfapi / provider / src / test / java / org / onap / sdnc / vnfapi / VnfTopologyOperationRPCTest.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.vnfapi;
23
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.onap.sdnc.vnfapi.util.PropBuilder;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.VnfTopologyOperationInput;
33 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.VnfTopologyOperationOutput;
34 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.sdnc.request.header.SdncRequestHeader.SvcAction;
35 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.vnf.model.infrastructure.VnfList;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37
38 import static org.onap.sdnc.vnfapi.util.MDSALUtil.build;
39 import static org.onap.sdnc.vnfapi.util.MDSALUtil.exec;
40 import static org.onap.sdnc.vnfapi.util.MDSALUtil.sdncRequestHeader;
41 import static org.onap.sdnc.vnfapi.util.MDSALUtil.serviceData;
42 import static org.onap.sdnc.vnfapi.util.MDSALUtil.serviceInformation;
43 import static org.onap.sdnc.vnfapi.util.MDSALUtil.vnfInformation;
44 import static org.onap.sdnc.vnfapi.util.MDSALUtil.vnfList;
45 import static org.onap.sdnc.vnfapi.util.MDSALUtil.vnfRequestInformation;
46 import static org.onap.sdnc.vnfapi.util.MDSALUtil.vnfTopologyOperationInput;
47 import static org.onap.sdnc.vnfapi.util.MDSALUtil.vnfTopologyOperationOutput;
48
49
50 /**
51  * This class test the VnfTopologyOperation mdsal RPC.
52  */
53 @RunWith(MockitoJUnitRunner.class)
54 public class VnfTopologyOperationRPCTest extends VnfApiProviderTest {
55
56
57     final String SVC_OPERATION = "vnf-topology-operation";
58
59
60     @Before
61     public void setUp() throws Exception {
62         super.setUp();
63         svcClient.setScvOperation(SVC_OPERATION);
64     }
65
66
67     /**
68      * Verify  VnfTopologyOperation RPC executes Delete VNFList entry
69      * {@link VnfTopologyOperationOutput} and persisted the expected {@link VnfList} in the {@link DataBroker}
70      */
71     @Test
72     public void testVnfTopologyOperationRPC_Delete_VNFList_Entry_Success() throws Exception {
73
74
75         //mock svcClient to perform a successful execution with the expected parameters
76         svcClient.mockHasGraph(true);
77         PropBuilder svcResultProp = svcClient.createExecuteOKResult();
78         svcClient.mockExecute(svcResultProp);
79
80         // create the VnfTopologyOperationInput from the template
81         VnfTopologyOperationInput vnfTopologyOperationInput = createVTOI();
82
83         //persist a vnfList entry in the dataBroker
84         persistVnfListBroker(vnfTopologyOperationInput);
85
86         //execute the mdsal exec
87         VnfTopologyOperationOutput actualVnfTopologyOperationOutput = exec(
88                 vnfapiProvider::vnfTopologyOperation
89                 , vnfTopologyOperationInput
90                 , RpcResult::getResult
91         );
92
93
94         //verify the returned VnfTopologyOperationOutput
95         VnfTopologyOperationOutput expectedVnfTopologyOperationOutput = createExpectedVTOO(svcResultProp,vnfTopologyOperationInput);
96         Assert.assertEquals(expectedVnfTopologyOperationOutput,actualVnfTopologyOperationOutput);
97
98
99         //verify the persisted VnfList
100         VnfList actualVnfList = db.read(vnfTopologyOperationInput.getVnfRequestInformation().getVnfId(), LogicalDatastoreType.CONFIGURATION);
101         VnfList expectedVnfList = null;
102         Assert.assertEquals(expectedVnfList,actualVnfList);
103
104         LOG.debug("done");
105     }
106
107
108     public VnfTopologyOperationInput createVTOI(){
109         return build(vnfTopologyOperationInput()
110                 .setServiceInformation(
111                         build(serviceInformation()
112                                 .setServiceId("serviceId: xyz")
113                                 .setServiceInstanceId("serviceInstanceId: xyz")
114                                 .setServiceType("serviceType: xyz")
115                                 .setSubscriberName("subscriberName: xyz")
116                         )
117                 )
118                 .setVnfRequestInformation(
119                         build(vnfRequestInformation()
120                                 .setVnfId("vnfId: xyz")
121                                 .setVnfName("vnfName: xyz")//defect if missing
122                                 .setVnfType("vnfType: xyz")//defect if missing
123
124
125                         )
126                 )
127                 .setSdncRequestHeader(
128                         build(sdncRequestHeader()
129                           .setSvcAction(SvcAction.Delete)
130                         )
131                 )
132         );
133
134     }
135
136
137
138     private VnfList persistVnfListBroker(
139             VnfTopologyOperationInput vnfTopologyOperationInput
140     ) throws Exception{
141         VnfList service = build(
142                 vnfList()
143                         .setVnfId(vnfTopologyOperationInput.getVnfRequestInformation().getVnfId())
144                         .setServiceData(
145                                 build(serviceData()
146                                   .setVnfId(vnfTopologyOperationInput.getVnfRequestInformation().getVnfId())
147                                 )
148                         )
149         );
150         db.write(true,service, LogicalDatastoreType.CONFIGURATION);
151         return service;
152     }
153
154
155
156
157
158     private VnfTopologyOperationOutput createExpectedVTOO(PropBuilder expectedSvcResultProp,VnfTopologyOperationInput expectedVnfTopologyOperationInput){
159         return build(
160                 vnfTopologyOperationOutput()
161                         .setSvcRequestId(expectedVnfTopologyOperationInput.getSdncRequestHeader().getSvcRequestId())
162                         .setResponseCode(expectedSvcResultProp.get(svcClient.errorCode))
163                         .setAckFinalIndicator(expectedSvcResultProp.get(svcClient.ackFinal))
164                         .setResponseMessage(expectedSvcResultProp.get(svcClient.errorMessage))
165                         .setVnfInformation(build(vnfInformation()
166                                 .setVnfId(expectedVnfTopologyOperationInput.getVnfRequestInformation().getVnfId())
167                         ))
168         );
169     }
170
171
172
173
174 }