2 * Copyright © 2016 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.lib.schema.typed;
10 import static org.junit.Assert.assertEquals;
12 import com.google.common.collect.ImmutableMap;
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.util.Collections;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
19 import org.opendaylight.ovsdb.lib.notation.Version;
20 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
21 import org.opendaylight.ovsdb.lib.schema.DatabaseSchemaImpl;
22 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Test class for {@link TyperUtils}.
29 public class TyperUtilsTest {
30 private static final Logger LOG = LoggerFactory.getLogger(TyperUtilsTest.class);
32 @TypedTable(name = "TestTypedTable", database = "Open_vSwitch")
33 private class TestTypedTable {
37 private class TestUntypedTable {
42 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} returns the appropriate schema when given a
43 * table containing the appropriate schema, for a typed table (annotated).
46 public void testGetTableSchemaWithIncludedTypedTable() {
48 GenericTableSchema testTableSchema = new GenericTableSchema("TestTypedTable");
49 DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL,
50 ImmutableMap.of(testTableSchema.getName(), testTableSchema));
53 GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
56 assertEquals(testTableSchema, tableSchema);
60 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} returns the appropriate schema when given a
61 * table containing the appropriate schema, for an untyped table (non-annotated).
64 public void testGetTableSchemaWithIncludedUntypedTable() {
66 GenericTableSchema testTableSchema = new GenericTableSchema("TestUntypedTable");
67 DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL,
68 ImmutableMap.of(testTableSchema.getName(), testTableSchema));
71 GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
74 assertEquals(testTableSchema, tableSchema);
78 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} throws an {@link IllegalArgumentException}
79 * when the appropriate table schema isn't present in the database schema (for a typed table).
81 @Test(expected = IllegalArgumentException.class)
82 public void testGetTableSchemaWithoutIncludedTypedTable() {
84 DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL, Collections.emptyMap());
87 TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
91 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} throws an {@link IllegalArgumentException}
92 * when the appropriate table schema isn't present in the database schema (for an untyped table).
94 @Test(expected = IllegalArgumentException.class)
95 public void testGetTableSchemaWithoutIncludedUntypedTable() {
97 DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL, Collections.emptyMap());
100 TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
104 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} detects an old version. (The aim here isn't
105 * to test {@link Version#compareTo(Version)}, that should be done in
106 * {@link org.opendaylight.ovsdb.lib.notation.VersionTest}).
108 @Test(expected = SchemaVersionMismatchException.class)
109 public void testCheckOldVersionFails() throws SchemaVersionMismatchException {
110 callCheckVersion(new Version(1, 0, 0), new Version(1, 1, 0), Version.NULL);
114 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} detects a new version.
116 @Test(expected = SchemaVersionMismatchException.class)
117 public void testCheckNewVersionFails() throws SchemaVersionMismatchException {
118 callCheckVersion(new Version(2, 0, 0), Version.NULL, new Version(1, 1, 0));
122 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts null boundaries.
125 public void testCheckNullVersionsSucceed() throws SchemaVersionMismatchException {
126 callCheckVersion(new Version(2, 0, 0), Version.NULL, Version.NULL);
127 // This check succeeds in the absence of an exception
131 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts the lower boundary version.
134 public void testCheckLowerVersionBoundarySucceeds() throws SchemaVersionMismatchException {
135 callCheckVersion(new Version(2, 0, 0), new Version(2, 0, 0), Version.NULL);
136 // This check succeeds in the absence of an exception
140 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts the upper boundary version.
143 public void testCheckUpperVersionBoundarySucceeds() throws SchemaVersionMismatchException {
144 callCheckVersion(new Version(2, 0, 0), Version.NULL, new Version(2, 0, 0));
145 // This check succeeds in the absence of an exception
149 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts both boundary versions.
152 public void testCheckSingleVersionBoundarySucceeds() throws SchemaVersionMismatchException {
153 callCheckVersion(new Version(2, 0, 0), new Version(2, 0, 0), new Version(2, 0, 0));
154 // This check succeeds in the absence of an exception
158 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts a version within the boundaries
162 public void testCheckVersionWithinBoundariesSucceeds() throws SchemaVersionMismatchException {
163 callCheckVersion(new Version(1, 1, 0), new Version(1, 0, 0), new Version(2, 0, 0));
164 // This check succeeds in the absence of an exception
168 * Call {@link TyperUtils#checkVersion(Version, Version, Version)}.
170 * @param schema The schema version (to be checked).
171 * @param from The minimum supported version.
172 * @param to The maximum supported version.
173 * @throws SchemaVersionMismatchException if the schema version isn't supported.
175 // We extract the real cause, which “loses” the original cause, but that’s fine
176 @SuppressWarnings("checkstyle:AvoidHidingCauseException")
177 private static void callCheckVersion(final Version schema, final Version from, final Version to) {
180 TyperUtils.class.getDeclaredMethod("checkVersion", Version.class, Version.class, Version.class);
181 method.setAccessible(true);
182 method.invoke(TyperUtils.class, schema, from, to);
183 } catch (NoSuchMethodException e) {
184 LOG.error("Can't find TyperUtils::checkVersion(), TyperUtilsTest::callCheckVersion() may be obsolete", e);
185 } catch (IllegalAccessException e) {
186 LOG.error("Error invoking TyperUtils::checkVersion(), please check TyperUtilsTest::callCheckVersion()", e);
187 } catch (InvocationTargetException e) {
188 final Throwable cause = e.getCause();
189 if (cause instanceof SchemaVersionMismatchException) {
190 throw (SchemaVersionMismatchException) cause;
192 LOG.error("Unexpected exception thrown by TyperUtils::checkVersion()", cause);
193 Assert.fail("Unexpected exception thrown by TyperUtils::checkVersion()");