INT-3991: Add IntegrationGraphController

JIRA: https://jira.spring.io/browse/INT-3991

* Add `IntegrationGraphController` `@RestController` over `IntegrationGraphServer` bean
* Add `@EnableIntegrationGraphController` and `<int-http:graph-controller>` to register
`IntegrationGraphController` if the `DispatcherServlet` is in classpath.
* Also register the `IntegrationGraphServer` bean from the same place, if that isn't presented in the application context yet.
* Allow to configure the "root" `path` for the `IntegrationGraphController` as a placeholder value via
`spring.integration.graph.controller.request.mapping.path` property
* Add tests for both `@EnableIntegrationGraphController` and `<int-http:graph-controller>` cases based on the `MockMvc`
This commit is contained in:
Artem Bilan
2016-04-22 15:30:20 -04:00
committed by Gary Russell
parent b84747b090
commit 8df487c96f
15 changed files with 569 additions and 12 deletions

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/integration/http"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/integration/http
http://www.springframework.org/schema/integration/http/spring-integration-http.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<graph-controller path="/foo"/>
</beans:beans>

View File

@@ -0,0 +1,146 @@
/*
* Copyright 2016 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.integration.http.management;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
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.handler;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.http.config.EnableIntegrationGraphController;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
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.HandlerAdapter;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
/**
* @author Artem Bilan
* @since 4.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class IntegrationGraphControllerTests {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testIntegrationGraphGet() throws Exception {
this.mockMvc.perform(get("/testIntegration")
.accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(handler().handlerType(IntegrationGraphController.class))
.andExpect(handler().methodName("getGraph"))
.andExpect(jsonPath("$.nodes..name")
.value(Matchers.containsInAnyOrder("nullChannel", "errorChannel",
"_org.springframework.integration.errorLogger")))
.andExpect(jsonPath("$.links").exists());
}
@Test
public void testIntegrationGraphControllerParser() throws Exception {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
"IntegrationGraphControllerParserTests-context.xml", getClass());
HandlerMapping handlerMapping =
context.getBean(RequestMappingHandlerMapping.class.getName(), HandlerMapping.class);
HandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.setRequestURI("/foo");
MockHttpServletResponse response = new MockHttpServletResponse();
HandlerExecutionChain executionChain = handlerMapping.getHandler(request);
assertNotNull(executionChain);
Object handler = executionChain.getHandler();
handlerAdapter.handle(request, response, handler);
assertEquals(HttpStatus.OK.value(), response.getStatus());
assertThat(response.getContentAsString(), containsString("\"name\":\"nullChannel\","));
assertThat(response.getContentAsString(), not(containsString("\"name\":\"myChannel\",")));
context.getBeanFactory().registerSingleton("myChannel", new DirectChannel());
request = new MockHttpServletRequest();
request.setMethod("GET");
request.setRequestURI("/foo/refresh");
response = new MockHttpServletResponse();
executionChain = handlerMapping.getHandler(request);
assertNotNull(executionChain);
handler = executionChain.getHandler();
handlerAdapter.handle(request, response, handler);
assertEquals(HttpStatus.OK.value(), response.getStatus());
assertThat(response.getContentAsString(), containsString("\"name\":\"myChannel\","));
context.close();
}
@Configuration
@EnableWebMvc
@EnableIntegration
@EnableIntegrationGraphController(path = "/testIntegration")
public static class ContextConfiguration {
}
}

View File

@@ -0,0 +1,9 @@
log4j.rootCategory=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n
#log4j.category.org.springframework=DEBUG
log4j.category.org.springframework.integration=WARN
log4j.category.org.springframework.integration.http=WARN