Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint to retrieve all request mappings as JSON #265

Merged
merged 3 commits into from
Jul 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("[]"));
}
}