forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionValueMatcherTest.java
More file actions
120 lines (104 loc) · 4.97 KB
/
RegularExpressionValueMatcherTest.java
File metadata and controls
120 lines (104 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.skyscreamer.jsonassert;
import org.junit.Assert;
import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.comparator.CustomComparator;
/**
* Unit tests for RegularExpressionValueMatcher
*
* @author Duncan Mackinder
*
*/
public class RegularExpressionValueMatcherTest {
private static final String ARRAY_ELEMENT_PREFIX = "d.results[0].__metadata.uri";
private static final String JSON_STRING_WITH_ARRAY = "{d:{results:[{__metadata:{uri:\"http://localhost:80/Person('1')\",type:Person},id:1}]}}";
private static final String CONSTANT_URI_REGEX_EXPECTED_JSON = "{d:{results:[{__metadata:{uri:X}}]}}";
private void doTest(String jsonPath, String regex, String expectedJSON,
String actualJSON) throws JSONException {
JSONAssert.assertEquals(expectedJSON, actualJSON, new CustomComparator(
JSONCompareMode.STRICT_ORDER, new Customization(jsonPath,
new RegularExpressionValueMatcher<Object>(regex))));
}
@Test
public void constantRegexWithSimplePathMatchsStringAttribute() throws JSONException {
doTest("a", "v.", "{a:x}", "{a:v1}");
}
@Test
public void constantRegexWithThreeLevelPathMatchsStringAttribute() throws JSONException {
doTest("a.b.c", ".*Is.*", "{a:{b:{c:x}}}", "{a:{b:{c:thisIsAString}}}");
}
@Test
public void dynamicRegexWithSimplePathMatchsStringAttribute() throws JSONException {
doTest("a", null, "{a:\"v.\"}", "{a:v1}");
}
@Test
public void dynamicRegexWithThreeLevelPathMatchsStringAttribute() throws JSONException {
doTest("a.b.c", null, "{a:{b:{c:\".*Is.*\"}}}",
"{a:{b:{c:thisIsAString}}}");
}
@Test
public void constantRegexMatchesStringAttributeInsideArray() throws JSONException {
doTest(ARRAY_ELEMENT_PREFIX, "http://localhost:80/Person\\('\\d+'\\)", CONSTANT_URI_REGEX_EXPECTED_JSON, JSON_STRING_WITH_ARRAY);
}
@Test
public void dynamicRegexMatchesStringAttributeInsideArray() throws JSONException {
doTest(ARRAY_ELEMENT_PREFIX, null, "{d:{results:[{__metadata:{uri:\"http://localhost:80/Person\\\\('\\\\d+'\\\\)\"}}]}}", JSON_STRING_WITH_ARRAY);
}
@Test
public void dynamicRegexMatchesStringAttributeInsideArrayWithNoArgConstructor() throws JSONException {
JSONAssert.assertEquals("{d:{results:[{__metadata:{uri:\"http://localhost:80/Person\\\\('\\\\d+'\\\\)\"}}]}}", JSON_STRING_WITH_ARRAY, new CustomComparator(
JSONCompareMode.STRICT_ORDER, new Customization(ARRAY_ELEMENT_PREFIX,
new RegularExpressionValueMatcher<Object>())));
}
@Test
public void failsWhenDynamicRegexInvalid() throws JSONException {
try {
doTest(ARRAY_ELEMENT_PREFIX, null, "{d:{results:[{__metadata:{uri:\"http://localhost:80/Person('\\\\d+'\\\\)\"}}]}}", JSON_STRING_WITH_ARRAY);
}
catch (AssertionError e) {
Assert.assertTrue("Invalid exception message returned: "+ e.getMessage(), e.getMessage().startsWith(ARRAY_ELEMENT_PREFIX + ": Dynamic expected pattern invalid: "));
}
}
@Test
public void failsWhenDynamicRegexDoesNotMatchStringAttributeInsideArray() throws JSONException {
try {
doTest(ARRAY_ELEMENT_PREFIX, null, "{d:{results:[{__metadata:{uri:\"http://localhost:80/Person\\\\('\\\\w+'\\\\)\"}}]}}", JSON_STRING_WITH_ARRAY);
}
catch (AssertionError e) {
Assert.assertTrue("Invalid exception message returned: "+ e.getMessage(), e.getMessage().startsWith(ARRAY_ELEMENT_PREFIX + ": Dynamic expected pattern did not match value"));
}
}
@Test
public void failsWhenConstantRegexInvalid() throws JSONException {
try {
doTest(ARRAY_ELEMENT_PREFIX, "http://localhost:80/Person\\\\['\\\\d+'\\\\)", CONSTANT_URI_REGEX_EXPECTED_JSON, JSON_STRING_WITH_ARRAY);
}
catch (IllegalArgumentException e) {
Assert.assertTrue("Invalid exception message returned: "+ e.getMessage(), e.getMessage().startsWith("Constant expected pattern invalid: "));
}
}
@Test
public void failsWhenConstantRegexDoesNotMatchStringAttributeInsideArray() throws JSONException {
try {
doTest(ARRAY_ELEMENT_PREFIX, "http://localhost:80/Person\\\\('\\\\w+'\\\\)", CONSTANT_URI_REGEX_EXPECTED_JSON, JSON_STRING_WITH_ARRAY);
}
catch (AssertionError e) {
Assert.assertTrue("Invalid exception message returned: "+ e.getMessage(), e.getMessage().startsWith(ARRAY_ELEMENT_PREFIX + ": Constant expected pattern did not match value"));
}
}
}