Add Revision test suite 57/66757/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 25 Dec 2017 15:09:48 +0000 (16:09 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 25 Dec 2017 16:02:00 +0000 (17:02 +0100)
This adds a simplistic test suite for testing Revision class.

Change-Id: Ibed986f5c53e7f201c3eb16cf448e022b09e6b85
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/RevisionTest.java [new file with mode: 0644]

diff --git a/yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/RevisionTest.java b/yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/RevisionTest.java
new file mode 100644 (file)
index 0000000..2a43314
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.time.format.DateTimeParseException;
+import java.util.Optional;
+import org.junit.Test;
+
+public class RevisionTest {
+    @Test
+    public void testOf() {
+        assertEquals("2017-12-25", Revision.of("2017-12-25").toString());
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testOfNull() {
+        Revision.of(null);
+    }
+
+    @Test(expected = DateTimeParseException.class)
+    public void testOfEmpty() {
+        Revision.of("");
+    }
+
+    @Test(expected = DateTimeParseException.class)
+    public void testOfInvalid() {
+        Revision.of("invalid");
+    }
+
+    @Test(expected = DateTimeParseException.class)
+    public void testOfInvalidDate1() {
+        Revision.of("2017-13-01");
+    }
+
+    @Test(expected = DateTimeParseException.class)
+    public void testOfInvalidDate2() {
+        Revision.of("2017-12-00");
+    }
+
+    @Test(expected = DateTimeParseException.class)
+    public void testOfInvalidDate3() {
+        Revision.of("2017-12-32");
+    }
+
+    @Test
+    public void testEquals() {
+        final Revision rev1 = Revision.of("2017-12-25");
+        final Revision rev1dup = Revision.of("2017-12-25");
+        final Revision rev2 = Revision.of("2017-12-26");
+
+        assertFalse(rev1.equals(null));
+        assertTrue(rev1.equals(rev1));
+        assertTrue(rev1.equals(rev1dup));
+        assertTrue(rev1dup.equals(rev1));
+        assertFalse(rev1.equals(rev2));
+        assertFalse(rev2.equals(rev1));
+    }
+
+    @Test
+    public void testOfNullable() {
+        assertEquals(Optional.empty(), Revision.ofNullable(null));
+
+        final Optional<Revision> opt = Revision.ofNullable("2017-12-25");
+        assertTrue(opt.isPresent());
+        assertEquals("2017-12-25", opt.get().toString());
+    }
+
+    @Test
+    public void testCompareOptional() {
+        assertEquals(0, Revision.compare(Optional.empty(), Optional.empty()));
+        assertEquals(0, Revision.compare(Revision.ofNullable("2017-12-25"), Revision.ofNullable("2017-12-25")));
+        assertEquals(-1, Revision.compare(Optional.empty(), Revision.ofNullable("2017-12-25")));
+        assertEquals(1, Revision.compare(Revision.ofNullable("2017-12-25"), Optional.empty()));
+    }
+
+    @Test
+    public void testSerialization() throws IOException, ClassNotFoundException {
+        final Revision source = Revision.of("2017-12-25");
+        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
+            oos.writeObject(source);
+        }
+
+        final Object read;
+        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+            read = ois.readObject();
+        }
+
+        assertEquals(source, read);
+    }
+}