|
1 | 1 | package org.folio.rest.workflow.controller.advice; |
2 | 2 |
|
3 | | -import static org.folio.spring.test.mock.MockMvcConstant.APP_JSON; |
4 | | -import static org.folio.spring.test.mock.MockMvcConstant.JSON_OBJECT; |
5 | | -import static org.folio.spring.test.mock.MockMvcConstant.OKAPI_HEAD; |
6 | 3 | import static org.folio.spring.test.mock.MockMvcConstant.VALUE; |
7 | | -import static org.folio.spring.test.mock.MockMvcRequest.appendBody; |
8 | | -import static org.folio.spring.test.mock.MockMvcRequest.appendHeaders; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
9 | 6 | import static org.junit.jupiter.api.Assertions.assertTrue; |
10 | | -import static org.mockito.ArgumentMatchers.any; |
11 | | -import static org.mockito.BDDMockito.given; |
12 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
13 | | -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log; |
14 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
15 | 7 |
|
16 | 8 | import java.nio.file.FileSystemException; |
17 | 9 | import java.util.regex.Matcher; |
18 | 10 | import java.util.regex.Pattern; |
19 | | -import java.util.stream.Stream; |
20 | | -import org.folio.rest.workflow.controller.EventController; |
21 | 11 | import org.folio.rest.workflow.exception.EventPublishException; |
22 | 12 | import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
23 | 14 | import org.junit.jupiter.api.extension.ExtendWith; |
24 | | -import org.junit.jupiter.params.ParameterizedTest; |
25 | | -import org.junit.jupiter.params.provider.Arguments; |
26 | | -import org.junit.jupiter.params.provider.MethodSource; |
27 | | -import org.mockito.Mock; |
28 | 15 | import org.mockito.junit.jupiter.MockitoExtension; |
29 | | -import org.springframework.beans.factory.annotation.Autowired; |
30 | | -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
31 | | -import org.springframework.boot.test.context.SpringBootTest; |
32 | | -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 16 | +import org.springframework.http.HttpStatus; |
| 17 | +import org.springframework.http.MediaType; |
| 18 | +import org.springframework.http.ResponseEntity; |
33 | 19 | import org.springframework.test.context.ActiveProfiles; |
34 | | -import org.springframework.test.web.servlet.MockMvc; |
35 | | -import org.springframework.test.web.servlet.MvcResult; |
36 | | -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
37 | | -import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
38 | 20 |
|
39 | | -@SpringBootTest(webEnvironment = WebEnvironment.MOCK) |
40 | | -@AutoConfigureMockMvc |
41 | 21 | @ExtendWith(MockitoExtension.class) |
42 | 22 | @ActiveProfiles("test") |
43 | 23 | class EventControllerAdviceTest { |
44 | 24 |
|
45 | | - private static final String PATH = "/events"; |
| 25 | + private static final Exception RUNTIME_EXC = new RuntimeException("Runtime failure."); |
46 | 26 |
|
47 | | - private static final String PATH_HANDLE = PATH + "/handle"; |
| 27 | + private static final EventPublishException EP_EXC = new EventPublishException(VALUE, RUNTIME_EXC); |
48 | 28 |
|
49 | | - @Autowired |
50 | | - private EventControllerAdvice eventControllerAdvice; |
| 29 | + private static final FileSystemException FS_EXC = new FileSystemException(VALUE); |
51 | 30 |
|
52 | | - @Autowired |
53 | | - @Mock |
54 | | - private EventController eventController; |
55 | | - |
56 | | - private MockMvc mvc; |
| 31 | + private EventControllerAdvice advice; |
57 | 32 |
|
58 | 33 | @BeforeEach |
59 | 34 | void beforeEach() { |
60 | | - mvc = MockMvcBuilders.standaloneSetup(eventController) |
61 | | - .setControllerAdvice(eventControllerAdvice) |
62 | | - .build(); |
| 35 | + advice = new EventControllerAdvice(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void handleEventPublishExceptionTest() { |
| 40 | + |
| 41 | + final String simpleName = EventPublishException.class.getSimpleName(); |
| 42 | + final ResponseEntity<String> response = advice.handleEventPublishException(EP_EXC); |
| 43 | + |
| 44 | + assertNotNull(response); |
| 45 | + assertNotNull(response.getBody()); |
| 46 | + |
| 47 | + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); |
| 48 | + assertEquals(MediaType.APPLICATION_JSON, response.getHeaders().getContentType()); |
| 49 | + assertTrue(matchBody(response, simpleName)); |
63 | 50 | } |
64 | 51 |
|
65 | | - @ParameterizedTest |
66 | | - @MethodSource("provideExceptionsToMatchForActivateWorkflow") |
67 | | - void exceptionsThrownForActivateWorkflowTest(Exception exception, String simpleName, int status) throws Exception { |
68 | | - given(eventController.postHandleEvents(any(), any())).willAnswer(invocation -> { throw exception; }); |
| 52 | + @Test |
| 53 | + void handleFileSystemExceptionTest() { |
69 | 54 |
|
70 | | - MockHttpServletRequestBuilder request = appendHeaders(post(PATH_HANDLE), OKAPI_HEAD, APP_JSON, APP_JSON); |
| 55 | + final String simpleName = FileSystemException.class.getSimpleName(); |
| 56 | + final ResponseEntity<String> response = advice.handleFileSystemException(FS_EXC); |
71 | 57 |
|
72 | | - MvcResult result = mvc.perform(appendBody(request, JSON_OBJECT)) |
73 | | - .andDo(log()).andExpect(status().is(status)).andReturn(); |
| 58 | + assertNotNull(response); |
| 59 | + assertNotNull(response.getBody()); |
74 | 60 |
|
75 | | - Pattern pattern = Pattern.compile("\"type\":\"" + simpleName + "\""); |
76 | | - Matcher matcher = pattern.matcher(result.getResponse().getContentAsString()); |
77 | | - assertTrue(matcher.find()); |
| 61 | + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); |
| 62 | + assertEquals(MediaType.APPLICATION_JSON, response.getHeaders().getContentType()); |
| 63 | + assertTrue(matchBody(response, simpleName)); |
78 | 64 | } |
79 | 65 |
|
80 | 66 | /** |
81 | | - * Helper function for parameterized test providing the exceptions to be matched for activate workflow. |
| 67 | + * Match the class simple name in the response. |
82 | 68 | * |
83 | | - * @return |
84 | | - * The arguments array stream with the stream columns as: |
85 | | - * - Exception exception. |
86 | | - * - String simpleName (exception name to match). |
87 | | - * - int status (response HTTP status code for the exception). |
| 69 | + * @param response The response to search. |
| 70 | + * @param simpleName The class name to match. |
| 71 | + * |
| 72 | + * @return TRUE on match; FALSE otherwise. |
88 | 73 | */ |
89 | | - private static Stream<Arguments> provideExceptionsToMatchForActivateWorkflow() { |
90 | | - Exception runtime = new RuntimeException("Runtime failure."); |
| 74 | + private boolean matchBody(ResponseEntity<String> response, String simpleName) { |
| 75 | + |
| 76 | + final Pattern pattern = Pattern.compile("\"type\":\"" + simpleName + "\""); |
| 77 | + final Matcher matcher = pattern.matcher(response.getBody()); |
91 | 78 |
|
92 | | - return Stream.of( |
93 | | - Arguments.of(new EventPublishException(VALUE, runtime), EventPublishException.class.getSimpleName(), 500), |
94 | | - Arguments.of(new FileSystemException(VALUE), FileSystemException.class.getSimpleName(), 400) |
95 | | - ); |
| 79 | + return matcher.find(); |
96 | 80 | } |
97 | 81 |
|
98 | 82 | } |
0 commit comments