Skip to content

Commit

Permalink
Merge pull request #265 from chrismayer/add-endpointdoc
Browse files Browse the repository at this point in the history
Add endpoint to retrieve all request mappings as JSON
  • Loading branch information
chrismayer authored Jul 31, 2017
2 parents 86e18b7 + 063eb46 commit e218096
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.terrestris.shogun2.service;

import java.util.Set;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import de.terrestris.shogun2.web.EndpointDocController;

/**
* Service class for the {@link EndpointDocController}.
*
* @author Christian Mayer
*/
@Service("endpointDocService")
public class EndpointDocService {

/**
* Returns all RequestMappingInfo instances from type and
* method-level @RequestMapping annotations in @Controller classes.
*
* @param requestMappingHandlerMapping
* The RequestMappingInfo collection of Spring
* @return A set of RequestMappingInfo
*/
@PreAuthorize("hasRole(@configHolder.getSuperAdminRoleName())")
public Set<RequestMappingInfo> getEndpoints(RequestMappingHandlerMapping requestMappingHandlerMapping) {

if (requestMappingHandlerMapping.getHandlerMethods() != null) {
return requestMappingHandlerMapping.getHandlerMethods().keySet();
}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package de.terrestris.shogun2.web;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import de.terrestris.shogun2.service.EndpointDocService;

/**
* Web-controller for endpoint documentation.
*
* @author Christian mayer
*/
@Controller
public class EndpointDocController {

/**
* Creates RequestMappingInfo instances from type and
* method-level @RequestMapping annotations in @Controller classes.
*/
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;

/**
* The service layer instance
*/
@Autowired
@Qualifier("endpointDocService")
private EndpointDocService service;

/**
* Provides an overview of all mapped endpoints.
*/
@RequestMapping(value = "/endpointdoc", method = RequestMethod.GET)
public @ResponseBody Set<RequestMappingInfo> getEndpoints() {

return this.service.getEndpoints(requestMappingHandlerMapping);
}


/**
* @param service
* the service to set
*/
public void setService(EndpointDocService service) {
this.service = service;
}

/**
* @param requestMappingHandlerMapping
* the requestMappingHandlerMapping to set
*/
public void setRequestMappingHandlerMapping(RequestMappingHandlerMapping requestMappingHandlerMapping) {
this.requestMappingHandlerMapping = requestMappingHandlerMapping;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package de.terrestris.shogun2.web;

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;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import de.terrestris.shogun2.service.EndpointDocService;

/**
*
* @author Christian Mayer
*/
public class EndpointDocControllerTest {

private MockMvc mockMvc;

/**
* The controller to test
*/
private EndpointDocController endpointDocController;

@Mock
private EndpointDocService endpointDocServiceMock;

@Mock
private RequestMappingHandlerMapping requestMappingHandlerMapping;

@Before
public void setUp() {

// Process mock annotations
MockitoAnnotations.initMocks(this);

// init the controller to test. this is necessary as InjectMocks
// annotation will not work with the constructors of the controllers
// (entityClass). see https://goo.gl/jLbMZe
endpointDocController = new EndpointDocController();
endpointDocController.setService(endpointDocServiceMock);
endpointDocController.setRequestMappingHandlerMapping(requestMappingHandlerMapping);
// Setup Spring test in standalone mode
this.mockMvc = MockMvcBuilders.standaloneSetup(endpointDocController).build();
}

@Test
public void findAllRequestMappings_shouldReturnAllRequestMappings() throws Exception {

// Perform and test the GET-Request
mockMvc.perform(get("/endpointdoc"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(content().string("[]"));
}
}

0 comments on commit e218096

Please sign in to comment.