Rename opendaylight.mdsal.binding.runtime.spi
[yangtools.git] / common / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / RevisionTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.yangtools.yang.common;
9
10 import static org.junit.Assert.assertSame;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertFalse;
13 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
16
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.io.ObjectInputStream;
20 import java.io.ObjectOutputStream;
21 import java.time.format.DateTimeParseException;
22 import java.util.Optional;
23 import org.junit.jupiter.api.Test;
24
25 public class RevisionTest {
26     @Test
27     void testOf() {
28         assertEquals("2017-12-25", Revision.of("2017-12-25").toString());
29     }
30
31     @Test
32     void testOfNull() {
33         assertThrows(NullPointerException.class, () -> Revision.of(null));
34     }
35
36     @Test
37     void testOfEmpty() {
38         assertThrowsParse("", "Text '' could not be parsed at index 0");
39     }
40
41     @Test
42     void testOfInvalid() {
43         assertThrowsParse("invalid", "Text 'invalid' could not be parsed at index 0");
44     }
45
46     @Test
47     void testOfInvalidDate1() {
48         assertThrowsParse("2017-13-01",
49             "Text '2017-13-01' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13");
50     }
51
52     @Test
53     void testOfInvalidDate2() {
54         assertThrowsParse("2017-12-00",
55             "Text '2017-12-00' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 0");
56     }
57
58     @Test
59     void testOfInvalidDate3() {
60         assertThrowsParse("2017-12-32",
61             "Text '2017-12-32' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 32");
62     }
63
64     private static void assertThrowsParse(final String input, final String expectedMessage) {
65         final var ex = assertThrows(DateTimeParseException.class, () -> Revision.of(input));
66         assertEquals(expectedMessage, ex.getMessage());
67     }
68
69     @Test
70     void testEquals() {
71         final var rev1 = Revision.of("2017-12-25");
72         final var rev1dup = Revision.of("2017-12-25");
73         final var rev2 = Revision.of("2017-12-26");
74
75         assertFalse(rev1.equals(null));
76         assertTrue(rev1.equals(rev1));
77         assertTrue(rev1.equals(rev1dup));
78         assertTrue(rev1dup.equals(rev1));
79         assertFalse(rev1.equals(rev2));
80         assertFalse(rev2.equals(rev1));
81     }
82
83     @Test
84     void testOfNullable() {
85         assertEquals(Optional.empty(), Revision.ofNullable(null));
86         assertEquals(Optional.of(Revision.of("2017-12-25")), Revision.ofNullable("2017-12-25"));
87     }
88
89     @Test
90     void testCompareOptional() {
91         assertEquals(0, Revision.compare(Optional.empty(), Optional.empty()));
92         assertEquals(0, Revision.compare(Revision.ofNullable("2017-12-25"), Revision.ofNullable("2017-12-25")));
93         assertEquals(-1, Revision.compare(Optional.empty(), Revision.ofNullable("2017-12-25")));
94         assertEquals(1, Revision.compare(Revision.ofNullable("2017-12-25"), Optional.empty()));
95     }
96
97     @Test
98     void testSerializationRevision() throws Exception {
99         final var revision = Revision.of("2017-12-25");
100         assertEquals(revision, testSerialization(revision, 78));
101     }
102
103     @Test
104     void testSerializationNotRevision() throws Exception {
105         assertSame(NotRevision.of(), testSerialization(NotRevision.of(), 68));
106     }
107
108     private static RevisionUnion testSerialization(final RevisionUnion obj, final int expectedLength) throws Exception {
109         final var bos = new ByteArrayOutputStream();
110         try (var oos = new ObjectOutputStream(bos)) {
111             oos.writeObject(obj);
112         }
113
114         final var bytes = bos.toByteArray();
115         assertEquals(expectedLength, bytes.length);
116
117         try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
118             return assertInstanceOf(obj.getClass(), ois.readObject());
119         }
120     }
121 }