e64203b3fc6c10a9f525ede3e1584e3b4a1ea2a4
[ovsdb.git] / library / impl / src / test / java / org / opendaylight / ovsdb / lib / schema / typed / TyperUtilsTest.java
1 /*
2  * Copyright © 2016 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.lib.schema.typed;
10
11 import static org.junit.Assert.assertEquals;
12
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;
25
26 /**
27  * Test class for {@link TyperUtils}.
28  */
29 public class TyperUtilsTest {
30     private static final Logger LOG = LoggerFactory.getLogger(TyperUtilsTest.class);
31
32     @TypedTable(name = "TestTypedTable", database = "Open_vSwitch")
33     private class TestTypedTable {
34
35     }
36
37     private class TestUntypedTable {
38
39     }
40
41     /**
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).
44      */
45     @Test
46     public void testGetTableSchemaWithIncludedTypedTable() {
47         // Given ...
48         GenericTableSchema testTableSchema = new GenericTableSchema("TestTypedTable");
49         DatabaseSchema dbSchema = new DatabaseSchema(ImmutableMap.of(testTableSchema.getName(), testTableSchema));
50
51         // When ...
52         GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
53
54         // Then ...
55         assertEquals(testTableSchema, tableSchema);
56     }
57
58     /**
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).
61      */
62     @Test
63     public void testGetTableSchemaWithIncludedUntypedTable() {
64         // Given ...
65         GenericTableSchema testTableSchema = new GenericTableSchema("TestUntypedTable");
66         DatabaseSchema dbSchema = new DatabaseSchema(ImmutableMap.of(testTableSchema.getName(), testTableSchema));
67
68         // When ...
69         GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
70
71         // Then ...
72         assertEquals(testTableSchema, tableSchema);
73     }
74
75     /**
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).
78      */
79     @Test(expected = IllegalArgumentException.class)
80     public void testGetTableSchemaWithoutIncludedTypedTable() {
81         // Given ...
82         DatabaseSchema dbSchema = new DatabaseSchema(Collections.emptyMap());
83
84         // When ...
85         TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
86     }
87
88     /**
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).
91      */
92     @Test(expected = IllegalArgumentException.class)
93     public void testGetTableSchemaWithoutIncludedUntypedTable() {
94         // Given ...
95         DatabaseSchema dbSchema = new DatabaseSchema(Collections.emptyMap());
96
97         // When ...
98         TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
99     }
100
101     /**
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}).
105      */
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);
109     }
110
111     /**
112      * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} detects a new version.
113      */
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));
117     }
118
119     /**
120      * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts null boundaries.
121      */
122     @Test
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
126     }
127
128     /**
129      * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts the lower boundary version.
130      */
131     @Test
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
135     }
136
137     /**
138      * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts the upper boundary version.
139      */
140     @Test
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
144     }
145
146     /**
147      * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts both boundary versions.
148      */
149     @Test
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
153     }
154
155     /**
156      * Test that {@link TyperUtils#checkVersion(Version, Version, Version)} accepts a version within the boundaries
157      * (strictly).
158      */
159     @Test
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
163     }
164
165     /**
166      * Call {@link TyperUtils#checkVersion(Version, Version, Version)}.
167      *
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.
172      */
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 {
176         try {
177             Method method =
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;
189             }
190             LOG.error("Unexpected exception thrown by TyperUtils::checkVersion()", cause);
191             Assert.fail("Unexpected exception thrown by TyperUtils::checkVersion()");
192         }
193     }
194 }