|
1 | 1 | /* |
2 | | - * Copyright 2013-2021 the original author or authors. |
| 2 | + * Copyright 2013-2024 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
18 | 18 |
|
19 | 19 | import static io.netty.handler.codec.http.HttpMethod.GET; |
20 | 20 | import static io.netty.handler.codec.http.HttpResponseStatus.OK; |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.ArgumentMatchers.eq; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.when; |
21 | 26 |
|
| 27 | +import java.net.URI; |
22 | 28 | import java.time.Duration; |
23 | 29 | import java.util.Collections; |
24 | 30 | import org.cloudfoundry.logcache.v1.Envelope; |
|
34 | 40 | import org.cloudfoundry.logcache.v1.Metric; |
35 | 41 | import org.cloudfoundry.logcache.v1.ReadRequest; |
36 | 42 | import org.cloudfoundry.logcache.v1.ReadResponse; |
| 43 | +import org.cloudfoundry.reactor.ConnectionContext; |
| 44 | +import org.cloudfoundry.reactor.DefaultConnectionContext; |
37 | 45 | import org.cloudfoundry.reactor.InteractionContext; |
| 46 | +import org.cloudfoundry.reactor.RootProvider; |
38 | 47 | import org.cloudfoundry.reactor.TestRequest; |
39 | 48 | import org.cloudfoundry.reactor.TestResponse; |
40 | 49 | import org.junit.jupiter.api.Test; |
| 50 | +import reactor.core.publisher.Mono; |
41 | 51 | import reactor.test.StepVerifier; |
42 | 52 |
|
43 | 53 | class ReactorLogCacheClientTest extends AbstractLogCacheApiTest { |
| 54 | + private static final String API_ROOT = |
| 55 | + "http://api.my.rapid.server.com"; // the ".rAPId." part also contains the string "api". |
| 56 | + private static final String LOGCACHE = |
| 57 | + "http://log-cache.my.rapid.server.com"; // only the "api" at the start of |
| 58 | + // the url should be replaced. |
44 | 59 |
|
45 | 60 | private final ReactorLogCacheEndpoints logCacheEndpoints = |
46 | 61 | new ReactorLogCacheEndpoints( |
47 | 62 | CONNECTION_CONTEXT, this.root, TOKEN_PROVIDER, Collections.emptyMap()); |
48 | 63 |
|
| 64 | + @Test |
| 65 | + void getRootFromFallback() { |
| 66 | + URI webServerUri = URI.create(this.root.block(Duration.ofSeconds(5))); |
| 67 | + Mono<String> apiRoot = Mono.just(API_ROOT); |
| 68 | + RootProvider rootProvider = mock(RootProvider.class); |
| 69 | + when(rootProvider.getRoot(eq("log_cache"), any())) |
| 70 | + .thenReturn(Mono.error(new IllegalArgumentException())); // trigger fallback |
| 71 | + when(rootProvider.getRoot(any())).thenReturn(apiRoot); |
| 72 | + ConnectionContext connectionContext = |
| 73 | + DefaultConnectionContext.builder() |
| 74 | + .rootProvider(rootProvider) |
| 75 | + .apiHost(webServerUri.getHost()) |
| 76 | + .port(webServerUri.getPort()) |
| 77 | + .secure(false) |
| 78 | + .build(); |
| 79 | + ReactorLogCacheClient examinee = |
| 80 | + ReactorLogCacheClient.builder() |
| 81 | + .connectionContext(connectionContext) |
| 82 | + .tokenProvider(TOKEN_PROVIDER) |
| 83 | + .build(); |
| 84 | + Mono<String> logCacheRoot = examinee.getRoot(); |
| 85 | + String rootString = logCacheRoot.block(Duration.ofSeconds(15)); |
| 86 | + assertThat(rootString).isEqualTo(LOGCACHE); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void getRootFromEndpoint() { |
| 91 | + mockRequest( |
| 92 | + InteractionContext.builder() |
| 93 | + .request(TestRequest.builder().method(GET).path("/").build()) |
| 94 | + .response( |
| 95 | + TestResponse.builder() |
| 96 | + .status(OK) |
| 97 | + .payload("fixtures/GET_response.json") |
| 98 | + .build()) |
| 99 | + .build()); |
| 100 | + URI webServerUri = URI.create(this.root.block(Duration.ofSeconds(5))); |
| 101 | + ConnectionContext connectionContext = |
| 102 | + DefaultConnectionContext.builder() |
| 103 | + .apiHost(webServerUri.getHost()) |
| 104 | + .port(webServerUri.getPort()) |
| 105 | + .secure(false) |
| 106 | + .build(); |
| 107 | + ReactorLogCacheClient examinee = |
| 108 | + ReactorLogCacheClient.builder() |
| 109 | + .connectionContext(connectionContext) |
| 110 | + .tokenProvider(TOKEN_PROVIDER) |
| 111 | + .build(); |
| 112 | + Mono<String> logCacheRoot = examinee.getRoot(); |
| 113 | + String rootString = logCacheRoot.block(Duration.ofSeconds(5)); |
| 114 | + assertThat(rootString) |
| 115 | + .isEqualTo("http://cache-for-logging.run.pivotal.io:" + webServerUri.getPort()); |
| 116 | + } |
| 117 | + |
49 | 118 | @Test |
50 | 119 | void info() { |
51 | 120 | mockRequest( |
|
0 commit comments