-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from chrismayer/add-endpointdoc
Add endpoint to retrieve all request mappings as JSON
- Loading branch information
Showing
3 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/shogun2-core/src/main/java/de/terrestris/shogun2/service/EndpointDocService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
src/shogun2-core/src/main/java/de/terrestris/shogun2/web/EndpointDocController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
src/shogun2-core/src/test/java/de/terrestris/shogun2/web/EndpointDocControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("[]")); | ||
} | ||
} |