Skip to content

Commit

Permalink
Merge pull request #68 from bcgov/bugfix/jira-578
Browse files Browse the repository at this point in the history
Fix for failed query bug
  • Loading branch information
alexjoybc authored Dec 20, 2019
2 parents 68dbc4a + db949b9 commit 4a8e5ed
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 36 deletions.
9 changes: 1 addition & 8 deletions src/main/java/ca/bc/gov/iamp/bcparis/api/MessageApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,9 @@ private ResponseEntity<Object> message( @RequestBody Layer7Message message ){

context.setRequestObject(message);
Object response = null;
try {
response = processor.processMessage(message);
} catch (InvalidMessage e) {
String respMessage = String.format("%s %s",message.getEnvelope().getBody().getMsgFFmt(),"Unable to parse/formatting error");

message.getEnvelope().getBody().setMsgFFmt(respMessage);
response = message;
}
response = processor.processMessage(message);


return ResponseEntity.ok(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,29 @@ public class DriverProcessor implements DatagramProcessor{

public Layer7Message process(Layer7Message message) {
log.info("Processing Driver message.");

Body body = message.getEnvelope().getBody();

List<IMSRequest> requests = createIMSContent(message);

if (requests.isEmpty()) {
message.getEnvelope().getBody().setMsgFFmt(messageService.buildResponse(message.getEnvelope().getBody(), "Unable to parse/formatting error"));
log.warn("Processing Driver: Unable to parse/formatting error");
return message;
}
try {

List<String> responseParsed = requests.parallelStream()
.map(request -> icbcRepository.requestDetails(message, request))
.map( icbcResponse -> messageService.parseResponse(icbcResponse))
.collect(Collectors.toList());

final String response = String.join("\n\n", responseParsed);
final String msgFFmt = messageService.buildResponse(body, response);
body.setMsgFFmt(msgFFmt);
message.getEnvelope().getBody().setMsgFFmt(messageService.buildResponse(message.getEnvelope().getBody(), String.join("\n\n", responseParsed)));

log.info("Driver message processing completed.");
return message;

}catch (ICBCRestException e) {
String content = messageService.parseResponseError(e.getResponseContent());
content = messageService.parseResponse(content);
content = messageService.buildResponse(body, content);
body.setMsgFFmt(content);
content = messageService.buildResponse(message.getEnvelope().getBody(), content);
message.getEnvelope().getBody().setMsgFFmt(content);
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,31 @@ public class VehicleProcessor implements DatagramProcessor{

public Layer7Message process(Layer7Message message) {
log.info("Processing Vehicle message.");

Body body = message.getEnvelope().getBody();

List<IMSRequest> requests = createIMSContent(message);

if (requests.isEmpty()) {
message.getEnvelope().getBody().setMsgFFmt(messageService.buildResponse(message.getEnvelope().getBody(), "Unable to parse/formatting error"));
log.warn("Processing Vehicle: Unable to parse/formatting error");
return message;
}
try {

List<String> responseParsed = requests.parallelStream()
.map(request -> icbcRepository.requestDetails(message, request))
.map( icbcResponse -> messageService.parseResponse(icbcResponse))
.collect(Collectors.toList());

final String response = String.join("\n\n", responseParsed);
final String msgFFmt = messageService.buildResponse(body, response);

body.setMsgFFmt(msgFFmt);


message.getEnvelope().getBody().setMsgFFmt(messageService.buildResponse(message.getEnvelope().getBody(), String.join("\n\n", responseParsed)));

log.info("Vehicle message processing completed.");
return message;

}catch (ICBCRestException e) {
//TODO: This is for testing and the test should be updated to expect an exception
String content = messageService.parseResponseError(e.getResponseContent());
content = messageService.parseResponse(content);
content = messageService.buildResponse(body, content);
body.setMsgFFmt(content);
content = messageService.buildResponse(message.getEnvelope().getBody(), content);
message.getEnvelope().getBody().setMsgFFmt(content);
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ public List<String> getQueryAttributesList(Body body, List<String> validAttribut
result.addAll(body.getAttributeList(paramName));
}

if(result.size() > 0)
return result;

throw new InvalidMessage("No valid query. Valid params: " + validAttributes);
return result;
}

public String buildResponse(final Body body, final String icbcResponse) {
public String buildResponse(final Body body, final String message) {
final String receiver = this.parseResponse(body.getCDATAAttribute("FROM")); //This becomes the receiver of the message
final String sender = this.parseResponse(body.getCDATAAttribute("TO")); //This will become the sender
final String text = this.parseResponse(body.getCDATAAttribute("TEXT"));
Expand All @@ -45,7 +42,7 @@ public String buildResponse(final Body body, final String icbcResponse) {
.replace("${to}", receiver)
.replace("${text}", text)
.replace("${re}", body.containAttribute("RE") ? "RE:" + re : "")
.replace("${icbc_response}", icbcResponse);
.replace("${icbc_response}", message);
}

public String escape(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ public void create_query_params_list_success() {
int count = StringUtils.countOccurrencesOf(message.getEnvelope().getBody().getMsgFFmt(), "TEXT:BCPARIS Diagnostic Test qwe20190827173834");
Assert.assertEquals(1, count);
}

@Test
public void invalid_query() {
final Layer7Message message = BCPARISTestUtil.getInvalidDriverQuery();

ArgumentCaptor<IMSRequest> argument = ArgumentCaptor.forClass(IMSRequest.class);
Mockito.when(icbc.requestDetails(Mockito.any(Layer7Message.class), argument.capture())).thenReturn("ICBC Response");

processor.process(message);

int count = StringUtils.countOccurrencesOf(message.getEnvelope().getBody().getMsgFFmt(), "Unable to parse/formatting error");
Assert.assertEquals(1, count);
}

@Test
public void error_during_ICBC_call() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ public void create_query_params_list_success() {
Assert.assertEquals(8, countICBCResponse);
}

@Test
public void invalid_query() {
final Layer7Message message = BCPARISTestUtil.getInvalidVehicleQuery();

ArgumentCaptor<IMSRequest> argument = ArgumentCaptor.forClass(IMSRequest.class);
Mockito.when(icbc.requestDetails(Mockito.any(Layer7Message.class), argument.capture())).thenReturn("ICBC Response");

processor.process(message);

int count = StringUtils.countOccurrencesOf(message.getEnvelope().getBody().getMsgFFmt(), "Unable to parse/formatting error");
Assert.assertEquals(1, count);
}

@Test
public void error_during_ICBC_call() {
String errorContent = TestUtil.readFile("ICBC/response-error-2.xml");
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/test/util/BCPARISTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ public static Layer7Message getMessageDriverDL() {
public static Layer7Message getMessageDriverMultipleParams() {
return getMessage("cdata/sample-driver-multiple-params");
}


public static Layer7Message getInvalidDriverQuery() {
return getMessage("cdata/sample-invalid-driver");
}

public static Layer7Message getInvalidVehicleQuery() {
return getMessage("cdata/sample-invalid-vehicle");
}


public static Layer7Message getMessageVehicleVIN() {
return getMessage("cdata/sample-vehicle-vin");
}
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/cdata/sample-invalid-driver
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<![CDATA[SN:M00001-0001 MT:DUF MSID:BRKR-190515-20:02:07 FROM:BC41127 TO:BC41027 TEXT:\n]]>
4 changes: 4 additions & 0 deletions src/test/resources/cdata/sample-invalid-vehicle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<![CDATA[SN:M00001-0001 MT:MUF MSID:BRKR-190515-20:02:04 FROM:BC41127 TO:BC41028 TEXT:RE: 8261
HC BC11422
BC41028
]]>

0 comments on commit 4a8e5ed

Please sign in to comment.