|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.endpoint.mvc; |
| 18 | + |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | + |
| 23 | +import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration; |
| 24 | +import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration; |
| 25 | +import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration; |
| 26 | +import org.springframework.boot.actuate.health.Health; |
| 27 | +import org.springframework.boot.actuate.health.HealthIndicator; |
| 28 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 29 | +import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; |
| 30 | +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
| 31 | +import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; |
| 32 | +import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; |
| 33 | +import org.springframework.boot.testutil.ClassPathExclusions; |
| 34 | +import org.springframework.boot.testutil.FilteredClassPathRunner; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Configuration; |
| 37 | +import org.springframework.mock.web.MockServletContext; |
| 38 | +import org.springframework.test.web.servlet.MockMvc; |
| 39 | +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
| 40 | + |
| 41 | +import static org.hamcrest.CoreMatchers.containsString; |
| 42 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 43 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 44 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 45 | +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; |
| 46 | + |
| 47 | +/** |
| 48 | + * Integration tests for the health endpoint when Spring Security is not available. |
| 49 | + * |
| 50 | + * @author Andy Wilkinson |
| 51 | + */ |
| 52 | +@RunWith(FilteredClassPathRunner.class) |
| 53 | +@ClassPathExclusions("spring-security-*.jar") |
| 54 | +public class NoSpringSecurityHealthMvcEndpointIntegrationTests { |
| 55 | + |
| 56 | + private AnnotationConfigWebApplicationContext context; |
| 57 | + |
| 58 | + @After |
| 59 | + public void closeContext() { |
| 60 | + this.context.close(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void healthDetailIsPresent() throws Exception { |
| 65 | + this.context = new AnnotationConfigWebApplicationContext(); |
| 66 | + this.context.setServletContext(new MockServletContext()); |
| 67 | + this.context.register(TestConfiguration.class); |
| 68 | + this.context.refresh(); |
| 69 | + MockMvc mockMvc = webAppContextSetup(this.context).build(); |
| 70 | + mockMvc.perform(get("/health")).andExpect(status().isOk()) |
| 71 | + .andExpect(content().string(containsString("\"hello\":\"world\""))); |
| 72 | + } |
| 73 | + |
| 74 | + @ImportAutoConfiguration({ JacksonAutoConfiguration.class, |
| 75 | + HttpMessageConvertersAutoConfiguration.class, EndpointAutoConfiguration.class, |
| 76 | + EndpointWebMvcAutoConfiguration.class, |
| 77 | + ManagementServerPropertiesAutoConfiguration.class, |
| 78 | + PropertyPlaceholderAutoConfiguration.class, WebMvcAutoConfiguration.class }) |
| 79 | + @Configuration |
| 80 | + static class TestConfiguration { |
| 81 | + |
| 82 | + @Bean |
| 83 | + public HealthIndicator testHealthIndicator() { |
| 84 | + return new HealthIndicator() { |
| 85 | + |
| 86 | + @Override |
| 87 | + public Health health() { |
| 88 | + return Health.up().withDetail("hello", "world").build(); |
| 89 | + } |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments