|
| 1 | +package org.everit.json.schema.loader; |
| 2 | + |
| 3 | +import static java.util.Arrays.asList; |
| 4 | +import static java.util.Collections.emptyList; |
| 5 | +import static java.util.Collections.emptyMap; |
| 6 | +import static java.util.Collections.singleton; |
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | +import static org.junit.Assert.assertFalse; |
| 9 | +import static org.junit.Assert.assertTrue; |
| 10 | +import static org.mockito.Matchers.any; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | +import static org.mockito.Mockito.never; |
| 13 | +import static org.mockito.Mockito.verify; |
| 14 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 15 | + |
| 16 | +import java.util.HashSet; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.function.Consumer; |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +import org.everit.json.schema.SchemaException; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import com.google.common.collect.ImmutableMap; |
| 25 | + |
| 26 | +public class ProjectedJsonObjectTest { |
| 27 | + |
| 28 | + private JsonObject original = new JsonObject(ImmutableMap.<String, Object>builder() |
| 29 | + .put("minimum", 2) |
| 30 | + .put("maximum", 20) |
| 31 | + .put("not", ImmutableMap.<String, Object>builder() |
| 32 | + .put("multipleOf", 3) |
| 33 | + .put("maximum", 5) |
| 34 | + .build()) |
| 35 | + .build()); |
| 36 | + |
| 37 | + { |
| 38 | + original.ls = new LoadingState(LoaderConfig.defaultV4Config(), emptyMap(), original, original, null, emptyList()); |
| 39 | + } |
| 40 | + |
| 41 | + private ProjectedJsonObject createSubject() { |
| 42 | + return new ProjectedJsonObject(original, |
| 43 | + new HashSet<>(asList("minimum", "maximum"))); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void childForReturnsJsonObject_withoutProjection() { |
| 48 | + JsonValue actual = createSubject().childFor("not"); |
| 49 | + assertEquals(original.childFor("not"), actual); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void containsKeySuccess() { |
| 54 | + assertTrue(createSubject().containsKey("not")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void containsKeyIsFalseForHiddenKeys() { |
| 59 | + assertFalse(createSubject().containsKey("minimum")); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void requireWithConsumerSucceeds() { |
| 64 | + Consumer<JsonValue> consumer = mock(Consumer.class); |
| 65 | + createSubject().require("not", consumer); |
| 66 | + verify(consumer).accept(JsonValue.of(original.get("not"))); |
| 67 | + } |
| 68 | + |
| 69 | + @Test(expected = SchemaException.class) |
| 70 | + public void requireWithConsumerHidesKeys() { |
| 71 | + Consumer<JsonValue> consumer = mock(Consumer.class); |
| 72 | + createSubject().require("minimum", consumer); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void requireSucceeds() { |
| 77 | + JsonValue actual = createSubject().require("not"); |
| 78 | + assertEquals(JsonValue.of(original.get("not")), actual); |
| 79 | + } |
| 80 | + |
| 81 | + @Test(expected = SchemaException.class) |
| 82 | + public void requireHidesKeys() { |
| 83 | + createSubject().require("minimum"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void requireMappingSucceeds() { |
| 88 | + Boolean actual = createSubject().requireMapping("not", val -> true); |
| 89 | + assertTrue(actual); |
| 90 | + } |
| 91 | + |
| 92 | + @Test(expected = SchemaException.class) |
| 93 | + public void requireMappingFailsForHiddenKey() { |
| 94 | + createSubject().requireMapping("minimum", val -> true); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void requireObjectSuccess() { |
| 99 | + Function<JsonObject, Object> mapper = mock(Function.class); |
| 100 | + ProjectedJsonObject subject = createSubject(); |
| 101 | + subject.requireObject(mapper); |
| 102 | + verify(mapper).apply(subject); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void maybeSucceeds() { |
| 107 | + assertTrue(createSubject().maybe("not").isPresent()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void maybeHidesHiddenKeys() { |
| 112 | + assertFalse(createSubject().maybe("minimum").isPresent()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void maybeWithConsumerSucceeds() { |
| 117 | + Consumer<JsonValue> consumer = mock(Consumer.class); |
| 118 | + createSubject().maybe("not", consumer); |
| 119 | + verify(consumer).accept(JsonValue.of(original.get("not"))); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void maybeWithConsumerHidesHiddenKeys() { |
| 124 | + Consumer<JsonValue> consumer = mock(Consumer.class); |
| 125 | + createSubject().maybe("minimum", consumer); |
| 126 | + verify(consumer, never()).accept(any()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void maybeMappingSucceeds() { |
| 131 | + assertTrue(createSubject().maybeMapping("not", val -> true).isPresent()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void maybeMappingHidesKey() { |
| 136 | + assertFalse(createSubject().maybeMapping("minimum", val -> true).isPresent()); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void forEachOmitsHiddenKeys() { |
| 141 | + JsonObjectIterator iterator = mock(JsonObjectIterator.class); |
| 142 | + createSubject().forEach(iterator); |
| 143 | + verify(iterator).apply("not", JsonValue.of(original.get("not"))); |
| 144 | + verifyNoMoreInteractions(iterator); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void keySetExcludesHiddenKeys() { |
| 149 | + assertEquals(singleton("not"), createSubject().keySet()); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void isEmptyTakesHiddenKeysIntoAccount() { |
| 154 | + assertFalse(createSubject().isEmpty()); |
| 155 | + |
| 156 | + ProjectedJsonObject subject = new ProjectedJsonObject(new JsonObject(ImmutableMap.<String, Object>builder() |
| 157 | + .put("minimum", 2) |
| 158 | + .put("maximum", 20).build()), new HashSet<>(asList("minimum", "maximum"))); |
| 159 | + assertTrue(subject.isEmpty()); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void unwrapExcludesHiddenKeys() { |
| 164 | + Map<String, Object> unwrapped = (Map<String, Object>) createSubject().unwrap(); |
| 165 | + assertEquals(singleton("not"), unwrapped.keySet()); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void toMapExcludesHiddenKeys() { |
| 170 | + Map<String, Object> map = createSubject().toMap(); |
| 171 | + assertEquals(singleton("not"), map.keySet()); |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments