diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/endpoint/ServiceRegistryEndpointTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/endpoint/ServiceRegistryEndpointTests.java index f9876561..6b93f310 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/endpoint/ServiceRegistryEndpointTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/endpoint/ServiceRegistryEndpointTests.java @@ -1,9 +1,10 @@ package org.springframework.cloud.client.serviceregistry.endpoint; +import java.util.Collections; +import java.util.Map; import java.util.concurrent.atomic.AtomicReference; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -19,6 +20,8 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import com.fasterxml.jackson.databind.ObjectMapper; + import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.containsString; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -55,10 +58,10 @@ public class ServiceRegistryEndpointTests { } @Test - @Ignore //FIXME: 2.0.0 public void testPost() throws Exception { + Map status = Collections.singletonMap("status", UPDATED_STATUS); this.mvc.perform(post("/application/service-registry") - .content(UPDATED_STATUS) + .content(new ObjectMapper().writeValueAsString(status)) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); assertThat(this.serviceRegistry.getUpdatedStatus().get()).isEqualTo(UPDATED_STATUS); diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/RefreshEndpoint.java b/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/RefreshEndpoint.java index 6ab7a250..6fe8c993 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/RefreshEndpoint.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/endpoint/RefreshEndpoint.java @@ -20,7 +20,7 @@ import java.util.Collection; import java.util.Set; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; -import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; +import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; import org.springframework.cloud.context.refresh.ContextRefresher; /** @@ -36,7 +36,7 @@ public class RefreshEndpoint { this.contextRefresher = contextRefresher; } - @ReadOperation + @WriteOperation public Collection refresh() { Set keys = contextRefresher.refresh(); return keys; diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/MoreRefreshScopeIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/MoreRefreshScopeIntegrationTests.java index e84a5102..74d50ab2 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/MoreRefreshScopeIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/MoreRefreshScopeIntegrationTests.java @@ -16,6 +16,10 @@ package org.springframework.cloud.context.scope.refresh; +import java.lang.reflect.Field; +import java.util.LinkedHashMap; +import java.util.Map; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.After; @@ -28,27 +32,23 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.boot.test.util.TestPropertyValues.Type; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.scope.refresh.MoreRefreshScopeIntegrationTests.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.jmx.export.annotation.ManagedAttribute; -import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.core.env.*; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.ReflectionUtils; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest(classes = TestConfiguration.class) @@ -94,16 +94,42 @@ public class MoreRefreshScopeIntegrationTests { assertEquals("Hello scope!", this.service.getMessage()); String id1 = this.service.toString(); // Change the dynamic property source... - TestPropertyValues.of("message:Foo").applyTo(this.environment); + TestPropertyValues.of("message:Foo").applyTo(this.environment, Type.MAP, "morerefreshtests"); + // apply(TestPropertyValues.of("message:Foo")); // ...and then refresh, so the bean is re-initialized: this.scope.refreshAll(); String id2 = this.service.toString(); - assertEquals("Foo", this.service.getMessage()); + String message = this.service.getMessage(); + assertEquals("Foo", message); assertEquals(1, TestService.getInitCount()); assertEquals(1, TestService.getDestroyCount()); assertNotSame(id1, id2); } + @SuppressWarnings("unchecked") + private void apply(TestPropertyValues propertyValues) { + Field field = ReflectionUtils.findField(TestPropertyValues.class, "properties"); + ReflectionUtils.makeAccessible(field); + Map properties = (Map) ReflectionUtils.getField(field, propertyValues); + MutablePropertySources sources = this.environment.getPropertySources(); + addToSources(properties, sources, Type.MAP, "morerefreshtests"); + ConfigurationPropertySources.attach(this.environment); + } + + private void addToSources(Map properties, MutablePropertySources sources, Type type, String name) { + if (sources.contains(name)) { + PropertySource propertySource = sources.get(name); + if (propertySource.getClass().equals(type.getSourceClass())) { + ((Map) propertySource.getSource()) + .putAll(properties); + return; + } + } + Map source = new LinkedHashMap<>(properties); + sources.addLast((type.equals(Type.MAP) ? new MapPropertySource(name, source) + : new SystemEnvironmentPropertySource(name, source))); + } + @Test @DirtiesContext @Ignore //FIXME: 2.0.x @@ -128,6 +154,7 @@ public class MoreRefreshScopeIntegrationTests { assertEquals("Foo", this.service.getMessage()); } + public static class TestService implements InitializingBean, DisposableBean { private static Log logger = LogFactory.getLog(TestService.class); @@ -191,8 +218,7 @@ public class MoreRefreshScopeIntegrationTests { @Configuration @EnableConfigurationProperties - @Import({ RefreshAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class }) + @EnableAutoConfiguration protected static class TestConfiguration { @Bean @@ -213,14 +239,14 @@ public class MoreRefreshScopeIntegrationTests { } @ConfigurationProperties - @ManagedResource + // @ManagedResource protected static class TestProperties { private String message; private int delay; - @ManagedAttribute + // @ManagedAttribute public String getMessage() { return this.message; } @@ -229,7 +255,7 @@ public class MoreRefreshScopeIntegrationTests { this.message = message; } - @ManagedAttribute + // @ManagedAttribute public int getDelay() { return this.delay; } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshEndpointIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshEndpointIntegrationTests.java index 2767a72a..f22ecac9 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshEndpointIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshEndpointIntegrationTests.java @@ -17,10 +17,9 @@ package org.springframework.cloud.context.scope.refresh; import java.net.URI; import java.net.URISyntaxException; -import java.util.Arrays; -import java.util.Collections; +import java.util.HashMap; +import java.util.Map; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; @@ -38,8 +37,6 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.RequestEntity; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -51,20 +48,19 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen * */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = ClientApp.class, webEnvironment = RANDOM_PORT) +@SpringBootTest(classes = ClientApp.class, properties = "endpoints.default.web.enabled=true", webEnvironment = RANDOM_PORT) public class RefreshEndpointIntegrationTests { @LocalServerPort private int port; @Test - @Ignore //FIXME: 2.0.x public void webAccess() throws Exception { TestRestTemplate template = new TestRestTemplate(); template.exchange( getUrlEncodedEntity("http://localhost:" + this.port + "/application/env", "message", "Hello Dave!"), String.class); - template.postForObject("http://localhost:" + this.port + "/application/refresh", "", String.class); + template.postForObject("http://localhost:" + this.port + "/application/refresh", null, String.class); String message = template.getForObject("http://localhost:" + this.port + "/", String.class); assertEquals("Hello Dave!", message); @@ -72,12 +68,13 @@ public class RefreshEndpointIntegrationTests { private RequestEntity getUrlEncodedEntity(String uri, String key, String value) throws URISyntaxException { - MultiValueMap env = new LinkedMultiValueMap( - Collections.singletonMap(key, Arrays.asList(value))); + Map property = new HashMap<>(); + property.put("name", key); + property.put("value", value); HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - RequestEntity> entity = new RequestEntity>( - env, headers, HttpMethod.POST, new URI(uri)); + headers.setContentType(MediaType.APPLICATION_JSON); + RequestEntity> entity = new RequestEntity<>( + property, headers, HttpMethod.POST, new URI(uri)); return entity; }