From 582736ae19bfac8941e92358b93212cab9c40354 Mon Sep 17 00:00:00 2001 From: ptorres-prowide Date: Wed, 8 Nov 2023 17:54:49 -0300 Subject: [PATCH] CU-86ayb02z5_Crete-model-for-DistinguishedName --- CHANGELOG.md | 3 + .../com/prowidesoftware/swift/model/BIC.java | 9 +- .../swift/model/DistinguishedName.java | 94 +++++++++++++++++++ .../swift/model/DistinguishedNameTest.java | 60 ++++++++++++ 4 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/prowidesoftware/swift/model/DistinguishedName.java create mode 100644 src/test/java/com/prowidesoftware/swift/model/DistinguishedNameTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ea3bb588..63984cdae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Prowide Core - CHANGELOG +#### 9.3.20 - SNAPSHOT + * Added DistinguishedName with Builder in order to encapsulate the BIC branch name logic + #### 9.3.19 - October 2023 * Added default methods for sender, receiver, and identifier extraction to the MessageExtractionStrategy * Added JSON to the `FileFormat` enumeration diff --git a/src/main/java/com/prowidesoftware/swift/model/BIC.java b/src/main/java/com/prowidesoftware/swift/model/BIC.java index 993765fda..a9bff6b6e 100644 --- a/src/main/java/com/prowidesoftware/swift/model/BIC.java +++ b/src/main/java/com/prowidesoftware/swift/model/BIC.java @@ -356,14 +356,11 @@ public String distinguishedName() { * @since 9.3.15 */ public String distinguishedName(boolean includeDefaultBranch) { - StringBuilder result = new StringBuilder(); + DistinguishedName.Builder dnBuilder = new DistinguishedName.Builder(getBic8()); if (includeDefaultBranch || !Objects.equals(getBranchOrDefault(), "XXX")) { - result.append("ou=") - .append(StringUtils.lowerCase(getBranchOrDefault())) - .append(","); + dnBuilder.withBranch(getBranchOrDefault()); } - result.append("o=").append(StringUtils.lowerCase(getBic8())).append(",o=swift"); - return result.toString(); + return dnBuilder.build().toString(); } @Override diff --git a/src/main/java/com/prowidesoftware/swift/model/DistinguishedName.java b/src/main/java/com/prowidesoftware/swift/model/DistinguishedName.java new file mode 100644 index 000000000..e2974bc6e --- /dev/null +++ b/src/main/java/com/prowidesoftware/swift/model/DistinguishedName.java @@ -0,0 +1,94 @@ +package com.prowidesoftware.swift.model; + +import org.apache.commons.lang3.StringUtils; + +/** + * Represents a Distinguished Name (DN) in the context of a directory service. + * The DN is constructed using the organization unit (ou), Bank Identifier Code (BIC8), and SWIFT code. + * This class provides a builder pattern for creating DistinguishedName instances. + */ +public class DistinguishedName { + + /** + * The organization unit (ou) representing the branch in the Distinguished Name. + */ + private final String branch; + + /** + * The Bank Identifier Code (BIC8) in lowercase format. + */ + private final String bic8; + + /** + * The SWIFT code representing the organization. + */ + private final String swift; + + /** + * Private constructor to create a DistinguishedName instance using the Builder. + * + * @param builder The builder containing the required parameters for the DistinguishedName. + */ + private DistinguishedName(Builder builder) { + this.branch = builder.branch; + this.bic8 = builder.bic8; + this.swift = builder.swift; + } + + public String getBranch() { + return branch; + } + + public String getBic8() { + return bic8; + } + + public String getSwift() { + return swift; + } + + @Override + public String toString() { + StringBuilder dn = new StringBuilder(); + if (branch != null && !branch.isEmpty()) { + dn.append("ou=").append(branch).append(","); + } + dn.append("o=").append(bic8).append(",o=").append(swift); + return dn.toString(); + } + + public static String parseBIC(final String dn) { + if (StringUtils.isBlank(dn)) { + return null; + } + for (String s : StringUtils.split(dn, ",")) { + if (StringUtils.startsWith(s, "o=") && !StringUtils.equals(s, "o=swift")) { + return StringUtils.upperCase(StringUtils.substringAfter(s, "o=")); + } + } + return null; + } + + /** + * Builder class for constructing DistinguishedName instances. + */ + public static class Builder { + private String branch; + private final String bic8; + private final String swift; + + public Builder(String bic8) { + this.bic8 = StringUtils.lowerCase(bic8); + this.swift = "swift"; + } + + public Builder withBranch(String ou) { + this.branch = StringUtils.lowerCase(ou); + return this; + } + + public DistinguishedName build() { + return new DistinguishedName(this); + } + } +} diff --git a/src/test/java/com/prowidesoftware/swift/model/DistinguishedNameTest.java b/src/test/java/com/prowidesoftware/swift/model/DistinguishedNameTest.java new file mode 100644 index 000000000..821791651 --- /dev/null +++ b/src/test/java/com/prowidesoftware/swift/model/DistinguishedNameTest.java @@ -0,0 +1,60 @@ +package com.prowidesoftware.swift.model; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +class DistinguishedNameTest { + + @Test + void testDistinguishedNameWithCommonNameAndOU() { + String bic8 = "BIC8CODE"; + String ou = "FOO"; + DistinguishedName dn = + new DistinguishedName.Builder(bic8).withBranch(ou).build(); + assertEquals("ou=foo,o=bic8code,o=swift", dn.toString()); + } + + @Test + void testDistinguishedNameWithOUOnly() { + String bic8 = "bic8code"; + String ou = "bar"; + DistinguishedName dn = + new DistinguishedName.Builder(bic8).withBranch(ou).build(); + assertEquals("ou=bar,o=bic8code,o=swift", dn.toString()); + } + + @Test + void testDistinguishedNameWithNoOUOrCN() { + String bic8 = "BIC8CODE"; + DistinguishedName dn = new DistinguishedName.Builder(bic8).build(); + assertEquals("o=bic8code,o=swift", dn.toString()); + } + + @Test + void testDistinguishedNameEmptyValues() { + String bic8 = "BIC8CODE"; + DistinguishedName dn = + new DistinguishedName.Builder(bic8).withBranch("").build(); + assertEquals("o=bic8code,o=swift", dn.toString()); + } + + @Test + void testParseBIC() { + String dn1 = "cn=John Doe,o=swift,ou=users,dc=example,dc=com"; + assertNull(DistinguishedName.parseBIC(dn1)); + + dn1 = "o=bic8code,o=swift"; + assertEquals("BIC8CODE", DistinguishedName.parseBIC(dn1)); + + dn1 = "ou=bar,o=bic8code,o=swift"; + assertEquals("BIC8CODE", DistinguishedName.parseBIC(dn1)); + + assertNull(DistinguishedName.parseBIC(null)); + + assertNull(DistinguishedName.parseBIC("")); + + assertNull(DistinguishedName.parseBIC("XXX")); + } +}