Migrate common module to JUnit5
[transportpce.git] / common / src / test / java / org / opendaylight / transportpce / common / fixedflex / GridUtilsTest.java
1 /*
2  * Copyright © 2020 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
9 package org.opendaylight.transportpce.common.fixedflex;
10
11
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
15
16 import java.math.BigDecimal;
17 import java.util.Arrays;
18 import java.util.Map;
19 import org.junit.jupiter.api.Test;
20 import org.opendaylight.transportpce.common.ServiceRateConstant;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathInput;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathInputBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev211210.FrequencyGHz;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev211210.FrequencyTHz;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.available.freq.map.AvailFreqMaps;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev211210.available.freq.map.AvailFreqMapsKey;
28 import org.opendaylight.yangtools.yang.common.Decimal64;
29 import org.opendaylight.yangtools.yang.common.Uint32;
30
31 public class GridUtilsTest {
32
33     @Test
34     void getWaveLengthIndexFromSpectrumAssigmentTest() {
35         assertEquals(15, GridUtils.getWaveLengthIndexFromSpectrumAssigment(647), "Wavelength index should be 15");
36     }
37
38     @Test
39     void getFrequencyFromIndexTest() {
40         BigDecimal[] expectedFrequencies = new BigDecimal[768];
41         BigDecimal frequency = BigDecimal.valueOf(191.325);
42         for (int i = 0; i < expectedFrequencies.length; i++) {
43             expectedFrequencies[i] = frequency;
44             frequency = frequency.add(BigDecimal.valueOf(0.00625));
45         }
46         assertEquals(0, expectedFrequencies[0].compareTo(GridUtils.getStartFrequencyFromIndex(0)),
47             "Frequency should be 191.325");
48         assertEquals(0, expectedFrequencies[284].compareTo(GridUtils.getStartFrequencyFromIndex(284)),
49             "Frequency should be 193.1");
50         assertEquals(0, expectedFrequencies[767].compareTo(GridUtils.getStartFrequencyFromIndex(767)),
51             "Frequency should be 196.1188");
52     }
53
54     @Test
55     void initFreqMaps4FixedGrid2AvailableTest() {
56         AvailFreqMapsKey key = new AvailFreqMapsKey(GridConstant.C_BAND);
57         byte[] byteArray = new byte[GridConstant.NB_OCTECTS];
58         Arrays.fill(byteArray, (byte) GridConstant.AVAILABLE_SLOT_VALUE);
59         Map<AvailFreqMapsKey, AvailFreqMaps> availFreqMaps = GridUtils.initFreqMaps4FixedGrid2Available();
60         assertEquals(1, availFreqMaps.size(), "Should contains 1 element");
61         assertTrue(availFreqMaps.containsKey(key), "should contains cband key");
62         assertTrue(Arrays.equals(byteArray, availFreqMaps.get(key).getFreqMap()), "Should have available freq map");
63     }
64
65     @Test
66     void getIndexFromFrequencyTest() {
67         assertEquals(693, GridUtils.getIndexFromFrequency(Decimal64.valueOf("195.65625")), "Index should be 693");
68         assertEquals(0, GridUtils.getIndexFromFrequency(Decimal64.valueOf("191.325")), "Index should be 0");
69         assertEquals(767, GridUtils.getIndexFromFrequency(Decimal64.valueOf("196.11875")), "Index should be 767");
70         assertEquals(8, GridUtils.getIndexFromFrequency(Decimal64.valueOf("191.375")), "Index should be 8");
71         assertEquals(15, GridUtils.getIndexFromFrequency(Decimal64.valueOf("191.41875")), "Index should be 15");
72         assertEquals(768, GridUtils.getIndexFromFrequency(Decimal64.valueOf("196.125")), "Index should be 768");
73     }
74
75     @Test
76     void getIndexFromFrequencyExceptionTest() {
77         IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
78             GridUtils.getIndexFromFrequency(Decimal64.valueOf("196.13125"));
79         });
80         assertEquals("Frequency not in range 196.13125", exception.getMessage());
81     }
82
83     @Test
84     void getIndexFromFrequencyException2Test() {
85         IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
86             GridUtils.getIndexFromFrequency(Decimal64.valueOf("191.31875"));
87         });
88         assertEquals("Frequency not in range 191.31875", exception.getMessage());
89     }
90
91     @Test
92     void getWidthFromRateAndModulationFormatTest() {
93         assertEquals(
94             new FrequencyGHz(Decimal64.valueOf(GridConstant.WIDTH_75)),
95             GridUtils.getWidthFromRateAndModulationFormat(ServiceRateConstant.RATE_400, ModulationFormat.DpQam16),
96             "Width should be 75");
97     }
98
99     @Test
100     void getWidthFromRateAndModulationFormatNotFoundTest() {
101         assertEquals(
102             new FrequencyGHz(Decimal64.valueOf(GridConstant.WIDTH_40)),
103             GridUtils.getWidthFromRateAndModulationFormat(ServiceRateConstant.RATE_100, ModulationFormat.DpQam16),
104             "As not found width should be 40");
105     }
106
107     @Test
108     void getCentralFrequencyTest() {
109         assertEquals(
110             new FrequencyTHz(Decimal64.valueOf(BigDecimal.valueOf(191.35).setScale(3))),
111             GridUtils.getCentralFrequency(BigDecimal.valueOf(191.325), BigDecimal.valueOf(191.375)),
112             "Central frequency should be 191.350");
113     }
114
115     @Test
116     void getCentralFrequencyWithPrecisionTest() {
117         assertEquals(
118             new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz(
119                     Decimal64.valueOf(BigDecimal.valueOf(191.35).setScale(4))),
120             GridUtils.getCentralFrequencyWithPrecision(BigDecimal.valueOf(191.325), BigDecimal.valueOf(191.375), 4),
121             "Central frequency should be 191.3500");
122     }
123
124     @Test
125     void getCentralFrequencyWithPrecisionAndRoundTest() {
126         assertEquals(
127             new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz(
128                     Decimal64.valueOf("191.3499")),
129             GridUtils.getCentralFrequencyWithPrecision(
130                     BigDecimal.valueOf(191.3244445), BigDecimal.valueOf(191.3754457788), 4),
131             "Central frequency should be 191.3499");
132     }
133
134     @Test
135     void initSpectrumInformationFromServicePathInputTest() {
136         ServicePathInput input = new ServicePathInputBuilder()
137                 .setWaveNumber(Uint32.valueOf(1))
138                 .setCenterFreq(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
139                         .FrequencyTHz(Decimal64.valueOf("196.1")))
140                 .setHigherSpectralSlotNumber(Uint32.valueOf(768))
141                 .setLowerSpectralSlotNumber(Uint32.valueOf(761))
142                 .setMaxFreq(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
143                         .FrequencyTHz(Decimal64.valueOf("196.125")))
144                 .setMinFreq(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
145                         .FrequencyTHz(Decimal64.valueOf("196.075")))
146                 .setNmcWidth(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
147                         .FrequencyGHz(Decimal64.valueOf(GridConstant.WIDTH_40)))
148                 .build();
149         SpectrumInformation spectrumInformation = GridUtils.initSpectrumInformationFromServicePathInput(input);
150         assertEquals(BigDecimal.valueOf(40), spectrumInformation.getWidth(), "Width should be 40");
151         assertEquals(Uint32.valueOf(1), spectrumInformation.getWaveLength(), "Wavelength should be 1");
152         assertEquals(BigDecimal.valueOf(196.1).setScale(4), spectrumInformation.getCenterFrequency(),
153             "Center freq should be 196.1");
154         assertEquals(761, spectrumInformation.getLowerSpectralSlotNumber(), "Lower slot number should be 761");
155         assertEquals(768, spectrumInformation.getHigherSpectralSlotNumber(), "Higher slot number should be 768");
156         assertEquals(BigDecimal.valueOf(196.075).setScale(4), spectrumInformation.getMinFrequency(),
157             "Min freq should be 196.075");
158         assertEquals(BigDecimal.valueOf(196.125).setScale(4), spectrumInformation.getMaxFrequency(),
159             "Max freq should be 196.125");
160     }
161
162     @Test
163     void initSpectrumInformationFromServicePathInputNoCenterFreqTest() {
164         ServicePathInput input = new ServicePathInputBuilder()
165                 .setWaveNumber(Uint32.valueOf(1))
166                 .setHigherSpectralSlotNumber(Uint32.valueOf(768))
167                 .setLowerSpectralSlotNumber(Uint32.valueOf(761))
168                 .setMaxFreq(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
169                         .FrequencyTHz(Decimal64.valueOf("196.125")))
170                 .setMinFreq(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
171                         .FrequencyTHz(Decimal64.valueOf("196.075")))
172                 .setNmcWidth(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
173                         .FrequencyGHz(Decimal64.valueOf(GridConstant.WIDTH_40)))
174                 .build();
175         SpectrumInformation spectrumInformation = GridUtils.initSpectrumInformationFromServicePathInput(input);
176         assertEquals(BigDecimal.valueOf(40), spectrumInformation.getWidth(), "Width should be 40");
177         assertEquals(Uint32.valueOf(1), spectrumInformation.getWaveLength(), "Wavelength should be 1");
178         assertEquals(BigDecimal.valueOf(196.1).setScale(4), spectrumInformation.getCenterFrequency(),
179             "Center freq should be 196.1");
180         assertEquals(761, spectrumInformation.getLowerSpectralSlotNumber(), "Lower slot number should be 761");
181         assertEquals(768, spectrumInformation.getHigherSpectralSlotNumber(), "Higher slot number should be 768");
182         assertEquals(BigDecimal.valueOf(196.075).setScale(4), spectrumInformation.getMinFrequency(),
183             "Min freq should be 196.075");
184         assertEquals(BigDecimal.valueOf(196.125).setScale(4), spectrumInformation.getMaxFrequency(),
185             "Max freq should be 196.125");
186     }
187
188     @Test
189     void initSpectrumInformationFromServicePathInputNoFreqTest() {
190         ServicePathInput input = new ServicePathInputBuilder()
191                 .setWaveNumber(Uint32.valueOf(1))
192                 .setHigherSpectralSlotNumber(Uint32.valueOf(768))
193                 .setLowerSpectralSlotNumber(Uint32.valueOf(761))
194                 .setNmcWidth(new org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019
195                         .FrequencyGHz(Decimal64.valueOf(GridConstant.WIDTH_40)))
196                 .build();
197         SpectrumInformation spectrumInformation = GridUtils.initSpectrumInformationFromServicePathInput(input);
198         assertEquals(BigDecimal.valueOf(40), spectrumInformation.getWidth(), "Width should be 40");
199         assertEquals(Uint32.valueOf(1), spectrumInformation.getWaveLength(), "Wavelength should be 1");
200         assertEquals(BigDecimal.valueOf(196.1).setScale(4), spectrumInformation.getCenterFrequency(),
201             "Center freq should be 196.1");
202         assertEquals(761, spectrumInformation.getLowerSpectralSlotNumber(), "Lower slot number should be 761");
203         assertEquals(768, spectrumInformation.getHigherSpectralSlotNumber(), "Higher slot number should be 768");
204         assertEquals(BigDecimal.valueOf(196.075).setScale(4), spectrumInformation.getMinFrequency(),
205             "Min freq should be 196.075");
206         assertEquals(BigDecimal.valueOf(196.125).setScale(4), spectrumInformation.getMaxFrequency(),
207             "Max freq should be 196.125");
208     }
209
210     @Test
211     void initSpectrumInformationFromServicePathInputNoSlotTest() {
212         IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
213             GridUtils.initSpectrumInformationFromServicePathInput(new ServicePathInputBuilder().build());
214         });
215         assertEquals("low and higher spectral slot numbers cannot be null", exception.getMessage());
216     }
217 }