]> git.basschouten.com Git - openhab-addons.git/blob
7f4552e2f2b3014bd2531ac5a37fd46f2078d5dc
[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         assertThat(Converter.fromRouterosTime("2021-02-13 23:59:59"),
41                 is(equalTo(LocalDateTime.of(2021, 2, 13, 23, 59, 59, 0))));
42         assertThat(Converter.fromRouterosTime("2020-12-11 20:45:40"),
43                 is(equalTo(LocalDateTime.of(2020, 12, 11, 20, 45, 40, 0))));
44         assertThat(Converter.fromRouterosTime("2021-01-07 09:14:11"),
45                 is(equalTo(LocalDateTime.of(2021, 1, 7, 9, 14, 11, 0))));
46
47         assertNull(Converter.fromRouterosTime(null));
48         assertNull(Converter.fromRouterosTime(""));
49         assertNull(Converter.fromRouterosTime("2021-18-07 09:14:11"));
50         assertNull(Converter.fromRouterosTime("feb/41/2021 23:59:59"));
51     }
52
53     @Test
54     public void testFromRouterosPeriod() {
55         LocalDateTime fromDateTime = LocalDateTime.of(2021, 2, 1, 0, 0, 0, 0);
56
57         assertThat(Converter.routerosPeriodBack("1y3w4d5h6m7s11ms", fromDateTime),
58                 is(equalTo(LocalDateTime.parse("2020-01-06T18:53:53.011"))));
59
60         assertNull(Converter.routerosPeriodBack(null));
61
62         /*
63          * uptime = 6w6h31m31s
64          * uptime = 3d7h6m43s710ms
65          * uptime = 16h39m58s220ms
66          * uptime = 1h38m53s110ms
67          * uptime = 53m53s950ms
68          */
69
70         assertThat(Converter.routerosPeriodBack("6w6h31m31s", fromDateTime),
71                 is(equalTo(LocalDateTime.parse("2020-12-20T17:28:29"))));
72
73         assertThat(Converter.routerosPeriodBack("3d7h6m43s710ms", fromDateTime),
74                 is(equalTo(LocalDateTime.parse("2021-01-28T16:53:17.710"))));
75
76         assertThat(Converter.routerosPeriodBack("16h39m58s220ms", fromDateTime),
77                 is(equalTo(LocalDateTime.parse("2021-01-31T07:20:02.220"))));
78
79         assertThat(Converter.routerosPeriodBack("1h38m53s110ms", fromDateTime),
80                 is(equalTo(LocalDateTime.parse("2021-01-31T22:21:07.110"))));
81
82         assertThat(Converter.routerosPeriodBack("53m53s950ms", fromDateTime),
83                 is(equalTo(LocalDateTime.parse("2021-01-31T23:06:07.950"))));
84     }
85 }