AdminApplicationHazelcastTest.java 6.22 KB
Newer Older
1
/*
2
 * Copyright 2014-2018 the original author or authors.
3 4 5 6 7
 *
 * 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
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15
 *
 * 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
package de.codecentric.boot.admin.server;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
20 21
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
22

23
import java.util.stream.Collectors;
24 25
import org.junit.After;
import org.junit.Before;
26
import org.junit.Test;
27
import org.springframework.boot.SpringBootConfiguration;
28
import org.springframework.boot.WebApplicationType;
29
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
30 31
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
32
import org.springframework.context.annotation.Bean;
33
import org.springframework.http.MediaType;
34 35 36
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
37
import org.springframework.test.web.reactive.server.WebTestClient;
38 39
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
40
import com.hazelcast.config.InMemoryFormat;
41 42 43 44
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.TcpIpConfig;

import static java.util.Collections.singletonList;
45
import static org.assertj.core.api.Assertions.assertThat;
46 47 48 49 50 51

/**
 * Integration test to verify the correct functionality of the REST API with Hazelcast
 *
 * @author Dennis Schulte
 */
52
public class AdminApplicationHazelcastTest extends AbstractAdminApplicationTest {
53 54
    private ConfigurableApplicationContext instance1;
    private ConfigurableApplicationContext instance2;
55
    private WebTestClient webClient2;
56 57

    @Before
58
    public void setUp() {
59
        System.setProperty("hazelcast.wait.seconds.before.join", "0");
60 61 62
        instance1 = new SpringApplicationBuilder().sources(TestAdminApplication.class)
                                                  .web(WebApplicationType.REACTIVE)
                                                  .run("--server.port=0", "--management.endpoints.web.base-path=/mgmt",
63 64
                                                      "--endpoints.health.enabled=true", "--info.test=foobar",
                                                      "--spring.jmx.enabled=false", "--eureka.client.enabled=false");
65 66 67 68

        instance2 = new SpringApplicationBuilder().sources(TestAdminApplication.class)
                                                  .web(WebApplicationType.REACTIVE)
                                                  .run("--server.port=0", "--management.endpoints.web.base-path=/mgmt",
69 70
                                                      "--endpoints.health.enabled=true", "--info.test=foobar",
                                                      "--spring.jmx.enabled=false", "--eureka.client.enabled=false");
71 72 73

        super.setUp(instance1.getEnvironment().getProperty("local.server.port", Integer.class, 0));
        this.webClient2 = createWebClient(
74
            instance2.getEnvironment().getProperty("local.server.port", Integer.class, 0));
75 76 77 78 79 80 81 82 83
    }


    @Test
    @Override
    public void lifecycle() {
        super.lifecycle();

        Mono<String> events1 = getWebClient().get()
84
                                             .uri("/instances/events")
85 86 87 88 89 90 91 92 93
                                             .accept(MediaType.APPLICATION_JSON)
                                             .exchange()
                                             .expectStatus()
                                             .isOk()
                                             .returnResult(String.class)
                                             .getResponseBody()
                                             .collect(Collectors.joining());

        Mono<String> events2 = webClient2.get()
94
                                         .uri("/instances/events")
95 96 97 98 99 100 101 102
                                         .accept(MediaType.APPLICATION_JSON)
                                         .exchange()
                                         .expectStatus()
                                         .isOk()
                                         .returnResult(String.class)
                                         .getResponseBody()
                                         .collect(Collectors.joining());

103
        StepVerifier.create(events1.zipWith(events2))
104 105
                    .assertNext(t -> assertThat(t.getT1()).isEqualTo(t.getT2()))
                    .verifyComplete();
106 107
    }

108 109 110 111
    @After
    public void shutdown() {
        instance1.close();
        instance2.close();
112 113
    }

114 115
    @SpringBootConfiguration
    @EnableAutoConfiguration
116
    @EnableAdminServer
117
    @EnableWebFluxSecurity
118
    public static class TestAdminApplication {
119 120 121 122 123 124 125
        @Bean
        SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
            return http.authorizeExchange().anyExchange().permitAll()//
                       .and().csrf().disable()//
                       .build();
        }

126 127 128
        @Bean
        public Config hazelcastConfig() {
            Config config = new Config();
129 130 131
            config.addMapConfig(new MapConfig("spring-boot-admin-event-store").setInMemoryFormat(InMemoryFormat.OBJECT)
                                                                              .setBackupCount(1)
                                                                              .setEvictionPolicy(EvictionPolicy.NONE));
132 133 134 135 136 137 138 139 140

            config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
            TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig();
            tcpIpConfig.setEnabled(true);
            tcpIpConfig.setMembers(singletonList("127.0.0.1"));
            return config;
        }
    }
}