Commit 5b2d8f73 by Johannes Edmeier

Add wiremock test to client

parent d665d083
...@@ -28,10 +28,8 @@ import org.junit.After; ...@@ -28,10 +28,8 @@ import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration; import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
......
...@@ -69,5 +69,11 @@ ...@@ -69,5 +69,11 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>2.5.1</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -25,7 +25,6 @@ import org.slf4j.Logger; ...@@ -25,7 +25,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
...@@ -70,7 +69,7 @@ public class ApplicationRegistrator { ...@@ -70,7 +69,7 @@ public class ApplicationRegistrator {
@SuppressWarnings("rawtypes") ResponseEntity<Map> response = template.postForEntity(adminUrl, @SuppressWarnings("rawtypes") ResponseEntity<Map> response = template.postForEntity(adminUrl,
new HttpEntity<>(self, HTTP_HEADERS), Map.class); new HttpEntity<>(self, HTTP_HEADERS), Map.class);
if (response.getStatusCode().equals(HttpStatus.CREATED)) { if (response.getStatusCode().is2xxSuccessful()) {
if (registeredId.compareAndSet(null, response.getBody().get("id").toString())) { if (registeredId.compareAndSet(null, response.getBody().get("id").toString())) {
LOGGER.info("Application registered itself as {}", response.getBody()); LOGGER.info("Application registered itself as {}", response.getBody());
} else { } else {
......
package de.codecentric.boot.admin.client.config;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"management.port=0", "spring.boot.admin.client.url=http://example.com"})
public class ClientApplicationTest {
@Autowired
private ClientProperties properties;
@Test
public void test_context() {
assertThat(properties).isNotNull();
}
}
@Configuration
@EnableAutoConfiguration
class TestClientApplication {
}
/*
* Copyright 2014-2017 the original author or authors.
*
* 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.client.config;
import wiremock.org.apache.http.HttpStatus;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.moreThanOrExactly;
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.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
public class ClientServletApplicationTest {
private ConfigurableApplicationContext instance;
private WireMockServer wiremock;
@Before
public void setUp() throws Exception {
wiremock = new WireMockServer(wireMockConfig().dynamicPort());
wiremock.start();
configureFor(wiremock.port());
ResponseDefinitionBuilder response = aResponse().withStatus(HttpStatus.SC_CREATED)
.withHeader("Content-Type", "application/json")
.withHeader("Location", "http://localhost:" +
wiremock.port() +
"/api/applications/abcdef")
.withBody("{ \"id\" : \"abcdef\" }");
stubFor(post(urlEqualTo("/api/applications")).willReturn(response));
instance = SpringApplication.run(TestClientApplication.class, "--spring.main.web-application-type=servlet",
"--spring.application.name=Test-Client", "--server.port=0", "--management.port=0",
"--management.context-path=/mgmt",
"--spring.boot.admin.client.url=http://localhost:" + wiremock.port());
}
@Test
public void test_context() throws InterruptedException {
Thread.sleep(1000L);
String serviceHost = "http://localhost:" + instance.getEnvironment().getProperty("local.server.port");
String managementHost = "http://localhost:" + instance.getEnvironment().getProperty("local.management.port");
String body = "{ \"name\" : \"Test-Client\"," + //
" \"managementUrl\" : \"" + managementHost + "/mgmt/\"," + //
" \"healthUrl\" : \"" + managementHost + "/mgmt/health/\"," + //
" \"serviceUrl\" : \"" + serviceHost + "/\", " + //
" \"metadata\" : {} }";
RequestPatternBuilder request = postRequestedFor(urlEqualTo("/api/applications")).withHeader("Content-Type",
equalTo("application/json")).withRequestBody(equalToJson(body));
verify(moreThanOrExactly(1), request);
}
@After
public void shutdown() {
instance.close();
wiremock.stop();
}
@SpringBootConfiguration
@EnableAutoConfiguration
public static class TestClientApplication {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment