Skip to content

Commit

Permalink
VKT(Backend) test for excel rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
jrkkp committed Jan 22, 2024
1 parent e8cdf05 commit 7ebfb97
Showing 1 changed file with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@
import fi.oph.vkt.util.ExamEventUtil;
import fi.oph.vkt.util.exception.APIException;
import fi.oph.vkt.util.exception.APIExceptionType;
import fi.oph.vkt.view.ExamEventXlsxData;
import fi.oph.vkt.view.ExamEventXlsxDataRowUtil;
import fi.oph.vkt.view.ExamEventXlsxView;
import jakarta.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.stream.IntStream;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.servlet.view.document.AbstractXlsxView;

Expand Down Expand Up @@ -122,7 +132,7 @@ public void testListExamEvents() {

assertExamEventListDTODetails(expected, dto);
assertEquals(expected == futureEvent ? 2 : 0, dto.participants());
assertEquals(expected == futureEvent ? true : false, dto.isUnusedSeats());
assertEquals(expected == futureEvent, dto.isUnusedSeats());
assertEquals(expected == hiddenEvent, dto.isHidden());
});

Expand Down Expand Up @@ -405,4 +415,32 @@ public void testGetExamEventExcel() {

verify(auditService).logById(VktOperation.GET_EXAM_EVENT_EXCEL, examEvent.getId());
}

@Test
void testExcelRender() throws Exception {
final ExamEvent examEvent = Factory.examEvent();
final Person person = Factory.person();
final Enrollment enrollment = Factory.enrollment(examEvent, person);

entityManager.persist(examEvent);
entityManager.persist(person);
entityManager.persist(enrollment);

final ExamEventXlsxData data = ExamEventXlsxDataRowUtil.createExcelData(examEvent, examEvent.getEnrollments());
final ExamEventXlsxView excelView = new ExamEventXlsxView(data);

final MockHttpServletResponse response = new MockHttpServletResponse();
final MockHttpServletRequest request = new MockHttpServletRequest();

excelView.render(new HashMap<>(), request, response);

try (final Workbook workbook = WorkbookFactory.create(new ByteArrayInputStream(response.getContentAsByteArray()))) {
assertEquals(1, workbook.getNumberOfSheets());

final Sheet sheet = workbook.getSheetAt(0);
assertEquals(2, sheet.getPhysicalNumberOfRows());
assertEquals(21, sheet.getRow(1).getPhysicalNumberOfCells());
assertEquals("Tester", sheet.getRow(1).getCell(3).getStringCellValue());
}
}
}

0 comments on commit 7ebfb97

Please sign in to comment.