forked from voldemort/voldemort
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
test/unit/voldemort/serialization/avro/versioned/AvroBackwardsCompatibilityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package voldemort.serialization.avro.versioned; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.generic.GenericData; | ||
import org.apache.avro.generic.GenericData.Record; | ||
import org.apache.avro.util.Utf8; | ||
|
||
/** | ||
* A test that the json serialization remains compatible with older serialized | ||
* data | ||
* | ||
* | ||
*/ | ||
public class AvroBackwardsCompatibilityTest extends TestCase { | ||
|
||
private static byte[] writeVersion0(Schema s0) { | ||
|
||
GenericData.Record record = new GenericData.Record(s0); | ||
record.put("original", new Utf8("Abhinay")); | ||
AvroVersionedGenericSerializer serializer = new AvroVersionedGenericSerializer(s0.toString()); | ||
return serializer.toBytes(record); | ||
|
||
} | ||
|
||
private static Object readVersion0(Map<Integer, String> versions, byte[] versionZeroBytes) { | ||
|
||
AvroVersionedGenericSerializer serializer = new AvroVersionedGenericSerializer(versions); | ||
return serializer.toObject(versionZeroBytes); | ||
|
||
} | ||
|
||
public static void testAvroSchemaEvolution() throws IOException { | ||
|
||
String versionZero = "{\"type\": \"record\", \"name\": \"myrec\",\"fields\": [{ \"name\": \"original\", \"type\": \"string\" }]}"; | ||
|
||
String versionOne = "{\"type\": \"record\", \"name\": \"myrec\",\"fields\": [{ \"name\": \"original\", \"type\": \"string\" } ," | ||
+ "{ \"name\": \"new-field\", \"type\": \"string\", \"default\":\"\" }]}"; | ||
|
||
Schema s0 = Schema.parse(versionZero); | ||
Schema s1 = Schema.parse(versionOne); | ||
|
||
Map<Integer, String> versions = new HashMap<Integer, String>(); | ||
|
||
versions.put(0, versionZero); | ||
versions.put(1, versionOne); | ||
|
||
byte[] versionZeroBytes = writeVersion0(s0); | ||
|
||
GenericData.Record record = (Record) readVersion0(versions, versionZeroBytes); | ||
|
||
} | ||
} |