]> git.basschouten.com Git - openhab-addons.git/blob
02d62d12c1fc6ae9ee2f95796f5f42615645be53
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mikrotik.internal.util;
14
15 import static org.hamcrest.CoreMatchers.equalTo;
16 import static org.hamcrest.CoreMatchers.is;
17 import static org.hamcrest.MatcherAssert.assertThat;
18 import static org.junit.jupiter.api.Assertions.assertNull;
19
20 import java.time.LocalDateTime;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24
25 /**
26  * @author Oleg Vivtash - Initial contribution
27  */
28 @NonNullByDefault
29 public class ConverterTest {
30
31     @Test
32     public void testFromRouterosTime() {
33         assertThat(Converter.fromRouterosTime("dec/11/2020 20:45:40"),
34                 is(equalTo(LocalDateTime.of(2020, 12, 11, 20, 45, 40, 0))));
35         assertThat(Converter.fromRouterosTime("jan/07/2021 09:14:11"),
36                 is(equalTo(LocalDateTime.of(2021, 1, 7, 9, 14, 11, 0))));
37         assertThat(Converter.fromRouterosTime("feb/13/2021 23:59:59"),
38                 is(equalTo(LocalDateTime.of(2021, 2, 13, 23, 59, 59, 0))));
39     }
40
41     @Test
42     public void testFromRouterosPeriod() {
43         LocalDateTime fromDateTime = LocalDateTime.of(2021, 2, 1, 0, 0, 0, 0);
44
45         assertThat(Converter.routerosPeriodBack("1y3w4d5h6m7s11ms", fromDateTime),
46                 is(equalTo(LocalDateTime.parse("2020-01-06T18:53:53.011"))));
47
48         assertNull(Converter.routerosPeriodBack(null));
49
50         /*
51          * uptime = 6w6h31m31s
52          * uptime = 3d7h6m43s710ms
53          * uptime = 16h39m58s220ms
54          * uptime = 1h38m53s110ms
55          * uptime = 53m53s950ms
56          */
57
58         assertThat(Converter.routerosPeriodBack("6w6h31m31s", fromDateTime),
59                 is(equalTo(LocalDateTime.parse("2020-12-20T17:28:29"))));
60
61         assertThat(Converter.routerosPeriodBack("3d7h6m43s710ms", fromDateTime),
62                 is(equalTo(LocalDateTime.parse("2021-01-28T16:53:17.710"))));
63
64         assertThat(Converter.routerosPeriodBack("16h39m58s220ms", fromDateTime),
65                 is(equalTo(LocalDateTime.parse("2021-01-31T07:20:02.220"))));
66
67         assertThat(Converter.routerosPeriodBack("1h38m53s110ms", fromDateTime),
68                 is(equalTo(LocalDateTime.parse("2021-01-31T22:21:07.110"))));
69
70         assertThat(Converter.routerosPeriodBack("53m53s950ms", fromDateTime),
71                 is(equalTo(LocalDateTime.parse("2021-01-31T23:06:07.950"))));
72     }
73 }