2 * Copyright 2017 Gregory Moyer
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openhab.binding.sleepiq.api.model;
18 import static org.junit.Assert.assertEquals;
20 import java.time.Duration;
22 import org.junit.Test;
23 import org.openhab.binding.sleepiq.api.model.TimeSince;
25 public class TimeSinceTest
28 public void testWithDuration()
30 assertEquals(new TimeSince().withDuration(0, 0, 0, 0).getDuration(),
31 new TimeSince().withDuration(Duration.parse("PT00H00M00S")).getDuration());
32 assertEquals(new TimeSince().withDuration(0, 2, 3, 4).getDuration(),
33 new TimeSince().withDuration(Duration.parse("PT02H03M04S")).getDuration());
34 assertEquals(new TimeSince().withDuration(0, 12, 34, 56).getDuration(),
35 new TimeSince().withDuration(Duration.parse("PT12H34M56S")).getDuration());
36 assertEquals(new TimeSince().withDuration(1, 2, 3, 4).getDuration(),
37 new TimeSince().withDuration(Duration.parse("P1DT02H03M04S")).getDuration());
38 assertEquals(new TimeSince().withDuration(12, 23, 34, 45).getDuration(),
39 new TimeSince().withDuration(Duration.parse("P12DT23H34M45S")).getDuration());
43 public void testToString()
45 assertEquals("00:00:00",
46 new TimeSince().withDuration(Duration.parse("PT00H00M00S")).toString());
47 assertEquals("02:03:04",
48 new TimeSince().withDuration(Duration.parse("PT02H03M04S")).toString());
49 assertEquals("12:34:56",
50 new TimeSince().withDuration(Duration.parse("PT12H34M56S")).toString());
51 assertEquals("1 d 02:03:04",
52 new TimeSince().withDuration(Duration.parse("P1DT02H03M04S")).toString());
53 assertEquals("12 d 23:34:45",
54 new TimeSince().withDuration(Duration.parse("P12DT23H34M45S")).toString());
58 public void testParse()
60 assertEquals(Duration.parse("PT00H00M00S"), TimeSince.parse("00:00:00").getDuration());
61 assertEquals(Duration.parse("PT2H3M4S"), TimeSince.parse("02:03:04").getDuration());
62 assertEquals(Duration.parse("PT12H34M56S"), TimeSince.parse("12:34:56").getDuration());
63 assertEquals(Duration.parse("P1DT2H3M4S"), TimeSince.parse("1 d 02:03:04").getDuration());
64 assertEquals(Duration.parse("P12DT23H34M45S"),
65 TimeSince.parse("12 d 23:34:45").getDuration());