diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-08-27 17:00:25 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-08-27 17:01:24 +0200 |
commit | 73a38b94539a3f69c084c210cb2df9287d2c00e2 (patch) | |
tree | bfcfbcbc3edadfda8365c93e3e16adf9e6d407af /sonar-testing-harness/src | |
parent | aba9715aa0e4fcf433f3f7639098823e95db8c83 (diff) | |
download | sonarqube-73a38b94539a3f69c084c210cb2df9287d2c00e2.tar.gz sonarqube-73a38b94539a3f69c084c210cb2df9287d2c00e2.zip |
JsonAssert can ignore fields and small improvements
Diffstat (limited to 'sonar-testing-harness/src')
4 files changed, 71 insertions, 52 deletions
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/JsonAssert.java b/sonar-testing-harness/src/main/java/org/sonar/test/JsonAssert.java index 9e6cc013149..2f9023676d7 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/JsonAssert.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/JsonAssert.java @@ -22,12 +22,11 @@ package org.sonar.test; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonParser; -import org.apache.commons.io.IOUtils; -import org.junit.ComparisonFailure; - import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; +import org.apache.commons.io.IOUtils; +import org.junit.ComparisonFailure; /** * Assertion to compare JSON documents. Comparison is not strict: @@ -37,10 +36,10 @@ import java.nio.charset.StandardCharsets; * <li>objects can contain more elements than expected, for example <code>{"one":1, "two":2}</code> * matches <code>{"one":1}</code></li> * <li>order of elements in arrays <code>[]</code> is not verified by default, for example <code>[1, 2]</code> - * matches <code>[2, 1]</code>. This mode can be disabled with {@link #setStrictArrayOrder(boolean)}</li> + * matches <code>[2, 1]</code>. This mode can be disabled with {@link #withStrictArrayOrder()}</li> * <li>timezones in datetime values are not strictly verified, for example <code>{"foo": "2015-01-01T13:00:00+2000"}</code> * matches <code>{"foo": "2015-01-01T10:00:00-1000"}</code>. This feature can be disabled with - * {@link #setStrictTimezone(boolean)} + * {@link #withStrictTimezone()} * </li> * </ul> * @@ -68,13 +67,18 @@ public class JsonAssert { this.actualJson = actualJson; } - public JsonAssert setStrictTimezone(boolean b) { - comparison.setStrictTimezone(b); + public JsonAssert withStrictTimezone() { + comparison.withTimezone(); + return this; + } + + public JsonAssert withStrictArrayOrder() { + comparison.withStrictArrayOrder(); return this; } - public JsonAssert setStrictArrayOrder(boolean b) { - comparison.setStrictArrayOrder(b); + public JsonAssert ignoreFields(String... ignoredFields) { + comparison.setIgnoredFields(ignoredFields); return this; } diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/JsonComparison.java b/sonar-testing-harness/src/main/java/org/sonar/test/JsonComparison.java index 8626253000d..7aab3e9eb67 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/JsonComparison.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/JsonComparison.java @@ -19,34 +19,37 @@ */ package org.sonar.test; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import javax.annotation.concurrent.ThreadSafe; - import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import javax.annotation.concurrent.ThreadSafe; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; + +import static java.util.Collections.synchronizedSet; @ThreadSafe class JsonComparison { private boolean strictTimezone = false; private boolean strictArrayOrder = false; + private Set<String> ignoredFields = synchronizedSet(new HashSet<String>()); boolean isStrictTimezone() { return strictTimezone; } - JsonComparison setStrictTimezone(boolean b) { - this.strictTimezone = b; + JsonComparison withTimezone() { + this.strictTimezone = true; return this; } @@ -54,8 +57,13 @@ class JsonComparison { return strictArrayOrder; } - JsonComparison setStrictArrayOrder(boolean b) { - this.strictArrayOrder = b; + JsonComparison withStrictArrayOrder() { + this.strictArrayOrder = true; + return this; + } + + JsonComparison setIgnoredFields(String... ignoredFields) { + Collections.addAll(this.ignoredFields, ignoredFields); return this; } @@ -177,6 +185,9 @@ class JsonComparison { // each key-value of expected map must exist in actual map for (Map.Entry<Object, Object> expectedEntry : (Set<Map.Entry<Object, Object>>) expectedMap.entrySet()) { Object key = expectedEntry.getKey(); + if (shouldIgnoreField(key)) { + continue; + } if (!actualMap.containsKey(key)) { return false; } @@ -187,6 +198,10 @@ class JsonComparison { return true; } + private boolean shouldIgnoreField(Object key) { + return key instanceof String && ignoredFields.contains((String) key); + } + @CheckForNull static Date tryParseDate(String s) { try { diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/JsonAssertTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/JsonAssertTest.java index de21bc3068d..85d38aa816c 100644 --- a/sonar-testing-harness/src/test/java/org/sonar/test/JsonAssertTest.java +++ b/sonar-testing-harness/src/test/java/org/sonar/test/JsonAssertTest.java @@ -19,11 +19,12 @@ */ package org.sonar.test; -import org.junit.ComparisonFailure; -import org.junit.Test; - import java.io.File; import java.net.URL; +import org.junit.ComparisonFailure; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -31,6 +32,9 @@ import static org.sonar.test.JsonAssert.assertJson; public class JsonAssertTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Test public void isSimilarAs_strings() { assertJson("{}").isSimilarTo("{}"); @@ -50,48 +54,44 @@ public class JsonAssertTest { URL url1 = getClass().getResource("JsonAssertTest/sample1.json"); URL url2 = getClass().getResource("JsonAssertTest/sample2.json"); assertJson(url1).isSimilarTo(url1); + expectedException.expect(AssertionError.class); - try { - assertJson(url1).isSimilarTo(url2); - fail(); - } catch (AssertionError error) { - // ok - } + assertJson(url1).isSimilarTo(url2); } @Test public void actual_can_be_superset_of_expected() { assertJson("{\"foo\": \"bar\"}").isSimilarTo("{}"); - try { - assertJson("{}").isSimilarTo("{\"foo\": \"bar\"}"); - fail(); - } catch (AssertionError error) { - // ok - } + expectedException.expect(AssertionError.class); + + assertJson("{}").isSimilarTo("{\"foo\": \"bar\"}"); } - @Test(expected = IllegalStateException.class) + @Test public void fail_to_load_url() throws Exception { + expectedException.expect(IllegalStateException.class); + assertJson(new File("target/missing").toURI().toURL()); } @Test public void enable_strict_order_of_arrays() { - try { - assertJson("[1,2]").setStrictArrayOrder(true).isSimilarTo("[2, 1]"); - fail(); - } catch (AssertionError error) { - // ok - } + expectedException.expect(AssertionError.class); + + assertJson("[1,2]").withStrictArrayOrder().isSimilarTo("[2, 1]"); } @Test public void enable_strict_timezone() { - try { - assertJson("[\"2010-05-18T15:50:45+0100\"]").setStrictTimezone(true).isSimilarTo("[\"2010-05-18T16:50:45+0200\"]"); - fail(); - } catch (AssertionError error) { - // ok - } + expectedException.expect(AssertionError.class); + + assertJson("[\"2010-05-18T15:50:45+0100\"]").withStrictTimezone().isSimilarTo("[\"2010-05-18T16:50:45+0200\"]"); + } + + @Test + public void ignore_fields() { + assertJson("{\"foo\": \"bar\"}") + .ignoreFields("ignore-me") + .isSimilarTo("{\"foo\": \"bar\", \"ignore-me\": \"value\"}"); } } diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/JsonComparisonTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/JsonComparisonTest.java index d640a76367d..d52fe865e67 100644 --- a/sonar-testing-harness/src/test/java/org/sonar/test/JsonComparisonTest.java +++ b/sonar-testing-harness/src/test/java/org/sonar/test/JsonComparisonTest.java @@ -137,7 +137,7 @@ public class JsonComparisonTest { @Test public void strict_timezone() { - assertThat(new JsonComparison().setStrictTimezone(true).isStrictTimezone()).isTrue(); + assertThat(new JsonComparison().withTimezone().isStrictTimezone()).isTrue(); // same instant, same timezone assertThat(isSimilar_strict_timezone("{\"foo\": \"2010-05-18T15:50:45+0100\"}", "{\"foo\": \"2010-05-18T15:50:45+0100\"}")).isTrue(); @@ -169,10 +169,10 @@ public class JsonComparisonTest { } private boolean isSimilar_strict_timezone(String expected, String actual) { - return new JsonComparison().setStrictTimezone(true).areSimilar(expected, actual); + return new JsonComparison().withTimezone().areSimilar(expected, actual); } private boolean isSimilar_strict_array_order(String expected, String actual) { - return new JsonComparison().setStrictArrayOrder(true).areSimilar(expected, actual); + return new JsonComparison().withStrictArrayOrder().areSimilar(expected, actual); } } |