|
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 spock.lang.Specification
|
9 | 14 |
|
10 |
| -@SuppressWarnings('UnnecessaryObjectReferences') |
| 15 | +@SuppressWarnings(['UnnecessaryObjectReferences', 'LineLength']) |
11 | 16 | class MarkdownReporterSpec extends Specification {
|
12 | 17 |
|
13 | 18 | private File reportDir
|
@@ -80,4 +85,117 @@ class MarkdownReporterSpec extends Specification {
|
80 | 85 | results.contains('## Verifying a pact between _Consumer_ and _provider1_\n\nInteraction 1 ')
|
81 | 86 | }
|
82 | 87 |
|
| 88 | + @SuppressWarnings(['MethodSize', 'TrailingWhitespace']) |
| 89 | + def 'generates the correct markdown for validation failures'() { |
| 90 | + given: |
| 91 | + def reporter = new MarkdownReporter('test', reportDir) |
| 92 | + def provider1 = new ProviderInfo(name: 'provider1') |
| 93 | + def consumer = new ConsumerInfo(name: 'Consumer') |
| 94 | + def interaction1 = new RequestResponseInteraction('Interaction 1', [], new Request(), new Response()) |
| 95 | + |
| 96 | + when: |
| 97 | + reporter.initialise(provider1) |
| 98 | + reporter.reportVerificationForConsumer(consumer, provider1, null) |
| 99 | + reporter.interactionDescription(interaction1) |
| 100 | + reporter.statusComparisonFailed(200, 'expected status of 201 but was 200') |
| 101 | + reporter.headerComparisonFailed('HEADER-X', ['Y'], [ |
| 102 | + new HeaderMismatch('HEADER-X', 'Y', '', "Expected a header 'HEADER-X' but was missing") |
| 103 | + ]) |
| 104 | + reporter.bodyComparisonFailed( |
| 105 | + new Either.Right(new BodyComparisonResult([ |
| 106 | + '$.0': [ |
| 107 | + new BodyMismatch( |
| 108 | + JsonParser.parseString('{"doesNotExist":"Test","documentId":0}'), |
| 109 | + JsonParser.parseString('{"documentId":0,"documentCategoryId":5,"documentCategoryCode":null,"contentLength":0,"tags":null}'), |
| 110 | + 'Expected doesNotExist="Test" but was missing', '$.0', '''{ |
| 111 | + - "doesNotExist": "Test", |
| 112 | + - "documentId": 0 |
| 113 | + + "documentId": 0, |
| 114 | + + "documentCategoryId": 5, |
| 115 | + + "documentCategoryCode": null, |
| 116 | + + "contentLength": 0, |
| 117 | + + "tags": null |
| 118 | + }''')], |
| 119 | + '$.1': [ |
| 120 | + new BodyMismatch(JsonParser.parseString('{"doesNotExist":"Test","documentId":0}'), |
| 121 | + JsonParser.parseString('{"documentId":1,"documentCategoryId":5,"documentCategoryCode":null,"contentLength":0,"tags":null}'), |
| 122 | + 'Expected doesNotExist="Test" but was missing', '$.1', '''{ |
| 123 | + - "doesNotExist": "Test", |
| 124 | + - "documentId": 0 |
| 125 | + + "documentId": 1, |
| 126 | + + "documentCategoryId": 5, |
| 127 | + + "documentCategoryCode": null, |
| 128 | + + "contentLength": 0, |
| 129 | + + "tags": null |
| 130 | + }''')] |
| 131 | + ], [ |
| 132 | + ' {', |
| 133 | + '- " doesNotExist ": " Test ",', |
| 134 | + '- " documentId ": 0', |
| 135 | + '+ " documentId ": 0,', |
| 136 | + '+ " documentCategoryId ": 5,', |
| 137 | + '+ " documentCategoryCode ": null,', |
| 138 | + '+ " contentLength ": 0,', |
| 139 | + '+ " tags ": null', |
| 140 | + '+ },', |
| 141 | + '+ {', |
| 142 | + '+ " documentId ": 1,', |
| 143 | + '+ " documentCategoryId ": 5,', |
| 144 | + '+ " documentCategoryCode ": null,', |
| 145 | + '+ " contentLength ": 0,', |
| 146 | + '+ " tags ": null', |
| 147 | + ' }' |
| 148 | + ])) |
| 149 | + ) |
| 150 | + reporter.finaliseReport() |
| 151 | + |
| 152 | + def results = new File(reportDir, 'provider1.md').text |
| 153 | + |
| 154 | + then: |
| 155 | + results.contains( |
| 156 | + '''| has status code **200** (<span style='color:red'>FAILED</span>) |
| 157 | + | |
| 158 | + |``` |
| 159 | + |expected status of 201 but was 200 |
| 160 | + |```'''.stripMargin() |
| 161 | + ) |
| 162 | + results.contains( |
| 163 | + '''| "**HEADER-X**" with value "**[Y]**" (<span style='color:red'>FAILED</span>) |
| 164 | + | |
| 165 | + |``` |
| 166 | + |Expected a header 'HEADER-X' but was missing |
| 167 | + |```'''.stripMargin() |
| 168 | + ) |
| 169 | + results.contains( |
| 170 | + '''| has a matching body (<span style='color:red'>FAILED</span>) |
| 171 | + | |
| 172 | + || Path | Failure | |
| 173 | + || ---- | ------- | |
| 174 | + ||`$.0`|Expected doesNotExist="Test" but was missing| |
| 175 | + ||`$.1`|Expected doesNotExist="Test" but was missing| |
| 176 | + | |
| 177 | + | |
| 178 | + |Diff: |
| 179 | + | |
| 180 | + |```diff |
| 181 | + | { |
| 182 | + |- " doesNotExist ": " Test ", |
| 183 | + |- " documentId ": 0 |
| 184 | + |+ " documentId ": 0, |
| 185 | + |+ " documentCategoryId ": 5, |
| 186 | + |+ " documentCategoryCode ": null, |
| 187 | + |+ " contentLength ": 0, |
| 188 | + |+ " tags ": null |
| 189 | + |+ }, |
| 190 | + |+ { |
| 191 | + |+ " documentId ": 1, |
| 192 | + |+ " documentCategoryId ": 5, |
| 193 | + |+ " documentCategoryCode ": null, |
| 194 | + |+ " contentLength ": 0, |
| 195 | + |+ " tags ": null |
| 196 | + | } |
| 197 | + |```'''.stripMargin() |
| 198 | + ) |
| 199 | + } |
| 200 | + |
83 | 201 | }
|
0 commit comments