Upgrade to Service Path 1.7
[transportpce.git] / servicehandler / src / test / java / org / opendaylight / transportpce / servicehandler / service / PCEServiceWrapperTest.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.service;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.verifyZeroInteractions;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.InjectMocks;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
24 import org.opendaylight.transportpce.common.ResponseCodes;
25 import org.opendaylight.transportpce.pce.service.PathComputationService;
26 import org.opendaylight.transportpce.servicehandler.utils.ServiceDataUtils;
27 import org.opendaylight.transportpce.test.AbstractTest;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveInput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveOutput;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.CancelResourceReserveOutputBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestInput;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutput;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutputBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ServiceNotificationTypes;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommon;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommonBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceCreateInput;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceCreateInputBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.TempServiceCreateInput;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.TempServiceCreateInputBuilder;
41
42 public class PCEServiceWrapperTest extends AbstractTest {
43
44     @Mock
45     private PathComputationService pathComputationServiceMock;
46     @Mock
47     private NotificationPublishService notificationPublishServiceMock;
48     @InjectMocks
49     private PCEServiceWrapper pceServiceWrapperMock;
50
51     @Before
52     public void init() throws NoSuchMethodException {
53         MockitoAnnotations.initMocks(this);
54     }
55
56     @Test
57     public void performPCENullSdncRequestHeader() {
58         ServiceCreateInput input =  ServiceDataUtils.buildServiceCreateInput();
59         input = new ServiceCreateInputBuilder(input).setSdncRequestHeader(null).build();
60         PathComputationRequestOutput pceResponse = this.pceServiceWrapperMock.performPCE(input, true);
61         Assert.assertEquals(ResponseCodes.FINAL_ACK_YES,
62                 pceResponse.getConfigurationResponseCommon().getAckFinalIndicator());
63         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
64                 pceResponse.getConfigurationResponseCommon().getResponseCode());
65         verifyZeroInteractions(this.pathComputationServiceMock);
66     }
67
68     @Test
69     public void performPCENullServiceName() {
70         ServiceCreateInput input = ServiceDataUtils.buildServiceCreateInput();
71         input = new ServiceCreateInputBuilder(input).setServiceName(null).build();
72         PathComputationRequestOutput pceResponse = this.pceServiceWrapperMock.performPCE(input, true);
73         Assert.assertEquals(ResponseCodes.FINAL_ACK_YES,
74                 pceResponse.getConfigurationResponseCommon().getAckFinalIndicator());
75         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
76                 pceResponse.getConfigurationResponseCommon().getResponseCode());
77         verifyZeroInteractions(this.pathComputationServiceMock);
78     }
79
80     @Test
81     public void performPCENullCommonId() {
82         TempServiceCreateInput input = ServiceDataUtils.buildTempServiceCreateInput();
83         input = new TempServiceCreateInputBuilder(input).setCommonId(null).build();
84         PathComputationRequestOutput pceResponse = this.pceServiceWrapperMock.performPCE(input, true);
85         Assert.assertEquals(ResponseCodes.FINAL_ACK_YES,
86                 pceResponse.getConfigurationResponseCommon().getAckFinalIndicator());
87         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
88                 pceResponse.getConfigurationResponseCommon().getResponseCode());
89         verifyZeroInteractions(this.pathComputationServiceMock);
90     }
91
92
93     @Test
94     public void cancelPCEResourceNullServiceName() {
95         CancelResourceReserveOutput pceResponse =
96                 this.pceServiceWrapperMock.cancelPCEResource(null, ServiceNotificationTypes.ServiceDeleteResult);
97         Assert.assertEquals(ResponseCodes.FINAL_ACK_YES,
98                 pceResponse.getConfigurationResponseCommon().getAckFinalIndicator());
99         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
100                 pceResponse.getConfigurationResponseCommon().getResponseCode());
101         verifyZeroInteractions(this.pathComputationServiceMock);
102     }
103
104     @Test
105     public void cancelPCEResourceValid() {
106         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
107                 .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
108                 .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
109         CancelResourceReserveOutput output = new CancelResourceReserveOutputBuilder()
110                 .setConfigurationResponseCommon(configurationResponseCommon).build();
111         ListenableFuture<CancelResourceReserveOutput> response = ServiceDataUtils.returnFuture(output);
112         Mockito.when(this.pathComputationServiceMock.cancelResourceReserve(any(CancelResourceReserveInput.class)))
113                 .thenReturn(response);
114         CancelResourceReserveOutput pceResponse =
115                 this.pceServiceWrapperMock.cancelPCEResource("service 1", ServiceNotificationTypes.ServiceDeleteResult);
116         Assert.assertEquals(ResponseCodes.FINAL_ACK_NO,
117                 pceResponse.getConfigurationResponseCommon().getAckFinalIndicator());
118         Assert.assertEquals(ResponseCodes.RESPONSE_OK,
119                 pceResponse.getConfigurationResponseCommon().getResponseCode());
120         Assert.assertEquals("PCE calculation in progress",
121                 pceResponse.getConfigurationResponseCommon().getResponseMessage());
122         verify(this.pathComputationServiceMock).cancelResourceReserve(any(CancelResourceReserveInput.class));
123     }
124
125     @Test
126     public void performPCEValid() {
127         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
128                 .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
129                 .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
130         PathComputationRequestOutput output = new PathComputationRequestOutputBuilder()
131                 .setConfigurationResponseCommon(configurationResponseCommon).build();
132         ListenableFuture<PathComputationRequestOutput> response = ServiceDataUtils.returnFuture(output);
133         Mockito.when(this.pathComputationServiceMock.pathComputationRequest(any(PathComputationRequestInput.class)))
134                 .thenReturn(response);
135         ServiceCreateInput input =  ServiceDataUtils.buildServiceCreateInput();
136         PathComputationRequestOutput pceResponse = this.pceServiceWrapperMock.performPCE(input, true);
137         Assert.assertEquals(ResponseCodes.FINAL_ACK_NO,
138                 pceResponse.getConfigurationResponseCommon().getAckFinalIndicator());
139         Assert.assertEquals(ResponseCodes.RESPONSE_OK,
140                 pceResponse.getConfigurationResponseCommon().getResponseCode());
141         Assert.assertEquals("PCE calculation in progress",
142                 pceResponse.getConfigurationResponseCommon().getResponseMessage());
143         verify(this.pathComputationServiceMock).pathComputationRequest((any(PathComputationRequestInput.class)));
144     }
145
146     @Test
147     public void performPCETempValid() {
148         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
149                 .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
150                 .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
151         PathComputationRequestOutput output = new PathComputationRequestOutputBuilder()
152                 .setConfigurationResponseCommon(configurationResponseCommon).build();
153         ListenableFuture<PathComputationRequestOutput> response = ServiceDataUtils.returnFuture(output);
154         Mockito.when(this.pathComputationServiceMock.pathComputationRequest(any(PathComputationRequestInput.class)))
155                 .thenReturn(response);
156         TempServiceCreateInput input = ServiceDataUtils.buildTempServiceCreateInput();
157         PathComputationRequestOutput pceResponse = this.pceServiceWrapperMock.performPCE(input, true);
158         Assert.assertEquals(ResponseCodes.FINAL_ACK_NO,
159                 pceResponse.getConfigurationResponseCommon().getAckFinalIndicator());
160         Assert.assertEquals(ResponseCodes.RESPONSE_OK, pceResponse.getConfigurationResponseCommon().getResponseCode());
161         Assert.assertEquals("PCE calculation in progress",
162                 pceResponse.getConfigurationResponseCommon().getResponseMessage());
163         verify(this.pathComputationServiceMock).pathComputationRequest((any(PathComputationRequestInput.class)));
164     }
165 }