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
9 package org.opendaylight.ovsdb.lib.schema.typed;
11 import static org.junit.Assert.assertEquals;
13 import com.google.common.collect.ImmutableMap;
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16 import java.util.Collections;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
20 import org.opendaylight.ovsdb.lib.notation.Version;
21 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
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 DatabaseSchema(ImmutableMap.of(testTableSchema.getName(), testTableSchema));
52 GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
55 assertEquals(testTableSchema, tableSchema);
59 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} returns the appropriate schema when given a
60 * table containing the appropriate schema, for an untyped table (non-annotated).
63 public void testGetTableSchemaWithIncludedUntypedTable() {
65 GenericTableSchema testTableSchema = new GenericTableSchema("TestUntypedTable");
66 DatabaseSchema dbSchema = new DatabaseSchema(ImmutableMap.of(testTableSchema.getName(), testTableSchema));
69 GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
72 assertEquals(testTableSchema, tableSchema);
76 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} throws an {@link IllegalArgumentException}
77 * when the appropriate table schema isn't present in the database schema (for a typed table).
79 @Test(expected = IllegalArgumentException.class)
80 public void testGetTableSchemaWithoutIncludedTypedTable() {
82 DatabaseSchema dbSchema = new DatabaseSchema(Collections.emptyMap());
85 TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
89 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} throws an {@link IllegalArgumentException}
90 * when the appropriate table schema isn't present in the database schema (for an untyped table).
92 @Test(expected = IllegalArgumentException.class)
93 public void testGetTableSchemaWithoutIncludedUntypedTable() {
95 DatabaseSchema dbSchema = new DatabaseSchema(Collections.emptyMap());
98 TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
102 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} detects an old version. (The aim here isn't
103 * to test {@link Version#compareTo(Version)}, that should be done in
104 * {@link org.opendaylight.ovsdb.lib.notation.VersionTest}).
106 @Test(expected = SchemaVersionMismatchException.class)
107 public void testCheckOldVersionFails() throws SchemaVersionMismatchException {
108 callCheckVersion(new Version(1, 0, 0), new Version(1, 1, 0), Version.NULL);
112 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} detects a new version.
114 @Test(expected = SchemaVersionMismatchException.class)
115 public void testCheckNewVersionFails() throws SchemaVersionMismatchException {
116 callCheckVersion(new Version(2, 0, 0), Version.NULL, new Version(1, 1, 0));
120 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts null boundaries.
123 public void testCheckNullVersionsSucceed() throws SchemaVersionMismatchException {
124 callCheckVersion(new Version(2, 0, 0), Version.NULL, Version.NULL);
125 // This check succeeds in the absence of an exception
129 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts the lower boundary version.
132 public void testCheckLowerVersionBoundarySucceeds() throws SchemaVersionMismatchException {
133 callCheckVersion(new Version(2, 0, 0), new Version(2, 0, 0), Version.NULL);
134 // This check succeeds in the absence of an exception
138 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts the upper boundary version.
141 public void testCheckUpperVersionBoundarySucceeds() throws SchemaVersionMismatchException {
142 callCheckVersion(new Version(2, 0, 0), Version.NULL, new Version(2, 0, 0));
143 // This check succeeds in the absence of an exception
147 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts both boundary versions.
150 public void testCheckSingleVersionBoundarySucceeds() throws SchemaVersionMismatchException {
151 callCheckVersion(new Version(2, 0, 0), new Version(2, 0, 0), new Version(2, 0, 0));
152 // This check succeeds in the absence of an exception
156 * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts a version within the boundaries
160 public void testCheckVersionWithinBoundariesSucceeds() throws SchemaVersionMismatchException {
161 callCheckVersion(new Version(1, 1, 0), new Version(1, 0, 0), new Version(2, 0, 0));
162 // This check succeeds in the absence of an exception
166 * Call {@link TyperUtils#checkVersion(Version, Version, Version)}.
168 * @param schema The schema version (to be checked).
169 * @param from The minimum supported version.
170 * @param to The maximum supported version.
171 * @throws SchemaVersionMismatchException if the schema version isn't supported.
173 // We extract the real cause, which “loses” the original cause, but that’s fine
174 @SuppressWarnings("checkstyle:AvoidHidingCauseException")
175 private void callCheckVersion(Version schema, Version from, Version to) throws SchemaVersionMismatchException {
178 TyperUtils.class.getDeclaredMethod("checkVersion", Version.class, Version.class, Version.class);
179 method.setAccessible(true);
180 method.invoke(TyperUtils.class, schema, from, to);
181 } catch (NoSuchMethodException e) {
182 LOG.error("Can't find TyperUtils::checkVersion(), TyperUtilsTest::callCheckVersion() may be obsolete", e);
183 } catch (IllegalAccessException e) {
184 LOG.error("Error invoking TyperUtils::checkVersion(), please check TyperUtilsTest::callCheckVersion()", e);
185 } catch (InvocationTargetException e) {
186 final Throwable cause = e.getCause();
187 if (cause instanceof SchemaVersionMismatchException) {
188 throw (SchemaVersionMismatchException) cause;
190 LOG.error("Unexpected exception thrown by TyperUtils::checkVersion()", cause);
191 Assert.fail("Unexpected exception thrown by TyperUtils::checkVersion()");