forked from cloudfoundry/cf-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegrationTestConfiguration.java
More file actions
766 lines (712 loc) · 34.9 KB
/
IntegrationTestConfiguration.java
File metadata and controls
766 lines (712 loc) · 34.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cloudfoundry;
import static org.assertj.core.api.Assertions.fail;
import static org.cloudfoundry.uaa.tokens.GrantType.AUTHORIZATION_CODE;
import static org.cloudfoundry.uaa.tokens.GrantType.CLIENT_CREDENTIALS;
import static org.cloudfoundry.uaa.tokens.GrantType.PASSWORD;
import static org.cloudfoundry.uaa.tokens.GrantType.REFRESH_TOKEN;
import static org.cloudfoundry.util.tuple.TupleUtils.function;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.github.zafarkhaja.semver.Version;
import java.io.IOException;
import java.nio.file.Path;
import java.security.SecureRandom;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import org.cloudfoundry.client.CloudFoundryClient;
import org.cloudfoundry.client.v2.info.GetInfoRequest;
import org.cloudfoundry.client.v2.organizationquotadefinitions.CreateOrganizationQuotaDefinitionRequest;
import org.cloudfoundry.client.v2.organizations.AssociateOrganizationManagerRequest;
import org.cloudfoundry.client.v2.organizations.CreateOrganizationRequest;
import org.cloudfoundry.client.v2.spaces.CreateSpaceRequest;
import org.cloudfoundry.client.v2.stacks.ListStacksRequest;
import org.cloudfoundry.client.v2.stacks.StackEntity;
import org.cloudfoundry.client.v2.stacks.StackResource;
import org.cloudfoundry.client.v2.userprovidedserviceinstances.CreateUserProvidedServiceInstanceRequest;
import org.cloudfoundry.doppler.DopplerClient;
import org.cloudfoundry.logcache.v1.TestLogCacheEndpoints;
import org.cloudfoundry.networking.NetworkingClient;
import org.cloudfoundry.operations.DefaultCloudFoundryOperations;
import org.cloudfoundry.reactor.ConnectionContext;
import org.cloudfoundry.reactor.DefaultConnectionContext;
import org.cloudfoundry.reactor.ProxyConfiguration;
import org.cloudfoundry.reactor.TokenProvider;
import org.cloudfoundry.reactor.client.ReactorCloudFoundryClient;
import org.cloudfoundry.reactor.doppler.ReactorDopplerClient;
import org.cloudfoundry.reactor.logcache.v1.ReactorLogCacheClient;
import org.cloudfoundry.reactor.networking.ReactorNetworkingClient;
import org.cloudfoundry.reactor.routing.ReactorRoutingClient;
import org.cloudfoundry.reactor.tokenprovider.ClientCredentialsGrantTokenProvider;
import org.cloudfoundry.reactor.tokenprovider.PasswordGrantTokenProvider;
import org.cloudfoundry.reactor.uaa.ReactorUaaClient;
import org.cloudfoundry.routing.RoutingClient;
import org.cloudfoundry.uaa.UaaClient;
import org.cloudfoundry.uaa.clients.CreateClientRequest;
import org.cloudfoundry.uaa.groups.AddMemberRequest;
import org.cloudfoundry.uaa.groups.CreateGroupRequest;
import org.cloudfoundry.uaa.groups.CreateGroupResponse;
import org.cloudfoundry.uaa.groups.Group;
import org.cloudfoundry.uaa.groups.ListGroupsRequest;
import org.cloudfoundry.uaa.groups.ListGroupsResponse;
import org.cloudfoundry.uaa.groups.MemberType;
import org.cloudfoundry.uaa.users.CreateUserRequest;
import org.cloudfoundry.uaa.users.CreateUserResponse;
import org.cloudfoundry.uaa.users.Email;
import org.cloudfoundry.uaa.users.Name;
import org.cloudfoundry.util.PaginationUtils;
import org.cloudfoundry.util.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
/**
* Default configuration class for ALL integration tests.
* <p>
* Some beans are annotated as {@link Lazy}, meaning that they will only be initialized
* if a test class actually uses them through {@link Autowired} injection. This allows us
* to declare some CF applications as beans, but only push them when they are actually
* used by the class under tests. This makes our tests faster, as pushing an app can take
* several minutes.
*/
@Configuration
@EnableAutoConfiguration
public class IntegrationTestConfiguration {
private static final List<String> GROUPS =
Arrays.asList(
"clients.admin",
"clients.secret",
"cloud_controller.admin",
"idps.write",
"network.admin",
"routing.router_groups.read",
"routing.router_groups.write",
"routing.routes.read",
"routing.routes.write",
"scim.create",
"scim.invite",
"scim.read",
"scim.userids",
"scim.write",
"scim.zones",
"uaa.admin",
"zones.read",
"zones.write");
private static final List<String> SCOPES =
Arrays.asList(
"clients.admin",
"clients.secret",
"cloud_controller.admin",
"cloud_controller.read",
"cloud_controller.write",
"idps.write",
"network.admin",
"openid",
"password.write",
"routing.router_groups.read",
"routing.router_groups.write",
"routing.routes.read",
"routing.routes.write",
"scim.create",
"scim.invite",
"scim.read",
"scim.userids",
"scim.write",
"scim.zones",
"uaa.admin",
"uaa.user",
"zones.read",
"zones.write");
private final Logger logger = LoggerFactory.getLogger("cloudfoundry-client.test");
@Bean
@Qualifier("admin")
ReactorCloudFoundryClient adminCloudFoundryClient(
ConnectionContext connectionContext,
@Value("${test.admin.password}") String password,
@Value("${test.admin.username}") String username) {
return ReactorCloudFoundryClient.builder()
.connectionContext(connectionContext)
.tokenProvider(
PasswordGrantTokenProvider.builder()
.password(password)
.username(username)
.build())
.build();
}
@Bean
@Qualifier("admin")
NetworkingClient adminNetworkingClient(
ConnectionContext connectionContext,
@Value("${test.admin.password}") String password,
@Value("${test.admin.username}") String username) {
return ReactorNetworkingClient.builder()
.connectionContext(connectionContext)
.tokenProvider(
PasswordGrantTokenProvider.builder()
.password(password)
.username(username)
.build())
.build();
}
@Bean
@Qualifier("admin")
UaaClient adminUaaClient(
ConnectionContext connectionContext,
@Value("${test.admin.clientId}") String clientId,
@Value("${test.admin.clientSecret}") String clientSecret,
@Value("${uaa.api.request.limit:#{null}}") Integer environmentRequestLimit) {
ReactorUaaClient unthrottledClient =
ReactorUaaClient.builder()
.connectionContext(connectionContext)
.tokenProvider(
ClientCredentialsGrantTokenProvider.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.build())
.build();
if (environmentRequestLimit == null) {
return unthrottledClient;
} else {
return new ThrottlingUaaClient(unthrottledClient, environmentRequestLimit);
}
}
@Bean(initMethod = "block")
@DependsOn("cloudFoundryCleaner")
Mono<Tuple2<String, String>> client(
@Qualifier("admin") UaaClient uaaClient, String clientId, String clientSecret) {
return uaaClient
.clients()
.create(
CreateClientRequest.builder()
.authorizedGrantTypes(
AUTHORIZATION_CODE,
CLIENT_CREDENTIALS,
PASSWORD,
REFRESH_TOKEN)
.autoApprove(String.valueOf(true))
.clientId(clientId)
.clientSecret(clientSecret)
.redirectUriPattern("https://test.com/login")
.scopes(SCOPES)
.build())
.thenReturn(Tuples.of(clientId, clientSecret))
.doOnSubscribe(
s -> this.logger.debug(">> CLIENT ({}/{}) <<", clientId, clientSecret))
.doOnError(Throwable::printStackTrace)
.doOnSuccess(r -> this.logger.debug("<< CLIENT ({})>>", clientId));
}
@Bean
String clientId(NameFactory nameFactory) {
return nameFactory.getClientId();
}
@Bean
String clientSecret(NameFactory nameFactory) {
return nameFactory.getClientSecret();
}
@Bean
CloudFoundryCleaner cloudFoundryCleaner(
@Qualifier("admin") CloudFoundryClient cloudFoundryClient,
NameFactory nameFactory,
@Qualifier("admin") NetworkingClient networkingClient,
Version serverVersion,
@Qualifier("admin") UaaClient uaaClient) {
return new CloudFoundryCleaner(
cloudFoundryClient, nameFactory, networkingClient, serverVersion, uaaClient);
}
@Bean
ReactorCloudFoundryClient cloudFoundryClient(
ConnectionContext connectionContext, TokenProvider tokenProvider) {
return ReactorCloudFoundryClient.builder()
.connectionContext(connectionContext)
.tokenProvider(tokenProvider)
.build();
}
@Bean
DefaultCloudFoundryOperations cloudFoundryOperations(
CloudFoundryClient cloudFoundryClient,
DopplerClient dopplerClient,
NetworkingClient networkingClient,
RoutingClient routingClient,
UaaClient uaaClient,
String organizationName,
String spaceName) {
return DefaultCloudFoundryOperations.builder()
.cloudFoundryClient(cloudFoundryClient)
.dopplerClient(dopplerClient)
.networkingClient(networkingClient)
.routingClient(routingClient)
.uaaClient(uaaClient)
.organization(organizationName)
.space(spaceName)
.build();
}
@Bean
CloudFoundryVersionConditionalRule cloudFoundryVersionConditionalRule(Version serverVersion) {
return new CloudFoundryVersionConditionalRule(serverVersion);
}
@Bean
DefaultConnectionContext connectionContext(
@Value("${test.apiHost}") String apiHost,
@Value("${test.proxy.host:}") String proxyHost,
@Value("${test.proxy.password:}") String proxyPassword,
@Value("${test.proxy.port:8080}") Integer proxyPort,
@Value("${test.proxy.username:}") String proxyUsername,
@Value("${test.skipSslValidation:false}") Boolean skipSslValidation) {
DefaultConnectionContext.Builder connectionContext =
DefaultConnectionContext.builder()
.apiHost(apiHost)
.problemHandler(
new FailingDeserializationProblemHandler()) // Test-only problem
// handler
.skipSslValidation(skipSslValidation)
.sslHandshakeTimeout(Duration.ofSeconds(30));
if (StringUtils.hasText(proxyHost)) {
ProxyConfiguration.Builder proxyConfiguration =
ProxyConfiguration.builder().host(proxyHost).port(proxyPort);
if (StringUtils.hasText(proxyUsername)) {
proxyConfiguration.password(proxyPassword).username(proxyUsername);
}
connectionContext.proxyConfiguration(proxyConfiguration.build());
}
return connectionContext.build();
}
@Bean
DopplerClient dopplerClient(ConnectionContext connectionContext, TokenProvider tokenProvider) {
return ReactorDopplerClient.builder()
.connectionContext(connectionContext)
.tokenProvider(tokenProvider)
.build();
}
@Bean
ReactorLogCacheClient logCacheClient(
ConnectionContext connectionContext, TokenProvider tokenProvider) {
return ReactorLogCacheClient.builder()
.connectionContext(connectionContext)
.tokenProvider(tokenProvider)
.build();
}
@Bean
RandomNameFactory nameFactory() {
return new RandomNameFactory(new SecureRandom());
}
@Bean(initMethod = "block")
@DependsOn("cloudFoundryCleaner")
Mono<String> metricRegistrarServiceInstance(
CloudFoundryClient cloudFoundryClient, Mono<String> spaceId, NameFactory nameFactory) {
return spaceId.flatMap(
spaceIdValue ->
cloudFoundryClient
.userProvidedServiceInstances()
.create(
CreateUserProvidedServiceInstanceRequest.builder()
.name(nameFactory.getServiceInstanceName())
.spaceId(spaceIdValue)
.syslogDrainUrl("structured-format://json")
.build()))
.map(ResourceUtils::getId)
.cache();
}
@Bean
NetworkingClient networkingClient(
ConnectionContext connectionContext, TokenProvider tokenProvider) {
return ReactorNetworkingClient.builder()
.connectionContext(connectionContext)
.tokenProvider(tokenProvider)
.build();
}
@Bean(initMethod = "block")
@DependsOn("cloudFoundryCleaner")
Mono<String> organizationId(
CloudFoundryClient cloudFoundryClient,
String organizationName,
String organizationQuotaName,
Mono<String> userId) {
return userId.flatMap(
userId1 ->
cloudFoundryClient
.organizationQuotaDefinitions()
.create(
CreateOrganizationQuotaDefinitionRequest.builder()
.applicationInstanceLimit(-1)
.applicationTaskLimit(-1)
.instanceMemoryLimit(-1)
.memoryLimit(16384)
.name(organizationQuotaName)
.nonBasicServicesAllowed(true)
.totalPrivateDomains(-1)
.totalReservedRoutePorts(-1)
.totalRoutes(-1)
.totalServiceKeys(-1)
.totalServices(-1)
.build())
.map(ResourceUtils::getId)
.zipWith(Mono.just(userId1)))
.flatMap(
function(
(quotaId, userId1) ->
cloudFoundryClient
.organizations()
.create(
CreateOrganizationRequest.builder()
.name(organizationName)
.quotaDefinitionId(quotaId)
.build())
.map(ResourceUtils::getId)
.zipWith(Mono.just(userId1))))
.flatMap(
function(
(organizationId, userId1) ->
cloudFoundryClient
.organizations()
.associateManager(
AssociateOrganizationManagerRequest
.builder()
.organizationId(organizationId)
.managerId(userId1)
.build())
.thenReturn(organizationId)))
.doOnSubscribe(s -> this.logger.debug(">> ORGANIZATION ({}) <<", organizationName))
.doOnError(Throwable::printStackTrace)
.doOnSuccess(id -> this.logger.debug("<< ORGANIZATION ({}) >>", id))
.cache();
}
@Bean
String organizationName(NameFactory nameFactory) {
return nameFactory.getOrganizationName();
}
@Bean
String organizationQuotaName(NameFactory nameFactory) {
return nameFactory.getQuotaDefinitionName();
}
@Bean
String password(NameFactory nameFactory) {
return nameFactory.getPassword();
}
@Bean
String planName(NameFactory nameFactory) {
return nameFactory.getPlanName();
}
@Bean
RoutingClient routingClient(ConnectionContext connectionContext, TokenProvider tokenProvider) {
return ReactorRoutingClient.builder()
.connectionContext(connectionContext)
.tokenProvider(tokenProvider)
.build();
}
@Bean
Version serverVersion(@Qualifier("admin") CloudFoundryClient cloudFoundryClient) {
return cloudFoundryClient
.info()
.get(GetInfoRequest.builder().build())
.map(response -> Version.valueOf(response.getApiVersion()))
.doOnSubscribe(s -> this.logger.debug(">> CLOUD FOUNDRY VERSION <<"))
.doOnSuccess(r -> this.logger.debug("<< CLOUD FOUNDRY VERSION >>"))
.block();
}
@Lazy
@Bean(initMethod = "block")
@DependsOn("cloudFoundryCleaner")
Mono<String> serviceBrokerId(
CloudFoundryClient cloudFoundryClient,
NameFactory nameFactory,
String planName,
String serviceBrokerName,
String serviceName,
Mono<String> spaceId) {
return spaceId.flatMap(
spaceId1 ->
ServiceBrokerUtils.createServiceBroker(
cloudFoundryClient,
nameFactory,
planName,
serviceBrokerName,
serviceName,
spaceId1,
false)
.map(response -> response.serviceBrokerId))
.doOnSubscribe(
s ->
this.logger.debug(
">> SERVICE BROKER ({} {}/{}) <<",
serviceBrokerName,
serviceName,
planName))
.doOnError(Throwable::printStackTrace)
.doOnSuccess(id -> this.logger.debug("<< SERVICE_BROKER ({})>>", id))
.cache();
}
@Bean
String serviceBrokerName(NameFactory nameFactory) {
return nameFactory.getServiceBrokerName();
}
@Bean
String serviceName(NameFactory nameFactory) {
return nameFactory.getServiceName();
}
@Bean(initMethod = "block")
@DependsOn("cloudFoundryCleaner")
Mono<String> spaceId(
CloudFoundryClient cloudFoundryClient, Mono<String> organizationId, String spaceName) {
return organizationId
.flatMap(
orgId ->
cloudFoundryClient
.spaces()
.create(
CreateSpaceRequest.builder()
.name(spaceName)
.organizationId(orgId)
.build()))
.map(ResourceUtils::getId)
.doOnSubscribe(s -> this.logger.debug(">> SPACE ({}) <<", spaceName))
.doOnError(Throwable::printStackTrace)
.doOnSuccess(id -> this.logger.debug("<< SPACE ({}) >>", id))
.cache();
}
@Bean
String spaceName(NameFactory nameFactory) {
return nameFactory.getSpaceName();
}
@Bean(initMethod = "block")
@DependsOn("cloudFoundryCleaner")
Mono<String> stackId(CloudFoundryClient cloudFoundryClient, Mono<String> stackName) {
return stackName
.flux()
.flatMap(
name ->
PaginationUtils.requestClientV2Resources(
page ->
cloudFoundryClient
.stacks()
.list(
ListStacksRequest.builder()
.name(name)
.page(page)
.build())))
.single()
.map(ResourceUtils::getId)
.doOnSubscribe(s -> this.logger.debug(">> STACK ({}) <<", stackName))
.doOnError(Throwable::printStackTrace)
.doOnSuccess(id -> this.logger.debug("<< STACK ({})>>", id))
.cache();
}
/**
* Select the most recent stack available, matching {@code cflinuxfs*}, based
* on the stack number.
*/
@Bean(initMethod = "block")
@DependsOn("cloudFoundryCleaner")
Mono<String> stackName(CloudFoundryClient cloudFoundryClient) {
return PaginationUtils.requestClientV2Resources(
page ->
cloudFoundryClient
.stacks()
.list(ListStacksRequest.builder().page(page).build()))
.map(StackResource::getEntity)
.map(StackEntity::getName)
.filter(s -> s.matches("^cflinuxfs\\d$"))
.sort(Comparator.reverseOrder())
.next()
.cache();
}
@Lazy
@Bean(initMethod = "block")
@DependsOn("cloudFoundryCleaner")
Mono<ApplicationUtils.ApplicationMetadata> testLogCacheApp(
CloudFoundryClient cloudFoundryClient,
Mono<String> spaceId,
Mono<String> metricRegistrarServiceInstance,
String testLogCacheAppName,
String testLogCacheHostName,
Path testLogCacheAppbits) {
return metricRegistrarServiceInstance
.zipWith(spaceId)
.flatMap(
function(
(metricsRegistrarServiceInstanceId, spaceIdValue) ->
ApplicationUtils.pushApplication(
cloudFoundryClient,
testLogCacheAppbits,
testLogCacheAppName,
Collections.singleton(
metricsRegistrarServiceInstanceId),
new HashMap<>(),
testLogCacheHostName,
spaceIdValue)))
.cache();
}
@Bean
Path testLogCacheAppBits() throws IOException {
return new ClassPathResource("test-log-cache.jar").getFile().toPath();
}
@Bean
String testLogCacheAppName(NameFactory nameFactory) {
return nameFactory.getApplicationName();
}
@Lazy
@Bean
TestLogCacheEndpoints testLogCacheEndpoints(
ConnectionContext connectionContext,
TokenProvider tokenProvider,
Mono<ApplicationUtils.ApplicationMetadata> testLogCacheApp) {
return new TestLogCacheEndpoints(
connectionContext, testLogCacheApp.map(app -> app.uri), tokenProvider);
}
@Bean
String testLogCacheHostName(NameFactory nameFactory) {
return nameFactory.getHostName();
}
@Bean
@DependsOn({"client", "userId"})
PasswordGrantTokenProvider tokenProvider(
String clientId, String clientSecret, String password, String username) {
return PasswordGrantTokenProvider.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.password(password)
.username(username)
.build();
}
@Bean
UaaClient uaaClient(
ConnectionContext connectionContext,
TokenProvider tokenProvider,
@Value("${uaa.api.request.limit:#{null}}") Integer environmentRequestLimit) {
ReactorUaaClient unthrottledClient =
ReactorUaaClient.builder()
.connectionContext(connectionContext)
.tokenProvider(tokenProvider)
.build();
if (environmentRequestLimit == null) {
return unthrottledClient;
} else {
return new ThrottlingUaaClient(unthrottledClient, environmentRequestLimit);
}
}
@Bean(initMethod = "block")
@DependsOn("cloudFoundryCleaner")
Mono<String> userId(@Qualifier("admin") UaaClient uaaClient, String password, String username) {
return uaaClient
.users()
.create(
CreateUserRequest.builder()
.email(
Email.builder()
.primary(true)
.value(
String.format(
"%s@%s.com", username, username))
.build())
.name(Name.builder().givenName("Test").familyName("User").build())
.password(password)
.userName(username)
.build())
.map(CreateUserResponse::getId)
.delayUntil(
userId ->
Flux.fromIterable(GROUPS)
.flatMap(
group ->
uaaClient
.groups()
.list(
ListGroupsRequest.builder()
.filter(
String
.format(
"displayName"
+ " eq \"%s\"",
group))
.build())
.flatMapIterable(
ListGroupsResponse
::getResources)
.singleOrEmpty()
.map(Group::getId)
.switchIfEmpty(
uaaClient
.groups()
.create(
CreateGroupRequest
.builder()
.displayName(
group)
.build())
.map(
CreateGroupResponse
::getId))
.flatMap(
groupId ->
uaaClient
.groups()
.addMember(
AddMemberRequest
.builder()
.groupId(
groupId)
.memberId(
userId)
.origin(
"uaa")
.type(
MemberType
.USER)
.build()))))
.doOnSubscribe(s -> this.logger.debug(">> USER ({}/{}) <<", username, password))
.doOnError(Throwable::printStackTrace)
.doOnSuccess(id -> this.logger.debug("<< USER ({})>>", id))
.cache();
}
@Bean
String username(NameFactory nameFactory) {
return nameFactory.getUserName();
}
public static final class FailingDeserializationProblemHandler
extends DeserializationProblemHandler {
@Override
public boolean handleUnknownProperty(
DeserializationContext ctxt,
JsonParser jp,
JsonDeserializer<?> deserializer,
Object beanOrClass,
String propertyName) {
fail(
String.format(
"Found unexpected property %s in payload for %s",
propertyName, beanOrClass.getClass().getName()));
return false;
}
}
}