Fix checkstyle
[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 package org.opendaylight.ovsdb.lib.schema.typed;
9
10 import static org.junit.Assert.assertEquals;
11
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;
21
22 /**
23  * Test class for {@link TyperUtils}.
24  */
25 public class TyperUtilsTest {
26     @TypedTable(name = "TestTypedTable", database = "Open_vSwitch")
27     private static final class TestTypedTable {
28
29     }
30
31     private static final class TestUntypedTable {
32
33     }
34
35     /**
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).
38      */
39     @Test
40     public void testGetTableSchemaWithIncludedTypedTable() {
41         // Given ...
42         GenericTableSchema testTableSchema = new GenericTableSchema("TestTypedTable");
43         DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL,
44             ImmutableMap.of(testTableSchema.getName(), testTableSchema));
45
46         // When ...
47         GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
48
49         // Then ...
50         assertEquals(testTableSchema, tableSchema);
51     }
52
53     /**
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).
56      */
57     @Test
58     public void testGetTableSchemaWithIncludedUntypedTable() {
59         // Given ...
60         GenericTableSchema testTableSchema = new GenericTableSchema("TestUntypedTable");
61         DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL,
62             ImmutableMap.of(testTableSchema.getName(), testTableSchema));
63
64         // When ...
65         GenericTableSchema tableSchema = TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
66
67         // Then ...
68         assertEquals(testTableSchema, tableSchema);
69     }
70
71     /**
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).
74      */
75     @Test(expected = IllegalArgumentException.class)
76     public void testGetTableSchemaWithoutIncludedTypedTable() {
77         // Given ...
78         DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL, Collections.emptyMap());
79
80         // When ...
81         TyperUtils.getTableSchema(dbSchema, TestTypedTable.class);
82     }
83
84     /**
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).
87      */
88     @Test(expected = IllegalArgumentException.class)
89     public void testGetTableSchemaWithoutIncludedUntypedTable() {
90         // Given ...
91         DatabaseSchema dbSchema = new DatabaseSchemaImpl("testDb", Version.NULL, Collections.emptyMap());
92
93         // When ...
94         TyperUtils.getTableSchema(dbSchema, TestUntypedTable.class);
95     }
96
97     /**
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}).
101      */
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);
105     }
106
107     /**
108      * Test that {@link TyperUtils#checkVersion(Version, Range)} detects a new version.
109      */
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));
113     }
114
115     /**
116      * Test that {@link TyperUtils#checkVersion(Version, Range)} accepts null boundaries.
117      */
118     @Test
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
122     }
123
124     /**
125      * Test that {@link TyperUtils#checkVersion(Version, Range)} accepts the lower boundary version.
126      */
127     @Test
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
131     }
132
133     /**
134      * Test that {@link TyperUtils#checkVersion(Version, Range)} accepts the upper boundary version.
135      */
136     @Test
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
140     }
141
142     /**
143      * Test that {@link TyperUtils#checkVersion(Version, Range)} accepts both boundary versions.
144      */
145     @Test
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
149     }
150
151     /**
152      * Test that {@link TyperUtils#checkVersion(Version, Range)} accepts a version within the boundaries
153      * (strictly).
154      */
155     @Test
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
159     }
160
161     /**
162      * Call {@link TyperUtils#checkVersion(Version, Range)}.
163      *
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.
168      */
169     private static void callCheckVersion(final Version schema, final Version from, final Version to) {
170         TyperUtils.checkVersion(schema, Version.createRangeOf(from, to));
171     }
172 }