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