Skip to content
This repository has been archived by the owner on Apr 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #189 from open-prevo/feature/186_ExcelWriterTest
Browse files Browse the repository at this point in the history
Add test for excel error notification writer
  • Loading branch information
mikaelkalt authored Jul 27, 2018
2 parents d99bcd8 + 4e8c92e commit a13a6c4
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 24 deletions.
2 changes: 2 additions & 0 deletions adapter-excel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ dependencies {

testCompile("junit:junit")
testCompile("org.assertj:assertj-core")
testCompile("org.hibernate:hibernate-validator")
testCompile("org.glassfish:javax.el")
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
Expand Down Expand Up @@ -53,4 +56,19 @@ public static void assertRow(String filename, String sheet, int rowIndex, Object
}
}

public static void assertRowComments(String filename, String sheet, int rowIndex, String... expectedCommentsValues) throws IOException, InvalidFormatException {
try (final Workbook workbook = WorkbookFactory.create(new File(filename), null, true)) {
final Row row = workbook.getSheet(sheet).getRow(rowIndex);
List<String> commentsInRow = collectAllCommentsInRow(row);
assertThat(commentsInRow).containsExactlyInAnyOrder(expectedCommentsValues);
}
}

private static List<String> collectAllCommentsInRow(Row row) {
return IntStream.range(0, row.getLastCellNum())
.mapToObj(row::getCell)
.filter(cell -> cell.getCellComment() != null && cell.getCellComment().getString().length() > 0)
.map(cell -> cell.getCellComment().getString().getString())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*============================================================================*
* Copyright (c) 2018 - Prevo-System AG and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License, v. 2.0 are satisfied: GNU General Public License, version 3
* with the GNU Classpath Exception which is
* available at https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-3.0 WITH Classpath-exception-2.0
*
* Contributors:
* Prevo-System AG - initial API and implementation
*===========================================================================*/
package ch.prevo.open.node.adapter.excel;

import ch.prevo.open.data.api.Address;
import ch.prevo.open.data.api.CapitalTransferInformation;
import ch.prevo.open.data.api.EmploymentCommencement;
import ch.prevo.open.data.api.EmploymentInfo;
import ch.prevo.open.data.api.EmploymentTermination;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import static ch.prevo.open.node.adapter.excel.ExcelAssertions.assertRowComments;

public class ExcelProviderErrorNotificationTest {

private static final String MUST_NOT_BE_NULL = "must not be null";
private static final String PLZ_ERROR = "must match \"[0-9]{4}\"";
private static final String RETIREMENT_FUND_ERROR = "must match \"CHE-([0-9]{3}\\.){2}[0-9]{3}\"";
private static final String INVALID_OASI_NUMBER = "Invalid OASI number";

private ExcelProvider excelProvider;
private Validator validator;
private static final String BASE_FILE_NAME = "src/test/resources/retirement-fund-error-test-data";
private Locale defaultLocale;

@Before
public void init() {
defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.ENGLISH);

System.setProperty(ExcelProvider.FILE_PROPERTY, BASE_FILE_NAME + ".xlsx");
excelProvider = new ExcelProvider();
validator = Validation.buildDefaultValidatorFactory().getValidator();
}

@After
public void tearDown() {
Locale.setDefault(defaultLocale);
}

@Test
public void notifyCommencementErrorsTest() throws IOException, InvalidFormatException {
// given
EmploymentCommencement employmentCommencement = getInvalidEmploymentCommencement();
Map<EmploymentCommencement, Set<ConstraintViolation<EmploymentCommencement>>> validationErrorMap = validate(employmentCommencement);

// when
excelProvider.notifyCommencementErrors(validationErrorMap);

// then
String filename = BASE_FILE_NAME + "-error.xlsx";
new File(filename).deleteOnExit();
assertRowComments(filename, ExcelConstants.COMMENCEMENTS_LABEL, 3, INVALID_OASI_NUMBER, MUST_NOT_BE_NULL,
RETIREMENT_FUND_ERROR, PLZ_ERROR, MUST_NOT_BE_NULL);

}

@Test
public void notifyTerminationErrorsTest() throws IOException, InvalidFormatException {
// given
EmploymentTermination employmentTermination = getInvalidEmploymentTermination();
Map<EmploymentTermination, Set<ConstraintViolation<EmploymentTermination>>> validationErrorMap = validate(employmentTermination);

// when
excelProvider.notifyTerminationErrors(validationErrorMap);

// then
String filename = BASE_FILE_NAME + "-error.xlsx";
new File(filename).deleteOnExit();
assertRowComments(filename, ExcelConstants.TERMINATION_LABEL, 3, INVALID_OASI_NUMBER, MUST_NOT_BE_NULL,
RETIREMENT_FUND_ERROR);

}

private EmploymentCommencement getInvalidEmploymentCommencement() {
EmploymentInfo employmentInfo = new EmploymentInfo("CH-109.740.078", "our-ref-56", "7569678192441", null, null);
Address add = new Address("Str/Postfach", "PLZ", "Ort");
CapitalTransferInformation transferInformation = new CapitalTransferInformation("Bâloise-Sammelstiftung für die ausserobligatorische berufliche Vorsorge", "Zusatzname", add, null);
return new EmploymentCommencement(employmentInfo, transferInformation);
}

private EmploymentTermination getInvalidEmploymentTermination() {
EmploymentInfo employmentInfo = new EmploymentInfo("CHE-123.123.12", null, "1341234328313", null, null);
return new EmploymentTermination(employmentInfo);
}

private <T> Map<T, Set<ConstraintViolation<T>>> validate(T objectToValidate) {
Set<ConstraintViolation<T>> constraints = validator.validate(objectToValidate);

Map<T, Set<ConstraintViolation<T>>> errors = new HashMap<>();
errors.put(objectToValidate, constraints);
return errors;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/*============================================================================*
* Copyright (c) 2018 - Prevo-System AG and others.
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License, v. 2.0 are satisfied: GNU General Public License, version 3
* with the GNU Classpath Exception which is
* available at https://www.gnu.org/software/classpath/license.html.
*
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-3.0 WITH Classpath-exception-2.0
*
*
* Contributors:
* Prevo-System AG - initial API and implementation
*===========================================================================*/
package ch.prevo.open.node.adapter.excel;

import ch.prevo.open.data.api.Address;
import ch.prevo.open.data.api.CapitalTransferInformation;
import ch.prevo.open.data.api.EmploymentCommencement;
import ch.prevo.open.data.api.EmploymentInfo;
import ch.prevo.open.data.api.EmploymentTermination;
Expand All @@ -27,37 +29,60 @@
import java.time.LocalDate;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

import static org.junit.Assert.assertEquals;

public class ExcelProviderTest {

@Before
public void init(){
System.setProperty(ExcelProvider.FILE_PROPERTY, "src/test/resources/retirement-fund-test-data_de.xlsx");
}
private ExcelProvider excelProvider;

@Before
public void init() {
System.setProperty(ExcelProvider.FILE_PROPERTY, "src/test/resources/retirement-fund-test-data_de.xlsx");
excelProvider = new ExcelProvider();
}

@Test
public void readEmploymentTerminations() {
final ExcelProvider provider = new ExcelProvider();
List<EmploymentTermination> employmentTerminations = provider.getEmploymentTerminations();
assertEquals(2, employmentTerminations.size());

EmploymentInfo employmentInfo = employmentTerminations.get(1).getEmploymentInfo();
assertEquals("7568152139908", employmentInfo.getOasiNumber());
assertEquals(LocalDate.of(2018, 8, 16), employmentInfo.getDate());
assertEquals("CHE-223.471.073", employmentInfo.getRetirementFundUid());
// given
EmploymentInfo employmentInfo = new EmploymentInfo("CHE-223.471.073", "our-ref-2", "7568152139908", null, LocalDate.of(2018, 8, 16));

// when
List<EmploymentTermination> employmentTerminations = excelProvider.getEmploymentTerminations();

// then
assertThat(employmentTerminations).hasSize(2);
assertThat(employmentTerminations).last().extracting("employmentInfo").containsExactly(employmentInfo);
}

@Test
public void readEmploymentCommencements() {
final ExcelProvider provider = new ExcelProvider();
List<EmploymentCommencement> employmentCommencements = provider.getEmploymentCommencements();
// given
EmploymentInfo employmentInfo = new EmploymentInfo("CHE-109.740.084", "our-ref-55", "7566374437536", null, LocalDate.of(2018, 1, 1));

// when
List<EmploymentCommencement> employmentCommencements = excelProvider.getEmploymentCommencements();

// then
assertThat(employmentCommencements).hasSize(2);
assertThat(employmentCommencements).first().extracting("employmentInfo").containsExactly(employmentInfo);

}

@Test
public void readEmploymentCommencementsWithCapitalTransferInformation() {
// given
EmploymentInfo employmentInfo = new EmploymentInfo("CHE-109.740.078", "our-ref-56", "7569678192446", null, LocalDate.of(2018, 8, 15));
Address add = new Address("Str/Postfach", "PLZ", "Ort");
CapitalTransferInformation transferInformation = new CapitalTransferInformation("Bâloise-Sammelstiftung für die ausserobligatorische berufliche Vorsorge", "Zusatzname", add, "IBAN");
EmploymentCommencement employmentCommencement = new EmploymentCommencement(employmentInfo, transferInformation);

assertEquals(2, employmentCommencements.size());
// when
List<EmploymentCommencement> employmentCommencements = excelProvider.getEmploymentCommencements();

EmploymentInfo employmentInfo = employmentCommencements.get(0).getEmploymentInfo();
assertEquals("7566374437536", employmentInfo.getOasiNumber());
assertEquals(LocalDate.of(2018, 1, 1), employmentInfo.getDate());
assertEquals("CHE-109.740.084", employmentInfo.getRetirementFundUid());
// then
assertThat(employmentCommencements).hasSize(2);
assertThat(employmentCommencements).last().isEqualTo(employmentCommencement);
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*===========================================================================*/
package ch.prevo.open.data.api;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

Expand Down Expand Up @@ -45,6 +47,26 @@ public void setEmploymentInfo(EmploymentInfo employmentInfo) {
this.employmentInfo = employmentInfo;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

AbstractEmployment that = (AbstractEmployment) o;

return new EqualsBuilder()
.append(employmentInfo, that.employmentInfo)
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(employmentInfo)
.toHashCode();
}

@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @see <a href="https://www.admin.ch/opc/de/classified-compilation/20071554/index.html#app1ahref1">https://www.admin.ch</a>
*/
class OASINumberValidator implements ConstraintValidator<OASI, String> {
public class OASINumberValidator implements ConstraintValidator<OASI, String> {

@Override
public void initialize(OASI constraintAnnotation) {
Expand Down

0 comments on commit a13a6c4

Please sign in to comment.