AdminApplicationDiscoveryTest.java 5 KB
Newer Older
1
/*
2
 * Copyright 2014-2018 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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.
 */
16

17 18 19 20 21 22 23 24 25 26
package de.codecentric.boot.admin.server;

import de.codecentric.boot.admin.server.config.EnableAdminServer;

import java.net.URI;
import java.util.List;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.springframework.boot.SpringBootConfiguration;
27
import org.springframework.boot.WebApplicationType;
28
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
29
import org.springframework.boot.builder.SpringApplicationBuilder;
30 31
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryProperties;
32 33
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
34
import org.springframework.http.MediaType;
35 36 37
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
38 39 40 41 42 43

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;


public class AdminApplicationDiscoveryTest extends AbstractAdminApplicationTest {
44
    private ConfigurableApplicationContext instance;
45 46 47
    private SimpleDiscoveryProperties simpleDiscovery;

    @Before
48
    public void setUp() {
49 50 51
        instance = new SpringApplicationBuilder().sources(TestAdminApplication.class)
                                                 .web(WebApplicationType.REACTIVE)
                                                 .run("--server.port=0", "--management.endpoints.web.base-path=/mgmt",
52 53
                                                     "--endpoints.health.enabled=true", "--info.test=foobar",
                                                     "--eureka.client.enabled=false");
54 55 56

        simpleDiscovery = instance.getBean(SimpleDiscoveryProperties.class);

57
        super.setUp(instance.getEnvironment().getProperty("local.server.port", Integer.class, 0));
58 59 60 61
    }


    @Override
62 63 64
    protected URI registerInstance() {
        //We register the instance by setting static values for the SimpleDiscoveryClient and issuing a
        //InstanceRegisteredEvent that makes sure the instance gets registered.
65
        SimpleDiscoveryProperties.SimpleServiceInstance serviceInstance = new SimpleDiscoveryProperties.SimpleServiceInstance();
66
        serviceInstance.setServiceId("Test-Instance");
67 68 69 70 71 72
        serviceInstance.setUri(URI.create("http://localhost:" + getPort()));
        serviceInstance.getMetadata().put("management.context-path", "/mgmt");
        simpleDiscovery.getInstances().put("Test-Application", singletonList(serviceInstance));

        instance.publishEvent(new InstanceRegisteredEvent<>(new Object(), null));

73
        //To get the location of the registered instances we fetch the instance with the name.
74
        List<JSONObject> applications = getWebClient().get()
75
                                                      .uri("/instances?name=Test-Instance")
76 77 78 79 80 81 82 83 84
                                                      .accept(MediaType.APPLICATION_JSON)
                                                      .exchange()
                                                      .expectStatus()
                                                      .isOk()
                                                      .returnResult(JSONObject.class)
                                                      .getResponseBody()
                                                      .collectList()
                                                      .block();
        assertThat(applications).hasSize(1);
85
        return URI.create("http://localhost:" + getPort() + "/instances/" + applications.get(0).optString("id"));
86 87 88 89
    }


    @Override
90
    protected void deregisterInstance(URI uri) {
91 92 93 94 95 96 97 98 99 100 101 102
        simpleDiscovery.getInstances().clear();
        instance.publishEvent(new InstanceRegisteredEvent<>(new Object(), null));
    }

    @After
    public void shutdown() {
        instance.close();
    }

    @EnableAdminServer
    @EnableAutoConfiguration
    @SpringBootConfiguration
103
    @EnableWebFluxSecurity
104
    public static class TestAdminApplication {
105 106 107 108 109 110
        @Bean
        SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
            return http.authorizeExchange().anyExchange().permitAll()//
                       .and().csrf().disable()//
                       .build();
        }
111 112
    }
}