@@ -2,6 +2,9 @@ package au.com.dius.pact.provider.junit5
2
2
3
3
import au.com.dius.pact.core.model.Consumer
4
4
import au.com.dius.pact.core.model.FilteredPact
5
+ import au.com.dius.pact.core.model.Interaction
6
+ import au.com.dius.pact.core.model.OptionalBody
7
+ import au.com.dius.pact.core.model.Pact
5
8
import au.com.dius.pact.core.model.PactBrokerSource
6
9
import au.com.dius.pact.core.model.Provider
7
10
import au.com.dius.pact.core.model.Request
@@ -13,39 +16,79 @@ import au.com.dius.pact.core.support.expressions.ValueResolver
13
16
import au.com.dius.pact.provider.IConsumerInfo
14
17
import au.com.dius.pact.provider.IProviderInfo
15
18
import au.com.dius.pact.provider.IProviderVerifier
19
+ import au.com.dius.pact.provider.ProviderVerifier
20
+ import au.com.dius.pact.provider.RequestData
21
+ import au.com.dius.pact.provider.RequestDataToBeVerified
16
22
import au.com.dius.pact.provider.TestResultAccumulator
23
+ import org.apache.hc.core5.http.ClassicHttpRequest
24
+ import org.apache.hc.core5.http.HttpRequest
17
25
import org.junit.jupiter.api.extension.ExtensionContext
26
+ import org.junit.jupiter.api.extension.ParameterContext
18
27
import spock.lang.Issue
28
+ import spock.lang.Shared
19
29
import spock.lang.Specification
20
30
21
- class PactVerificationExtensionSpec extends Specification {
31
+ import java.lang.reflect.Parameter
22
32
23
- def ' updateTestResult uses the original pact when pact is filtered ' () {
24
- given :
25
- PactVerificationContext context
26
- ExtensionContext.Store store = Stub {
33
+ import static org.mockito.Mockito.mock
34
+ import static org.mockito.Mockito.when
35
+
36
+ @SuppressWarnings (' UnnecessaryGetter' )
37
+ class PactVerificationExtensionSpec extends Specification {
38
+ @Shared PactVerificationContext context
39
+ PactVerificationExtension extension
40
+ @Shared ExtensionContext.Store store
41
+ @Shared ExtensionContext extContext
42
+ @Shared Map<String , Object > contextMap
43
+ ValueResolver mockValueResolver
44
+ @Shared RequestResponseInteraction interaction1, interaction2
45
+ @Shared RequestResponsePact pact
46
+ PactBrokerSource pactSource
47
+ @Shared ClassicHttpRequest classicHttpRequest
48
+ @Shared ProviderVerifier verifier
49
+ @Shared RequestDataToBeVerified data
50
+
51
+ def setupSpec () {
52
+ verifier = Mock (ProviderVerifier )
53
+ store = Stub {
27
54
get(_) >> { args ->
28
55
if (args[0 ] == ' interactionContext' ) {
29
56
context
57
+ } else {
58
+ contextMap[args[0 ]]
30
59
}
31
60
}
61
+ put(_, _) >> { args -> contextMap[args[0 ]] = args[1 ] }
32
62
}
33
- ExtensionContext extContext = Stub {
63
+
64
+ extContext = Stub {
34
65
getStore(_) >> store
35
66
}
36
- def mockValueResolver = Mock (ValueResolver )
67
+ interaction1 = new RequestResponseInteraction (' interaction1' , [], new Request (), new Response ())
68
+ interaction2 = new RequestResponseInteraction (' interaction2' , [], new Request (), new Response ())
69
+ pact = new RequestResponsePact (new Provider (), new Consumer (), [interaction1, interaction2])
70
+ classicHttpRequest = Mock (ClassicHttpRequest )
71
+ context = new PactVerificationContext (store, extContext, Stub (TestTarget ), Stub (IProviderVerifier ),
72
+ Stub (ValueResolver ), Stub (IProviderInfo ), Stub (IConsumerInfo ), interaction1, pact, [])
73
+ data = new RequestDataToBeVerified (OptionalBody . empty(), [:])
74
+ }
75
+
76
+ def setup () {
77
+ mockValueResolver = Mock (ValueResolver )
78
+ pactSource = new PactBrokerSource (' localhost' , ' 80' , ' http' )
79
+ contextMap = [
80
+ httpRequest : classicHttpRequest,
81
+ verifier : verifier
82
+ ]
83
+ }
37
84
38
- def interaction1 = new RequestResponseInteraction (' interaction1' , [], new Request (), new Response ())
39
- def interaction2 = new RequestResponseInteraction (' interaction2' , [], new Request (), new Response ())
40
- def pact = new RequestResponsePact (new Provider (), new Consumer (), [interaction1, interaction2])
85
+ def ' updateTestResult uses the original pact when pact is filtered ' () {
86
+ given :
41
87
def filteredPact = new FilteredPact (pact, { it. description == ' interaction1' })
42
88
PactBrokerSource pactSource = new PactBrokerSource (' localhost' , ' 80' , ' http' )
43
89
44
- context = new PactVerificationContext (store, extContext, Stub (TestTarget ), Stub (IProviderVerifier ),
45
- Stub (ValueResolver ), Stub (IProviderInfo ), Stub (IConsumerInfo ), interaction1, pact, [])
46
-
47
- PactVerificationExtension extension = new PactVerificationExtension (filteredPact, pactSource, interaction1,
48
- ' service' , ' consumer' , mockValueResolver)
90
+ extension = new PactVerificationExtension (filteredPact, pactSource, interaction1, ' service' , ' consumer' ,
91
+ mockValueResolver)
49
92
extension. testResultAccumulator = Mock (TestResultAccumulator )
50
93
51
94
when :
@@ -58,28 +101,7 @@ class PactVerificationExtensionSpec extends Specification {
58
101
59
102
def ' updateTestResult uses the pact itself when pact is not filtered ' () {
60
103
given :
61
- PactVerificationContext context
62
- ExtensionContext.Store store = Stub {
63
- get(_) >> { args ->
64
- if (args[0 ] == ' interactionContext' ) {
65
- context
66
- }
67
- }
68
- }
69
- ExtensionContext extContext = Stub {
70
- getStore(_) >> store
71
- }
72
- def mockValueResolver = Mock (ValueResolver )
73
-
74
- def interaction1 = new RequestResponseInteraction (' interaction1' , [], new Request (), new Response ())
75
- def interaction2 = new RequestResponseInteraction (' interaction2' , [], new Request (), new Response ())
76
- def pact = new RequestResponsePact (new Provider (), new Consumer (), [interaction1, interaction2])
77
- PactBrokerSource pactSource = new PactBrokerSource (' localhost' , ' 80' , ' http' )
78
-
79
- context = new PactVerificationContext (store, extContext, Stub (TestTarget ), Stub (IProviderVerifier ),
80
- Stub (ValueResolver ), Stub (IProviderInfo ), Stub (IConsumerInfo ), interaction1, pact, [])
81
-
82
- PactVerificationExtension extension = new PactVerificationExtension (pact, pactSource, interaction1,
104
+ extension = new PactVerificationExtension (pact, pactSource, interaction1,
83
105
' service' , ' consumer' , mockValueResolver)
84
106
extension. testResultAccumulator = Mock (TestResultAccumulator )
85
107
@@ -92,28 +114,9 @@ class PactVerificationExtensionSpec extends Specification {
92
114
93
115
def ' if updateTestResult fails, throw an exception' () {
94
116
given :
95
- PactVerificationContext context
96
- ExtensionContext.Store store = Stub {
97
- get(_) >> { args ->
98
- if (args[0 ] == ' interactionContext' ) {
99
- context
100
- }
101
- }
102
- }
103
- ExtensionContext extContext = Stub {
104
- getStore(_) >> store
105
- }
106
- def mockValueResolver = Mock (ValueResolver )
117
+ pact = new RequestResponsePact (new Provider (), new Consumer (), [interaction1, interaction2], [:], pactSource)
107
118
108
- def interaction1 = new RequestResponseInteraction (' interaction1' )
109
- def interaction2 = new RequestResponseInteraction (' interaction2' )
110
- PactBrokerSource pactSource = new PactBrokerSource (' localhost' , ' 80' , ' http' )
111
- def pact = new RequestResponsePact (new Provider (), new Consumer (), [interaction1, interaction2 ], [:], pactSource)
112
-
113
- context = new PactVerificationContext (store, extContext, Stub (TestTarget ), Stub (IProviderVerifier ),
114
- Stub (ValueResolver ), Stub (IProviderInfo ), Stub (IConsumerInfo ), interaction1, pact, [])
115
-
116
- PactVerificationExtension extension = new PactVerificationExtension (pact, pactSource, interaction1,
119
+ extension = new PactVerificationExtension (pact, pactSource, interaction1,
117
120
' service' , ' consumer' , mockValueResolver)
118
121
extension. testResultAccumulator = Mock (TestResultAccumulator )
119
122
@@ -130,27 +133,75 @@ class PactVerificationExtensionSpec extends Specification {
130
133
@Issue (' #1572' )
131
134
def ' beforeEach method passes the property resolver on to the verification context' () {
132
135
given :
133
- def map = [:]
134
- ExtensionContext.Store store = Stub {
135
- get(_) >> { args -> map[args[0 ]] }
136
- put(_, _) >> { args -> map[args[0 ]] = args[1 ] }
137
- }
138
- ExtensionContext extContext = Stub {
139
- getStore(_) >> store
140
- }
141
- def mockValueResolver = Mock (ValueResolver )
136
+ pact = new RequestResponsePact (new Provider (), new Consumer (), [interaction1], [:], pactSource)
142
137
143
- def interaction1 = new RequestResponseInteraction (' interaction1' )
144
- PactBrokerSource pactSource = new PactBrokerSource (' localhost' , ' 80' , ' http' )
145
- def pact = new RequestResponsePact (new Provider (), new Consumer (), [interaction1], [:], pactSource)
146
-
147
- PactVerificationExtension extension = new PactVerificationExtension (pact, pactSource, interaction1,
138
+ extension = new PactVerificationExtension (pact, pactSource, interaction1,
148
139
' service' , ' consumer' , mockValueResolver)
149
140
150
141
when :
151
142
extension. beforeEach(extContext)
152
143
153
144
then :
154
- map[' interactionContext' ]. valueResolver == mockValueResolver
145
+ contextMap[' interactionContext' ]. valueResolver == mockValueResolver
146
+ }
147
+
148
+ def ' supports parameter test' () {
149
+ given :
150
+ context. target = target
151
+ extension = new PactVerificationExtension (pact, pactSource, interaction1, ' service' , ' consumer' ,
152
+ mockValueResolver)
153
+ Parameter parameter = mock(Parameter )
154
+ when(parameter. getType()). thenReturn(parameterType)
155
+ ParameterContext parameterContext = Stub {
156
+ getParameter() >> parameter
157
+ }
158
+
159
+ expect :
160
+ extension. supportsParameter(parameterContext, extContext) == result
161
+
162
+ where :
163
+
164
+ parameterType | target | result
165
+ Pact | new HttpTestTarget () | true
166
+ Interaction | new HttpTestTarget () | true
167
+ ClassicHttpRequest | new HttpTestTarget () | true
168
+ ClassicHttpRequest | new HttpsTestTarget () | true
169
+ ClassicHttpRequest | new MessageTestTarget () | false
170
+ HttpRequest | new HttpTestTarget () | true
171
+ HttpRequest | new HttpsTestTarget () | true
172
+ HttpRequest | new MessageTestTarget () | false
173
+ PactVerificationContext | new HttpTestTarget () | true
174
+ ProviderVerifier | new HttpTestTarget () | true
175
+ String | new HttpTestTarget () | false
176
+ RequestData | new HttpTestTarget () | false
177
+ RequestData | new PluginTestTarget () | true
178
+ }
179
+
180
+ def ' resolve parameter test' () {
181
+ given :
182
+ extension = new PactVerificationExtension (pact, pactSource, interaction1, ' service' , ' consumer' ,
183
+ mockValueResolver)
184
+ Parameter parameter = mock(Parameter )
185
+ when(parameter. getType()). thenReturn(parameterType)
186
+ ParameterContext parameterContext = Stub {
187
+ getParameter() >> parameter
188
+ }
189
+
190
+ contextMap[' request' ] = data
191
+
192
+ expect :
193
+ extension. resolveParameter(parameterContext, extContext) == result
194
+
195
+ where :
196
+
197
+ parameterType | result
198
+ Pact | pact
199
+ Interaction | interaction1
200
+ ClassicHttpRequest | classicHttpRequest
201
+ HttpRequest | classicHttpRequest
202
+ PactVerificationContext | context
203
+ ProviderVerifier | verifier
204
+ String | null
205
+ RequestData | data
155
206
}
156
207
}
0 commit comments