@@ -12,10 +12,6 @@ import au.com.dius.pact.model.ResponseMatching$
12
12
import au.com.dius.pact.model.ResponsePartMismatch
13
13
import au.com.dius.pact.model.StatusMismatch
14
14
import au.com.dius.pact.model.v3.messaging.Message
15
- import difflib.Delta
16
- import difflib.DiffUtils
17
- import difflib.Patch
18
- import groovy.json.JsonBuilder
19
15
import groovy.json.JsonOutput
20
16
import groovy.json.JsonSlurper
21
17
import org.apache.commons.lang3.StringUtils
@@ -28,9 +24,7 @@ import scala.collection.JavaConverters$
28
24
*/
29
25
class ResponseComparison {
30
26
31
- private static final String NL = ' \n '
32
-
33
- Response expected
27
+ Response expected
34
28
Map actual
35
29
int actualStatus
36
30
Map actualHeaders
@@ -118,60 +112,43 @@ class ResponseComparison {
118
112
" type was '${ bodyTypeMismatch.actual()} '" ]
119
113
} else if (mismatches. any { it instanceof BodyMismatch }) {
120
114
result. comparison = mismatches. findAll { it instanceof BodyMismatch }. collectEntries {
121
- BodyMismatch bodyMismatch -> [bodyMismatch. path(), bodyMismatch. mismatch(). defined ?
122
- bodyMismatch. mismatch(). get() : ' mismatch' ]
123
- }
124
-
125
- String actualBodyString = ' '
126
- if (actualBody) {
127
- if (actual. contentType. mimeType ==~ ' application/.*json' ) {
128
- def bodyMap = new JsonSlurper (). parseText(actualBody)
129
- def bodyJson = JsonOutput . toJson(bodyMap)
130
- actualBodyString = JsonOutput . prettyPrint(bodyJson)
131
- } else {
132
- actualBodyString = actualBody
133
- }
134
- }
135
-
136
- String expectedBodyString = ' '
137
- if (expected. body. present) {
138
- if (expected. jsonBody()) {
139
- expectedBodyString = JsonOutput . prettyPrint(expected. body. value)
140
- } else {
141
- expectedBodyString = expected. body. value
142
- }
115
+ BodyMismatch bodyMismatch -> [
116
+ bodyMismatch. path(), [
117
+ mismatch : bodyMismatch. mismatch(). defined ? bodyMismatch. mismatch(). get() : ' mismatch' ,
118
+ diff : bodyMismatch. diff(). defined ? bodyMismatch. diff(). get() : ' '
119
+ ]
120
+ ]
143
121
}
144
122
145
- def expectedLines = expectedBodyString. split(NL ) as List
146
- def actualLines = actualBodyString. split(NL ) as List
147
- Patch<String > patch = DiffUtils . diff(expectedLines, actualLines)
148
-
149
- def diff = []
150
-
151
- patch. deltas. each { Delta<String > delta ->
152
- diff << " @${ delta.original.position} "
153
- if (delta. original. position > 1 ) {
154
- diff << expectedLines[delta. original. position - 1 ]
155
- }
156
-
157
- delta. original. lines. each {
158
- diff << " -$it "
159
- }
160
- delta. revised. lines. each {
161
- diff << " +$it "
162
- }
123
+ result. diff = generateFullDiff(actualBody, this . actual. contentType. mimeType as String ,
124
+ expected. body. present ? expected. body. value : ' ' , expected. jsonBody())
125
+ }
163
126
164
- int pos = delta. original. position + delta. original. lines. size()
165
- if (pos < expectedLines. size()) {
166
- diff << expectedLines[pos]
167
- }
127
+ result
128
+ }
168
129
169
- diff << ' '
130
+ private static generateFullDiff (String actual , String mimeType , String response , Boolean jsonBody ) {
131
+ String actualBodyString = ' '
132
+ if (actual) {
133
+ if (mimeType ==~ ' application/.*json' ) {
134
+ def bodyMap = new JsonSlurper (). parseText(actual)
135
+ def bodyJson = JsonOutput . toJson(bodyMap)
136
+ actualBodyString = JsonOutput . prettyPrint(bodyJson)
137
+ } else {
138
+ actualBodyString = actual
170
139
}
140
+ }
171
141
172
- result. diff = diff
142
+ String expectedBodyString = ' '
143
+ if (response) {
144
+ if (jsonBody) {
145
+ expectedBodyString = JsonOutput . prettyPrint(response)
146
+ } else {
147
+ expectedBodyString = response
148
+ }
173
149
}
174
150
175
- result
151
+ DiffUtils . generateDiff(expectedBodyString, actualBodyString)
176
152
}
153
+
177
154
}
0 commit comments