Skip to content

Commit fcb7d3f

Browse files
committed
adding toString() implementations to CombinedSchema events
1 parent 50f6679 commit fcb7d3f

File tree

7 files changed

+61
-8
lines changed

7 files changed

+61
-8
lines changed

core/src/main/java/org/everit/json/schema/FalseSchema.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public static Builder builder() {
1818
return new Builder();
1919
}
2020

21+
public static final FalseSchema INSTANCE = FalseSchema.builder().build();
22+
2123
/**
2224
* Constructor.
2325
*

core/src/main/java/org/everit/json/schema/listener/CombinedSchemaMatchEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public CombinedSchemaMatchEvent(CombinedSchema schema, Schema subSchema,
1313

1414
@Override void describeTo(JSONObject obj) {
1515
obj.put("type", "match");
16+
obj.put("keyword", schema.getCriterion().toString());
1617
}
1718

1819
@Override public boolean equals(Object o) {

core/src/main/java/org/everit/json/schema/listener/CombinedSchemaMismatchEvent.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.everit.json.schema.Schema;
77
import org.everit.json.schema.ValidationException;
88
import org.json.JSONObject;
9+
import org.json.JSONTokener;
910

1011
public class CombinedSchemaMismatchEvent extends CombinedSchemaValidationEvent implements MismatchEvent {
1112

@@ -21,7 +22,10 @@ public CombinedSchemaMismatchEvent(CombinedSchema schema, Schema subSchema, Obje
2122
}
2223

2324
@Override void describeTo(JSONObject obj) {
24-
throw new UnsupportedOperationException("not yet implemented");
25+
obj.put("type", "mismatch");
26+
obj.put("keyword", schema.getCriterion().toString());
27+
obj.put("subSchema", new JSONTokener(subSchema.toString()).nextValue());
28+
obj.put("failure", failure.toJSON());
2529
}
2630

2731
@Override public boolean equals(Object o) {

core/src/main/java/org/everit/json/schema/listener/CombinedSchemaValidationEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public abstract class CombinedSchemaValidationEvent extends ValidationEvent<CombinedSchema> {
99

10-
private final Schema subSchema;
10+
final Schema subSchema;
1111

1212
public CombinedSchemaValidationEvent(CombinedSchema schema, Schema subSchema, Object instance) {
1313
super(schema, instance);

core/src/test/java/org/everit/json/schema/listener/EventToStringTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package org.everit.json.schema.listener;
22

3+
import static java.util.Arrays.asList;
34
import static org.everit.json.schema.JSONMatcher.sameJsonAs;
45
import static org.everit.json.schema.listener.ConditionalSchemaValidationEvent.Keyword.IF;
5-
import static org.everit.json.schema.listener.ConditionalSchemaValidationEvent.Keyword.THEN;
66
import static org.junit.Assert.assertThat;
77

8+
import org.everit.json.schema.CombinedSchema;
89
import org.everit.json.schema.ConditionalSchema;
10+
import org.everit.json.schema.FalseSchema;
911
import org.everit.json.schema.ReferenceSchema;
1012
import org.everit.json.schema.ResourceLoader;
1113
import org.everit.json.schema.StringSchema;
1214
import org.everit.json.schema.TrueSchema;
1315
import org.everit.json.schema.ValidationException;
1416
import org.json.JSONArray;
1517
import org.json.JSONObject;
16-
import org.junit.Ignore;
1718
import org.junit.Test;
1819

1920
public class EventToStringTest {
@@ -33,6 +34,12 @@ public class EventToStringTest {
3334
private static final ConditionalSchema CONDITIONAL_SCHEMA = ConditionalSchema.builder()
3435
.ifSchema(TrueSchema.builder().build())
3536
.build();
37+
public static final CombinedSchema COMBINED_SCHEMA = CombinedSchema.builder()
38+
.criterion(CombinedSchema.ANY_CRITERION)
39+
.subschemas(asList(
40+
TrueSchema.INSTANCE,
41+
FalseSchema.INSTANCE
42+
)).build();
3643

3744
static {
3845
INSTANCE.put("hello", new JSONArray("[\"world\"]"));
@@ -86,12 +93,24 @@ public void conditionalSchemaMismatchEvent() {
8693
assertThat(actual, sameJsonAs(expected));
8794
}
8895

89-
@Test @Ignore
90-
public void combinedSchemaMatchEventToString_withBooleanSchema() {
91-
JSONObject expected = LOADER.readObj("conditional-match-then-boolean.json");
92-
ConditionalSchemaMatchEvent subject = new ConditionalSchemaMatchEvent(CONDITIONAL_SCHEMA, INSTANCE, THEN);
96+
@Test
97+
public void combinedSchemaMatchEventToString() {
98+
JSONObject expected = LOADER.readObj("combined-schema-match.json");
99+
CombinedSchemaMatchEvent subject = new CombinedSchemaMatchEvent(COMBINED_SCHEMA, TrueSchema.INSTANCE, INSTANCE);
100+
101+
JSONObject actual = new JSONObject(subject.toString());
102+
103+
assertThat(actual, sameJsonAs(expected));
104+
}
105+
106+
@Test
107+
public void combinedSchemaMismatchEventToString() {
108+
JSONObject expected = LOADER.readObj("combined-schema-mismatch.json");
109+
ValidationException exc = new ValidationException(COMBINED_SCHEMA, "message", "anyOf", "#/schema/location");
110+
CombinedSchemaMismatchEvent subject = new CombinedSchemaMismatchEvent(COMBINED_SCHEMA, FalseSchema.INSTANCE, INSTANCE, exc);
93111

94112
JSONObject actual = new JSONObject(subject.toJSON(true, true).toString());
113+
95114
assertThat(actual, sameJsonAs(expected));
96115
}
97116

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "match",
3+
"keyword": "anyOf"
4+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"type": "mismatch",
3+
"keyword": "anyOf",
4+
"schema": {
5+
"anyOf": [
6+
{},
7+
false
8+
]
9+
},
10+
"subSchema": false,
11+
"instance": {
12+
"hello": [
13+
"world"
14+
]
15+
},
16+
"failure": {
17+
"schemaLocation": "#/schema/location",
18+
"pointerToViolation": "#",
19+
"causingExceptions": [],
20+
"keyword": "anyOf",
21+
"message": "message"
22+
}
23+
}

0 commit comments

Comments
 (0)