AbstractAdminApplicationTest.java 6.27 KB
Newer Older
1
/*
Johannes Edmeier committed
2
 * Copyright 2014-2018 the original author or authors.
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
 *
 * 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 de.codecentric.boot.admin.server;

import de.codecentric.boot.admin.server.domain.values.Registration;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

import java.net.URI;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicReference;
import org.json.JSONObject;
import org.junit.Test;
import org.springframework.http.MediaType;
28 29
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
30
import org.springframework.test.web.reactive.server.WebTestClient;
31 32 33
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
34 35 36 37 38 39 40 41 42

import static org.assertj.core.api.Assertions.assertThat;


public abstract class AbstractAdminApplicationTest {
    private WebTestClient webClient;
    private int port;


Johannes Edmeier committed
43
    public void setUp(int port) {
44
        this.port = port;
45
        this.webClient = createWebClient(port);
46 47 48
    }

    @Test
49
    public void lifecycle() {
50 51
        AtomicReference<URI> location = new AtomicReference<>();

Johannes Edmeier committed
52
        StepVerifier.create(getEventStream().log())
53 54
                    .expectSubscription()
                    .then(() -> {
55 56
                        listEmptyInstances();
                        location.set(registerInstance());
57 58 59
                    })
                    .assertNext((event) -> assertThat(event.opt("type")).isEqualTo("REGISTERED"))
                    .assertNext((event) -> assertThat(event.opt("type")).isEqualTo("STATUS_CHANGED"))
60
                    .assertNext((event) -> assertThat(event.opt("type")).isEqualTo("ENDPOINTS_DETECTED"))
61 62
                    .assertNext((event) -> assertThat(event.opt("type")).isEqualTo("INFO_CHANGED"))
                    .then(() -> {
63 64 65
                        getInstance(location.get());
                        listInstances();
                        deregisterInstance(location.get());
66 67
                    })
                    .assertNext((event) -> assertThat(event.opt("type")).isEqualTo("DEREGISTERED"))
68
                    .then(this::listEmptyInstances)
Johannes Edmeier committed
69
                    .thenCancel().verify(Duration.ofSeconds(60));
70 71
    }

72
    protected Flux<JSONObject> getEventStream() {
73
        //@formatter:off
74
        return webClient.get().uri("/instances/events").accept(MediaType.TEXT_EVENT_STREAM)
75 76
                        .exchange()
                        .expectStatus().isOk()
77
                        .expectHeader().contentType(MediaType.TEXT_EVENT_STREAM)
78 79 80 81
                        .returnResult(JSONObject.class).getResponseBody();
        //@formatter:on
    }

82
    protected URI registerInstance() {
83
        //@formatter:off
84
       return webClient.post().uri("/instances").contentType(MediaType.APPLICATION_JSON).syncBody(createRegistration())
85 86
                       .exchange()
                       .expectStatus().isCreated()
87
                       .expectHeader().valueMatches("location", "^http://localhost:" + port + "/instances/[a-f0-9]+$")
88 89 90 91
                       .returnResult(Void.class).getResponseHeaders().getLocation();
        //@formatter:on
    }

92
    protected void getInstance(URI uri) {
93 94 95 96 97
        //@formatter:off
        webClient.get().uri(uri).accept(MediaType.APPLICATION_JSON_UTF8)
                 .exchange()
                 .expectStatus().isOk()
                 .expectBody()
98 99 100
                 .jsonPath("$.registration.name").isEqualTo("Test-Instance")
                 .jsonPath("$.statusInfo.status").isEqualTo("UP")
                 .jsonPath("$.info.test").isEqualTo("foobar");
101 102 103
       //@formatter:on
    }

104
    protected void listInstances() {
105
        //@formatter:off
106
        webClient.get().uri("/instances").accept(MediaType.APPLICATION_JSON_UTF8)
107 108 109
                 .exchange()
                 .expectStatus().isOk()
                 .expectBody()
110
                     .jsonPath("$[0].registration.name").isEqualTo("Test-Instance")
111 112 113 114 115
                     .jsonPath("$[0].statusInfo.status").isEqualTo("UP")
                     .jsonPath("$[0].info.test").isEqualTo("foobar");
       //@formatter:on
    }

116
    protected void listEmptyInstances() {
117
        //@formatter:off
118
        webClient.get().uri("/instances").accept(MediaType.APPLICATION_JSON_UTF8)
119 120 121 122 123 124
                 .exchange()
                 .expectStatus().isOk()
                 .expectBody().json("[]");
       //@formatter:on
    }

125
    protected void deregisterInstance(URI uri) {
126 127 128 129 130 131
        webClient.delete().uri(uri).exchange().expectStatus().isNoContent();
    }


    private Registration createRegistration() {
        return Registration.builder()
132
                           .name("Test-Instance")
133
                           .healthUrl("http://localhost:" + port + "/mgmt/health")
134 135 136 137
                           .managementUrl("http://localhost:" + port + "/mgmt")
                           .serviceUrl("http://localhost:" + port)
                           .build();
    }
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    protected WebTestClient createWebClient(int port) {
        ObjectMapper mapper = new ObjectMapper().registerModule(new JsonOrgModule());
        return WebTestClient.bindToServer()
                            .baseUrl("http://localhost:" + port)
                            .exchangeStrategies(ExchangeStrategies.builder().codecs((configurer) -> {
                                configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper));
                                configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper));
                            }).build())
                            .build();
    }

    public int getPort() {
        return port;
    }

    public WebTestClient getWebClient() {
        return webClient;
    }
157
}