Bug 6692: improve TyperUtils tests (first pass) 31/45431/4
authorStephen Kitt <skitt@redhat.com>
Mon, 5 Sep 2016 12:35:58 +0000 (14:35 +0200)
committerStephen Kitt <skitt@redhat.com>
Mon, 12 Sep 2016 09:09:25 +0000 (09:09 +0000)
This also drops the unused type parameter on getTableSchema().

For now we preserve the IllegalArgumentException-based behaviour in
getTableSchema() (instead of handling the missing schema and returning
null).

Change-Id: If704a3d94694f44d6ccad6536cb230d09fa4e4b5
Signed-off-by: Stephen Kitt <skitt@redhat.com>
library/impl/src/main/java/org/opendaylight/ovsdb/lib/schema/typed/TyperUtils.java
library/impl/src/test/java/org/opendaylight/ovsdb/lib/schema/typed/TyperUtilsTest.java

index fc9dd639c306ebca578ec068f5b5ad59561e7e0e..dc6731e4a74a86c6c7b66a7f232b5a1e2b8e92c7 100644 (file)
@@ -30,6 +30,9 @@ import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.lib.schema.TableSchema;
 
+/**
+ * Utility methods for typed OVSDB schema data.
+ */
 public class TyperUtils {
 
     private static final String GET_STARTS_WITH = "get";
@@ -49,7 +52,15 @@ public class TyperUtils {
         return klazz.getSimpleName();
     }
 
-    public static <T> GenericTableSchema getTableSchema(DatabaseSchema dbSchema, Class<T> klazz) {
+    /**
+     * Retrieve the table schema for the given table in the given database schema.
+     *
+     * @param dbSchema The database schema.
+     * @param klazz The class whose table schema should be retrieved. Classes are matched in the database schema either
+     *     using their {@link TypedTable} annotation, if they have one, or by name.
+     * @return the table schema.
+     */
+    public static GenericTableSchema getTableSchema(DatabaseSchema dbSchema, Class<?> klazz) {
         String tableName = getTableName(klazz);
         return dbSchema.table(tableName, GenericTableSchema.class);
     }
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