]> source.dussan.org Git - sonarqube.git/commitdiff
JsonAssert can ignore fields and small improvements
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 27 Aug 2015 15:00:25 +0000 (17:00 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 27 Aug 2015 15:01:24 +0000 (17:01 +0200)
server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesActionTest.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AvailableActionTest.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledActionTest.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PendingActionTest.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/PluginWSCommonsTest.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/UpdatesActionTest.java
sonar-testing-harness/src/main/java/org/sonar/test/JsonAssert.java
sonar-testing-harness/src/main/java/org/sonar/test/JsonComparison.java
sonar-testing-harness/src/test/java/org/sonar/test/JsonAssertTest.java
sonar-testing-harness/src/test/java/org/sonar/test/JsonComparisonTest.java

index 0f9f96525c98cd8f3ee1f8dd6c2009a2b26d8c5d..5c7bf51b6a69534241d6dd4bc443196f869e619a 100644 (file)
@@ -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"));
   }
 
index 2524a4962ce8dedc9635cc40ce1cfcc9233c3ba2..97335260c3210bddf6a42897464b89c7d56605b1 100644 (file)
@@ -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
index 6e0ba04f45c31615824d42507d0d9b684cc47194..5cec03674fb4cc6b4ebdec627e4e6e718dfc66d5 100644 (file)
@@ -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\":" +
         "  [" +
index b35c4c31c1a24defceeb7df742faf03db7b0f1f6..69ce2b3f46190a75a12d17d190990fb4fd5b8d0d 100644 (file)
@@ -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\": " +
index 146115b6a4ba957ab60771fd05c8829e8f3768f9..8eabde81459e075a19f81b770784038057440f97 100644 (file)
@@ -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\"" +
index 30c33a49e401790a75e738e958731a0c542f2417..54783222df91ad43703c117f4508bd9e4e619666 100644 (file)
@@ -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\": [" +
         "    {" +
index 9e6cc013149dabf22642f98370f46f9c6201ef9d..2f9023676d7e59ce099dba07caac08efd8fc619b 100644 (file)
@@ -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;
   }
 
index 8626253000d300165b47288e4f517bf1c4021372..7aab3e9eb679f694da7eecc03a47547edc7f7886 100644 (file)
  */
 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 {
index de21bc3068d95a93e475d699dacdff685c77a296..85d38aa816c49d2d6b6c316d015b9a98429b61d1 100644 (file)
  */
 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\"}");
   }
 }
index d640a76367d5adcef11d6c137f2117a07fb50319..d52fe865e672b868b402b9f7d056eefd5746f451 100644 (file)
@@ -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);
   }
 }