Skip to content

Commit

Permalink
Unit test for json string normalization
Browse files Browse the repository at this point in the history
Signed-off-by: Shivesh Ranjan <[email protected]>
  • Loading branch information
shiveshr committed Jul 29, 2020
1 parent 7a8457e commit 6c24e12
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,8 @@ private SchemaInfo normalizeSchemaBinary(SchemaInfo schemaInfo) {
// in alphabetical order. This ensures that identical schemas with different order of fields are
// treated to be equal.
JsonNode jsonNode = OBJECT_MAPPER.readTree(schemaString);
schemaBinary = ByteBuffer.wrap(OBJECT_MAPPER.writeValueAsString(jsonNode).getBytes(Charsets.UTF_8));
Object obj = OBJECT_MAPPER.treeToValue(jsonNode, Object.class);
schemaBinary = ByteBuffer.wrap(OBJECT_MAPPER.writeValueAsString(obj).getBytes(Charsets.UTF_8));
break;
case Any:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package io.pravega.schemaregistry.service;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import io.pravega.common.Exceptions;
Expand All @@ -31,6 +32,7 @@
import io.pravega.schemaregistry.storage.ContinuationToken;
import io.pravega.schemaregistry.storage.Etag;
import io.pravega.schemaregistry.storage.SchemaStore;
import io.pravega.schemaregistry.storage.SchemaStoreFactory;
import io.pravega.schemaregistry.storage.StoreExceptions;
import io.pravega.schemaregistry.storage.impl.group.InMemoryGroupTable;
import io.pravega.test.common.AssertExtensions;
Expand All @@ -48,6 +50,7 @@
import java.util.concurrent.ScheduledExecutorService;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -645,4 +648,58 @@ public void testDeleteUsingTypeAndVersion() {
() -> service.deleteSchema(null, groupName, schemaName, version).join(),
e -> e instanceof RuntimeException);
}

@Test
public void testSchemaNormalization() {
SchemaStore schemaStore = SchemaStoreFactory.createInMemoryStore(executor);
SchemaRegistryService service = new SchemaRegistryService(schemaStore, executor);
String namespace = "n";
String group = "g";
service.createGroup(namespace, group,
GroupProperties.builder().allowMultipleTypes(false).properties(ImmutableMap.<String, String>builder().build())
.serializationFormat(SerializationFormat.Json)
.compatibility(Compatibility.allowAny()).build()).join();

String jsonSchemaString = "{" +
"\"title\": \"Person\", " +
"\"type\": \"object\", " +
"\"properties\": { " +
"\"name\": {" +
"\"type\": \"string\"" +
"}," +
"\"age\": {" +
"\"type\": \"integer\", \"minimum\": 0" +
"}" +
"}" +
"}";
String jsonSchemaString2 = "{" +
"\"title\": \"Person\", " +
"\"type\": \"object\", " +
"\"properties\": { " +
"\"age\": {" +
"\"type\": \"integer\", \"minimum\": 0" +
"}," +
"\"name\": {" +
"\"type\": \"string\"" +
"}" +
"}" +
"}";
SchemaInfo original = SchemaInfo.builder().type("person").serializationFormat(SerializationFormat.Json)
.schemaData(ByteBuffer.wrap(jsonSchemaString.getBytes(Charsets.UTF_8)))
.properties(ImmutableMap.of()).build();
VersionInfo v = service.addSchema(namespace, group, original).join();
SchemaInfo schema = service.getSchema(namespace, group, v.getId()).join();
assertEquals(schema, original);

// check with different order
SchemaInfo secondOrder = SchemaInfo.builder().type("person").serializationFormat(SerializationFormat.Json)
.schemaData(ByteBuffer.wrap(jsonSchemaString2.getBytes(Charsets.UTF_8)))
.properties(ImmutableMap.of()).build();
VersionInfo v2 = service.addSchema(namespace, group, secondOrder).join();
// add should have been idempotent
assertEquals(v2, v);

schema = service.getSchema(namespace, group, v.getId()).join();
assertNotEquals(schema, secondOrder);
}
}

0 comments on commit 6c24e12

Please sign in to comment.