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
underTest.handle(request, response);
- assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_UPGRADE_LIST);
+ assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_UPGRADE_LIST);
}
@Test
underTest.handle(request, response);
- assertJson(response.outputAsString()).setStrictArrayOrder(true)
+ assertJson(response.outputAsString()).withStrictArrayOrder()
.isSimilarTo(getClass().getResource("example-upgrades_plugins.json"));
}
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
underTest.handle(request, response);
- assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_PLUGIN_LIST);
+ assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_PLUGIN_LIST);
}
@Test
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
underTest.handle(request, response);
- assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(JSON_EMPTY_PLUGIN_LIST);
+ assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_PLUGIN_LIST);
}
@Test
underTest.handle(request, response);
- assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(
+ assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(
"{" +
" \"plugins\":" +
" [" +
underTest.handle(request, response);
- assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(
+ assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(
"{" +
" \"plugins\":" +
" [" +
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\": []" +
underTest.handle(request, response);
- assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(
+ assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(
"{" +
" \"installing\": []," +
" \"removing\": []" +
underTest.handle(request, response);
- assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(
+ assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(
"{" +
" \"installing\": " +
" [" +
underTest.handle(request, response);
- assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(
+ assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(
"{" +
" \"installing\": []," +
" \"removing\": " +
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.\"," +
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.\"," +
jsonWriter.endObject();
jsonWriter.close();
- assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" +
+ assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo("{" +
" \"release\": {" +
" \"version\": \"1.0\"," +
" \"date\": \"2015-04-16\"," +
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\"" +
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
underTest.handle(request, response);
- assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo(
+ assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(
"{" +
" \"plugins\": [" +
" {" +
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:
* <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>
*
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;
}
*/
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;
}
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;
}
// 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;
}
return true;
}
+ private boolean shouldIgnoreField(Object key) {
+ return key instanceof String && ignoredFields.contains((String) key);
+ }
+
@CheckForNull
static Date tryParseDate(String s) {
try {
*/
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;
public class JsonAssertTest {
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
@Test
public void isSimilarAs_strings() {
assertJson("{}").isSimilarTo("{}");
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\"}");
}
}
@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();
}
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);
}
}