Skip to content

Commit 5abd12f

Browse files
uarlouskibaev
authored andcommitted
rft use custom render in case when custom publisher is used (via allure-framework#333)
1 parent fc31536 commit 5abd12f

File tree

8 files changed

+355
-92
lines changed

8 files changed

+355
-92
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2019 Qameta Software OÜ
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure.jsonunit;
17+
18+
import java.math.BigDecimal;
19+
20+
import org.hamcrest.Matcher;
21+
22+
import net.javacrumbs.jsonunit.core.Configuration;
23+
import net.javacrumbs.jsonunit.core.Option;
24+
import net.javacrumbs.jsonunit.core.internal.Diff;
25+
import net.javacrumbs.jsonunit.core.internal.Options;
26+
import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
27+
28+
/**
29+
* Сontains basic matcher functionality and implementation of methods for matching configuration.
30+
*
31+
* @param <T> the type
32+
*/
33+
@SuppressWarnings("unchecked")
34+
public abstract class AbstractJsonPatchMatcher<T> {
35+
36+
private static final String EMPTY_PATH = "";
37+
private static final String FULL_JSON = "fullJson";
38+
39+
private Configuration configuration = Configuration.empty();
40+
private String differences;
41+
42+
public T withTolerance(final BigDecimal tolerance) {
43+
this.configuration = configuration.withTolerance(tolerance);
44+
return (T) this;
45+
}
46+
47+
public T withTolerance(final double tolerance) {
48+
this.configuration = configuration.withTolerance(tolerance);
49+
return (T) this;
50+
}
51+
52+
public T when(final Option first, final Option... next) {
53+
this.configuration = configuration.when(first, next);
54+
return (T) this;
55+
}
56+
57+
public T withOptions(final Options options) {
58+
this.configuration = configuration.withOptions(options);
59+
return (T) this;
60+
}
61+
62+
public T withMatcher(final String matcherName, final Matcher matcher) {
63+
this.configuration = configuration.withMatcher(matcherName, matcher);
64+
return (T) this;
65+
}
66+
67+
public T whenIgnoringPaths(final String... paths) {
68+
this.configuration = configuration.whenIgnoringPaths(paths);
69+
return (T) this;
70+
}
71+
72+
public T withDifferenceListener(final DifferenceListener differenceListener) {
73+
this.configuration = configuration.withDifferenceListener(differenceListener);
74+
return (T) this;
75+
}
76+
77+
public boolean matches(final Object expected, final Object actual) {
78+
final Diff diff = Diff.create(expected, actual, FULL_JSON, EMPTY_PATH, configuration);
79+
final boolean similar = diff.similar();
80+
if (!similar) {
81+
differences = diff.differences();
82+
render(configuration.getDifferenceListener());
83+
}
84+
return similar;
85+
}
86+
87+
protected abstract void render(DifferenceListener listener);
88+
89+
public String getDifferences() {
90+
return differences;
91+
}
92+
93+
public Configuration getConfiguration() {
94+
return configuration;
95+
}
96+
}

allure-jsonunit/src/main/java/io/qameta/allure/jsonunit/AllureConfigurableJsonMatcher.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import net.javacrumbs.jsonunit.core.Option;
1919
import net.javacrumbs.jsonunit.core.internal.Options;
20+
import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
2021
import org.hamcrest.Matcher;
2122

2223
import java.math.BigDecimal;
@@ -38,4 +39,6 @@ public interface AllureConfigurableJsonMatcher<T> extends Matcher<T> {
3839
AllureConfigurableJsonMatcher<T> withMatcher(String matcherName, Matcher<?> matcher);
3940

4041
AllureConfigurableJsonMatcher<T> whenIgnoringPaths(String... paths);
42+
43+
AllureConfigurableJsonMatcher<T> withDifferenceListener(DifferenceListener differenceListener);
4144
}

allure-jsonunit/src/main/java/io/qameta/allure/jsonunit/DiffAttachment.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,10 @@
2020
/**
2121
* @author Victor Orlovsky
2222
*/
23-
public class DiffAttachment implements AttachmentData {
23+
public class DiffAttachment extends DiffModel implements AttachmentData {
2424

25-
private final String patch;
26-
private final String actual;
27-
private final String expected;
28-
29-
public DiffAttachment(final String actual, final String expected, final String patch) {
30-
this.actual = actual;
31-
this.expected = expected;
32-
this.patch = patch;
33-
}
34-
35-
public String getPatch() {
36-
return patch;
37-
}
38-
39-
public String getActual() {
40-
return actual;
41-
}
42-
43-
public String getExpected() {
44-
return expected;
25+
public DiffAttachment(final DiffModel diffModel) {
26+
super(diffModel.getActual(), diffModel.getExpected(), diffModel.getPatch());
4527
}
4628

4729
@Override
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2019 Qameta Software OÜ
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure.jsonunit;
17+
18+
/**
19+
* The class represents diff model containing patch, actual and expected data.
20+
*/
21+
public class DiffModel {
22+
23+
private final String patch;
24+
private final String actual;
25+
private final String expected;
26+
27+
public DiffModel(final String actual, final String expected, final String patch) {
28+
this.actual = actual;
29+
this.expected = expected;
30+
this.patch = patch;
31+
}
32+
33+
public String getPatch() {
34+
return patch;
35+
}
36+
37+
public String getActual() {
38+
return actual;
39+
}
40+
41+
public String getExpected() {
42+
return expected;
43+
}
44+
}

allure-jsonunit/src/main/java/io/qameta/allure/jsonunit/JsonPatchListener.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
2323
import org.apache.commons.lang3.StringUtils;
2424

25+
import java.io.UncheckedIOException;
2526
import java.util.ArrayList;
2627
import java.util.HashMap;
2728
import java.util.List;
@@ -35,6 +36,7 @@
3536
public class JsonPatchListener implements DifferenceListener {
3637

3738
private static final String UNKNOWN_TYPE_ERROR = "Difference has unknown type";
39+
private static final ObjectMapper MAPPER = new ObjectMapper();
3840
private final List<Difference> differences = new ArrayList<>();
3941

4042
private DifferenceContext context;
@@ -94,6 +96,13 @@ private List<Object> getPatch(final Difference difference) {
9496
}
9597
}
9698

99+
public DiffModel getDiffModel() {
100+
return new DiffModel(
101+
writeAsString(context.getActualSource(), "actual"),
102+
writeAsString(context.getExpectedSource(), "expected"),
103+
getJsonPatch());
104+
}
105+
97106
@SuppressWarnings({"all", "unchecked"})
98107
public String getJsonPatch() {
99108
final Map<String, Object> jsonDiffPatch = new HashMap<>();
@@ -154,4 +163,12 @@ public String getJsonPatch() {
154163
throw new IllegalStateException("Could not process patch json", e);
155164
}
156165
}
166+
167+
private static String writeAsString(final Object object, final String failDescription) {
168+
try {
169+
return MAPPER.writeValueAsString(object);
170+
} catch (JsonProcessingException e) {
171+
throw new UncheckedIOException(String.format("Could not process %s json", failDescription), e);
172+
}
173+
}
157174
}

allure-jsonunit/src/main/java/io/qameta/allure/jsonunit/JsonPatchMatcher.java

Lines changed: 13 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,11 @@
1515
*/
1616
package io.qameta.allure.jsonunit;
1717

18-
import com.fasterxml.jackson.core.JsonProcessingException;
19-
import com.fasterxml.jackson.databind.ObjectMapper;
2018
import io.qameta.allure.attachment.DefaultAttachmentProcessor;
2119
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
22-
import net.javacrumbs.jsonunit.core.Configuration;
23-
import net.javacrumbs.jsonunit.core.Option;
24-
import net.javacrumbs.jsonunit.core.internal.Diff;
25-
import net.javacrumbs.jsonunit.core.internal.Options;
26-
import org.hamcrest.Description;
27-
import org.hamcrest.Matcher;
20+
import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
2821

29-
import java.math.BigDecimal;
22+
import org.hamcrest.Description;
3023

3124
/**
3225
* JsonPatchMatcher is extension of JsonUnit matcher,
@@ -35,13 +28,10 @@
3528
* @param <T> the type
3629
*/
3730
@SuppressWarnings("unused")
38-
public final class JsonPatchMatcher<T> implements AllureConfigurableJsonMatcher<T> {
31+
public final class JsonPatchMatcher<T> extends AbstractJsonPatchMatcher<AllureConfigurableJsonMatcher<T>>
32+
implements AllureConfigurableJsonMatcher<T> {
3933

40-
private static final String EMPTY_PATH = "";
41-
private static final String FULL_JSON = "fullJson";
42-
private Configuration configuration = Configuration.empty();
4334
private final Object expected;
44-
private String differences;
4535

4636
private JsonPatchMatcher(final Object expected) {
4737
this.expected = expected;
@@ -51,52 +41,10 @@ public static <T> AllureConfigurableJsonMatcher<T> jsonEquals(final Object expec
5141
return new JsonPatchMatcher<T>(expected);
5242
}
5343

54-
@Override
55-
public AllureConfigurableJsonMatcher<T> withTolerance(final BigDecimal tolerance) {
56-
configuration = configuration.withTolerance(tolerance);
57-
return this;
58-
}
59-
60-
@Override
61-
public AllureConfigurableJsonMatcher<T> withTolerance(final double tolerance) {
62-
configuration = configuration.withTolerance(tolerance);
63-
return this;
64-
}
65-
66-
@Override
67-
public AllureConfigurableJsonMatcher<T> when(final Option first, final Option... next) {
68-
configuration = configuration.when(first, next);
69-
return this;
70-
}
71-
72-
@Override
73-
public AllureConfigurableJsonMatcher<T> withOptions(final Options options) {
74-
configuration = configuration.withOptions(options);
75-
return this;
76-
}
77-
78-
@Override
79-
public AllureConfigurableJsonMatcher<T> withMatcher(final String matcherName, final Matcher matcher) {
80-
configuration = configuration.withMatcher(matcherName, matcher);
81-
return this;
82-
}
83-
84-
@Override
85-
public AllureConfigurableJsonMatcher<T> whenIgnoringPaths(final String... paths) {
86-
configuration = configuration.whenIgnoringPaths(paths);
87-
return this;
88-
}
89-
9044
@Override
9145
public boolean matches(final Object actual) {
92-
final JsonPatchListener listener = new JsonPatchListener();
93-
final Diff diff = Diff.create(expected, actual, FULL_JSON, EMPTY_PATH,
94-
configuration.withDifferenceListener(listener));
95-
if (!diff.similar()) {
96-
differences = diff.differences();
97-
render(listener);
98-
}
99-
return diff.similar();
46+
super.withDifferenceListener(new JsonPatchListener());
47+
return super.matches(expected, actual);
10048
}
10149

10250
@Override
@@ -106,7 +54,7 @@ public void describeTo(final Description description) {
10654

10755
@Override
10856
public void describeMismatch(final Object item, final Description description) {
109-
description.appendText(differences);
57+
description.appendText(super.getDifferences());
11058
}
11159

11260
@SuppressWarnings("deprecation")
@@ -115,17 +63,11 @@ public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
11563
//do nothing
11664
}
11765

118-
private void render(final JsonPatchListener listener) {
119-
final ObjectMapper mapper = new ObjectMapper();
120-
final String patch = listener.getJsonPatch();
121-
try {
122-
final String actual = mapper.writeValueAsString(listener.getContext().getActualSource());
123-
final String expected = mapper.writeValueAsString(listener.getContext().getExpectedSource());
124-
final DiffAttachment attachment = new DiffAttachment(actual, expected, patch);
125-
new DefaultAttachmentProcessor().addAttachment(attachment,
126-
new FreemarkerAttachmentRenderer("diff.ftl"));
127-
} catch (JsonProcessingException e) {
128-
throw new IllegalStateException("Could not process actual/expected json", e);
129-
}
66+
@Override
67+
protected void render(final DifferenceListener listener) {
68+
final JsonPatchListener jsonDiffListener = (JsonPatchListener) listener;
69+
final DiffAttachment attachment = new DiffAttachment(jsonDiffListener.getDiffModel());
70+
new DefaultAttachmentProcessor().addAttachment(attachment,
71+
new FreemarkerAttachmentRenderer("diff.ftl"));
13072
}
13173
}

allure-jsonunit/src/test/java/io/qameta/allure/jsonunit/JsonPatchListenerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ void shouldArrayChangeToNode() {
181181
assertThat(listener.getJsonPatch()).isEqualTo("[[[1,2],[2,3],[1,1]],{\"test\":\"1\"}]");
182182
}
183183

184+
@Test
185+
void shouldSeeDiffModel() {
186+
Diff diff = Diff.create("{\"test\": \"1\"}", "{}", "", "", commonConfig());
187+
diff.similar();
188+
DiffModel model = listener.getDiffModel();
189+
assertThat(model.getExpected()).isEqualTo("{\"test\":\"1\"}");
190+
assertThat(model.getActual()).isEqualTo("{}");
191+
assertThat(model.getPatch()).isEqualTo("{\"test\":[\"1\",0,0]}");
192+
}
193+
184194
private Configuration commonConfig() {
185195
return Configuration.empty().withDifferenceListener(listener);
186196
}

0 commit comments

Comments
 (0)