|
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.io.IOException; |
| 28 | +import java.net.URI; |
22 | 29 | import java.time.Duration;
|
23 | 30 | import java.util.Collections;
|
24 | 31 | import org.cloudfoundry.logcache.v1.Envelope;
|
|
34 | 41 | import org.cloudfoundry.logcache.v1.Metric;
|
35 | 42 | import org.cloudfoundry.logcache.v1.ReadRequest;
|
36 | 43 | import org.cloudfoundry.logcache.v1.ReadResponse;
|
| 44 | +import org.cloudfoundry.reactor.ConnectionContext; |
| 45 | +import org.cloudfoundry.reactor.DefaultConnectionContext; |
37 | 46 | import org.cloudfoundry.reactor.InteractionContext;
|
| 47 | +import org.cloudfoundry.reactor.RootProvider; |
38 | 48 | import org.cloudfoundry.reactor.TestRequest;
|
39 | 49 | import org.cloudfoundry.reactor.TestResponse;
|
40 | 50 | import org.junit.jupiter.api.Test;
|
| 51 | +import reactor.core.publisher.Mono; |
41 | 52 | import reactor.test.StepVerifier;
|
42 | 53 |
|
43 | 54 | class ReactorLogCacheClientTest extends AbstractLogCacheApiTest {
|
| 55 | + private static final String API_ROOT = "http://api.my.rapid.server.com"; |
| 56 | + private static final String LOGCACHE = "http://log-cache.my.rlog-cached.server.com"; |
44 | 57 |
|
45 | 58 | private final ReactorLogCacheEndpoints logCacheEndpoints =
|
46 | 59 | new ReactorLogCacheEndpoints(
|
47 | 60 | CONNECTION_CONTEXT, this.root, TOKEN_PROVIDER, Collections.emptyMap());
|
48 | 61 |
|
| 62 | + @Test |
| 63 | + void getRootFromFallback() throws IOException { |
| 64 | + URI webServerUri = URI.create(this.root.block(Duration.ofSeconds(5))); |
| 65 | + Mono<String> apiRoot = Mono.just(API_ROOT); |
| 66 | + RootProvider rootProvider = mock(RootProvider.class); |
| 67 | + when(rootProvider.getRoot(eq("log_cache"), any())) |
| 68 | + .thenReturn(Mono.error(new IllegalArgumentException())); // trigger fallback |
| 69 | + when(rootProvider.getRoot(any())).thenReturn(apiRoot); |
| 70 | + ConnectionContext connectionContext = |
| 71 | + DefaultConnectionContext.builder() |
| 72 | + .rootProvider(rootProvider) |
| 73 | + .apiHost(webServerUri.getHost()) |
| 74 | + .port(webServerUri.getPort()) |
| 75 | + .secure(false) |
| 76 | + .build(); |
| 77 | + ReactorLogCacheClient examinee = |
| 78 | + ReactorLogCacheClient.builder() |
| 79 | + .connectionContext(connectionContext) |
| 80 | + .tokenProvider(TOKEN_PROVIDER) |
| 81 | + .build(); |
| 82 | + Mono<String> logCacheRoot = examinee.getRoot(); |
| 83 | + String rootString = logCacheRoot.block(Duration.ofSeconds(15)); |
| 84 | + assertThat(rootString).isEqualTo(LOGCACHE); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void getRootFromEndpoint() { |
| 89 | + mockRequest( |
| 90 | + InteractionContext.builder() |
| 91 | + .request(TestRequest.builder().method(GET).path("/").build()) |
| 92 | + .response( |
| 93 | + TestResponse.builder() |
| 94 | + .status(OK) |
| 95 | + .payload("fixtures/GET_response.json") |
| 96 | + .build()) |
| 97 | + .build()); |
| 98 | + URI webServerUri = URI.create(this.root.block(Duration.ofSeconds(5))); |
| 99 | + ConnectionContext connectionContext = |
| 100 | + DefaultConnectionContext.builder() |
| 101 | + .apiHost(webServerUri.getHost()) |
| 102 | + .port(webServerUri.getPort()) |
| 103 | + .secure(false) |
| 104 | + .build(); |
| 105 | + ReactorLogCacheClient examinee = |
| 106 | + ReactorLogCacheClient.builder() |
| 107 | + .connectionContext(connectionContext) |
| 108 | + .tokenProvider(TOKEN_PROVIDER) |
| 109 | + .build(); |
| 110 | + Mono<String> logCacheRoot = examinee.getRoot(); |
| 111 | + String rootString = logCacheRoot.block(Duration.ofSeconds(5)); |
| 112 | + assertThat(rootString) |
| 113 | + .isEqualTo( |
| 114 | + "http://cache-for-logging.cf.lod-cfcli3.cfrt-sof.sapcloud.io:" |
| 115 | + + webServerUri.getPort()); |
| 116 | + } |
| 117 | + |
49 | 118 | @Test
|
50 | 119 | void info() {
|
51 | 120 | mockRequest(
|
|
0 commit comments