AbstractClientApplicationTest.java 4.58 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
 *
 * 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.
 */

17
package de.codecentric.boot.admin.client;
18

Johannes Edmeier committed
19 20 21 22 23
import de.codecentric.boot.admin.client.registration.ApplicationRegistrator;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.CountDownLatch;
24
import org.junit.Rule;
25
import org.junit.Test;
Johannes Edmeier committed
26 27 28 29 30
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
31
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
Johannes Edmeier committed
32 33
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
34 35
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;

36
import static com.github.tomakehurst.wiremock.client.WireMock.created;
37
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
38 39
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath;
40 41 42 43
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
Johannes Edmeier committed
44
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
45 46

public abstract class AbstractClientApplicationTest {
47 48

    @Rule
Johannes Edmeier committed
49 50
    public WireMockRule wireMock = new WireMockRule(options().dynamicPort().notifier(new ConsoleNotifier(true)));
    private static final CountDownLatch cdl = new CountDownLatch(1);
51 52

    public void setUp() throws Exception {
53
        ResponseDefinitionBuilder response = created().withHeader("Content-Type", "application/json")
Johannes Edmeier committed
54
                                                      .withHeader("Connection", "close")
55 56 57
                                                      .withHeader("Location", wireMock.url("/instances/abcdef"))
                                                      .withBody("{ \"id\" : \"abcdef\" }");
        wireMock.stubFor(post(urlEqualTo("/instances")).willReturn(response));
58 59 60
    }

    @Test
Johannes Edmeier committed
61 62 63 64 65 66
    public void test_context() throws InterruptedException, UnknownHostException {
        cdl.await();
        Thread.sleep(2500);
        String hostName = InetAddress.getLocalHost().getCanonicalHostName();
        String serviceHost = "http://" + hostName + ":" + getServerPort();
        String managementHost = "http://" + hostName + ":" + getManagementPort();
67 68 69 70 71 72 73
        RequestPatternBuilder request = postRequestedFor(urlEqualTo("/instances"));
        request.withHeader("Content-Type", equalTo("application/json"))
               .withRequestBody(matchingJsonPath("$.name", equalTo("Test-Client")))
               .withRequestBody(matchingJsonPath("$.healthUrl", equalTo(managementHost + "/mgmt/health")))
               .withRequestBody(matchingJsonPath("$.managementUrl", equalTo(managementHost + "/mgmt")))
               .withRequestBody(matchingJsonPath("$.serviceUrl", equalTo(serviceHost + "/")))
               .withRequestBody(matchingJsonPath("$.metadata.startup", matching(".+")));
Johannes Edmeier committed
74 75

        verify(request);
76 77 78 79 80
    }

    protected abstract int getServerPort();

    protected abstract int getManagementPort();
Johannes Edmeier committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    @SpringBootConfiguration
    @EnableAutoConfiguration
    public static class TestClientApplication {
        @Autowired
        private ApplicationRegistrator registrator;

        @EventListener
        public void ping(ApplicationReadyEvent ev) {
            new Thread(() -> {
                try {
                    while (registrator.getRegisteredId() == null) {
                        Thread.sleep(500);
                    }
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
                cdl.countDown();
            }).start();
        }
    }
102
}