Skip to content

Commit

Permalink
Enhanced MtId constructor with regex matching
Browse files Browse the repository at this point in the history
  • Loading branch information
zubri committed Jun 27, 2022
1 parent 2eb8367 commit 4683fa4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
Prowide Core - CHANGELOG
-----------------------------------------------------------------------------------------------------------------------

9.2.14 SNAPSHOT
RELEASE 9.2.14 - June 2022
* PW-867: Enhanced the parsing of party fields A, B and D, to be more strict when splitting the /D/ or /C/ prefix from the account
* Enhanced MtId constructor with regex matching
* Added method namespaceURI() in the MtId class to return for example "urn:swift:xsd:fin.103.2021" for the MT103

RELEASE 9.2.13 - April 2022
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/prowidesoftware/swift/model/MtId.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Structured identification of MT message types, composed by the business process, actual type and variant.
Expand All @@ -31,6 +33,7 @@
* @since 7.8.4
*/
public class MtId {
private static final Pattern TYPE_PATTERN = Pattern.compile("\\d{3}");
private String businessProcess = "fin";
private String messageType;
private String variant;
Expand All @@ -49,7 +52,11 @@ public MtId() {
public MtId(String identifier) {
this();
if (identifier != null) {
this.messageType = identifier.replaceAll("\\D+", "");
// match exactly three digits from the identifier string
Matcher matcher = TYPE_PATTERN.matcher(identifier);
if (matcher.find()) {
this.messageType = matcher.group(0);
}

if (StringUtils.isNotBlank(this.messageType)) {
// if message type was extracted, we try to extract the variant as well
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/com/prowidesoftware/swift/model/MtIdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
class MtIdTest {

@Test
public void test() {
public void testConstructor() {
// from well defined identifier
assertEquals("fin.103", new MtId("103").id());
assertEquals("fin.103", new MtId("fin.103").id());
assertEquals("fin.103.REMIT", new MtId("fin.103.REMIT").id());
Expand All @@ -36,8 +37,17 @@ public void test() {
assertEquals("fin.103.STP", new MtId("fin.103STP").id());
assertEquals("fin.202.COV", new MtId("fin.202_COV").id());
assertEquals("fin.202.COV", new MtId("fin.202COV").id());

// from ISO 15022 namespaces
assertEquals("fin.103", new MtId("urn:swift:xsd:fin.103.2021").id());
assertEquals("fin.103.REMIT", new MtId("urn:swift:xsd:fin.103.REMIT.2022").id());
assertEquals("fin.103.STP", new MtId("urn:swift:xsd:fin.103.STP.2022").id());

// ACK/NACK
assertEquals("fin.ACK", new MtId("ACK").id());
assertEquals("fin.NAK", new MtId("NAK").id());

// Other strings
assertEquals("fin.BypassFoobar", new MtId("BypassFoobar").id());
}

Expand Down

0 comments on commit 4683fa4

Please sign in to comment.