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 com.google.common.collect.Range;
14 import java.util.Collections;
15 import org.junit.Test;
16 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
17 import org.opendaylight.ovsdb.lib.notation.Version;
18 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
19 import org.opendaylight.ovsdb.lib.schema.DatabaseSchemaImpl;
20 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
23 * Test class for {@link TyperUtils}.
25 public class TyperUtilsTest {
26 @TypedTable(name = "TestTypedTable", database = "Open_vSwitch")
27 private static final class TestTypedTable {
31 private static final class TestUntypedTable {
36 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} returns the appropriate schema when given a
37 * table containing the appropriate schema, for a typed table (annotated).
40 public void testGetTableSchemaWithIncludedTypedTable() {
42 GenericTableSchema testTableSchema = new GenericTableSchema("TestTypedTable");
43 DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL,
44 ImmutableMap.of(testTableSchema.getName(), testTableSchema));
47 GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
50 assertEquals(testTableSchema, tableSchema);
54 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} returns the appropriate schema when given a
55 * table containing the appropriate schema, for an untyped table (non-annotated).
58 public void testGetTableSchemaWithIncludedUntypedTable() {
60 GenericTableSchema testTableSchema = new GenericTableSchema("TestUntypedTable");
61 DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL,
62 ImmutableMap.of(testTableSchema.getName(), testTableSchema));
65 GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
68 assertEquals(testTableSchema, tableSchema);
72 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} throws an {@link IllegalArgumentException}
73 * when the appropriate table schema isn't present in the database schema (for a typed table).
75 @Test(expected = IllegalArgumentException.class)
76 public void testGetTableSchemaWithoutIncludedTypedTable() {
78 DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL, Collections.emptyMap());
81 TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
85 * Test that {@link TyperUtils#getTableSchema(DatabaseSchema, Class)} throws an {@link IllegalArgumentException}
86 * when the appropriate table schema isn't present in the database schema (for an untyped table).
88 @Test(expected = IllegalArgumentException.class)
89 public void testGetTableSchemaWithoutIncludedUntypedTable() {
91 DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL, Collections.emptyMap());
94 TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
98 * Test that {@link TyperUtils#checkVersion(Version, Range)} detects an old version. (The aim here isn't
99 * to test {@link Version#compareTo(Version)}, that should be done in
100 * {@link org.opendaylight.ovsdb.lib.notation.VersionTest}).
102 @Test(expected = SchemaVersionMismatchException.class)
103 public void testCheckOldVersionFails() throws SchemaVersionMismatchException {
104 callCheckVersion(new Version(1, 0, 0), new Version(1, 1, 0), Version.NULL);
108 * Test that {@link TyperUtils#checkVersion(Version, Range)} detects a new version.
110 @Test(expected = SchemaVersionMismatchException.class)
111 public void testCheckNewVersionFails() throws SchemaVersionMismatchException {
112 callCheckVersion(new Version(2, 0, 0), Version.NULL, new Version(1, 1, 0));
116 * Test that {@link TyperUtils#checkVersion(Version, Range)} accepts null boundaries.
119 public void testCheckNullVersionsSucceed() throws SchemaVersionMismatchException {
120 callCheckVersion(new Version(2, 0, 0), Version.NULL, Version.NULL);
121 // This check succeeds in the absence of an exception
125 * Test that {@link TyperUtils#checkVersion(Version, Range)} accepts the lower boundary version.
128 public void testCheckLowerVersionBoundarySucceeds() throws SchemaVersionMismatchException {
129 callCheckVersion(new Version(2, 0, 0), new Version(2, 0, 0), Version.NULL);
130 // This check succeeds in the absence of an exception
134 * Test that {@link TyperUtils#checkVersion(Version, Range)} accepts the upper boundary version.
137 public void testCheckUpperVersionBoundarySucceeds() throws SchemaVersionMismatchException {
138 callCheckVersion(new Version(2, 0, 0), Version.NULL, new Version(2, 0, 0));
139 // This check succeeds in the absence of an exception
143 * Test that {@link TyperUtils#checkVersion(Version, Range)} accepts both boundary versions.
146 public void testCheckSingleVersionBoundarySucceeds() throws SchemaVersionMismatchException {
147 callCheckVersion(new Version(2, 0, 0), new Version(2, 0, 0), new Version(2, 0, 0));
148 // This check succeeds in the absence of an exception
152 * Test that {@link TyperUtils#checkVersion(Version, Range)} accepts a version within the boundaries
156 public void testCheckVersionWithinBoundariesSucceeds() throws SchemaVersionMismatchException {
157 callCheckVersion(new Version(1, 1, 0), new Version(1, 0, 0), new Version(2, 0, 0));
158 // This check succeeds in the absence of an exception
162 * Call {@link TyperUtils#checkVersion(Version, Range)}.
164 * @param schema The schema version (to be checked).
165 * @param from The minimum supported version.
166 * @param to The maximum supported version.
167 * @throws SchemaVersionMismatchException if the schema version isn't supported.
169 private static void callCheckVersion(final Version schema, final Version from, final Version to) {
170 TyperUtils.checkVersion(schema, Version.createRangeOf(from, to));