|
23 | 23 | import org.testng.Assert; |
24 | 24 | import org.testng.annotations.Test; |
25 | 25 |
|
26 | | -public class AvroSchemaCompatibilityCheckTest { |
| 26 | +public class AvroSchemaCompatibilityCheckTest extends BaseAvroSchemaCompatibilityTest{ |
27 | 27 |
|
28 | | - private static final String schemaJson1 = |
29 | | - "{\"type\":\"record\",\"name\":\"DefaultTest\",\"namespace\":\"org.apache.pulsar.broker.service.schema" + |
30 | | - ".AvroSchemaCompatibilityCheckTest$\",\"fields\":[{\"name\":\"field1\",\"type\":\"string\"}]}"; |
31 | | - private static final SchemaData schemaData1 = getSchemaData(schemaJson1); |
32 | | - |
33 | | - private static final String schemaJson2 = |
34 | | - "{\"type\":\"record\",\"name\":\"DefaultTest\",\"namespace\":\"org.apache.pulsar.broker.service.schema" + |
35 | | - ".AvroSchemaCompatibilityCheckTest$\",\"fields\":[{\"name\":\"field1\",\"type\":\"string\"}," + |
36 | | - "{\"name\":\"field2\",\"type\":\"string\",\"default\":\"foo\"}]}"; |
37 | | - private static final SchemaData schemaData2 = getSchemaData(schemaJson2); |
38 | | - |
39 | | - private static final String schemaJson3 = |
40 | | - "{\"type\":\"record\",\"name\":\"DefaultTest\",\"namespace\":\"org" + |
41 | | - ".apache.pulsar.broker.service.schema.AvroSchemaCompatibilityCheckTest$\"," + |
42 | | - "\"fields\":[{\"name\":\"field1\",\"type\":\"string\"},{\"name\":\"field2\",\"type\":\"string\"}]}"; |
43 | | - private static final SchemaData schemaData3 = getSchemaData(schemaJson3); |
44 | | - |
45 | | - private static final String schemaJson4 = |
46 | | - "{\"type\":\"record\",\"name\":\"DefaultTest\",\"namespace\":\"org.apache.pulsar.broker.service.schema" + |
47 | | - ".AvroSchemaCompatibilityCheckTest$\",\"fields\":[{\"name\":\"field1_v2\",\"type\":\"string\"," + |
48 | | - "\"aliases\":[\"field1\"]}]}"; |
49 | | - private static final SchemaData schemaData4 = getSchemaData(schemaJson4); |
50 | | - |
51 | | - private static final String schemaJson5 = |
52 | | - "{\"type\":\"record\",\"name\":\"DefaultTest\",\"namespace\":\"org.apache.pulsar.broker.service.schema" + |
53 | | - ".AvroSchemaCompatibilityCheckTest$\",\"fields\":[{\"name\":\"field1\",\"type\":[\"null\"," + |
54 | | - "\"string\"]}]}"; |
55 | | - private static final SchemaData schemaData5 = getSchemaData(schemaJson5); |
56 | | - |
57 | | - private static final String schemaJson6 = |
58 | | - "{\"type\":\"record\",\"name\":\"DefaultTest\",\"namespace\":\"org.apache.pulsar.broker.service.schema" + |
59 | | - ".AvroSchemaCompatibilityCheckTest$\",\"fields\":[{\"name\":\"field1\",\"type\":[\"null\"," + |
60 | | - "\"string\",\"int\"]}]}"; |
61 | | - private static final SchemaData schemaData6 = getSchemaData(schemaJson6); |
62 | | - |
63 | | - private static final String schemaJson7 = |
64 | | - "{\"type\":\"record\",\"name\":\"DefaultTest\",\"namespace\":\"org.apache.pulsar.broker.service.schema" + |
65 | | - ".AvroSchemaCompatibilityCheckTest$\",\"fields\":[{\"name\":\"field1\",\"type\":\"string\"}," + |
66 | | - "{\"name\":\"field2\",\"type\":\"string\",\"default\":\"foo\"},{\"name\":\"field3\"," + |
67 | | - "\"type\":\"string\",\"default\":\"bar\"}]}"; |
68 | | - private static final SchemaData schemaData7 = getSchemaData(schemaJson7); |
69 | | - |
70 | | - /** |
71 | | - * make sure new schema is backwards compatible with latest |
72 | | - */ |
73 | | - @Test |
74 | | - public void testBackwardCompatibility() { |
75 | | - |
76 | | - AvroSchemaCompatibilityCheck avroSchemaCompatibilityCheck = new AvroSchemaCompatibilityCheck( |
77 | | - SchemaCompatibilityStrategy.BACKWARD |
78 | | - ); |
79 | | - |
80 | | - // adding a field with default is backwards compatible |
81 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData1, schemaData2), |
82 | | - "adding a field with default is backwards compatible"); |
83 | | - // adding a field without default is NOT backwards compatible |
84 | | - Assert.assertFalse(avroSchemaCompatibilityCheck.isCompatible(schemaData1, schemaData3), |
85 | | - "adding a field without default is NOT backwards compatible"); |
86 | | - // Modifying a field name is not backwards compatible |
87 | | - Assert.assertFalse(avroSchemaCompatibilityCheck.isCompatible(schemaData1, schemaData4), |
88 | | - "Modifying a field name is not backwards compatible"); |
89 | | - // evolving field to a union is backwards compatible |
90 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData1, schemaData5), |
91 | | - "evolving field to a union is backwards compatible"); |
92 | | - // removing a field from a union is NOT backwards compatible |
93 | | - Assert.assertFalse(avroSchemaCompatibilityCheck.isCompatible(schemaData5, schemaData1), |
94 | | - "removing a field from a union is NOT backwards compatible"); |
95 | | - // adding a field to a union is backwards compatible |
96 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData5, schemaData6), |
97 | | - "adding a field to a union is backwards compatible"); |
98 | | - // removing a field a union is NOT backwards compatible |
99 | | - Assert.assertFalse(avroSchemaCompatibilityCheck.isCompatible(schemaData6, schemaData5), |
100 | | - "removing a field a union is NOT backwards compatible"); |
101 | | - } |
102 | | - |
103 | | - /** |
104 | | - * Check to make sure the last schema version is forward-compatible with new schemas |
105 | | - */ |
106 | | - @Test |
107 | | - public void testForwardCompatibility() { |
108 | | - |
109 | | - AvroSchemaCompatibilityCheck avroSchemaCompatibilityCheck = new AvroSchemaCompatibilityCheck( |
110 | | - SchemaCompatibilityStrategy.FORWARD |
111 | | - ); |
112 | | - |
113 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData1, schemaData2), |
114 | | - "adding a field is forward compatible"); |
115 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData1, schemaData3), |
116 | | - "adding a field is forward compatible"); |
117 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData2, schemaData3), |
118 | | - "adding a field is forward compatible"); |
119 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData3, schemaData2), |
120 | | - "adding a field is forward compatible"); |
121 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData3, schemaData2), |
122 | | - "adding a field is forward compatible"); |
123 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData2, schemaData7), |
124 | | - "removing fields is forward compatible"); |
125 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData2, schemaData1), |
126 | | - "removing fields with defaults forward compatible"); |
| 28 | + @Override |
| 29 | + public SchemaCompatibilityCheck getBackwardsCompatibleSchemaCheck() { |
| 30 | + return new AvroSchemaCompatibilityCheck(SchemaCompatibilityStrategy.BACKWARD); |
127 | 31 | } |
128 | 32 |
|
129 | | - /** |
130 | | - * Make sure the new schema is forward- and backward-compatible from the latest to newest and from the newest to latest. |
131 | | - */ |
132 | | - @Test |
133 | | - public void testFullCompatibility() { |
134 | | - AvroSchemaCompatibilityCheck avroSchemaCompatibilityCheck = new AvroSchemaCompatibilityCheck( |
135 | | - SchemaCompatibilityStrategy.FULL |
136 | | - ); |
137 | | - Assert.assertTrue(avroSchemaCompatibilityCheck.isCompatible(schemaData1, schemaData2), |
138 | | - "adding a field with default fully compatible"); |
139 | | - Assert.assertFalse(avroSchemaCompatibilityCheck.isCompatible(schemaData1, schemaData3), |
140 | | - "adding a field without default is not fully compatible"); |
141 | | - Assert.assertFalse(avroSchemaCompatibilityCheck.isCompatible(schemaData3, schemaData1), |
142 | | - "adding a field without default is not fully compatible"); |
143 | | - |
| 33 | + @Override |
| 34 | + public SchemaCompatibilityCheck getForwardCompatibleSchemaCheck() { |
| 35 | + return new AvroSchemaCompatibilityCheck(SchemaCompatibilityStrategy.FORWARD); |
144 | 36 | } |
145 | 37 |
|
146 | | - private static SchemaData getSchemaData(String schemaJson) { |
147 | | - return SchemaData.builder().data(schemaJson.getBytes()).type(SchemaType.AVRO).build(); |
| 38 | + @Override |
| 39 | + public SchemaCompatibilityCheck getFullCompatibleSchemaCheck() { |
| 40 | + return new AvroSchemaCompatibilityCheck(SchemaCompatibilityStrategy.FULL); |
148 | 41 | } |
149 | 42 | } |
0 commit comments