Bug 6692: improve TyperUtils tests (first pass)
[ovsdb.git] / library / impl / src / test / java / org / opendaylight / ovsdb / lib / schema / typed / TyperUtilsTest.java
index 079b31c2fea3f9332a2466ce0ac8b76d5647c698..823d89f2e0b855fcf6a8549ca1007ecd884df025 100644 (file)
@@ -8,12 +8,18 @@
 
 package org.opendaylight.ovsdb.lib.schema.typed;
 
+import static org.junit.Assert.assertEquals;
+
+import com.google.common.collect.ImmutableMap;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.Collections;
 import org.junit.Assert;
 import org.junit.Test;
 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
 import org.opendaylight.ovsdb.lib.notation.Version;
+import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
+import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -23,6 +29,75 @@ import org.slf4j.LoggerFactory;
 public class TyperUtilsTest {
     private static final Logger LOG = LoggerFactory.getLogger(TyperUtilsTest.class);
 
+    @TypedTable(name = "TestTypedTable", database = "Open_vSwitch")
+    private class TestTypedTable {
+
+    }
+
+    private class TestUntypedTable {
+
+    }
+
+    /**
+     * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} returns the appropriate schema when given a
+     * table containing the appropriate schema, for a typed table (annotated).
+     */
+    @Test
+    public void testGetTableSchemaWithIncludedTypedTable() {
+        // Given ...
+        GenericTableSchema testTableSchema = new GenericTableSchema("TestTypedTable");
+        DatabaseSchema dbSchema = new DatabaseSchema(ImmutableMap.of(testTableSchema.getName(), testTableSchema));
+
+        // When ...
+        GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
+
+        // Then ...
+        assertEquals(testTableSchema, tableSchema);
+    }
+
+    /**
+     * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} returns the appropriate schema when given a
+     * table containing the appropriate schema, for an untyped table (non-annotated).
+     */
+    @Test
+    public void testGetTableSchemaWithIncludedUntypedTable() {
+        // Given ...
+        GenericTableSchema testTableSchema = new GenericTableSchema("TestUntypedTable");
+        DatabaseSchema dbSchema = new DatabaseSchema(ImmutableMap.of(testTableSchema.getName(), testTableSchema));
+
+        // When ...
+        GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
+
+        // Then ...
+        assertEquals(testTableSchema, tableSchema);
+    }
+
+    /**
+     * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} throws an {@link IllegalArgumentException}
+     * when the appropriate table schema isn't present in the database schema (for a typed table).
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetTableSchemaWithoutIncludedTypedTable() {
+        // Given ...
+        DatabaseSchema dbSchema = new DatabaseSchema(Collections.emptyMap());
+
+        // When ...
+        TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
+    }
+
+    /**
+     * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} throws an {@link IllegalArgumentException}
+     * when the appropriate table schema isn't present in the database schema (for an untyped table).
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetTableSchemaWithoutIncludedUntypedTable() {
+        // Given ...
+        DatabaseSchema dbSchema = new DatabaseSchema(Collections.emptyMap());
+
+        // When ...
+        TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
+    }
+
     /**
      * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} detects an old version. (The aim here isn't
      * to test {@link Version#compareTo(Version)}, that should be done in