org.sonar.test.JsonAssert Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-testing-harness Show documentation
Show all versions of sonar-testing-harness Show documentation
Open source platform for continuous inspection of code quality
/*
* SonarQube
* Copyright (C) 2009-2023 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.test;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
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:
*
* - formatting differences are ignored
* - order of elements in objects
{}
is not verified
* - objects can contain more elements than expected, for example
{"one":1, "two":2}
* matches {"one":1}
* - order of elements in arrays
[]
is not verified by default, for example [1, 2]
* matches [2, 1]
. This mode can be disabled with {@link #withStrictArrayOrder()}
* - timezones in datetime values are not strictly verified, for example
{"foo": "2015-01-01T13:00:00+2000"}
* matches {"foo": "2015-01-01T10:00:00-1000"}
. This feature can be disabled with
* {@link #withStrictTimezone()}
*
*
*
* Usage
*
* String actual = "{}";
* String expected = "{}";
* JsonAssert.assertJson(actual).isSimilarTo(expected);
*
*
* Expected JSON document can be loaded from URLs:
*
* String actual = "{}";
* JsonAssert.assertJson(actual).isSimilarTo(getClass().getResource("MyTest/expected.json"));
*
*
* @since 5.2
*/
public class JsonAssert {
private final String actualJson;
private final JsonComparison comparison = new JsonComparison();
private JsonAssert(String actualJson) {
this.actualJson = actualJson;
}
public JsonAssert withStrictTimezone() {
comparison.withTimezone();
return this;
}
public JsonAssert withStrictArrayOrder() {
comparison.withStrictArrayOrder();
return this;
}
public JsonAssert ignoreFields(String... ignoredFields) {
comparison.setIgnoredFields(ignoredFields);
return this;
}
public JsonAssert isSimilarTo(String expected) {
boolean similar = comparison.areSimilar(expected, actualJson);
if (!similar) {
throw new ComparisonFailure("Not a super-set of expected JSON -", pretty(expected), pretty(actualJson));
}
return this;
}
public JsonAssert isSimilarTo(URL expected) {
return isSimilarTo(urlToString(expected));
}
public JsonAssert isNotSimilarTo(String expected) {
boolean similar = comparison.areSimilar(expected, actualJson);
if (similar) {
throw new ComparisonFailure("It's a super-set of expected JSON -", pretty(expected), pretty(actualJson));
}
return this;
}
public JsonAssert isNotSimilarTo(URL expected) {
return isNotSimilarTo(urlToString(expected));
}
public static JsonAssert assertJson(String actualJson) {
return new JsonAssert(actualJson);
}
public static JsonAssert assertJson(URL actualJson) {
return new JsonAssert(urlToString(actualJson));
}
private static String urlToString(URL url) {
try {
return IOUtils.toString(url, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException("Fail to load JSON from " + url, e);
}
}
private static String pretty(String json) {
JsonElement gson = new JsonParser().parse(json);
return new GsonBuilder().setPrettyPrinting().serializeNulls().create().toJson(gson);
}
}