-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add DuplicateRowConstraint with unit tests
- Loading branch information
Showing
2 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
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
142 changes: 142 additions & 0 deletions
142
src/test/java/com/r4tylmz/betterpoi/validation/row/DuplicateRowConstraintTest.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,142 @@ | ||
package com.r4tylmz.betterpoi.validation.row; | ||
|
||
import com.r4tylmz.betterpoi.annotation.BPColumn; | ||
import com.r4tylmz.betterpoi.annotation.BPSheet; | ||
import com.r4tylmz.betterpoi.test.EmployeeWorkbookTest; | ||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import org.apache.poi.ss.usermodel.Sheet; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class DuplicateRowConstraintTest extends EmployeeWorkbookTest { | ||
|
||
private final String errorMessage = "Duplicate row found at row %d"; | ||
private DuplicateRowConstraint duplicateRowConstraint; | ||
|
||
@Before | ||
public void createRowDuplicateConstraint() throws Exception { | ||
duplicateRowConstraint = new DuplicateRowConstraint(); | ||
} | ||
|
||
private void createSheetWithHeaders(String[] headers) { | ||
Row row = getRow(headers.length); | ||
for (int i = 0; i < headers.length; i++) { | ||
row.getCell(i).setCellValue(headers[i]); | ||
} | ||
} | ||
|
||
private Sheet getSheetWithHeaders(String[] headers) { | ||
createSheetWithHeaders(headers); | ||
return workbook.getSheet("testSheet"); | ||
} | ||
|
||
@Test | ||
public void validate_duplicateRow() { | ||
String[] headers = {"header1", "header2", "header3"}; | ||
Sheet sheet = getSheetWithHeaders(headers); | ||
BPSheet bpSheet = Mockito.mock(BPSheet.class); | ||
BPColumn bpColumn1 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn1.headerTitle()).thenReturn("header1"); | ||
BPColumn bpColumn2 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn2.headerTitle()).thenReturn("header2"); | ||
BPColumn bpColumn3 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn3.headerTitle()).thenReturn("header3"); | ||
|
||
BPColumn[] bpColumns = {bpColumn1, bpColumn2, bpColumn3}; | ||
Mockito.when(bpSheet.columns()).thenReturn(bpColumns); | ||
|
||
Row row1 = sheet.createRow(1); | ||
for (int i = 0; i < headers.length; i++) { | ||
Cell cell = row1.createCell(i); | ||
cell.setCellValue("random value" + i); | ||
} | ||
Row row2 = sheet.createRow(2); | ||
for (int i = 0; i < headers.length; i++) { | ||
Cell cell = row2.createCell(i); | ||
cell.setCellValue("random value" + i); | ||
} | ||
|
||
Map<Integer, String> errorMap = duplicateRowConstraint.validate(sheet, bpSheet); | ||
assertEquals(1, errorMap.size()); | ||
assertEquals(String.format(errorMessage, 3), errorMap.get(2)); | ||
} | ||
|
||
@Test | ||
public void validate_emptySheet() { | ||
Sheet sheet = workbook.createSheet("testSheet"); | ||
BPSheet bpSheet = Mockito.mock(BPSheet.class); | ||
BPColumn bpColumn1 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn1.headerTitle()).thenReturn("header1"); | ||
BPColumn bpColumn2 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn2.headerTitle()).thenReturn("header2"); | ||
BPColumn bpColumn3 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn3.headerTitle()).thenReturn("header3"); | ||
|
||
BPColumn[] bpColumns = {bpColumn1, bpColumn2, bpColumn3}; | ||
Mockito.when(bpSheet.columns()).thenReturn(bpColumns); | ||
|
||
Map<Integer, String> errorMap = duplicateRowConstraint.validate(sheet, bpSheet); | ||
assertEquals(0, errorMap.size()); | ||
} | ||
|
||
@Test | ||
public void validate_noDuplicateRow() { | ||
String[] headers = {"header1", "header2", "header3"}; | ||
Sheet sheet = getSheetWithHeaders(headers); | ||
BPSheet bpSheet = Mockito.mock(BPSheet.class); | ||
BPColumn bpColumn1 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn1.headerTitle()).thenReturn("header1"); | ||
BPColumn bpColumn2 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn2.headerTitle()).thenReturn("header2"); | ||
BPColumn bpColumn3 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn3.headerTitle()).thenReturn("header3"); | ||
|
||
BPColumn[] bpColumns = {bpColumn1, bpColumn2, bpColumn3}; | ||
Mockito.when(bpSheet.columns()).thenReturn(bpColumns); | ||
|
||
Row row1 = sheet.createRow(1); | ||
for (int i = 0; i < headers.length; i++) { | ||
Cell cell = row1.createCell(i); | ||
cell.setCellValue("random value" + i); | ||
} | ||
Row row2 = sheet.createRow(2); | ||
for (int i = 0; i < headers.length; i++) { | ||
Cell cell = row2.createCell(i); | ||
cell.setCellValue("value" + i); | ||
} | ||
|
||
Map<Integer, String> errorMap = duplicateRowConstraint.validate(sheet, bpSheet); | ||
assertEquals(0, errorMap.size()); | ||
} | ||
|
||
@Test | ||
public void validate_singleRow() { | ||
String[] headers = {"header1", "header2", "header3"}; | ||
Sheet sheet = getSheetWithHeaders(headers); | ||
BPSheet bpSheet = Mockito.mock(BPSheet.class); | ||
BPColumn bpColumn1 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn1.headerTitle()).thenReturn("header1"); | ||
BPColumn bpColumn2 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn2.headerTitle()).thenReturn("header2"); | ||
BPColumn bpColumn3 = Mockito.mock(BPColumn.class); | ||
Mockito.when(bpColumn3.headerTitle()).thenReturn("header3"); | ||
|
||
BPColumn[] bpColumns = {bpColumn1, bpColumn2, bpColumn3}; | ||
Mockito.when(bpSheet.columns()).thenReturn(bpColumns); | ||
|
||
Row row1 = sheet.createRow(1); | ||
for (int i = 0; i < headers.length; i++) { | ||
Cell cell = row1.createCell(i); | ||
cell.setCellValue("random value" + i); | ||
} | ||
|
||
Map<Integer, String> errorMap = duplicateRowConstraint.validate(sheet, bpSheet); | ||
assertEquals(0, errorMap.size()); | ||
} | ||
} |