-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CU-86ayb02z5_Crete-model-for-DistinguishedName #182
Merged
zubri
merged 2 commits into
SRU2023
from
CU-86ayb02z5_Crete-model-for-DistinguishedName
Nov 16, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
94 changes: 94 additions & 0 deletions
94
src/main/java/com/prowidesoftware/swift/model/DistinguishedName.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,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); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/test/java/com/prowidesoftware/swift/model/DistinguishedNameTest.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,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")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unused classes and interfaces Note test