Skip to content

Commit

Permalink
Merge pull request #79 from bcgov/feature/bugfix/parser
Browse files Browse the repository at this point in the history
Feature/bugfix/parser
  • Loading branch information
Adam Kroon authored Dec 27, 2019
2 parents 4e26110 + 6ad0d44 commit 82611d0
Show file tree
Hide file tree
Showing 12 changed files with 626 additions and 6 deletions.
31 changes: 31 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Contributing

All Contributions to this repository start with a Jira Item associated with the work request.

## Pull Request Process

1. Fork the repository on you personal github space. see:[working with forks](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/working-with-forks).
1. [Build](README.md#build) and run the application on your local environment.
1. Commit your changes to your branch.
1. Update the README.md with details of changes to the interface, this includes new environment
variables, exposed ports, useful file locations and container parameters.
1. When your change is ready to be reviewed, open a PR on this repository using our [Pull Request Template](.github/PULL_REQUEST_TEMPLATE) and optionally assign one or more reviewer. Also, in the comment of the Jira item, put a link to the Pull Request and move the item to `in review`
1. If some changes are required, you will be notified in the PR, address any change requested and push to the same branch.
1. When your change is meeting the requirement, the reviewer will merge the code into master, you can celebrate!

## Open a new bug in jira

- When a bug is detected in the application, a jira bug must be open
the bug should be documented as followed:
- Expected Behavior
- Current Behavior
- Steps to Reproduce
- Environment
- Description (stack traces, screenshot, error messages, splunk logs query)
- Optionally you can suggest a possible solution
- set the target release

## Open a new feature request in jira

- For feature enhancements, a jira task or story must be open
- set the target release
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ cd bcparis-service
mvn install
```

## Contributing

Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.

## Run locally

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>ca.bc.gov.bcparis</groupId>
<artifactId>bcparis-service</artifactId>
<name>BCPARIS Legacy Migration</name>
<version>1.0.19</version>
<version>1.0.20</version>

<parent>
<groupId>ca.bc.gov.iamp</groupId>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/ca/bc/gov/iamp/bcparis/Keys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ca.bc.gov.iamp.bcparis;

public class Keys {

public static final String REQUEST_SCHEMA_FROM_KEY = "FROM";
public static final String REQUEST_SCHEMA_TO_KEY = "TO";
public static final String REQUEST_SCHEMA_TEXT_KEY = "TEXT";
public static final String REQUEST_SCHEMA_RE_KEY = "RE";
public static final String REQUEST_SCHEMA_SN_KEY = "SN";
public static final String REQUEST_SCHEMA_MT_KEY = "MT";
public static final String REQUEST_SCHEMA_MSID_KEY = "MSID";
public static final String REQUEST_SCHEMA_SUBJ_KEY = "SUBJ";
public static final String REQUEST_SCHEMA_SNME_KEY = "SNME";
public static final String REQUEST_SCHEMA_DL_KEY = "DL";
public static final String REQUEST_SCHEMA_LIC_KEY = "LIC";
public static final String REQUEST_SCHEMA_ODN_KEY = "ODN";
public static final String REQUEST_SCHEMA_FLC_KEY = "FLC";
public static final String REQUEST_SCHEMA_VIN_KEY = "VIN";
public static final String REQUEST_SCHEMA_REG_KEY = "REG";
public static final String REQUEST_SCHEMA_RNS_KEY = "RNS";
public static final String REQUEST_SCHEMA_RVL_KEY = "RVL";
// TODO: REMOVE TOKEN AFTER SATELLITE SERVICE IS DEPRECATED
public static final String REQUEST_SCHEMA_TEST_RNS_KEY = "TestRNS";

}
115 changes: 115 additions & 0 deletions src/main/java/ca/bc/gov/iamp/bcparis/message/MessageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package ca.bc.gov.iamp.bcparis.message;

import ca.bc.gov.iamp.bcparis.Keys;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.Set;

public class MessageUtils {

private static final String SEMICOLLON = ":";
private static final String STRING_END_ONE = "]]>$";
private static final String STRING_END_TWO = "\n$";

private static HashSet<String> KNOWN_TOKENS = new HashSet<String>() {{
add(Keys.REQUEST_SCHEMA_SN_KEY);
add(Keys.REQUEST_SCHEMA_MT_KEY);
add(Keys.REQUEST_SCHEMA_MSID_KEY);
add(Keys.REQUEST_SCHEMA_FROM_KEY);
add(Keys.REQUEST_SCHEMA_TO_KEY);
add(Keys.REQUEST_SCHEMA_SUBJ_KEY);
add(Keys.REQUEST_SCHEMA_TEXT_KEY);
add(Keys.REQUEST_SCHEMA_RE_KEY);
add(Keys.REQUEST_SCHEMA_SNME_KEY);
add(Keys.REQUEST_SCHEMA_DL_KEY);
add(Keys.REQUEST_SCHEMA_LIC_KEY);
add(Keys.REQUEST_SCHEMA_ODN_KEY);
add(Keys.REQUEST_SCHEMA_FLC_KEY);
add(Keys.REQUEST_SCHEMA_VIN_KEY);
add(Keys.REQUEST_SCHEMA_REG_KEY);
add(Keys.REQUEST_SCHEMA_RNS_KEY);
add(Keys.REQUEST_SCHEMA_RVL_KEY);
add(Keys.REQUEST_SCHEMA_TEST_RNS_KEY);
}};


/**
* Extract the attribute value based on a give token
* Known tokens includes:
* <ul>
* <li>FROM
* <li>TO
* <li>TEXT
* <li>RE
* <li>SN
* <li>MT
* <li>MSID
* <li>SUBJ
* <li>SNME
* <li>DL
* <li>LIC
* <li>ODN
* <li>FLC
* <li>VIN
* <li>REG
* <li>RNS
* <li>RVL
* <li>TestRNS
* </ul>
*
* @param message the source message
* @param key a known key
* @return the value of the attribute
* @throws IllegalArgumentException if the key is not a known key
* @since 1.0.20
*/
public static String GetValue(String message, String key) {

if (!KNOWN_TOKENS.contains(key)) throw new IllegalArgumentException("key must be a known token");

if (StringUtils.isEmpty(message)) return null;

message = removeKnownEnd(message);

message = removeToToken(message, key);

if (message == null) return null;

return message.substring(0, getEndIndex(message)).replaceAll("\\s+$", "");

}

private static String removeKnownEnd(String message) {
message = message.replaceAll(STRING_END_ONE, "");
return message.replaceAll(STRING_END_TWO, "");
}

private static String removeToToken(String message, String token) {
int startIndex = message.indexOf(token + SEMICOLLON);
if (startIndex == -1) return null;

startIndex += token.length() + 1;

return message.substring(startIndex);
}

private static int getEndIndex(String message) {
int currentEndIndex = message.length();

for (String token : KNOWN_TOKENS) {

int tokenIndex = message.indexOf(token + SEMICOLLON);

if (tokenIndex < currentEndIndex && tokenIndex >= 0) {
currentEndIndex = tokenIndex;
}

if (message.indexOf(":") > currentEndIndex) break;
}

return currentEndIndex;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Layer7Message process(Layer7Message message) {

List<IMSRequest> requests = createIMSContent(message);
if (requests.isEmpty()) {
message.getEnvelope().getBody().setMsgFFmt(messageService.buildResponse(message.getEnvelope().getBody(), "Unable to parse/formatting error"));
message.getEnvelope().getBody().setMsgFFmt(messageService.buildErrorResponse(message.getEnvelope().getBody(), "Unable to parse/formatting error"));
log.warn("Processing Driver: Unable to parse/formatting error");
return message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Layer7Message process(Layer7Message message) {

List<IMSRequest> requests = createIMSContent(message);
if (requests.isEmpty()) {
message.getEnvelope().getBody().setMsgFFmt(messageService.buildResponse(message.getEnvelope().getBody(), "Unable to parse/formatting error"));
message.getEnvelope().getBody().setMsgFFmt(messageService.buildErrorResponse(message.getEnvelope().getBody(), "Unable to parse/formatting error"));
log.warn("Processing Vehicle: Unable to parse/formatting error");
return message;
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/ca/bc/gov/iamp/bcparis/service/MessageService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package ca.bc.gov.iamp.bcparis.service;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import ca.bc.gov.iamp.bcparis.Keys;
import ca.bc.gov.iamp.bcparis.message.MessageUtils;
import org.springframework.stereotype.Service;

import ca.bc.gov.iamp.bcparis.exception.message.InvalidMessage;
import ca.bc.gov.iamp.bcparis.model.message.body.Body;


@Service
public class MessageService {

Expand All @@ -21,6 +24,7 @@ public class MessageService {
"TEXT:${text}${re}" + NEW_LINE +
NEW_LINE +
"${icbc_response}";
private final String messageFormat = "SEND MT:M\nFMT:Y\nFROM:{0}\nTO:{1}\nTEXT:{2}{3}\n\n{4}";

public List<String> getQueryAttributesList(Body body, List<String> validAttributes) {
final List<String> result = new ArrayList<>();
Expand All @@ -44,7 +48,15 @@ public String buildResponse(final Body body, final String message) {
.replace("${re}", body.containAttribute("RE") ? "RE:" + re : "")
.replace("${icbc_response}", message);
}


public String buildErrorResponse(final Body body, final String errorMessage) {
final String receiver = MessageUtils.GetValue(body.getMsgFFmt(),Keys.REQUEST_SCHEMA_FROM_KEY); //This becomes the receiver of the message
final String sender = MessageUtils.GetValue(body.getMsgFFmt(),Keys.REQUEST_SCHEMA_TO_KEY); //This will become the sender
final String text = MessageUtils.GetValue(body.getMsgFFmt(),Keys.REQUEST_SCHEMA_TEXT_KEY);
final String re = MessageUtils.GetValue(body.getMsgFFmt(),Keys.REQUEST_SCHEMA_RE_KEY);
return MessageFormat.format(messageFormat, sender, receiver, text,(re != null ? String.format("RE:%s", re):""), errorMessage);
}

public String escape(String message) {
return message
.replaceAll("&", "&amp;")
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/ca/bc/gov/iamp/bcparis/FakeCData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ca.bc.gov.iamp.bcparis;

public class FakeCData {

public static String SAMPLE_DRIVER_DL = "<![CDATA[SN:M00001-0001 MT:DUF MSID:BRKR-190515-20:02:07 FROM:BC41127 TO:BC41027 TEXT:DL:3559874\n]]>";
public static String SAMPLE_DRIVER_MULTIPLE_PARAMS = "<![CDATA[SN:M00001-0001 MT:DUF MSID:BRKR-190515-20:02:07 FROM:BC41127 TO:BC41027 TEXT:BCPARIS Diagnostic Test qwe20190827173834DL:3559874 SNME:NEWMAN/G1:OLDSON/G2:MIKE/DOB:19900214\n]]>";
public static String SAMPLE_DRIVER_SNME = "<![CDATA[SN:M00001-0001 MT:MUF MSID:BRKR-190515-20:05:48 FROM:BC41127TO:BC41027TEXT:RE: 0509\nHC BC40940\nBC41027\nSNME:NEWMAN/G1:OLDSON/G2:MIKE/DOB:19900214\n\n2019051520054820190515200548]]>";
public static String SAMPLE_INVALID_DRIVER = "<![CDATA[SN:M00001-0001 MT:DUF MSID:BRKR-190515-20:02:07 FROM:BC41127 TO:BC41027 TEXT:\n]]>";
public static String SAMPLE_INVALID_VEHICLE = "<![CDATA[SN:M00001-0001 MT:MUF MSID:BRKR-190515-20:02:04 FROM:BC41127 TO:BC41028 TEXT:RE: 8261\n" +
"HC BC11422\n" +
"BC41028\n" +
"]]>";
public static String SAMPLE_POR = "<![CDATA[SN:M00001-0001 MT:MUF MSID:BRKR-190515-20:05:48 FROM:BC41127TO:BC41029TEXT:RE: 0509\nHC BC40940\nBC41027\nSNME:WISKIN/G1:TOMAS/G2:GEORGE/G3:ALPHONSE/DOB:20050505\n\n2019051520054820190515200548]]>";
public static String SAMPLE_SATELITTE = "<![CDATA[SN:M00001-0001 MT:MUF MSID:BRKR-190820-16:26:19\n" +
"FROM:BC41127\n" +
"TO:BC41027\n" +
"TEXT:BCPARIS Diagnostic Test qwe20190820202619REG:2156746\n" +
" \n" +
"SNME:SMITH/G1:JOHN/\n" +
" \n" +
"2019082020261920190820202619\n" +
"]]>";
public static String SAMPLE_SATELITTE_ROUND_TRIP = "<![CDATA[SN:M00001-0001 MT:MUF MSID:BRKR-190820-16:26:19\n" +
"FROM:BC41127\n" +
"TO:BC41127\n" +
"TEXT:BCPARIS Diagnostic Test qwe20190820202619REG:2156746\n" +
" \n" +
"SNME:SMITH/G1:JOHN/\n" +
" \n" +
"2019082020261920190820202619\n" +
"]]>";
public static String SAMPLE_VEHICLE_LIC = "<![CDATA[SN:M00001-0001 MT:MUF MSID:BRKR-190515-20:02:04 FROM:BC41127 TO:BC41028 TEXT:RE: 8261\n" +
"HC BC11422 \n" +
"BC41028 \n" +
"LIC:PN890H\n" +
"\n" +
"2019051520020420190515200204\n" +
"\n" +
"]]>";
public static String SAMPLE_VEHICLE_MULTIPLE_PARAMS = "<![CDATA[SN:M00001-0001 MT:MUF MSID:BRKR-190515-20:09:47 FROM:BC41127 TO:BC41028 TEXT:BCPARIS Diagnostic Test qwe20190827173834\n" +
"\n" +
"LIC:233AWB/H/LIC:GVW143/H/LIC:007FJR/H/LIC:JXX477/REG:957167/VIN:1GNEL19W1XB163160/VIN:163160/P:Y/VIN:163160/P:Y/RSVP:16\n" +
"\n" +
"2019082717383420190827173834\n" +
"\n" +
"]]>";
public static String SAMPLE_VEHICLE_RNS = "<![CDATA[SN:M00001-0001 MT:DUF MSID:BRKR-190515-20:02:03 FROM:BC41127 TO:BC41028 SUBJ:AB5184 FOR ACCD ENTRY TEXT:\n" +
"RNS:845513634081303/\n" +
"]]>";
public static String SAMPLE_VEHICLE_RVL = "<![CDATA[SN:M00001-0001 MT:DUF MSID:BRKR-190515-20:02:03 FROM:BC41127 TO:BC41028 SUBJ:AB5184 FOR ACCD ENTRY TEXT:\n" +
"RVL:845513634081303/\n" +
"]]>";
public static String SAMPLE_VEHICLE_VIN = "<![CDATA[SN:M00001-0001 MT:MUF MSID:BRKR-190515-20:09:47 FROM:BC41127 TO:BC41028 TEXT:RE: 2505\n" +
"HC BC93181 \n" +
"BC41028 \n" +
"VIN:1FTEW1EF3GKF29092\n" +
"\n" +
"2019051520094620190515200946\n" +
"\n" +
"]]>";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ca.bc.gov.iamp.bcparis.message;

import ca.bc.gov.iamp.bcparis.Keys;
import ca.bc.gov.iamp.bcparis.model.message.body.Body;
import ca.bc.gov.iamp.bcparis.util.RegexTokenizer;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;

public class MessageUtilsEdgeCaseGetValueTest {

@Test(expected = IllegalArgumentException.class)
public void WithTokenNotKnownTokenShouldThrowException() {
MessageUtils.GetValue("A message", "token");
}


@Test
public void WithEmptyStringShouldReturnEmptyString() {
String result = MessageUtils.GetValue("", Keys.REQUEST_SCHEMA_FROM_KEY);
Assert.assertNull(result);
}

@Test
public void WithNullStringShouldReturnNullString() {
String result = MessageUtils.GetValue(null, Keys.REQUEST_SCHEMA_FROM_KEY);
Assert.assertNull(result);
}


}
Loading

0 comments on commit 82611d0

Please sign in to comment.