1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.caravan.hal.comparison.impl.difference;
21
22 import static io.wcm.caravan.hal.comparison.impl.util.HalJsonConversion.asJson;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.google.common.collect.ImmutableList;
29
30 import io.wcm.caravan.hal.comparison.HalComparisonContext;
31 import io.wcm.caravan.hal.comparison.HalDifference;
32 import io.wcm.caravan.hal.comparison.HalDifference.ChangeType;
33 import io.wcm.caravan.hal.comparison.HalDifference.EntityType;
34 import io.wcm.caravan.hal.comparison.impl.context.HalComparisonContextImpl;
35 import io.wcm.caravan.hal.resource.HalResource;
36 import io.wcm.caravan.hal.resource.Link;
37
38
39
40
41 public class HalDifferenceListBuilder {
42
43 private final List<HalDifference> differences = new ArrayList<>();
44 private final HalComparisonContextImpl context;
45
46
47
48
49 public HalDifferenceListBuilder(HalComparisonContextImpl context) {
50 this.context = context;
51 }
52
53
54
55
56 public List<HalDifference> build() {
57 return ImmutableList.copyOf(differences);
58 }
59
60
61
62
63 public void clearPreviouslyReported() {
64 differences.clear();
65 }
66
67
68
69
70
71 public void addAllFrom(HalDifferenceListBuilder other) {
72 differences.addAll(other.build());
73 }
74
75
76
77
78
79 public void addAll(List<HalDifference> other) {
80 differences.addAll(other);
81 }
82
83 private void addDifference(HalDifference.ChangeType changeType, HalDifference.EntityType entityType,
84 String description, JsonNode expectedJson, JsonNode actualJson) {
85
86 HalDifference diff = new HalDifferenceImpl(context,
87 changeType, entityType,
88 expectedJson, actualJson, description);
89
90 differences.add(diff);
91 }
92
93 private void addDifferenceWithNewContext(HalComparisonContext newContext, HalDifference.ChangeType changeType, HalDifference.EntityType entityType,
94 String description, JsonNode expectedJson, JsonNode actualJson) {
95
96 HalDifference diff = new HalDifferenceImpl(newContext,
97 changeType, entityType,
98 expectedJson, actualJson, description);
99
100 differences.add(diff);
101 }
102
103
104
105
106
107
108 public void reportAdditionalLink(String description, Link actual, int index) {
109 HalComparisonContext newContext = context.withHalPathIndex(index);
110 addDifferenceWithNewContext(newContext, ChangeType.ADDITIONAL, EntityType.LINK, description, null, asJson(actual));
111 }
112
113
114
115
116
117
118 public void reportMissingLink(String description, Link expected, int index) {
119 HalComparisonContext newContext = context.withHalPathIndex(index);
120 addDifferenceWithNewContext(newContext, ChangeType.MISSING, EntityType.LINK, description, asJson(expected), null);
121 }
122
123
124
125
126
127
128 public void reportModifiedLink(String description, Link expected, Link actual) {
129 addDifference(ChangeType.MODIFIED, EntityType.LINK, description, asJson(expected), asJson(actual));
130 }
131
132
133
134
135
136
137 public void reportReorderedLinks(String description, Iterable<Link> expected, Iterable<Link> actual) {
138 addDifference(ChangeType.REORDERED, EntityType.LINK, description, asJson(expected), asJson(actual));
139 }
140
141
142
143
144
145
146 public void reportAdditionalEmbedded(String description, HalResource actual, int index) {
147 HalComparisonContext newContext = context.withHalPathIndex(index);
148 addDifferenceWithNewContext(newContext, ChangeType.ADDITIONAL, EntityType.EMBEDDED, description, null, asJson(actual));
149 }
150
151
152
153
154
155
156 public void reportMissingEmbedded(String description, HalResource expected, int index) {
157 HalComparisonContext newContext = context.withHalPathIndex(index);
158 addDifferenceWithNewContext(newContext, ChangeType.MISSING, EntityType.EMBEDDED, description, asJson(expected), null);
159 }
160
161
162
163
164
165
166 public void reportModifiedEmbedded(String description, HalResource expected, HalResource actual) {
167 addDifference(ChangeType.MODIFIED, EntityType.EMBEDDED, description, asJson(expected), asJson(actual));
168 }
169
170
171
172
173
174
175 public void reportReorderedEmbedded(String description, Iterable<HalResource> expected, Iterable<HalResource> actual) {
176 addDifference(ChangeType.REORDERED, EntityType.EMBEDDED, description, asJson(expected), asJson(actual));
177 }
178
179
180
181
182
183 public void reportAdditionalProperty(String description, JsonNode actual) {
184 addDifference(ChangeType.ADDITIONAL, EntityType.PROPERTY, description, null, actual);
185 }
186
187
188
189
190
191
192 public void reportAdditionalProperty(String description, JsonNode actual, int index) {
193 HalComparisonContext newContext = context.withJsonPathIndex(index);
194 addDifferenceWithNewContext(newContext, ChangeType.ADDITIONAL, EntityType.PROPERTY, description, null, actual);
195 }
196
197
198
199
200
201 public void reportMissingProperty(String description, JsonNode expected) {
202 addDifference(ChangeType.MISSING, EntityType.PROPERTY, description, expected, null);
203 }
204
205
206
207
208
209
210 public void reportMissingProperty(String description, JsonNode expected, int index) {
211 HalComparisonContext newContext = context.withJsonPathIndex(index);
212 addDifferenceWithNewContext(newContext, ChangeType.MISSING, EntityType.PROPERTY, description, expected, null);
213 }
214
215
216
217
218
219
220 public void reportModifiedProperty(String description, JsonNode expected, JsonNode actual) {
221 addDifference(ChangeType.MODIFIED, EntityType.PROPERTY, description, expected, actual);
222 }
223
224
225
226
227
228
229 public void reportReorderedProperty(String description, JsonNode expected, JsonNode actual) {
230 addDifference(ChangeType.REORDERED, EntityType.PROPERTY, description, expected, actual);
231 }
232
233 }