Commit 51a03b30 by Dave Syer

Consolidate stub tests into one place

parent 561f3b2f
......@@ -20,10 +20,13 @@ 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.eureka.registry.PeerAwareInstanceRegistryImpl;
import org.junit.After;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.LocalServerPort;
......@@ -38,6 +41,7 @@ import org.springframework.restdocs.restassured.RestAssuredRestDocumentation;
import org.springframework.restdocs.restassured.RestDocumentationFilter;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
......@@ -54,10 +58,20 @@ public abstract class AbstractDocumentationTests {
@LocalServerPort
private int port = 0;
@Autowired
private PeerAwareInstanceRegistryImpl registry;
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(
"target/generated-snippets");
@After
public void init() {
registry.clearRegistry();
ReflectionTestUtils.setField(registry, "responseCache", null);
registry.initializedResponseCache();
}
private RestDocumentationFilter filter(String name) {
return RestAssuredRestDocumentation.document(name,
preprocessRequest(modifyUris().host("eureka.example.com").removePort(),
......
......@@ -16,6 +16,8 @@
package org.springframework.cloud.netflix.eureka.server.doc;
import java.util.UUID;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.netflix.appinfo.ApplicationInfoManager;
......@@ -26,12 +28,14 @@ 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;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.cloud.netflix.eureka.server.doc.RequestVerifierFilter.verify;
@RunWith(SpringJUnit4ClassRunner.class)
public class EmptyAppsTests extends AbstractDocumentationTests {
public class AppRegistrationTests extends AbstractDocumentationTests {
@Autowired
private EurekaInstanceConfigBean instanceConfig;
......@@ -41,7 +45,7 @@ public class EmptyAppsTests extends AbstractDocumentationTests {
@Test
public void addApp() throws Exception {
instanceConfig.setAppname("foo");
instanceConfig.setInstanceId("unique-id");
instanceConfig.setInstanceId(UUID.randomUUID().toString());
instanceConfig.setHostname("foo.example.com");
applicationInfoManager.initComponent(instanceConfig);
assure("add-app", applicationInfoManager.getInfo())
......@@ -50,6 +54,12 @@ public class EmptyAppsTests extends AbstractDocumentationTests {
.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")
.filter(verify(
WireMock.put(WireMock.urlPathMatching("/eureka/apps/FOO/.*"))
......@@ -58,6 +68,9 @@ public class EmptyAppsTests extends AbstractDocumentationTests {
.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()
.assertThat().body("applications.application", hasSize(1))
.statusCode(is(200));
assure("delete-app").when()
.delete("/eureka/apps/FOO/{id}",
applicationInfoManager.getInfo().getInstanceId())
......
/*
* 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 com.netflix.appinfo.ApplicationInfoManager;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.CloudEurekaClient;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasSize;
@RunWith(SpringJUnit4ClassRunner.class)
public class RegisteredAppsTests extends AbstractDocumentationTests {
@Autowired
private EurekaInstanceConfigBean instanceConfig;
@Autowired
private EurekaClientConfigBean clientConfig;
@Autowired
private ApplicationInfoManager applicationInfoManager;
@Autowired
private ApplicationEventPublisher publisher;
private static CloudEurekaClient client;
@Before
public void setUp() throws Exception {
if (client == null) {
instanceConfig.setAppname("foo");
instanceConfig.setLeaseRenewalIntervalInSeconds(1);
applicationInfoManager.initComponent(instanceConfig);
clientConfig.setInitialInstanceInfoReplicationIntervalSeconds(0);
clientConfig.setRegisterWithEureka(true);
client = new CloudEurekaClient(applicationInfoManager, clientConfig,
publisher);
// Give registration a chance to work
while (client.getLastSuccessfulHeartbeatTimePeriod() < 0) {
Thread.sleep(100L);
}
}
}
@AfterClass
public static void cleanUp() {
if (client != null) {
client.shutdown();
}
}
@Test
public void registeredApps() throws Exception {
assure().accept("application/json").when().get("/eureka/apps").then().assertThat()
.body("applications.application", hasSize(1)).statusCode(is(200));
}
}
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