Skip to content

Commit b2ad531

Browse files
committed
JSON Schema to avro: start testing
And fix an obvious bug. Signed-off-by: Francis Galiegue <[email protected]>
1 parent 6d72ce6 commit b2ad531

File tree

6 files changed

+148
-29
lines changed

6 files changed

+148
-29
lines changed

src/main/java/com/github/fge/jsonschema2avro/AvroWriterProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void main(final String... args)
4646
{
4747
final JsonNode node
4848
= JsonLoader.fromString("{\"type\": \"array\"," +
49-
"\"items\":{\"type\":\"null\"}}");
49+
"\"additionalItems\":{\"type\":\"null\"}}");
5050
final SchemaTree tree = new CanonicalSchemaTree(node);
5151
final SchemaHolder input = new SchemaHolder(tree);
5252
final AvroWriterProcessor p = new AvroWriterProcessor();

src/main/java/com/github/fge/jsonschema2avro/predicates/AvroPredicates.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.github.fge.jsonschema.processors.validation.ArraySchemaDigester;
5+
import com.github.fge.jsonschema.processors.validation.ObjectSchemaDigester;
56
import com.github.fge.jsonschema.util.NodeType;
67
import com.github.fge.jsonschema2avro.AvroPayload;
78
import com.google.common.base.Predicate;
@@ -19,12 +20,10 @@ public static Predicate<AvroPayload> simpleType()
1920
@Override
2021
public boolean apply(final AvroPayload input)
2122
{
22-
final JsonNode typeNode = schemaNode(input).path("type");
23-
24-
if (!typeNode.isTextual())
23+
final JsonNode node = schemaNode(input);
24+
final NodeType type = getType(node);
25+
if (type == null)
2526
return false;
26-
27-
final NodeType type = NodeType.fromName(typeNode.textValue());
2827
return type != NodeType.ARRAY && type != NodeType.OBJECT;
2928
}
3029
};
@@ -38,13 +37,10 @@ public static Predicate<AvroPayload> array()
3837
public boolean apply(final AvroPayload input)
3938
{
4039
final JsonNode node = schemaNode(input);
41-
final JsonNode typeNode = node.path("type");
42-
43-
if (!typeNode.isTextual())
40+
final NodeType type = getType(node);
41+
if (type == null)
4442
return false;
4543

46-
final NodeType type = NodeType.fromName(typeNode.textValue());
47-
4844
if (type != NodeType.ARRAY)
4945
return false;
5046

@@ -65,8 +61,42 @@ public boolean apply(final AvroPayload input)
6561
};
6662
}
6763

64+
public static Predicate<AvroPayload> map()
65+
{
66+
return new Predicate<AvroPayload>()
67+
{
68+
@Override
69+
public boolean apply(final AvroPayload input)
70+
{
71+
final JsonNode node = schemaNode(input);
72+
final NodeType type = getType(node);
73+
74+
if (type != NodeType.OBJECT)
75+
return false;
76+
77+
final JsonNode digest = ObjectSchemaDigester.getInstance()
78+
.digest(node);
79+
80+
// FIXME: as for array digester, the result should really be
81+
// a POJO
82+
if (!digest.get("hasAdditional").booleanValue())
83+
return false;
84+
85+
return digest.get("properties").size() == 0
86+
&& digest.get("patternProperties").size() == 0;
87+
}
88+
};
89+
}
90+
6891
private static JsonNode schemaNode(final AvroPayload payload)
6992
{
7093
return payload.getTree().getNode();
7194
}
95+
96+
private static NodeType getType(final JsonNode node)
97+
{
98+
final JsonNode typeNode = node.path("type");
99+
return typeNode.isTextual() ? NodeType.fromName(typeNode.textValue())
100+
: null;
101+
}
72102
}

src/test/java/com/github/fge/AvroTesting.java

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.github.fge.jsonschema2avro;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.github.fge.jsonschema.exceptions.ProcessingException;
5+
import com.github.fge.jsonschema.processors.data.SchemaHolder;
6+
import com.github.fge.jsonschema.report.ProcessingReport;
7+
import com.github.fge.jsonschema.tree.CanonicalSchemaTree;
8+
import com.github.fge.jsonschema.tree.SchemaTree;
9+
import com.github.fge.jsonschema.util.JsonLoader;
10+
import com.google.common.collect.Lists;
11+
import org.apache.avro.Schema;
12+
import org.testng.annotations.BeforeTest;
13+
import org.testng.annotations.DataProvider;
14+
import org.testng.annotations.Test;
15+
16+
import java.io.IOException;
17+
import java.util.Iterator;
18+
import java.util.List;
19+
20+
import static org.mockito.Mockito.*;
21+
import static org.testng.Assert.assertEquals;
22+
23+
public abstract class AvroWriterProcessorTest
24+
{
25+
private final JsonNode testNode;
26+
27+
private AvroWriterProcessor processor;
28+
private ProcessingReport report;
29+
30+
protected AvroWriterProcessorTest(final String prefix)
31+
throws IOException
32+
{
33+
testNode = JsonLoader.fromResource("/jsonschema2avro/" + prefix
34+
+ ".json");
35+
}
36+
37+
@BeforeTest
38+
public final void init()
39+
{
40+
processor = new AvroWriterProcessor();
41+
report = mock(ProcessingReport.class);
42+
}
43+
44+
@DataProvider
45+
public final Iterator<Object[]> testData()
46+
{
47+
final List<Object[]> list = Lists.newArrayList();
48+
49+
for (final JsonNode element: testNode)
50+
list.add(new Object[] {
51+
element.get("jsonSchema"),
52+
element.get("avroSchema")
53+
});
54+
55+
return list.iterator();
56+
}
57+
58+
@Test(dataProvider = "testData")
59+
public final void JsonSchemasAreCorrectlyTranslated(final JsonNode schema,
60+
final JsonNode avro)
61+
throws ProcessingException
62+
{
63+
final SchemaTree tree = new CanonicalSchemaTree(schema);
64+
final SchemaHolder input = new SchemaHolder(tree);
65+
final Schema expected = new Schema.Parser().parse(avro.toString());
66+
67+
final Schema actual = processor.process(report, input).getValue();
68+
assertEquals(expected, actual);
69+
}
70+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.fge.jsonschema2avro;
2+
3+
import java.io.IOException;
4+
5+
public final class SimpleTypesTest
6+
extends AvroWriterProcessorTest
7+
{
8+
public SimpleTypesTest()
9+
throws IOException
10+
{
11+
super("simple");
12+
}
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
{
3+
"avroSchema": "null",
4+
"jsonSchema": { "type": "null" }
5+
},
6+
{
7+
"avroSchema": "boolean",
8+
"jsonSchema": { "type": "boolean" }
9+
},
10+
{
11+
"avroSchema": "long",
12+
"jsonSchema": {
13+
"type": "integer"
14+
}
15+
},
16+
{
17+
"avroSchema": "double",
18+
"jsonSchema": { "type": "number" }
19+
},
20+
{
21+
"avroSchema": "string",
22+
"jsonSchema": { "type": "string" }
23+
}
24+
]

0 commit comments

Comments
 (0)