Commit d6a60405 by Dave Syer

Split tests up into individual methods

parent 51a03b30
......@@ -20,6 +20,8 @@ import com.jayway.restassured.RestAssured;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.filter.Filter;
import com.jayway.restassured.specification.RequestSpecification;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl;
import org.junit.After;
......@@ -33,6 +35,7 @@ import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.contract.wiremock.restdocs.WireMockSnippet;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.netflix.eureka.server.doc.AbstractDocumentationTests.Application;
import org.springframework.context.annotation.Configuration;
......@@ -61,6 +64,12 @@ public abstract class AbstractDocumentationTests {
@Autowired
private PeerAwareInstanceRegistryImpl registry;
@Autowired
private EurekaInstanceConfigBean instanceConfig;
@Autowired
private ApplicationInfoManager applicationInfoManager;
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(
"target/generated-snippets");
......@@ -72,6 +81,18 @@ public abstract class AbstractDocumentationTests {
registry.initializedResponseCache();
}
protected void register(String name, String id) {
registry.register(getInstance(name, id), false);
}
protected InstanceInfo getInstance(String name, String id) {
instanceConfig.setAppname(name);
instanceConfig.setInstanceId(id);
instanceConfig.setHostname("foo.example.com");
applicationInfoManager.initComponent(instanceConfig);
return applicationInfoManager.getInfo();
}
private RestDocumentationFilter filter(String name) {
return RestAssuredRestDocumentation.document(name,
preprocessRequest(modifyUris().host("eureka.example.com").removePort(),
......@@ -79,11 +100,11 @@ public abstract class AbstractDocumentationTests {
preprocessResponse(prettyPrint()));
}
private RequestSpecification document(Filter... filters) {
return document(null, filters);
private RequestSpecification spec(Filter... filters) {
return spec(null, filters);
}
private RequestSpecification document(Object body, Filter... filters) {
private RequestSpecification spec(Object body, Filter... filters) {
RequestSpecBuilder builder = new RequestSpecBuilder()
.addFilter(documentationConfiguration(this.restDocumentation).snippets()
.withAdditionalDefaults(new WireMockSnippet()));
......@@ -97,19 +118,25 @@ public abstract class AbstractDocumentationTests {
return spec;
}
protected RequestSpecification assure(String name, Object body) {
RestDocumentationFilter filter = filter(name);
RequestSpecification assured = RestAssured.given(document(body, filter));
protected RequestSpecification document() {
return document("{method-name}");
}
protected RequestSpecification document(Object body) {
RestDocumentationFilter filter = filter("{method-name}");
RequestSpecification assured = RestAssured.given(spec(body, filter));
return assured.filter(filter);
}
protected RequestSpecification assure(String name) {
protected RequestSpecification document(String name, Object body) {
RestDocumentationFilter filter = filter(name);
return RestAssured.given(document(filter)).filter(filter);
RequestSpecification assured = RestAssured.given(spec(body, filter));
return assured.filter(filter);
}
protected RequestSpecification assure() {
return assure("{method-name}");
protected RequestSpecification document(String name) {
RestDocumentationFilter filter = filter(name);
return RestAssured.given(spec(filter)).filter(filter);
}
@Configuration
......
......@@ -19,13 +19,10 @@ package org.springframework.cloud.netflix.eureka.server.doc;
import java.util.UUID;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.netflix.appinfo.ApplicationInfoManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.equalTo;
......@@ -37,50 +34,85 @@ import static org.springframework.cloud.netflix.eureka.server.doc.RequestVerifie
@RunWith(SpringJUnit4ClassRunner.class)
public class AppRegistrationTests extends AbstractDocumentationTests {
@Autowired
private EurekaInstanceConfigBean instanceConfig;
@Autowired
private ApplicationInfoManager applicationInfoManager;
@Test
public void startingApp() throws Exception {
register("foo", UUID.randomUUID().toString());
document().accept("application/json").when().get("/eureka/apps").then()
.assertThat()
.body("applications.application", hasSize(1),
"applications.application[0].instance[0].status",
equalTo("STARTING"))
.statusCode(is(200));
}
@Test
public void addApp() throws Exception {
instanceConfig.setAppname("foo");
instanceConfig.setInstanceId(UUID.randomUUID().toString());
instanceConfig.setHostname("foo.example.com");
applicationInfoManager.initComponent(instanceConfig);
assure("add-app", applicationInfoManager.getInfo())
public void addInstance() throws Exception {
document(getInstance("foo", UUID.randomUUID().toString()))
.filter(verify("$.instance.app").json("$.instance.hostName")
.json("$.instance[?(@.status=='STARTING')]")
.json("$.instance.instanceId")
.json("$.instance.dataCenterInfo.name"))
.when().post("/eureka/apps/FOO").then().assertThat().statusCode(is(204));
assure("starting-app").accept("application/json").when().get("/eureka/apps")
.then().assertThat()
.body("applications.application", hasSize(1),
"applications.application[0].instance[0].status",
equalTo("STARTING"))
.statusCode(is(200));
assure("up-app")
}
@Test
public void setStatus() throws Exception {
String id = UUID.randomUUID().toString();
register("foo", id);
document()
.filter(verify(
WireMock.put(WireMock.urlPathMatching("/eureka/apps/FOO/.*"))
.withQueryParam("value", WireMock.matching("UP"))))
.when()
.put("/eureka/apps/FOO/{id}/status?value={value}",
applicationInfoManager.getInfo().getInstanceId(), "UP")
.then().assertThat().statusCode(is(200));
assure("one-app").accept("application/json").when().get("/eureka/apps").then()
.when().put("/eureka/apps/FOO/{id}/status?value={value}", id, "UP").then()
.assertThat().statusCode(is(200));
}
@Test
public void allApps() throws Exception {
register("foo", UUID.randomUUID().toString());
document().accept("application/json").when().get("/eureka/apps").then()
.assertThat().body("applications.application", hasSize(1))
.statusCode(is(200));
assure("delete-app").when()
.delete("/eureka/apps/FOO/{id}",
applicationInfoManager.getInfo().getInstanceId())
.then().assertThat().statusCode(is(200));
}
@Test
public void oneInstance() throws Exception {
String id = UUID.randomUUID().toString();
register("foo", id);
document()
.filter(verify(
WireMock.get(WireMock.urlPathMatching("/eureka/apps/FOO/.*"))))
.accept("application/json").when().get("/eureka/apps/FOO/{id}", id).then()
.assertThat().body("instance.app", equalTo("FOO")).statusCode(is(200));
}
@Test
public void renew() throws Exception {
String id = UUID.randomUUID().toString();
register("foo", id);
document()
.filter(verify(
WireMock.put(WireMock.urlPathMatching("/eureka/apps/FOO/.*"))))
.accept("application/json").when().put("/eureka/apps/FOO/{id}", id).then()
.assertThat().statusCode(is(200));
}
@Test
public void deleteInstance() throws Exception {
String id = UUID.randomUUID().toString();
register("foo", id);
document()
.filter(verify(
WireMock.delete(WireMock.urlPathMatching("/eureka/apps/FOO/.*"))))
.when().delete("/eureka/apps/FOO/{id}", id).then().assertThat()
.statusCode(is(200));
}
@Test
public void emptyApps() {
assure().when().accept("application/json").get("/eureka/apps").then().assertThat()
.body("applications.application", emptyIterable()).statusCode(is(200));
document().when().accept("application/json").get("/eureka/apps").then()
.assertThat().body("applications.application", emptyIterable())
.statusCode(is(200));
}
}
......@@ -223,12 +223,20 @@ class WireMockRestAssuredRequestAdapter implements Request {
@Override
public String getHeader(String key) {
return request.getHeaders().getValue(key);
String value = request.getHeaders().getValue(key);
if ("accept".equals(key.toLowerCase()) && "*/*".equals(value)) {
return null;
}
return value;
}
@Override
public HttpHeader header(String key) {
return new HttpHeader(key, request.getHeaders().getValue(key));
String value = request.getHeaders().getValue(key);
if ("accept".equals(key.toLowerCase()) && "*/*".equals(value)) {
return null;
}
return new HttpHeader(key, value);
}
@Override
......@@ -240,6 +248,10 @@ class WireMockRestAssuredRequestAdapter implements Request {
public HttpHeaders getHeaders() {
List<HttpHeader> headers = new ArrayList<>();
for (Header header : request.getHeaders()) {
String value = header.getValue();
if ("accept".equals(header.getName().toLowerCase()) && "*/*".equals(value)) {
continue;
}
headers.add(new HttpHeader(header.getName(), header.getValue()));
}
return new HttpHeaders(headers);
......@@ -247,6 +259,10 @@ class WireMockRestAssuredRequestAdapter implements Request {
@Override
public boolean containsHeader(String key) {
String value = request.getHeaders().getValue(key);
if ("accept".equals(key.toLowerCase()) && "*/*".equals(value)) {
return false;
}
return request.getHeaders().hasHeaderWithName(key);
}
......@@ -254,6 +270,10 @@ class WireMockRestAssuredRequestAdapter implements Request {
public Set<String> getAllHeaderKeys() {
Set<String> headers = new LinkedHashSet<>();
for (Header header : request.getHeaders()) {
String value = header.getValue();
if ("accept".equals(header.getName().toLowerCase()) && "*/*".equals(value)) {
continue;
}
headers.add(header.getName());
}
return headers;
......
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