Commit d6a60405 by Dave Syer

Split tests up into individual methods

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