diff options
10 files changed, 90 insertions, 71 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesActionTest.java index 0f9f96525c9..5c7bf51b6a6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesActionTest.java @@ -85,7 +85,7 @@ public class UpgradesActionTest { public void empty_array_is_returned_when_there_is_no_upgrade_available() throws Exception { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_UPGRADE_LIST); + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_UPGRADE_LIST); } @Test @@ -94,7 +94,7 @@ public class UpgradesActionTest { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_UPGRADE_LIST); + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_UPGRADE_LIST); } @Test @@ -104,7 +104,7 @@ public class UpgradesActionTest { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true) + assertJson(response.outputAsString()).withStrictArrayOrder() .isSimilarTo(getClass().getResource("example-upgrades_plugins.json")); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AvailableActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AvailableActionTest.java index 2524a4962ce..97335260c32 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AvailableActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AvailableActionTest.java @@ -80,7 +80,7 @@ public class AvailableActionTest extends AbstractUpdateCenterBasedPluginsWsActio public void empty_array_is_returned_when_there_is_no_plugin_available() throws Exception { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_PLUGIN_LIST); + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_PLUGIN_LIST); } @Test @@ -89,7 +89,7 @@ public class AvailableActionTest extends AbstractUpdateCenterBasedPluginsWsActio underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_PLUGIN_LIST); + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_PLUGIN_LIST); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledActionTest.java index 6e0ba04f45c..5cec03674fb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledActionTest.java @@ -81,7 +81,7 @@ public class InstalledActionTest { public void empty_array_is_returned_when_there_is_not_plugin_installed() throws Exception { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_PLUGIN_LIST); + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_PLUGIN_LIST); } @Test @@ -90,7 +90,7 @@ public class InstalledActionTest { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_PLUGIN_LIST); + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_PLUGIN_LIST); } @Test @@ -169,7 +169,7 @@ public class InstalledActionTest { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo( + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo( "{" + " \"plugins\":" + " [" + @@ -193,7 +193,7 @@ public class InstalledActionTest { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo( + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo( "{" + " \"plugins\":" + " [" + diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PendingActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PendingActionTest.java index b35c4c31c1a..69ce2b3f461 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PendingActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PendingActionTest.java @@ -72,7 +72,7 @@ public class PendingActionTest { public void empty_arrays_are_returned_when_there_nothing_pending() throws Exception { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo( + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo( "{" + " \"installing\": []," + " \"removing\": []" + @@ -86,7 +86,7 @@ public class PendingActionTest { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo( + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo( "{" + " \"installing\": []," + " \"removing\": []" + @@ -169,7 +169,7 @@ public class PendingActionTest { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo( + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo( "{" + " \"installing\": " + " [" + @@ -201,7 +201,7 @@ public class PendingActionTest { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo( + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo( "{" + " \"installing\": []," + " \"removing\": " + diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PluginWSCommonsTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PluginWSCommonsTest.java index 146115b6a4b..8eabde81459 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PluginWSCommonsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PluginWSCommonsTest.java @@ -48,7 +48,7 @@ public class PluginWSCommonsTest { underTest.writePluginInfo(jsonWriter, gitPluginInfo(), null); jsonWriter.close(); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" + + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo("{" + " \"key\": \"scmgit\"," + " \"name\": \"Git\"," + " \"description\": \"Git SCM Provider.\"," + @@ -66,7 +66,7 @@ public class PluginWSCommonsTest { underTest.writePluginInfo(jsonWriter, gitPluginInfo(), "cat_1"); jsonWriter.close(); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" + + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo("{" + " \"key\": \"scmgit\"," + " \"name\": \"Git\"," + " \"description\": \"Git SCM Provider.\"," + @@ -127,7 +127,7 @@ public class PluginWSCommonsTest { jsonWriter.endObject(); jsonWriter.close(); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" + + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo("{" + " \"release\": {" + " \"version\": \"1.0\"," + " \"date\": \"2015-04-16\"," + @@ -144,7 +144,7 @@ public class PluginWSCommonsTest { jsonWriter.endObject(); jsonWriter.close(); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" + + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo("{" + " \"artifact\": {" + " \"name\": \"file.jar\"," + " \"url\": \"http://toto.com/file.jar\"" + diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/UpdatesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/UpdatesActionTest.java index 30c33a49e40..54783222df9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/UpdatesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/UpdatesActionTest.java @@ -95,7 +95,7 @@ public class UpdatesActionTest extends AbstractUpdateCenterBasedPluginsWsActionT public void empty_array_is_returned_when_there_is_no_plugin_available() throws Exception { underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_PLUGIN_LIST); + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_PLUGIN_LIST); } @Test @@ -145,7 +145,7 @@ public class UpdatesActionTest extends AbstractUpdateCenterBasedPluginsWsActionT underTest.handle(request, response); - assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo( + assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo( "{" + " \"plugins\": [" + " {" + 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); } } |