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";
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);
}
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;
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