Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky test by using JSONAssert #2881

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bundles/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,12 @@
<version>4.7.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.caconfig.ConfigurationBuilder;
import org.mockito.Mockito;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

import com.adobe.cq.wcm.core.components.internal.DataLayerConfig;
import com.adobe.cq.wcm.core.components.internal.jackson.DefaultMethodSkippingModuleProvider;
Expand Down Expand Up @@ -68,11 +70,22 @@ public static void testJSONExport(Object model, String expectedJsonResource) {
} catch (IOException e) {
fail(String.format("Unable to generate JSON export for model %s: %s", model.getClass().getName(), e.getMessage()));
}
JsonReader outputReader = Json.createReader(IOUtils.toInputStream(writer.toString(), StandardCharsets.UTF_8));
String actualJString = writer.toString();
InputStream is = Utils.class.getResourceAsStream(expectedJsonResource);
if (is != null) {
JsonReader expectedReader = Json.createReader(is);
assertEquals(expectedReader.read(), outputReader.read());
String expectedJString = "";
try {
expectedJString = IOUtils.toString(is, StandardCharsets.UTF_8);
}
catch(IOException e) {
fail("Error reading expected JSON resource: " + e.getMessage());
}
try {
JSONAssert.assertEquals(expectedJString, actualJString, JSONCompareMode.NON_EXTENSIBLE);
}
catch (org.json.JSONException e) {
fail(e.getMessage() + ": expected: " + expectedJString + " but was: " + actualJString);
}
} else {
fail("Unable to find test file " + expectedJsonResource + ".");
}
Expand Down