Skip to content

Commit

Permalink
fixed, documentation
Browse files Browse the repository at this point in the history
Issue #107
  • Loading branch information
rsoika committed May 3, 2021
1 parent c39b928 commit 47fc668
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 15 deletions.
18 changes: 18 additions & 0 deletions imixs-adapters-sepa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,24 @@ The error message can be configured by the resource bundles 'app' or 'custom' wi

For the IBAN/BIC validaten the open source library org.iban4j.IbanUtil is used


### Strict Validation

In the default mode the plugin ignores spaces between the digits of IBAN/BIC independent from its position. If you want to force a strict validation than you can set the environment variable *SEPA_VALIDATION_STRICT* to 'true'

SEPA_VALIDATION_STRICT=true

In this mode only correctly formated IBAN/BIC strings or strings without spaces will be accepted. See the following example:

# SEPA_VALIDATION_STRICT=false (default)
NL73HIYA0048846703 = OK
NL 73HIYA0048846703 = OK

# SEPA_VALIDATION_STRICT=true
NL73HIYA0048846703 = OK
NL73 HIYA 0048 8467 03 = OK
NL 73HIYA0048846703 = NOT OK

# Development

## Maven
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.iban4j.BicFormatException;
import org.iban4j.BicUtil;
import org.iban4j.IbanFormat;
Expand All @@ -24,6 +25,12 @@
* Validation is skipped in case no data is provided.
* <p>
* For validation we use the open source library 'iban4j'
* <p>
* Note: In some cases the IBAN/BIC is provided on a external invoice with
* spaces at positions which are strictly not allowed. For example a space
* between the country and the first to digits 'DE 99'. We accept those settings
* by removing all spaces before validation. You can disable this behaviour by
* setting the property sepa.validation.strict=true.
*
* @see https://github.com/arturmkrtchyan/iban4j
* @author rsoika
Expand All @@ -32,13 +39,9 @@
*/
public class IBANBICPlugin extends AbstractPlugin {

// is empty or match iban/bic pattern - this pattern allows blanks
// public static final String REGEX_IBAN_PATTERN = "^$|(^[A-Z]{2}(?:[
// ]?[A-Z0-9]){13,32}$)";
// public static final String REGEX_BIC_PATTERN =
// "^$|(^([a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?))";

public static final String ERROR_INVALID_IBANBIC = "ERROR_INVALID_IBANBIC";
public static final String SEPA_VALIDATION_STRICT = "SEPA_VALIDATION_STRICT";


public static final String[] IBAN_BIC_ITEMS = { SepaWorkflowService.ITEM_DBTR_IBAN,
SepaWorkflowService.ITEM_CDTR_IBAN, SepaWorkflowService.ITEM_CDTR_BIC, SepaWorkflowService.ITEM_DBTR_BIC };
Expand All @@ -48,6 +51,11 @@ public class IBANBICPlugin extends AbstractPlugin {

private static Logger logger = Logger.getLogger(IBANBICPlugin.class.getName());

@Inject
@ConfigProperty(name = SEPA_VALIDATION_STRICT, defaultValue = "false")
boolean sepaValidationStrict;


@Inject
ResourceBundleHandler resourceBundleHandler;

Expand Down Expand Up @@ -106,6 +114,11 @@ public ItemCollection run(ItemCollection workitem, ItemCollection event) throws
return workitem;
}


public void setSepaValidationStrict(boolean sepaValidationStrict) {
this.sepaValidationStrict = sepaValidationStrict;
}

/**
* This helper method trims the input if necessary
*/
Expand All @@ -130,13 +143,20 @@ private void trimInput(ItemCollection workitem, String[] itemList) {
* @return
* @throws PluginException
*/
public static void validateIBAN(ItemCollection workitem, String... itemNames) {
public void validateIBAN(ItemCollection workitem, String... itemNames) {

for (String itemName : itemNames) {
String iban = workitem.getItemValueString(itemName);
if (iban.isEmpty()) {
continue;
}

// strip spaces?
if (sepaValidationStrict==false) {
// yes...
iban=iban.replaceAll("\\s+", "");
}

if (iban.contains(" ")) {
// formated
IbanUtil.validate(iban, IbanFormat.Default);
Expand All @@ -158,13 +178,19 @@ public static void validateIBAN(ItemCollection workitem, String... itemNames) {
* @return
* @throws PluginException
*/
public static void validateBIC(ItemCollection workitem, String... itemNames) {
public void validateBIC(ItemCollection workitem, String... itemNames) {

for (String itemName : itemNames) {
String bic = workitem.getItemValueString(itemName);
if (bic.isEmpty()) {
continue;
}
// strip spaces?
if (sepaValidationStrict==false) {
// yes...
bic=bic.replaceAll("\\s+", "");
}

BicUtil.validate(bic);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package org.imixs.workflow.sepa;

import java.util.logging.Logger;

import org.iban4j.BicFormatException;
import org.iban4j.Iban;
import org.iban4j.IbanFormatException;
import org.iban4j.InvalidCheckDigitException;
import org.iban4j.UnsupportedCountryException;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.exceptions.PluginException;
import org.imixs.workflow.sepa.plugins.IBANBICPlugin;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
Expand All @@ -17,6 +21,14 @@
*
*/
public class TestIBANBIC {
private static Logger logger = Logger.getLogger(IBANBICPlugin.class.getName());

private IBANBICPlugin ibanbicPlugin = null;

@Before
public void setup() throws PluginException {
ibanbicPlugin = new IBANBICPlugin();
}

/**
* test the fieldlist of the first line of the file
Expand All @@ -28,13 +40,13 @@ public void testIBAN() {
// test random iban
Iban iban = Iban.random();
workitem.setItemValue("iban", iban.toString());
IBANBICPlugin.validateIBAN(workitem, "iban");
ibanbicPlugin.validateIBAN(workitem, "iban");

// test invalid iban
try {
// test random iban
workitem.setItemValue("iban", "DE55880109902211842211");
IBANBICPlugin.validateIBAN(workitem, "iban");
ibanbicPlugin.validateIBAN(workitem, "iban");
Assert.fail();
} catch (IbanFormatException | InvalidCheckDigitException | UnsupportedCountryException e) {
// expected exception
Expand All @@ -50,18 +62,60 @@ public void testBIC() {

ItemCollection workitem = new ItemCollection();

workitem.setItemValue("bic","DEUTDEFF");
IBANBICPlugin.validateBIC(workitem, "bic");
workitem.setItemValue("bic", "DEUTDEFF");
ibanbicPlugin.validateBIC(workitem, "bic");

// test invalid bic
try {
workitem.setItemValue("bic","DXUXXXFF");
IBANBICPlugin.validateBIC(workitem, "bic");
workitem.setItemValue("bic", "DXUXXXFF");
ibanbicPlugin.validateBIC(workitem, "bic");

Assert.fail();
} catch (BicFormatException | UnsupportedCountryException e) {
// expected exception
System.out.println("invalid bic: " + e.getMessage());
}
}

/**
* Here we are testing a IBAN with a space at a invalid position (3) This should
* be tolerated by the Imxis IBANPlugin in the mode SEPA_VALIDATION_STRICT=false
* (default)
*/
@Test
public void testValidationMode() {
ItemCollection workitem = new ItemCollection();

// test random iban
Iban iban = Iban.random();
workitem.setItemValue("iban", iban.toString());

// test invalid iban
try {
// test random iban
String test_iban = iban.toString();

logger.info("IBAN='" + test_iban + "'");
// add a space at position 3
test_iban = test_iban.substring(0, 2) + " " + test_iban.substring(2);
logger.info("IBAN='" + test_iban + "'");

workitem.setItemValue("iban", test_iban);
ibanbicPlugin.validateIBAN(workitem, "iban");

try {
// now we switch in the strict mode where a space is disallowed
ibanbicPlugin.setSepaValidationStrict(true);
ibanbicPlugin.validateIBAN(workitem, "iban");
Assert.fail();
} catch (Exception e) {
// in strict mode we expect a exception
}

} catch (IbanFormatException | InvalidCheckDigitException | UnsupportedCountryException e) {
// expected exception
System.out.println("invalid iban: " + e.getMessage());
Assert.fail();
}
}
}

0 comments on commit 47fc668

Please sign in to comment.