|
1 | 1 | package au.com.dius.pact.provider.reporters
|
2 | 2 |
|
| 3 | +import arrow.core.Either |
| 4 | +import au.com.dius.pact.core.matchers.BodyMismatch |
| 5 | +import au.com.dius.pact.core.matchers.HeaderMismatch |
3 | 6 | import au.com.dius.pact.core.model.Request
|
4 | 7 | import au.com.dius.pact.core.model.RequestResponseInteraction
|
5 | 8 | import au.com.dius.pact.core.model.Response
|
| 9 | +import au.com.dius.pact.provider.BodyComparisonResult |
6 | 10 | import au.com.dius.pact.provider.ConsumerInfo
|
7 | 11 | import au.com.dius.pact.provider.ProviderInfo
|
| 12 | +import com.google.gson.JsonParser |
8 | 13 | import groovy.json.JsonOutput
|
9 | 14 | import groovy.json.JsonSlurper
|
10 | 15 | import spock.lang.Specification
|
11 | 16 |
|
12 |
| -@SuppressWarnings('UnnecessaryObjectReferences') |
| 17 | +@SuppressWarnings(['UnnecessaryObjectReferences', 'LineLength']) |
13 | 18 | class JsonReporterSpec extends Specification {
|
14 | 19 |
|
15 | 20 | private File reportDir
|
@@ -105,4 +110,82 @@ class JsonReporterSpec extends Specification {
|
105 | 110 | reportJson.execution.first().interactions.first().interaction.description == 'Interaction 1'
|
106 | 111 | }
|
107 | 112 |
|
| 113 | + def 'generates the correct JSON for validation failures'() { |
| 114 | + given: |
| 115 | + def reporter = new JsonReporter('test', reportDir) |
| 116 | + def provider1 = new ProviderInfo(name: 'provider1') |
| 117 | + def consumer = new ConsumerInfo(name: 'Consumer') |
| 118 | + def interaction1 = new RequestResponseInteraction('Interaction 1', [], new Request(), new Response()) |
| 119 | + |
| 120 | + when: |
| 121 | + reporter.initialise(provider1) |
| 122 | + reporter.reportVerificationForConsumer(consumer, provider1, null) |
| 123 | + reporter.interactionDescription(interaction1) |
| 124 | + reporter.statusComparisonFailed(200, 'expected status of 201 but was 200') |
| 125 | + reporter.headerComparisonFailed('HEADER-X', [''], [ |
| 126 | + new HeaderMismatch('HEADER-X', 'Y', '', "Expected a header 'HEADER-X' but was missing") |
| 127 | + ]) |
| 128 | + reporter.bodyComparisonFailed( |
| 129 | + new Either.Right(new BodyComparisonResult([ |
| 130 | + '$.0': [ |
| 131 | + new BodyMismatch( |
| 132 | + JsonParser.parseString('{"doesNotExist":"Test","documentId":0}'), |
| 133 | + JsonParser.parseString('{"documentId":0,"documentCategoryId":5,"documentCategoryCode":null,"contentLength":0,"tags":null}'), |
| 134 | + 'Expected doesNotExist="Test" but was missing', '$.0', '''{ |
| 135 | + - "doesNotExist": "Test", |
| 136 | + - "documentId": 0 |
| 137 | + + "documentId": 0, |
| 138 | + + "documentCategoryId": 5, |
| 139 | + + "documentCategoryCode": null, |
| 140 | + + "contentLength": 0, |
| 141 | + + "tags": null |
| 142 | + }''')], |
| 143 | + '$.1': [ |
| 144 | + new BodyMismatch(JsonParser.parseString('{"doesNotExist":"Test","documentId":0}'), |
| 145 | + JsonParser.parseString('{"documentId":1,"documentCategoryId":5,"documentCategoryCode":null,"contentLength":0,"tags":null}'), |
| 146 | + 'Expected doesNotExist="Test" but was missing', '$.1', '''{ |
| 147 | + - "doesNotExist": "Test", |
| 148 | + - "documentId": 0 |
| 149 | + + "documentId": 1, |
| 150 | + + "documentCategoryId": 5, |
| 151 | + + "documentCategoryCode": null, |
| 152 | + + "contentLength": 0, |
| 153 | + + "tags": null |
| 154 | + }''')] |
| 155 | + ], [ |
| 156 | + ' {', |
| 157 | + '- " doesNotExist ": " Test ",', |
| 158 | + '- " documentId ": 0', |
| 159 | + '+ " documentId ": 0,', |
| 160 | + '+ " documentCategoryId ": 5,', |
| 161 | + '+ " documentCategoryCode ": null,', |
| 162 | + '+ " contentLength ": 0,', |
| 163 | + '+ " tags ": null', |
| 164 | + '+ },', |
| 165 | + '+ {', |
| 166 | + '+ " documentId ": 1,', |
| 167 | + '+ " documentCategoryId ": 5,', |
| 168 | + '+ " documentCategoryCode ": null,', |
| 169 | + '+ " contentLength ": 0,', |
| 170 | + '+ " tags ": null', |
| 171 | + ' }' |
| 172 | + ])) |
| 173 | + ) |
| 174 | + reporter.finaliseReport() |
| 175 | + |
| 176 | + def reportJson = new JsonSlurper().parse(new File(reportDir, 'provider1.json')) |
| 177 | + |
| 178 | + then: |
| 179 | + reportJson.provider.name == 'provider1' |
| 180 | + reportJson.execution.size == 1 |
| 181 | + reportJson.execution[0].interactions.size == 1 |
| 182 | + reportJson.execution[0].interactions[0].verification.result == 'failed' |
| 183 | + reportJson.execution[0].interactions[0].verification.status == ['expected status of 201 but was 200'] |
| 184 | + reportJson.execution[0].interactions[0].verification.header == ['HEADER-X': ["Expected a header 'HEADER-X' but was missing"]] |
| 185 | + reportJson.execution[0].interactions[0].verification.body.mismatches == [ |
| 186 | + '$.0': ['Expected doesNotExist="Test" but was missing'], |
| 187 | + '$.1': ['Expected doesNotExist="Test" but was missing'] |
| 188 | + ] |
| 189 | + } |
| 190 | + |
108 | 191 | }
|
0 commit comments