You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JsonAssert.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.test;
  21. import com.google.gson.GsonBuilder;
  22. import com.google.gson.JsonElement;
  23. import com.google.gson.JsonParser;
  24. import java.io.IOException;
  25. import java.net.URL;
  26. import java.nio.charset.StandardCharsets;
  27. import org.apache.commons.io.IOUtils;
  28. import org.junit.ComparisonFailure;
  29. /**
  30. * Assertion to compare JSON documents. Comparison is not strict:
  31. * <ul>
  32. * <li>formatting differences are ignored</li>
  33. * <li>order of elements in objects <code>{}</code> is not verified</li>
  34. * <li>objects can contain more elements than expected, for example <code>{"one":1, "two":2}</code>
  35. * matches <code>{"one":1}</code></li>
  36. * <li>order of elements in arrays <code>[]</code> is not verified by default, for example <code>[1, 2]</code>
  37. * matches <code>[2, 1]</code>. This mode can be disabled with {@link #withStrictArrayOrder()}</li>
  38. * <li>timezones in datetime values are not strictly verified, for example <code>{"foo": "2015-01-01T13:00:00+2000"}</code>
  39. * matches <code>{"foo": "2015-01-01T10:00:00-1000"}</code>. This feature can be disabled with
  40. * {@link #withStrictTimezone()}
  41. * </li>
  42. * </ul>
  43. *
  44. * <h3>Usage</h3>
  45. * <pre>
  46. * String actual = "{}";
  47. * String expected = "{}";
  48. * JsonAssert.assertJson(actual).isSimilarTo(expected);
  49. * </pre>
  50. *
  51. * <p>Expected JSON document can be loaded from URLs:</p>
  52. * <pre>
  53. * String actual = "{}";
  54. * JsonAssert.assertJson(actual).isSimilarTo(getClass().getResource("MyTest/expected.json"));
  55. * </pre>
  56. *
  57. * @since 5.2
  58. */
  59. public class JsonAssert {
  60. private final String actualJson;
  61. private final JsonComparison comparison = new JsonComparison();
  62. private JsonAssert(String actualJson) {
  63. this.actualJson = actualJson;
  64. }
  65. public JsonAssert withStrictTimezone() {
  66. comparison.withTimezone();
  67. return this;
  68. }
  69. public JsonAssert withStrictArrayOrder() {
  70. comparison.withStrictArrayOrder();
  71. return this;
  72. }
  73. public JsonAssert ignoreFields(String... ignoredFields) {
  74. comparison.setIgnoredFields(ignoredFields);
  75. return this;
  76. }
  77. public JsonAssert isSimilarTo(String expected) {
  78. boolean similar = comparison.areSimilar(expected, actualJson);
  79. if (!similar) {
  80. throw new ComparisonFailure("Not a super-set of expected JSON -", pretty(expected), pretty(actualJson));
  81. }
  82. return this;
  83. }
  84. public JsonAssert isSimilarTo(URL expected) {
  85. return isSimilarTo(urlToString(expected));
  86. }
  87. public JsonAssert isNotSimilarTo(String expected) {
  88. boolean similar = comparison.areSimilar(expected, actualJson);
  89. if (similar) {
  90. throw new ComparisonFailure("It's a super-set of expected JSON -", pretty(expected), pretty(actualJson));
  91. }
  92. return this;
  93. }
  94. public JsonAssert isNotSimilarTo(URL expected) {
  95. return isNotSimilarTo(urlToString(expected));
  96. }
  97. public static JsonAssert assertJson(String actualJson) {
  98. return new JsonAssert(actualJson);
  99. }
  100. public static JsonAssert assertJson(URL actualJson) {
  101. return new JsonAssert(urlToString(actualJson));
  102. }
  103. private static String urlToString(URL url) {
  104. try {
  105. return IOUtils.toString(url, StandardCharsets.UTF_8);
  106. } catch (IOException e) {
  107. throw new IllegalStateException("Fail to load JSON from " + url, e);
  108. }
  109. }
  110. private static String pretty(String json) {
  111. JsonElement gson = new JsonParser().parse(json);
  112. return new GsonBuilder().setPrettyPrinting().serializeNulls().create().toJson(gson);
  113. }
  114. }