4
4
import static org .assertj .core .api .Assertions .assertThat ;
5
5
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
6
6
import static org .mockito .ArgumentMatchers .argThat ;
7
+ import static org .mockito .Mockito .doReturn ;
7
8
import static org .mockito .Mockito .mock ;
8
9
import static org .mockito .Mockito .spy ;
9
10
import static org .mockito .Mockito .verify ;
10
11
import static org .mockito .Mockito .verifyNoInteractions ;
12
+ import static org .mockito .Mockito .when ;
11
13
12
14
import java .net .URI ;
13
15
14
16
import javax .annotation .Nonnull ;
15
17
16
- import org .junit .jupiter .api .AfterEach ;
17
18
import org .junit .jupiter .api .BeforeEach ;
19
+ import org .junit .jupiter .api .DisplayName ;
18
20
import org .junit .jupiter .api .Test ;
19
21
import org .junit .jupiter .api .extension .RegisterExtension ;
20
22
import org .mockito .Answers ;
25
27
import com .sap .cloud .environment .servicebinding .api .ServiceBinding ;
26
28
import com .sap .cloud .environment .servicebinding .api .ServiceIdentifier ;
27
29
import com .sap .cloud .sdk .cloudplatform .connectivity .exception .DestinationAccessException ;
30
+ import com .sap .cloud .sdk .cloudplatform .exception .ShouldNotHappenException ;
28
31
import com .sap .cloud .sdk .cloudplatform .security .AuthToken ;
29
32
import com .sap .cloud .sdk .cloudplatform .security .BasicCredentials ;
30
33
import com .sap .cloud .sdk .testutil .TestContext ;
@@ -61,61 +64,65 @@ public Try<HttpDestination> tryGetDestination( @Nonnull ServiceBindingDestinatio
61
64
@ RegisterExtension
62
65
static TestContext context = TestContext .withThreadContext ();
63
66
67
+ private DefaultHttpDestinationBuilderProxyHandler sut ;
68
+
64
69
@ BeforeEach
65
- @ AfterEach
66
- void clean ()
70
+ void setUp ()
67
71
{
68
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingConnectivity (null );
69
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingDestinationLoader (null );
72
+ sut = spy (new DefaultHttpDestinationBuilderProxyHandler ());
73
+ doReturn (destinationLoader ).when (sut ).getServiceBindingDestinationLoader ();
74
+ doReturn (connectivityService ).when (sut ).getServiceBindingConnectivity ();
70
75
}
71
76
72
77
@ Test
73
- void testNoProxy ()
78
+ @ DisplayName ( "Handler should only be invoked for destinations with proxy type ON_PREMISE" )
79
+ void testProxyTypeNotOnpremise ()
74
80
{
75
81
final DefaultHttpDestination .Builder builder = DefaultHttpDestination .builder ("http://foo" );
76
82
77
- assertThatThrownBy (() -> new DefaultHttpDestinationBuilderProxyHandler ().handle (builder ))
78
- .isExactlyInstanceOf (DestinationAccessException .class )
79
- .hasMessage ("Unable to resolve connectivity service binding." );
83
+ assertThatThrownBy (() -> sut .handle (builder )).isExactlyInstanceOf (ShouldNotHappenException .class );
80
84
verifyNoInteractions (destinationLoader );
81
85
}
82
86
83
87
@ Test
84
88
void testNoOnProxy ()
85
89
{
90
+ when (sut .getServiceBindingDestinationLoader ()).thenCallRealMethod ();
91
+ when (sut .getServiceBindingConnectivity ()).thenCallRealMethod ();
92
+
86
93
final DefaultHttpDestination .Builder builder =
87
94
DefaultHttpDestination .builder ("http://foo" ).proxyType (ProxyType .INTERNET );
88
95
89
- assertThatThrownBy (() -> new DefaultHttpDestinationBuilderProxyHandler () .handle (builder ))
96
+ assertThatThrownBy (() -> sut .handle (builder ))
90
97
.isExactlyInstanceOf (DestinationAccessException .class )
91
- .hasMessage ("Unable to resolve connectivity service binding." );
98
+ .hasMessageContaining ("Unable to resolve connectivity service binding." );
92
99
verifyNoInteractions (destinationLoader );
93
100
}
94
101
95
102
@ Test
96
103
void testNoConnectivity ()
97
104
{
105
+ when (sut .getServiceBindingDestinationLoader ()).thenCallRealMethod ();
106
+ when (sut .getServiceBindingConnectivity ()).thenCallRealMethod ();
107
+
98
108
final DefaultHttpDestination .Builder builder =
99
109
DefaultHttpDestination .builder ("http://foo" ).proxyType (ProxyType .ON_PREMISE );
100
110
101
- assertThatThrownBy (() -> new DefaultHttpDestinationBuilderProxyHandler () .handle (builder ))
111
+ assertThatThrownBy (() -> sut .handle (builder ))
102
112
.isExactlyInstanceOf (DestinationAccessException .class )
103
- .hasMessage ("Unable to resolve connectivity service binding." );
113
+ .hasMessageContaining ("Unable to resolve connectivity service binding." );
104
114
verifyNoInteractions (destinationLoader );
105
115
}
106
116
107
117
@ Test
108
118
void testNoAuth ()
109
119
{
110
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingDestinationLoader (destinationLoader );
111
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingConnectivity (connectivityService );
112
-
113
120
final URI uri = URI .create ("http://foo" );
114
121
final DefaultHttpDestination .Builder builder =
115
122
DefaultHttpDestination .builder (uri ).proxyType (ProxyType .ON_PREMISE );
116
123
117
124
// test
118
- final DefaultHttpDestination result = new DefaultHttpDestinationBuilderProxyHandler () .handle (builder );
125
+ final DefaultHttpDestination result = sut .handle (builder );
119
126
120
127
assertThat (result ).isNotNull ();
121
128
verify (destinationLoader ).tryGetDestination (argThat (options -> {
@@ -129,16 +136,13 @@ void testNoAuth()
129
136
@ Test
130
137
void testNotPrincipalPropagation ()
131
138
{
132
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingDestinationLoader (destinationLoader );
133
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingConnectivity (connectivityService );
134
-
135
139
final URI uri = URI .create ("http://foo" );
136
140
final BasicCredentials basicCredentials = new BasicCredentials ("foo" , "bar" );
137
141
final DefaultHttpDestination .Builder builder =
138
142
DefaultHttpDestination .builder (uri ).proxyType (ProxyType .ON_PREMISE ).basicCredentials (basicCredentials );
139
143
140
144
// test
141
- final DefaultHttpDestination result = new DefaultHttpDestinationBuilderProxyHandler () .handle (builder );
145
+ final DefaultHttpDestination result = sut .handle (builder );
142
146
143
147
assertThat (result ).isNotNull ();
144
148
verify (destinationLoader ).tryGetDestination (argThat (options -> {
@@ -154,8 +158,6 @@ void testNotPrincipalPropagation()
154
158
void testPrincipalPropagationDefault ()
155
159
{
156
160
context .setAuthToken (token1 );
157
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingDestinationLoader (destinationLoader );
158
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingConnectivity (connectivityService );
159
161
160
162
final URI uri = URI .create ("http://foo" );
161
163
final DefaultHttpDestination .Builder builder =
@@ -165,7 +167,7 @@ void testPrincipalPropagationDefault()
165
167
.authenticationType (AuthenticationType .PRINCIPAL_PROPAGATION );
166
168
167
169
// test
168
- final DefaultHttpDestination result = new DefaultHttpDestinationBuilderProxyHandler () .handle (builder );
170
+ final DefaultHttpDestination result = sut .handle (builder );
169
171
170
172
assertThat (result ).isNotNull ();
171
173
verify (destinationLoader ).tryGetDestination (argThat (options -> {
@@ -182,8 +184,6 @@ void testPrincipalPropagationDefault()
182
184
void testPrincipalPropagationCompatibility ()
183
185
{
184
186
context .setAuthToken (token1 );
185
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingDestinationLoader (destinationLoader );
186
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingConnectivity (connectivityService );
187
187
188
188
final URI uri = URI .create ("http://foo" );
189
189
final DefaultHttpDestination .Builder builder =
@@ -194,7 +194,7 @@ void testPrincipalPropagationCompatibility()
194
194
.property (DestinationProperty .PRINCIPAL_PROPAGATION_MODE , PrincipalPropagationMode .TOKEN_FORWARDING );
195
195
196
196
// test
197
- final DefaultHttpDestination result = new DefaultHttpDestinationBuilderProxyHandler () .handle (builder );
197
+ final DefaultHttpDestination result = sut .handle (builder );
198
198
199
199
assertThat (result ).isNotNull ();
200
200
verify (destinationLoader ).tryGetDestination (argThat (options -> {
@@ -210,9 +210,6 @@ void testPrincipalPropagationCompatibility()
210
210
@ Test
211
211
void testPrincipalPropagationRecommended ()
212
212
{
213
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingDestinationLoader (destinationLoader );
214
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingConnectivity (connectivityService );
215
-
216
213
final URI uri = URI .create ("http://foo" );
217
214
final DefaultHttpDestination .Builder builder =
218
215
DefaultHttpDestination
@@ -222,7 +219,7 @@ void testPrincipalPropagationRecommended()
222
219
.property (DestinationProperty .PRINCIPAL_PROPAGATION_MODE , PrincipalPropagationMode .TOKEN_EXCHANGE );
223
220
224
221
// test
225
- final DefaultHttpDestination result = new DefaultHttpDestinationBuilderProxyHandler () .handle (builder );
222
+ final DefaultHttpDestination result = sut .handle (builder );
226
223
227
224
assertThat (result ).isNotNull ();
228
225
verify (destinationLoader ).tryGetDestination (argThat (options -> {
@@ -236,9 +233,6 @@ void testPrincipalPropagationRecommended()
236
233
@ Test
237
234
void testNoAuthWithTenantId ()
238
235
{
239
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingDestinationLoader (destinationLoader );
240
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingConnectivity (connectivityService );
241
-
242
236
final URI uri = URI .create ("http://foo" );
243
237
final DefaultHttpDestinationBuilderProxyHandler sut = new DefaultHttpDestinationBuilderProxyHandler ();
244
238
@@ -287,11 +281,8 @@ void testNoAuthWithTenantId()
287
281
void testPrincipalPropagationWithTenantId ()
288
282
{
289
283
context .setAuthToken (token1 );
290
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingDestinationLoader (destinationLoader );
291
- DefaultHttpDestinationBuilderProxyHandler .setServiceBindingConnectivity (connectivityService );
292
284
293
285
final URI uri = URI .create ("http://foo" );
294
- final DefaultHttpDestinationBuilderProxyHandler sut = new DefaultHttpDestinationBuilderProxyHandler ();
295
286
296
287
// provider tenant
297
288
{
@@ -339,12 +330,15 @@ void testPrincipalPropagationWithTenantId()
339
330
@ Test
340
331
void testNotProxyTheProxy ()
341
332
{
333
+ when (sut .getServiceBindingDestinationLoader ()).thenCallRealMethod ();
334
+ when (sut .getServiceBindingConnectivity ()).thenCallRealMethod ();
335
+
342
336
final DefaultHttpDestination .Builder builder1 = DefaultHttpDestination .builder ("http://foo" );
343
337
final DefaultHttpDestination .Builder builder = DefaultHttpDestination .fromDestination (builder1 .build ());
344
338
345
- assertThatThrownBy (() -> new DefaultHttpDestinationBuilderProxyHandler () .handle (builder ))
339
+ assertThatThrownBy (() -> sut .handle (builder ))
346
340
.isExactlyInstanceOf (DestinationAccessException .class )
347
- .hasMessage ("Unable to resolve connectivity service binding." );
341
+ .hasMessageContaining ("Unable to resolve connectivity service binding." );
348
342
verifyNoInteractions (destinationLoader );
349
343
}
350
344
0 commit comments