Add tests to VnfApiProvider part 2
[sdnc/northbound.git] / vnfapi / provider / src / test / java / org / onap / sdnc / vnfapi / VnfApiProviderTest.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.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
31 import org.onap.sdnc.vnfapi.util.DataBrokerUtil;
32 import org.onap.sdnc.vnfapi.util.PropBuilder;
33 import org.onap.sdnc.vnfapi.util.VNFSDNSvcLogicServiceClientMockUtil;
34 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
35 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
36 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
37 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.VnfInstanceTopologyOperationInput;
42 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.VnfInstanceTopologyOperationOutput;
43 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.VnfInstanceTopologyOperationInputBuilder;
44 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.vnf.instance.request.information.VnfInstanceRequestInformation;
45 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.vnf.instance.request.information.VnfInstanceRequestInformationBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.vnf.instance.service.data.VnfInstanceServiceDataBuilder;
47
48 import java.util.concurrent.ExecutionException;
49 import java.util.concurrent.Future;
50
51
52 public class VnfApiProviderTest extends AbstractConcurrentDataBrokerTest {
53     private static final String INVALID_INPUT = "invalid input, null or empty vnf-instance-id";
54     private static final String NO_SERVICE_LOGIC = "No service logic active for VNF-API: \'vnf-instance-topology-operation\'";
55     private static final String VIID = "viid";
56     private static final String PRELOAD_NAME = "preloadName";
57     private static final String PRELOAD_TYPE = "preloadType";
58
59     protected VnfApiProvider vnfapiProvider;
60     protected DataBroker dataBroker;
61     protected @Mock NotificationPublishService mockNotificationPublishService;
62     protected @Mock RpcProviderRegistry mockRpcProviderRegistry;
63     protected @Mock VNFSDNSvcLogicServiceClient mockVNFSDNSvcLogicServiceClient;
64     protected static final Logger LOG = LoggerFactory.getLogger(VnfApiProvider.class);
65
66     protected DataBrokerUtil db;
67     protected VNFSDNSvcLogicServiceClientMockUtil svcClient;
68
69
70     @Before
71     public void setUp() throws Exception {
72         MockitoAnnotations.initMocks(this);
73         svcClient = new VNFSDNSvcLogicServiceClientMockUtil(mockVNFSDNSvcLogicServiceClient);
74         dataBroker = getDataBroker();
75         db = new DataBrokerUtil(dataBroker);
76          try {
77             vnfapiProvider = new VnfApiProvider(
78                     dataBroker,
79                     mockNotificationPublishService,
80                     mockRpcProviderRegistry,
81                     mockVNFSDNSvcLogicServiceClient
82             );
83         } catch (Exception e) {
84             LOG.error("Caught exception on setUp", e);
85             throw e;
86         }
87     }
88
89
90     public static PropBuilder prop(){
91         return (new PropBuilder());
92     }
93
94     @Test
95     public void vnfInstanceTopologyOperationInputIsNull() throws Exception {
96         VnfInstanceTopologyOperationInput input = null;
97         checkVnfInstanceTopologyOperation(input, "403", INVALID_INPUT);
98     }
99
100
101     @Test
102     public void vnfInstanceTopologyOperationInput_VnfInstanceRequestInformationIsNull() throws Exception {
103         VnfInstanceTopologyOperationInputBuilder builder = new VnfInstanceTopologyOperationInputBuilder();
104         builder.setVnfInstanceRequestInformation(null);
105         VnfInstanceTopologyOperationInput input = builder.build();
106
107         checkVnfInstanceTopologyOperation(input, "403", INVALID_INPUT);
108     }
109
110     @Test
111     public void vnfInstanceTopologyOperationInput_getVnfInstanceRequestInformationVnfInstanceIdIsNull() throws Exception {
112         VnfInstanceTopologyOperationInputBuilder builder = new VnfInstanceTopologyOperationInputBuilder();
113         builder.setVnfInstanceRequestInformation(createVnfInstanceRequestInformation(null));
114         VnfInstanceTopologyOperationInput input = builder.build();
115
116         checkVnfInstanceTopologyOperation(input, "403", INVALID_INPUT);
117     }
118
119     @Test
120     public void vnfInstanceTopologyOperationInput_VnfInstanceRequestInformationVnfInstanceIdIsZero() throws Exception {
121         VnfInstanceTopologyOperationInputBuilder builder = new VnfInstanceTopologyOperationInputBuilder();
122         builder.setVnfInstanceRequestInformation(createVnfInstanceRequestInformation(""));
123         VnfInstanceTopologyOperationInput input = builder.build();
124
125         checkVnfInstanceTopologyOperation(input, "403", INVALID_INPUT);
126     }
127
128     @Test
129     public void vnfInstanceTopologyOperationInput_svcLogicClientHasGrapheReturnFalse() throws Exception {
130         VnfInstanceTopologyOperationInputBuilder builder = new VnfInstanceTopologyOperationInputBuilder();
131         builder.setVnfInstanceRequestInformation(createVnfInstanceRequestInformation(VIID));
132         VnfInstanceTopologyOperationInput input = builder.build();
133
134         Mockito.when(mockVNFSDNSvcLogicServiceClient
135                 .hasGraph(Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any()))
136                 .thenReturn(false);
137
138         checkVnfInstanceTopologyOperation(input, "503", NO_SERVICE_LOGIC);
139     }
140
141
142     @Test
143     public void vnfInstanceTopologyOperationInput_svcLogicClientExecuteThrowsSvcLogicException() throws Exception {
144         VnfInstanceTopologyOperationInputBuilder builder = new VnfInstanceTopologyOperationInputBuilder();
145         builder.setVnfInstanceRequestInformation(createVnfInstanceRequestInformation(VIID));
146         VnfInstanceTopologyOperationInput input = builder.build();
147
148         Mockito.when(mockVNFSDNSvcLogicServiceClient
149                     .hasGraph(Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any()))
150                 .thenReturn(true);
151         setMockVNFSDNSvcLogicServiceClientToThrowException(SvcLogicException.class);
152         checkVnfInstanceTopologyOperation(input, "500", null);
153     }
154
155     @Test
156     public void vnfInstanceTopologyOperationInput_svcLogicClientExecuteThrowsException() throws Exception {
157         VnfInstanceTopologyOperationInputBuilder builder = new VnfInstanceTopologyOperationInputBuilder();
158         builder.setVnfInstanceRequestInformation(createVnfInstanceRequestInformation(VIID));
159         VnfInstanceTopologyOperationInput input = builder.build();
160
161         Mockito.when(mockVNFSDNSvcLogicServiceClient
162                 .hasGraph(Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any()))
163                 .thenReturn(true);
164         setMockVNFSDNSvcLogicServiceClientToThrowException(Exception.class);
165         checkVnfInstanceTopologyOperation(input, "500", null);
166     }
167
168     private void setMockVNFSDNSvcLogicServiceClientToThrowException(Class exceptionClass) throws Exception {
169         Mockito.when(mockVNFSDNSvcLogicServiceClient
170                 .execute(Mockito.any(),
171                         Mockito.any(),
172                         Mockito.any(),
173                         Mockito.any(),
174                         Mockito.any(VnfInstanceServiceDataBuilder.class),
175                         Mockito.any()))
176                 .thenThrow(exceptionClass.asSubclass(Throwable.class));
177     }
178     private VnfInstanceRequestInformation createVnfInstanceRequestInformation(String vnfInstanceId) {
179         return new VnfInstanceRequestInformationBuilder()
180                 .setVnfInstanceId(vnfInstanceId)
181                 .setVnfInstanceName(PRELOAD_NAME)
182                 .setVnfModelId(PRELOAD_TYPE)
183                 .build();
184     }
185
186     private void checkVnfInstanceTopologyOperation(VnfInstanceTopologyOperationInput input,
187         String expectedResponseCode, String expectedResponseMessage) throws ExecutionException, InterruptedException {
188
189         VnfInstanceTopologyOperationOutput output = executeVnfInstanceTopologyOperation(input);
190         checkVnfInstanceTopologyOperationOutput(output, expectedResponseCode, expectedResponseMessage);
191     }
192
193     private VnfInstanceTopologyOperationOutput executeVnfInstanceTopologyOperation(
194             VnfInstanceTopologyOperationInput input) throws ExecutionException, InterruptedException {
195         return vnfapiProvider
196                 .vnfInstanceTopologyOperation(input)
197                 .get()
198                 .getResult();
199     }
200
201     private void checkVnfInstanceTopologyOperationOutput(VnfInstanceTopologyOperationOutput result,
202             String expectedResponseCode, String expectedResponseMessage) {
203
204         String expectedAckFinalIndicator = "Y";
205
206         Assert.assertEquals(result.getResponseCode(), expectedResponseCode );
207         Assert.assertEquals(result.getResponseMessage(), expectedResponseMessage);
208         Assert.assertEquals(result.getAckFinalIndicator(), expectedAckFinalIndicator);
209     }
210  }