reintroduce SP 1.6 in SH
[transportpce.git] / servicehandler / src / test / java / org / opendaylight / transportpce / servicehandler / listeners / PceListenerImplTest.java
1 /*
2  * Copyright © 2018 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.servicehandler.listeners;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.times;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.verifyNoMoreInteractions;
14 import static org.mockito.Mockito.verifyZeroInteractions;
15
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.InjectMocks;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.transportpce.common.OperationResult;
23 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
24 import org.opendaylight.transportpce.servicehandler.ServiceInput;
25 import org.opendaylight.transportpce.servicehandler.service.PCEServiceWrapper;
26 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
27 import org.opendaylight.transportpce.servicehandler.utils.InjectField;
28 import org.opendaylight.transportpce.servicehandler.utils.ServiceDataUtils;
29 import org.opendaylight.transportpce.test.AbstractTest;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestOutput;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.ServicePathRpcResult;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.ServicePathRpcResultBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceImplementationRequestInput;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceCreateInput;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev171016.RpcStatusEx;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev171016.ServicePathNotificationTypes;
38
39
40 public class PceListenerImplTest extends AbstractTest {
41
42     @Mock
43     private ServiceDataStoreOperations serviceDataStoreOperationsMock;
44     @Mock
45     private RendererServiceOperations rendererServiceOperationsMock;
46     @Mock
47     private PCEServiceWrapper pceServiceWrapperMock;
48     @Mock
49     private ServiceInput serviceInputMock;
50     @InjectMocks
51     private PceListenerImpl pceListenerImplMock;
52
53
54     @Before
55     public void init() {
56         MockitoAnnotations.initMocks(this);
57     }
58
59     @Test
60     public void onServicePathRpcResultServiceInputIsNull() {
61         InjectField.inject(this.pceListenerImplMock, "serviceReconfigure", false);
62         InjectField.inject(this.pceListenerImplMock, "tempService", false);
63         InjectField.inject(this.pceListenerImplMock, "input", null);
64         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult(
65                 ServicePathNotificationTypes.PathComputationRequest, "service 1", RpcStatusEx.Successful, "", true);
66         this.pceListenerImplMock.onServicePathRpcResult(notification);
67         verifyNoMoreInteractions(this.serviceDataStoreOperationsMock);
68         verifyNoMoreInteractions(this.rendererServiceOperationsMock);
69     }
70
71     @Test
72     public void onServicePathRpcResultPathDescriptionIsNull() {
73         InjectField.inject(this.pceListenerImplMock, "serviceReconfigure", false);
74         InjectField.inject(this.pceListenerImplMock, "tempService", false);
75         InjectField.inject(this.pceListenerImplMock, "input", null);
76         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult(
77                 ServicePathNotificationTypes.PathComputationRequest, "service 1", RpcStatusEx.Successful, "", false);
78         this.pceListenerImplMock.onServicePathRpcResult(notification);
79         verifyNoMoreInteractions(this.serviceDataStoreOperationsMock);
80         verifyNoMoreInteractions(this.rendererServiceOperationsMock);
81     }
82
83     @Test
84     public void onServicePathRpcResultPCRSuccess() {
85         ServiceCreateInput serviceCreateInput = ServiceDataUtils.buildServiceCreateInput();
86         Mockito.when(this.serviceInputMock.getServiceCreateInput()).thenReturn(serviceCreateInput);
87         Mockito.when(this.serviceDataStoreOperationsMock.createService(any(ServiceCreateInput.class)))
88                 .thenReturn(OperationResult.ok("Successful"));
89         Mockito.when(this.serviceDataStoreOperationsMock.createServicePath(any(ServiceInput.class),
90                 any(PathComputationRequestOutput.class))).thenReturn(OperationResult.ok("Successful"));
91         Mockito.when(this.serviceInputMock.getServiceAEnd()).thenReturn(serviceCreateInput.getServiceAEnd());
92         Mockito.when(this.serviceInputMock.getServiceZEnd()).thenReturn(serviceCreateInput.getServiceZEnd());
93         Mockito.when(this.serviceInputMock.getSdncRequestHeader())
94                 .thenReturn(serviceCreateInput.getSdncRequestHeader());
95         InjectField.inject(this.pceListenerImplMock, "serviceReconfigure", false);
96         InjectField.inject(this.pceListenerImplMock, "tempService", false);
97         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult(
98                 ServicePathNotificationTypes.PathComputationRequest, "service 1", RpcStatusEx.Successful, "", true);
99         this.pceListenerImplMock.onServicePathRpcResult(notification);
100         verify(this.serviceDataStoreOperationsMock).createService(any(ServiceCreateInput.class));
101         verify(this.serviceDataStoreOperationsMock).createServicePath(any(ServiceInput.class),
102                 any(PathComputationRequestOutput.class));
103         verify(this.rendererServiceOperationsMock).serviceImplementation(any(ServiceImplementationRequestInput.class));
104     }
105
106     @Test
107     public void onServicePathRpcResultTempPCRSuccess() {
108         TempServiceCreateInput tempServiceCreateInput = ServiceDataUtils.buildTempServiceCreateInput();
109         Mockito.when(this.serviceInputMock.getTempServiceCreateInput()).thenReturn(tempServiceCreateInput);
110         Mockito.when(this.serviceDataStoreOperationsMock.createTempService(any(TempServiceCreateInput.class)))
111                 .thenReturn(OperationResult.ok("Successful"));
112         Mockito.when(this.serviceDataStoreOperationsMock.createServicePath(any(ServiceInput.class),
113                 any(PathComputationRequestOutput.class))).thenReturn(OperationResult.ok("Successful"));
114         Mockito.when(this.serviceInputMock.getServiceAEnd()).thenReturn(tempServiceCreateInput.getServiceAEnd());
115         Mockito.when(this.serviceInputMock.getServiceZEnd()).thenReturn(tempServiceCreateInput.getServiceZEnd());
116         Mockito.when(this.serviceInputMock.getSdncRequestHeader())
117                 .thenReturn(tempServiceCreateInput.getSdncRequestHeader());
118         InjectField.inject(this.pceListenerImplMock, "serviceReconfigure", false);
119         InjectField.inject(this.pceListenerImplMock, "tempService", true);
120         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult(
121                 ServicePathNotificationTypes.PathComputationRequest, "service 1", RpcStatusEx.Successful, "", true);
122         this.pceListenerImplMock.onServicePathRpcResult(notification);
123         verify(this.serviceDataStoreOperationsMock).createTempService(any(TempServiceCreateInput.class));
124         verify(this.serviceDataStoreOperationsMock).createServicePath(any(ServiceInput.class),
125                 any(PathComputationRequestOutput.class));
126         verify(this.rendererServiceOperationsMock).serviceImplementation(any(ServiceImplementationRequestInput.class));
127     }
128
129     @Test
130     public void onServicePathRpcResultPCRSuccessFeasabilityCheck() {
131         InjectField.inject(this.pceListenerImplMock, "serviceReconfigure", false);
132         InjectField.inject(this.pceListenerImplMock, "serviceFeasiblity", true);
133         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult(
134                 ServicePathNotificationTypes.PathComputationRequest, "service 1", RpcStatusEx.Successful, "", true);
135         this.pceListenerImplMock.onServicePathRpcResult(notification);
136         verifyZeroInteractions(this.serviceDataStoreOperationsMock);
137         verifyZeroInteractions(this.rendererServiceOperationsMock);
138     }
139
140     @Test
141     public void onServicePathRpcResultCRRSuccessWithNoReconfigure() {
142         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult(
143                 ServicePathNotificationTypes.CancelResourceReserve, "service 1", RpcStatusEx.Successful, "", false);
144         Mockito.when(this.serviceInputMock.getServiceName()).thenReturn(notification.getServiceName());
145         Mockito.when(this.serviceDataStoreOperationsMock.deleteServicePath(any(String.class)))
146                 .thenReturn(OperationResult.ok("Successful"));
147         Mockito.when(this.serviceDataStoreOperationsMock.deleteService(any(String.class)))
148                 .thenReturn(OperationResult.ok("Successful"));
149         InjectField.inject(this.pceListenerImplMock, "serviceReconfigure", false);
150         InjectField.inject(this.pceListenerImplMock, "tempService", false);
151         this.pceListenerImplMock.onServicePathRpcResult(notification);
152         verify(this.serviceDataStoreOperationsMock).deleteService(any(String.class));
153         verify(this.serviceDataStoreOperationsMock).deleteServicePath(any(String.class));
154     }
155
156     @Test
157     public void onServicePathRpcResultCRRSuccessWithReconfigure() {
158         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult(
159                 ServicePathNotificationTypes.CancelResourceReserve, "service 1", RpcStatusEx.Successful, "", false);
160         Mockito.when(this.serviceInputMock.getServiceName()).thenReturn(notification.getServiceName());
161         Mockito.when(this.serviceDataStoreOperationsMock.deleteServicePath(any(String.class)))
162                 .thenReturn(OperationResult.ok("Successful"));
163         Mockito.when(this.serviceDataStoreOperationsMock.deleteService(any(String.class)))
164                 .thenReturn(OperationResult.ok("Successful"));
165         InjectField.inject(this.pceListenerImplMock, "serviceReconfigure", true);
166         InjectField.inject(this.pceListenerImplMock, "tempService", false);
167         ServiceCreateInput serviceCreateInput = ServiceDataUtils.buildServiceCreateInput();
168         Mockito.when(this.serviceInputMock.getServiceCreateInput()).thenReturn(serviceCreateInput);
169         this.pceListenerImplMock.onServicePathRpcResult(notification);
170         verify(this.serviceDataStoreOperationsMock).deleteService(any(String.class));
171         verify(this.serviceDataStoreOperationsMock).deleteServicePath(any(String.class));
172         verify(this.pceServiceWrapperMock).performPCE(serviceCreateInput, true);
173     }
174
175     @Test
176     public void onServicePathRpcResultCRRTempSuccess() {
177         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult(
178                 ServicePathNotificationTypes.CancelResourceReserve, "service 1", RpcStatusEx.Successful, "", false);
179         Mockito.when(this.serviceInputMock.getServiceName()).thenReturn(notification.getServiceName());
180         Mockito.when(this.serviceDataStoreOperationsMock.deleteServicePath(any(String.class)))
181                 .thenReturn(OperationResult.ok("Successful"));
182         Mockito.when(this.serviceDataStoreOperationsMock.deleteTempService(any(String.class)))
183                 .thenReturn(OperationResult.ok("Successful"));
184         InjectField.inject(this.pceListenerImplMock, "serviceReconfigure", false);
185         InjectField.inject(this.pceListenerImplMock, "tempService", true);
186         this.pceListenerImplMock.onServicePathRpcResult(notification);
187         verify(this.serviceDataStoreOperationsMock).deleteTempService(any(String.class));
188         verify(this.serviceDataStoreOperationsMock).deleteServicePath(any(String.class));
189     }
190
191     @Test
192     public void onServicePathRpcResultCRRFailed() {
193         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult(
194                 ServicePathNotificationTypes.CancelResourceReserve, "service 1", RpcStatusEx.Failed, "", false);
195         InjectField.inject(this.pceListenerImplMock, "serviceReconfigure", false);
196         InjectField.inject(this.pceListenerImplMock, "tempService", false);
197         this.pceListenerImplMock.onServicePathRpcResult(notification);
198         verifyZeroInteractions(this.serviceDataStoreOperationsMock);
199         verifyZeroInteractions(this.pceServiceWrapperMock);
200     }
201
202     @Test
203     public void onServicePathRpcResultCRRTempFailed() {
204         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult(
205                 ServicePathNotificationTypes.CancelResourceReserve, "service 1", RpcStatusEx.Failed, "", false);
206         InjectField.inject(this.pceListenerImplMock, "serviceReconfigure", false);
207         InjectField.inject(this.pceListenerImplMock, "tempService", true);
208         this.pceListenerImplMock.onServicePathRpcResult(notification);
209         verifyZeroInteractions(this.serviceDataStoreOperationsMock);
210         verifyZeroInteractions(this.pceServiceWrapperMock);
211     }
212
213     @Test
214     public void onServicePathRpcResultRepeat() {
215         ServiceCreateInput serviceCreateInput = ServiceDataUtils.buildServiceCreateInput();
216         Mockito.when(this.serviceInputMock.getServiceCreateInput()).thenReturn(serviceCreateInput);
217         Mockito.when(this.serviceDataStoreOperationsMock.createService(any(ServiceCreateInput.class)))
218                 .thenReturn(OperationResult.ok("Successful"));
219         Mockito.when(this.serviceDataStoreOperationsMock.createServicePath(any(ServiceInput.class),
220                 any(PathComputationRequestOutput.class))).thenReturn(OperationResult.ok("Successful"));
221         Mockito.when(this.serviceInputMock.getServiceAEnd()).thenReturn(serviceCreateInput.getServiceAEnd());
222         Mockito.when(this.serviceInputMock.getServiceZEnd()).thenReturn(serviceCreateInput.getServiceZEnd());
223         Mockito.when(this.serviceInputMock.getSdncRequestHeader())
224                 .thenReturn(serviceCreateInput.getSdncRequestHeader());
225         this.pceListenerImplMock.onServicePathRpcResult(ServiceDataUtils.buildServicePathRpcResult());
226         verify(this.rendererServiceOperationsMock).serviceImplementation(any(ServiceImplementationRequestInput.class));
227         this.pceListenerImplMock.onServicePathRpcResult(ServiceDataUtils.buildServicePathRpcResult());
228         verifyNoMoreInteractions(this.rendererServiceOperationsMock);
229     }
230
231     @Test
232     public void onServicePathRpcResultFailed() {
233         this.pceListenerImplMock.onServicePathRpcResult(ServiceDataUtils.buildFailedPceServicePathRpcResult());
234         verifyZeroInteractions(this.rendererServiceOperationsMock);
235     }
236
237     @Test
238     public void onServicePathRpcResultRepeatFailedCompareCase1() {
239         ServiceCreateInput serviceCreateInput = ServiceDataUtils.buildServiceCreateInput();
240         Mockito.when(this.serviceInputMock.getServiceCreateInput()).thenReturn(serviceCreateInput);
241         Mockito.when(this.serviceDataStoreOperationsMock.createService(any(ServiceCreateInput.class)))
242                 .thenReturn(OperationResult.ok("Successful"));
243         Mockito.when(this.serviceDataStoreOperationsMock.createServicePath(any(ServiceInput.class),
244                 any(PathComputationRequestOutput.class))).thenReturn(OperationResult.ok("Successful"));
245         Mockito.when(this.serviceInputMock.getServiceAEnd()).thenReturn(serviceCreateInput.getServiceAEnd());
246         Mockito.when(this.serviceInputMock.getServiceZEnd()).thenReturn(serviceCreateInput.getServiceZEnd());
247         Mockito.when(this.serviceInputMock.getSdncRequestHeader())
248                 .thenReturn(serviceCreateInput.getSdncRequestHeader());
249         Mockito.when(this.serviceDataStoreOperationsMock.deleteServicePath(any(String.class)))
250                 .thenReturn(OperationResult.ok("Successful"));
251         Mockito.when(this.serviceDataStoreOperationsMock.deleteService(any(String.class)))
252                 .thenReturn(OperationResult.ok("Successful"));
253         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult();
254         this.pceListenerImplMock.onServicePathRpcResult(notification);
255         Mockito.when(this.serviceInputMock.getServiceName()).thenReturn(serviceCreateInput.getServiceName());
256         Mockito.when(this.serviceDataStoreOperationsMock.deleteServicePath(any(String.class)))
257                 .thenReturn(OperationResult.ok("Successful"));
258         Mockito.when(this.serviceDataStoreOperationsMock.deleteService(any(String.class)))
259                 .thenReturn(OperationResult.ok("Successful"));
260         ServicePathRpcResult notification2 = new ServicePathRpcResultBuilder(notification)
261                 .setNotificationType(ServicePathNotificationTypes.CancelResourceReserve).build();
262         this.pceListenerImplMock.onServicePathRpcResult(notification2);
263         verify(this.rendererServiceOperationsMock).serviceImplementation(any(ServiceImplementationRequestInput.class));
264     }
265
266     @Test
267     public void onServicePathRpcResultRepeatFailedCompareCase2() {
268         ServiceCreateInput serviceCreateInput = ServiceDataUtils.buildServiceCreateInput();
269         Mockito.when(this.serviceInputMock.getServiceCreateInput()).thenReturn(serviceCreateInput);
270         Mockito.when(this.serviceDataStoreOperationsMock.createService(any(ServiceCreateInput.class)))
271                 .thenReturn(OperationResult.ok("Successful"));
272         Mockito.when(this.serviceDataStoreOperationsMock.createServicePath(any(ServiceInput.class),
273                 any(PathComputationRequestOutput.class))).thenReturn(OperationResult.ok("Successful"));
274         Mockito.when(this.serviceInputMock.getServiceAEnd()).thenReturn(serviceCreateInput.getServiceAEnd());
275         Mockito.when(this.serviceInputMock.getServiceZEnd()).thenReturn(serviceCreateInput.getServiceZEnd());
276         Mockito.when(this.serviceInputMock.getSdncRequestHeader())
277                 .thenReturn(serviceCreateInput.getSdncRequestHeader());
278         Mockito.when(this.serviceDataStoreOperationsMock.deleteServicePath(any(String.class)))
279                 .thenReturn(OperationResult.ok("Successful"));
280         Mockito.when(this.serviceDataStoreOperationsMock.deleteService(any(String.class)))
281                 .thenReturn(OperationResult.ok("Successful"));
282         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult();
283         this.pceListenerImplMock.onServicePathRpcResult(notification);
284         Mockito.when(this.serviceInputMock.getServiceName()).thenReturn(serviceCreateInput.getServiceName());
285         Mockito.when(this.serviceDataStoreOperationsMock.deleteServicePath(any(String.class)))
286                 .thenReturn(OperationResult.ok("Successful"));
287         Mockito.when(this.serviceDataStoreOperationsMock.deleteService(any(String.class)))
288                 .thenReturn(OperationResult.ok("Successful"));
289         ServicePathRpcResult notification2 =
290                 new ServicePathRpcResultBuilder(notification).setServiceName("service 2").build();
291         this.pceListenerImplMock.onServicePathRpcResult(notification2);
292         verify(this.rendererServiceOperationsMock, times(2))
293                 .serviceImplementation(any(ServiceImplementationRequestInput.class));
294     }
295
296     @Test
297     public void onServicePathRpcResultRepeatFailedCompareCase3() {
298         ServiceCreateInput serviceCreateInput = ServiceDataUtils.buildServiceCreateInput();
299         Mockito.when(this.serviceInputMock.getServiceCreateInput()).thenReturn(serviceCreateInput);
300         Mockito.when(this.serviceDataStoreOperationsMock.createService(any(ServiceCreateInput.class)))
301                 .thenReturn(OperationResult.ok("Successful"));
302         Mockito.when(this.serviceDataStoreOperationsMock.createServicePath(any(ServiceInput.class),
303                 any(PathComputationRequestOutput.class))).thenReturn(OperationResult.ok("Successful"));
304         Mockito.when(this.serviceInputMock.getServiceAEnd()).thenReturn(serviceCreateInput.getServiceAEnd());
305         Mockito.when(this.serviceInputMock.getServiceZEnd()).thenReturn(serviceCreateInput.getServiceZEnd());
306         Mockito.when(this.serviceInputMock.getSdncRequestHeader())
307                 .thenReturn(serviceCreateInput.getSdncRequestHeader());
308         Mockito.when(this.serviceDataStoreOperationsMock.deleteServicePath(any(String.class)))
309                 .thenReturn(OperationResult.ok("Successful"));
310         Mockito.when(this.serviceDataStoreOperationsMock.deleteService(any(String.class)))
311                 .thenReturn(OperationResult.ok("Successful"));
312         ServicePathRpcResult notification = ServiceDataUtils.buildServicePathRpcResult();
313         this.pceListenerImplMock.onServicePathRpcResult(notification);
314         Mockito.when(this.serviceInputMock.getServiceName()).thenReturn(serviceCreateInput.getServiceName());
315         Mockito.when(this.serviceDataStoreOperationsMock.deleteServicePath(any(String.class)))
316                 .thenReturn(OperationResult.ok("Successful"));
317         Mockito.when(this.serviceDataStoreOperationsMock.deleteService(any(String.class)))
318                 .thenReturn(OperationResult.ok("Successful"));
319         ServicePathRpcResult notification2 =
320                 new ServicePathRpcResultBuilder(notification).setStatus(RpcStatusEx.Failed).build();
321         this.pceListenerImplMock.onServicePathRpcResult(notification2);
322         verify(this.rendererServiceOperationsMock, times(1))
323                 .serviceImplementation(any(ServiceImplementationRequestInput.class));
324     }
325 }