|
| 1 | +package ai.elimu.rest.v2.analytics; |
| 2 | + |
| 3 | +import org.json.JSONObject; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.mockito.InjectMocks; |
| 7 | +import org.mockito.Mock; |
| 8 | +import org.mockito.MockitoAnnotations; |
| 9 | +import org.springframework.mock.web.MockMultipartFile; |
| 10 | +import org.springframework.web.multipart.MultipartFile; |
| 11 | + |
| 12 | +import jakarta.servlet.http.HttpServletResponse; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.mockito.Mockito.*; |
| 15 | + |
| 16 | +public class NumberAssessmentEventsRestControllerTest { |
| 17 | + |
| 18 | + @InjectMocks |
| 19 | + private NumberAssessmentEventsRestController numberAssessmentEventsRestController; |
| 20 | + |
| 21 | + @Mock |
| 22 | + private HttpServletResponse response; |
| 23 | + |
| 24 | + @BeforeEach |
| 25 | + public void setUp() { |
| 26 | + MockitoAnnotations.openMocks(this); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testHandleUploadCsvRequest_invalidFilename() { |
| 31 | + MultipartFile multipartFile = new MockMultipartFile( |
| 32 | + "file", |
| 33 | + "invalid_filename.csv", |
| 34 | + "text/csv", |
| 35 | + "test content".getBytes() |
| 36 | + ); |
| 37 | + String jsonResponse = numberAssessmentEventsRestController.handleUploadCsvRequest(multipartFile, response); |
| 38 | + JSONObject jsonObject = new JSONObject(jsonResponse); |
| 39 | + assertEquals("error", jsonObject.getString("result")); |
| 40 | + assertEquals("Unexpected filename", jsonObject.getString("errorMessage")); |
| 41 | + verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testHandleUploadCsvRequest_emptyFile() { |
| 46 | + MultipartFile multipartFile = new MockMultipartFile( |
| 47 | + "file", |
| 48 | + "7161a85a0e4751cd_4000022_number-assessment-events_2025-07-01.csv", |
| 49 | + "text/csv", |
| 50 | + "".getBytes() |
| 51 | + ); |
| 52 | + String jsonResponse = numberAssessmentEventsRestController.handleUploadCsvRequest(multipartFile, response); |
| 53 | + JSONObject jsonObject = new JSONObject(jsonResponse); |
| 54 | + assertEquals("error", jsonObject.getString("result")); |
| 55 | + assertEquals("Empty file", jsonObject.getString("errorMessage")); |
| 56 | + verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
| 57 | + } |
| 58 | +} |
0 commit comments