Merge "Reactive remaining Junits tests for Al migration"
[transportpce.git] / pce / src / test / java / org / opendaylight / transportpce / pce / service / PathComputationServiceImplTest.java
1 /*
2  * Copyright © 2020 Orange Labs, 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.pce.service;
9
10 import java.math.BigDecimal;
11 import java.util.Map;
12 import org.junit.After;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mockito;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.transportpce.common.network.NetworkTransactionImpl;
19 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
20 import org.opendaylight.transportpce.common.network.RequestProcessor;
21 import org.opendaylight.transportpce.pce.gnpy.GnpyResult;
22 import org.opendaylight.transportpce.pce.gnpy.GnpyTopoImpl;
23 import org.opendaylight.transportpce.pce.utils.PceTestData;
24 import org.opendaylight.transportpce.pce.utils.PceTestUtils;
25 import org.opendaylight.transportpce.pce.utils.TransactionUtils;
26 import org.opendaylight.transportpce.test.AbstractTest;
27 import org.opendaylight.transportpce.test.DataStoreContext;
28 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.PathBandwidth;
29 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.PathPropertiesBuilder;
30 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.path.properties.PathMetric;
31 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.path.properties.PathMetricBuilder;
32 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.no.path.info.NoPathBuilder;
33 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.Response;
34 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.ResponseBuilder;
35 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.response.response.type.NoPathCaseBuilder;
36 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.response.response.type.PathCaseBuilder;
37
38 public class PathComputationServiceImplTest extends AbstractTest {
39
40     private PathComputationServiceImpl pathComputationServiceImpl;
41     private static NetworkTransactionService networkTransactionService = null;
42     private static GnpyTopoImpl gnpyTopoImpl = null;
43     private static GnpyResult gnpyResult = null;
44     private DataStoreContext dataStoreContext = this.getDataStoreContextUtil();
45     private DataBroker dataBroker = this.getDataBroker();
46
47     @Before
48     public void setUp() {
49         networkTransactionService = Mockito.mock(NetworkTransactionService.class);
50         gnpyTopoImpl = Mockito.mock(GnpyTopoImpl.class);
51         gnpyResult = Mockito.mock(GnpyResult.class);
52         pathComputationServiceImpl = new PathComputationServiceImpl(
53                 networkTransactionService,
54                 this.getNotificationPublishService(), null);
55         pathComputationServiceImpl.init();
56     }
57
58     @Test
59     public void pathComputationRequestTest() {
60         pathComputationServiceImpl.generateGnpyResponse(null,"path");
61         Assert.assertNotNull(
62                 pathComputationServiceImpl.pathComputationRequest(PceTestData.getPCE_simpletopology_test1_request()));
63
64     }
65
66     @Test
67     public void testPathComputationRequestNoPath() {
68         Response response = new ResponseBuilder().setResponseType(new NoPathCaseBuilder()
69                 .setNoPath(new NoPathBuilder().setNoPath("no path").build()).build()).build();
70
71         pathComputationServiceImpl.generateGnpyResponse(response,"path");
72         Assert.assertNotNull(
73                 pathComputationServiceImpl.pathComputationRequest(PceTestData.getPCE_test3_request_54()));
74
75     }
76
77     @Test
78     public void testPathComputationRequestPathCase() {
79         PathMetric pathMetric = new PathMetricBuilder()
80                 .setAccumulativeValue(new BigDecimal(21))
81                 .setMetricType(PathBandwidth.class).build();
82         Response response = new ResponseBuilder().setResponseType(new PathCaseBuilder()
83                 .setPathProperties(new PathPropertiesBuilder().setPathMetric(Map.of(pathMetric.key(),pathMetric))
84                 .build()).build()).build();
85
86         pathComputationServiceImpl.generateGnpyResponse(response,"path");
87         Assert.assertNotNull(
88                 pathComputationServiceImpl.pathComputationRequest(PceTestData.getPCE_test3_request_54()));
89
90     }
91
92     @Test(expected = Exception.class)
93     public void generateGnpyResponse() throws Exception {
94         PceTestUtils.writeNetworkIntoDataStore(dataBroker, dataStoreContext, TransactionUtils.getNetworkForSpanLoss());
95         GnpyResult gnpyResult2 =
96                 new GnpyResult("A-to-Z",
97                         new GnpyTopoImpl(new NetworkTransactionImpl(
98                                 new RequestProcessor(dataBroker))), null);
99         pathComputationServiceImpl.generateGnpyResponse(gnpyResult2.getResponse(), "A-to-Z");
100     }
101
102
103     @After
104     public void destroy() {
105         pathComputationServiceImpl.close();
106     }
107 }