diff --git a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/AbstractDocumentationTests.java b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/AbstractDocumentationTests.java index 44cf4c6e..76860c20 100644 --- a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/AbstractDocumentationTests.java +++ b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/AbstractDocumentationTests.java @@ -16,6 +16,8 @@ package org.springframework.cloud.netflix.eureka.server.doc; +import java.util.UUID; + import com.jayway.restassured.RestAssured; import com.jayway.restassured.builder.RequestSpecBuilder; import com.jayway.restassured.filter.Filter; @@ -53,7 +55,7 @@ import static org.springframework.restdocs.restassured.RestAssuredRestDocumentat import static org.springframework.restdocs.restassured.operation.preprocess.RestAssuredPreprocessors.modifyUris; @RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT, value = { +@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = { "spring.jmx.enabled=true", "management.security.enabled=false" }) @DirtiesContext public abstract class AbstractDocumentationTests { @@ -81,11 +83,20 @@ public abstract class AbstractDocumentationTests { registry.initializedResponseCache(); } - protected void register(String name, String id) { - registry.register(getInstance(name, id), false); + protected InstanceInfo register(String name) { + return register(name, UUID.randomUUID().toString()); + } + + protected InstanceInfo register(String name, String id) { + registry.register(instance(name, id), false); + return instance(); + } + + protected InstanceInfo instance(String name) { + return instance(name, UUID.randomUUID().toString()); } - protected InstanceInfo getInstance(String name, String id) { + protected InstanceInfo instance(String name, String id) { instanceConfig.setAppname(name); instanceConfig.setInstanceId(id); instanceConfig.setHostname("foo.example.com"); @@ -93,6 +104,10 @@ public abstract class AbstractDocumentationTests { return applicationInfoManager.getInfo(); } + protected InstanceInfo instance() { + return applicationInfoManager.getInfo(); + } + private RestDocumentationFilter filter(String name) { return RestAssuredRestDocumentation.document(name, preprocessRequest(modifyUris().host("eureka.example.com").removePort(), diff --git a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/AppRegistrationTests.java b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/AppRegistrationTests.java index 026e7bdc..a7160d32 100644 --- a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/AppRegistrationTests.java +++ b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/AppRegistrationTests.java @@ -18,13 +18,17 @@ package org.springframework.cloud.netflix.eureka.server.doc; import java.util.UUID; -import com.github.tomakehurst.wiremock.client.WireMock; - import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import static com.github.tomakehurst.wiremock.client.WireMock.delete; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.matching; +import static com.github.tomakehurst.wiremock.client.WireMock.put; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.emptyIterable; @@ -36,7 +40,7 @@ public class AppRegistrationTests extends AbstractDocumentationTests { @Test public void startingApp() throws Exception { - register("foo", UUID.randomUUID().toString()); + register("foo"); document().accept("application/json").when().get("/eureka/apps").then() .assertThat() .body("applications.application", hasSize(1), @@ -47,7 +51,7 @@ public class AppRegistrationTests extends AbstractDocumentationTests { @Test public void addInstance() throws Exception { - document(getInstance("foo", UUID.randomUUID().toString())) + document(instance("foo")) .filter(verify("$.instance.app").json("$.instance.hostName") .json("$.instance[?(@.status=='STARTING')]") .json("$.instance.instanceId") @@ -57,54 +61,73 @@ public class AppRegistrationTests extends AbstractDocumentationTests { @Test public void setStatus() throws Exception { - String id = UUID.randomUUID().toString(); - register("foo", id); + String id = register("foo").getInstanceId(); document() - .filter(verify( - WireMock.put(WireMock.urlPathMatching("/eureka/apps/FOO/.*")) - .withQueryParam("value", WireMock.matching("UP")))) + .filter(verify(put(urlPathMatching("/eureka/apps/FOO/.*/status")) + .withQueryParam("value", matching("UP")))) .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()); + register("foo"); document().accept("application/json").when().get("/eureka/apps").then() .assertThat().body("applications.application", hasSize(1)) .statusCode(is(200)); } + @Test + public void delta() throws Exception { + register("foo"); + document().accept("application/json").when().get("/eureka/apps/delta").then() + .assertThat().body("applications.application", hasSize(1)) + .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/.*")))) + document().filter(verify(get(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 lookupInstance() throws Exception { + String id = register("foo").getInstanceId(); + document().filter(verify(get(urlPathMatching("/eureka/instances/.*")))) + .accept("application/json").when().get("/eureka/instances/{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/.*")))) + String id = register("foo").getInstanceId(); + document().filter(verify(put(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); + public void updateMetadata() throws Exception { + String id = register("foo").getInstanceId(); document() - .filter(verify( - WireMock.delete(WireMock.urlPathMatching("/eureka/apps/FOO/.*")))) - .when().delete("/eureka/apps/FOO/{id}", id).then().assertThat() + .filter(verify(put(urlPathMatching("/eureka/apps/FOO/.*/metadata")) + .withQueryParam("key", matching(".*")))) + .accept("application/json").when() + .put("/eureka/apps/FOO/{id}/metadata?key=value", id).then().assertThat() + .statusCode(is(200)); + assertThat(instance().getMetadata()).containsEntry("key", "value"); + } + + @Test + public void deleteInstance() throws Exception { + String id = register("foo").getInstanceId(); + document().filter(verify(delete(urlPathMatching("/eureka/apps/FOO/.*")))).when() + .delete("/eureka/apps/FOO/{id}", id).then().assertThat() .statusCode(is(200)); } diff --git a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/EurekaServerTests.java b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/EurekaServerTests.java new file mode 100644 index 00000000..8e540c4d --- /dev/null +++ b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/doc/EurekaServerTests.java @@ -0,0 +1,45 @@ +/* + * Copyright 2013-2015 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 org.springframework.cloud.netflix.eureka.server.doc; + +import java.util.UUID; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.notNullValue; + +@RunWith(SpringJUnit4ClassRunner.class) +// TODO: maybe this should be the default (the test fails without it because the JSON is +// invalid) +@TestPropertySource(properties = "eureka.server.minAvailableInstancesForPeerReplication=0") +public class EurekaServerTests extends AbstractDocumentationTests { + + @Test + public void serverStatus() throws Exception { + register("foo", UUID.randomUUID().toString()); + document().accept("application/json").when().get("/eureka/status").then() + .assertThat().body("generalStats", notNullValue(), "applicationStats", + notNullValue(), "instanceInfo", notNullValue()) + .statusCode(is(200)); + } + +} diff --git a/spring-cloud-netflix-eureka-server/src/test/resources/application.properties b/spring-cloud-netflix-eureka-server/src/test/resources/application.properties index 1d7b7452..52c10d89 100644 --- a/spring-cloud-netflix-eureka-server/src/test/resources/application.properties +++ b/spring-cloud-netflix-eureka-server/src/test/resources/application.properties @@ -1,4 +1,4 @@ -server.port=${local.server.port:8761} +server.port=8761 spring.application.name=eureka eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false