|
| 1 | +package org.cbioportal.legacy.web.util; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.mockito.Mockito.*; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import jakarta.servlet.ServletInputStream; |
| 9 | +import jakarta.servlet.http.HttpServletRequest; |
| 10 | +import jakarta.servlet.http.HttpServletResponse; |
| 11 | +import java.io.ByteArrayInputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.PrintWriter; |
| 14 | +import java.io.StringWriter; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import org.cbioportal.legacy.persistence.cachemaputil.CacheMapUtil; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Test; |
| 19 | +import org.junit.runner.RunWith; |
| 20 | +import org.mockito.InjectMocks; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.Spy; |
| 23 | +import org.mockito.junit.MockitoJUnitRunner; |
| 24 | +import org.springframework.mock.web.DelegatingServletInputStream; |
| 25 | + |
| 26 | +@RunWith(MockitoJUnitRunner.class) |
| 27 | +public class InvolvedCancerStudyExtractorInterceptorTest { |
| 28 | + |
| 29 | + @InjectMocks private InvolvedCancerStudyExtractorInterceptor interceptor; |
| 30 | + |
| 31 | + @Mock private CacheMapUtil cacheMapUtil; |
| 32 | + |
| 33 | + @Mock private HttpServletRequest request; |
| 34 | + |
| 35 | + @Mock private HttpServletResponse response; |
| 36 | + |
| 37 | + @Spy private ObjectMapper objectMapper = new ObjectMapper(); |
| 38 | + |
| 39 | + private StringWriter responseWriter; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() throws IOException { |
| 43 | + responseWriter = new StringWriter(); |
| 44 | + when(response.getWriter()).thenReturn(new PrintWriter(responseWriter)); |
| 45 | + when(response.isCommitted()).thenReturn(false); |
| 46 | + when(cacheMapUtil.hasCacheEnabled()).thenReturn(false); |
| 47 | + } |
| 48 | + |
| 49 | + private ServletInputStream createInputStream(String content) { |
| 50 | + ByteArrayInputStream byteStream = |
| 51 | + new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)); |
| 52 | + return new DelegatingServletInputStream(byteStream); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testMalformedJsonReturnsBadRequest() throws Exception { |
| 57 | + when(request.getMethod()).thenReturn("POST"); |
| 58 | + when(request.getPathInfo()).thenReturn("/api/patients/fetch"); |
| 59 | + when(request.getInputStream()).thenReturn(createInputStream("{'invalid json'}")); |
| 60 | + |
| 61 | + boolean result = interceptor.preHandle(request, response, null); |
| 62 | + |
| 63 | + assertFalse("preHandle should return false for malformed JSON", result); |
| 64 | + verify(response).setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 65 | + verify(response).setContentType("application/json"); |
| 66 | + assertTrue( |
| 67 | + "Response should contain error message", |
| 68 | + responseWriter.toString().contains("Invalid request body")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testEmptyBodyReturnsBadRequest() throws Exception { |
| 73 | + when(request.getMethod()).thenReturn("POST"); |
| 74 | + when(request.getPathInfo()).thenReturn("/api/samples/fetch"); |
| 75 | + when(request.getInputStream()).thenReturn(createInputStream("")); |
| 76 | + |
| 77 | + boolean result = interceptor.preHandle(request, response, null); |
| 78 | + |
| 79 | + assertFalse("preHandle should return false for empty body", result); |
| 80 | + verify(response).setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testValidJsonPassesThrough() throws Exception { |
| 85 | + String validJson = |
| 86 | + "{\"sampleIdentifiers\":[{\"sampleId\":\"sample1\",\"studyId\":\"study1\"}]}"; |
| 87 | + when(request.getMethod()).thenReturn("POST"); |
| 88 | + when(request.getPathInfo()).thenReturn("/api/samples/fetch"); |
| 89 | + when(request.getInputStream()).thenReturn(createInputStream(validJson)); |
| 90 | + |
| 91 | + boolean result = interceptor.preHandle(request, response, null); |
| 92 | + |
| 93 | + assertTrue("preHandle should return true for valid JSON", result); |
| 94 | + verify(response, never()).setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testNonPostRequestPassesThrough() throws Exception { |
| 99 | + when(request.getMethod()).thenReturn("GET"); |
| 100 | + |
| 101 | + boolean result = interceptor.preHandle(request, response, null); |
| 102 | + |
| 103 | + assertTrue("preHandle should return true for GET requests", result); |
| 104 | + verify(response, never()).setStatus(anyInt()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testMessageTruncationRemovesJacksonDetails() throws Exception { |
| 109 | + when(request.getMethod()).thenReturn("POST"); |
| 110 | + when(request.getPathInfo()).thenReturn("/api/patients/fetch"); |
| 111 | + when(request.getInputStream()).thenReturn(createInputStream("{bad}")); |
| 112 | + |
| 113 | + interceptor.preHandle(request, response, null); |
| 114 | + |
| 115 | + String responseBody = responseWriter.toString(); |
| 116 | + assertFalse( |
| 117 | + "Response should not contain Jackson source details", responseBody.contains("at [Source:")); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testJsonEscapingForSpecialCharacters() throws Exception { |
| 122 | + // This tests that ObjectMapper properly escapes special characters |
| 123 | + when(request.getMethod()).thenReturn("POST"); |
| 124 | + when(request.getPathInfo()).thenReturn("/api/patients/fetch"); |
| 125 | + // JSON with unmatched quote that would cause parsing error with special chars in message |
| 126 | + when(request.getInputStream()).thenReturn(createInputStream("{\"field\": \"value with \\n}")); |
| 127 | + |
| 128 | + interceptor.preHandle(request, response, null); |
| 129 | + |
| 130 | + String responseBody = responseWriter.toString(); |
| 131 | + // Verify it's valid JSON (ObjectMapper would produce valid JSON) |
| 132 | + assertTrue("Response should be valid JSON", responseBody.startsWith("{\"message\":")); |
| 133 | + assertTrue("Response should end properly", responseBody.endsWith("}")); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testCommittedResponseDoesNotWrite() throws Exception { |
| 138 | + when(request.getMethod()).thenReturn("POST"); |
| 139 | + when(request.getPathInfo()).thenReturn("/api/patients/fetch"); |
| 140 | + when(request.getInputStream()).thenReturn(createInputStream("invalid")); |
| 141 | + when(response.isCommitted()).thenReturn(true); |
| 142 | + |
| 143 | + interceptor.preHandle(request, response, null); |
| 144 | + |
| 145 | + verify(response, never()).setStatus(anyInt()); |
| 146 | + verify(response, never()).getWriter(); |
| 147 | + } |
| 148 | +} |
0 commit comments