Replace @FrameworkEndpoint with MvcEndpoint interface

This commit is contained in:
Dave Syer
2013-12-18 12:44:54 +00:00
committed by Phillip Webb
parent 87e00cfae9
commit 7f1264bb65
17 changed files with 336 additions and 190 deletions

View File

@@ -24,8 +24,8 @@ import java.nio.charset.Charset;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.TestUtils;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.FrameworkEndpoint;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.actuate.properties.ManagementServerProperties;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
@@ -240,20 +240,29 @@ public class EndpointWebMvcAutoConfigurationTests {
}
@FrameworkEndpoint
public static class TestEndpoint extends AbstractEndpoint<String> {
public static class TestEndpoint implements MvcEndpoint {
public TestEndpoint() {
super("/endpoint", false, true);
}
@Override
@RequestMapping
@ResponseBody
public String invoke() {
return "endpointoutput";
}
@Override
public String getPath() {
return "/endpoint";
}
@Override
public boolean isSensitive() {
return true;
}
@Override
public Class<?> getEndpointType() {
return Endpoint.class;
}
}
}

View File

@@ -17,7 +17,6 @@
package org.springframework.boot.actuate.endpoint;
import org.junit.Test;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.actuate.endpoint.mvc;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
@@ -44,14 +45,10 @@ import static org.junit.Assert.assertThat;
public class EndpointHandlerMappingTests {
private StaticApplicationContext context = new StaticApplicationContext();
private EndpointHandlerMapping mapping = new EndpointHandlerMapping();
private Method method;
@Before
public void init() throws Exception {
this.context.getDefaultListableBeanFactory().registerSingleton("mapping",
this.mapping);
this.mapping.setApplicationContext(this.context);
this.method = ReflectionUtils.findMethod(TestMvcEndpoint.class, "invoke");
}
@@ -59,18 +56,17 @@ public class EndpointHandlerMappingTests {
public void withoutPrefix() throws Exception {
TestMvcEndpoint endpointA = new TestMvcEndpoint(new TestEndpoint("/a"));
TestMvcEndpoint endpointB = new TestMvcEndpoint(new TestEndpoint("/b"));
this.context.getDefaultListableBeanFactory().registerSingleton(
endpointA.getPath(), endpointA);
this.context.getDefaultListableBeanFactory().registerSingleton(
endpointB.getPath(), endpointB);
this.mapping.afterPropertiesSet();
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a"))
EndpointHandlerMapping mapping = new EndpointHandlerMapping(Arrays.asList(
endpointA, endpointB));
mapping.setApplicationContext(this.context);
mapping.afterPropertiesSet();
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a"))
.getHandler(),
equalTo((Object) new HandlerMethod(endpointA, this.method)));
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/b"))
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/b"))
.getHandler(),
equalTo((Object) new HandlerMethod(endpointB, this.method)));
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/c")),
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/c")),
nullValue());
}
@@ -78,59 +74,62 @@ public class EndpointHandlerMappingTests {
public void withPrefix() throws Exception {
TestMvcEndpoint endpointA = new TestMvcEndpoint(new TestEndpoint("/a"));
TestMvcEndpoint endpointB = new TestMvcEndpoint(new TestEndpoint("/b"));
this.context.getDefaultListableBeanFactory().registerSingleton(
endpointA.getPath(), endpointA);
this.context.getDefaultListableBeanFactory().registerSingleton(
endpointB.getPath(), endpointB);
this.mapping.setPrefix("/a");
this.mapping.afterPropertiesSet();
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a/a"))
EndpointHandlerMapping mapping = new EndpointHandlerMapping(Arrays.asList(
endpointA, endpointB));
mapping.setApplicationContext(this.context);
mapping.setPrefix("/a");
mapping.afterPropertiesSet();
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a/a"))
.getHandler(),
equalTo((Object) new HandlerMethod(endpointA, this.method)));
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a/b"))
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a/b"))
.getHandler(),
equalTo((Object) new HandlerMethod(endpointB, this.method)));
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a")),
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a")),
nullValue());
}
@Test(expected = HttpRequestMethodNotSupportedException.class)
public void onlyGetHttpMethodForNonActionEndpoints() throws Exception {
TestMvcEndpoint endpoint = new TestActionEndpoint(new TestEndpoint("/a"));
this.context.getDefaultListableBeanFactory().registerSingleton(
endpoint.getPath(), endpoint);
this.mapping.afterPropertiesSet();
assertNotNull(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a")));
assertNull(this.mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
TestActionEndpoint endpoint = new TestActionEndpoint(new TestEndpoint("/a"));
EndpointHandlerMapping mapping = new EndpointHandlerMapping(
Arrays.asList(endpoint));
mapping.setApplicationContext(this.context);
mapping.afterPropertiesSet();
assertNotNull(mapping.getHandler(new MockHttpServletRequest("GET", "/a")));
assertNull(mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
}
@Test
public void postHttpMethodForActionEndpoints() throws Exception {
TestMvcEndpoint endpoint = new TestActionEndpoint(new TestEndpoint("/a"));
this.context.getDefaultListableBeanFactory().registerSingleton(
endpoint.getPath(), endpoint);
this.mapping.afterPropertiesSet();
assertNotNull(this.mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
TestActionEndpoint endpoint = new TestActionEndpoint(new TestEndpoint("/a"));
EndpointHandlerMapping mapping = new EndpointHandlerMapping(
Arrays.asList(endpoint));
mapping.setApplicationContext(this.context);
mapping.afterPropertiesSet();
assertNotNull(mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
}
@Test(expected = HttpRequestMethodNotSupportedException.class)
public void onlyPostHttpMethodForActionEndpoints() throws Exception {
TestMvcEndpoint endpoint = new TestActionEndpoint(new TestEndpoint("/a"));
this.context.getDefaultListableBeanFactory().registerSingleton(
endpoint.getPath(), endpoint);
this.mapping.afterPropertiesSet();
assertNotNull(this.mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
assertNull(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a")));
TestActionEndpoint endpoint = new TestActionEndpoint(new TestEndpoint("/a"));
EndpointHandlerMapping mapping = new EndpointHandlerMapping(
Arrays.asList(endpoint));
mapping.setApplicationContext(this.context);
mapping.afterPropertiesSet();
assertNotNull(mapping.getHandler(new MockHttpServletRequest("POST", "/a")));
assertNull(mapping.getHandler(new MockHttpServletRequest("GET", "/a")));
}
@Test
public void disabled() throws Exception {
TestMvcEndpoint endpoint = new TestMvcEndpoint(new TestEndpoint("/a"));
this.context.getDefaultListableBeanFactory().registerSingleton(
endpoint.getPath(), endpoint);
this.mapping.setDisabled(true);
this.mapping.afterPropertiesSet();
assertThat(this.mapping.getHandler(new MockHttpServletRequest("GET", "/a")),
EndpointHandlerMapping mapping = new EndpointHandlerMapping(
Arrays.asList(endpoint));
mapping.setDisabled(true);
mapping.setApplicationContext(this.context);
mapping.afterPropertiesSet();
assertThat(mapping.getHandler(new MockHttpServletRequest("GET", "/a")),
nullValue());
}
@@ -141,14 +140,12 @@ public class EndpointHandlerMappingTests {
}
@Override
@RequestMapping(method = RequestMethod.GET)
public Object invoke() {
return null;
}
}
@FrameworkEndpoint
private static class TestMvcEndpoint extends GenericMvcEndpoint {
public TestMvcEndpoint(TestEndpoint delegate) {
@@ -157,8 +154,7 @@ public class EndpointHandlerMappingTests {
}
@FrameworkEndpoint
private static class TestActionEndpoint extends TestMvcEndpoint {
private static class TestActionEndpoint extends GenericMvcEndpoint {
public TestActionEndpoint(TestEndpoint delegate) {
super(delegate);
@@ -169,6 +165,7 @@ public class EndpointHandlerMappingTests {
public Object invoke() {
return null;
}
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2012-2013 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.boot.actuate.endpoint.mvc;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.TestUtils;
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpointTests.TestConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { TestConfiguration.class })
@WebAppConfiguration
public class EnvironmentMvcEndpointTests {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
@Before
public void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
TestUtils.addEnviroment((ConfigurableApplicationContext) this.context, "foo:bar");
}
@Test
public void home() throws Exception {
this.mvc.perform(get("/env")).andExpect(status().isOk())
.andExpect(content().string(containsString("systemProperties")));
}
@Test
public void sub() throws Exception {
this.mvc.perform(get("/env/foo")).andExpect(status().isOk())
.andExpect(content().string(equalToIgnoringCase("bar")));
}
@Import(EndpointWebMvcAutoConfiguration.class)
@EnableWebMvc
@Configuration
public static class TestConfiguration {
@Bean
public EnvironmentEndpoint endpoint() {
return new EnvironmentEndpoint();
}
@Bean
public EnvironmentMvcEndpoint mvcEndpoint() {
return new EnvironmentMvcEndpoint(endpoint());
}
}
}