Skip to content
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

NIMTP-2873 Update error message for unexpected top HL #155

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@
import com.walmartlabs.x12.util.loop.X12LoopHolder;
import com.walmartlabs.x12.util.loop.X12LoopUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;

/**
* ASN 856 is the Advance Shipping Notice Used to communicate the contents of a
Expand Down Expand Up @@ -725,7 +727,7 @@ protected void doLoopParsing(List<X12Loop> loops, AsnTransactionSet asnTx) {
X12Loop firstLoop = loops.get(0);
this.parseShipmentLoop(firstLoop, asnTx);
} else {
asnTx.addX12ErrorDetailForLoop(new X12ErrorDetail("HL", null, "expected one top level Shipment HL"));
asnTx.addX12ErrorDetailForLoop(this.evaluateX12ErrorDetail(loops));
}
}

Expand All @@ -747,4 +749,32 @@ private void handleOptionalSegments(SegmentIterator segments, AsnTransactionSet

}
}

private X12ErrorDetail evaluateX12ErrorDetail(List<X12Loop> loops) {
long shipmentCount = CollectionUtils.emptyIfNull(loops)
.stream()
.filter(Shipment::isShipmentLoop)
.count();

if (shipmentCount <= 0) {
// this scenario has already handled as first HL is not a shipment it was xx
} else if (shipmentCount == 1) {
Optional<X12Loop> nonShipmentLoop = loops.stream()
.filter(loop -> !Shipment.isShipmentLoop(loop))
.findFirst();

if (nonShipmentLoop.isPresent()) {
X12Loop x12Loop = nonShipmentLoop.get();
if (StringUtils.isBlank(x12Loop.getParentHierarchicalId())) {
String invalidValue = "Missing parent loop on " + x12Loop.getCode() + " loop";
return new X12ErrorDetail(X12Loop.HIERARCHY_LOOP_ID, null, "Missing parent loop", invalidValue);
}
}
} else {
return new X12ErrorDetail(X12Loop.HIERARCHY_LOOP_ID, null, "Multiple Shipment Loops");
}

// default error message
return new X12ErrorDetail(X12Loop.HIERARCHY_LOOP_ID, null, "expected one top level Shipment HL");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,32 @@ public void test_doParse_TwoTopLevelHierarchicalLoops() {

AsnTransactionSet asnTx = (AsnTransactionSet) txSet;

// looping check
List<X12ErrorDetail> loopErrors = asnTx.getLoopingErrors();
assertEquals(1, loopErrors.size());
assertEquals("Multiple Shipment Loops", loopErrors.get(0).getIssueText());

// BSN
assertEquals("05755986", asnTx.getShipmentIdentification());
}

@Test
public void test_doParse_missingShipmentLoops() {
X12Group x12Group = new X12Group();

List<X12Segment> segments = new ArrayList<>();
// 2 O loops
segments.add(new X12Segment("ST*856*368090001"));
segments.add(new X12Segment("BSN*00*05755986*20190523*171543*0002"));
segments.add(new X12Segment("HL*1**O"));
segments.add(new X12Segment("HL*2**O"));
segments.add(new X12Segment("SE*296*368090001"));

X12TransactionSet txSet = txParser.doParse(segments, x12Group);
assertNotNull(txSet);

AsnTransactionSet asnTx = (AsnTransactionSet) txSet;

// looping check
List<X12ErrorDetail> loopErrors = asnTx.getLoopingErrors();
assertEquals(1, loopErrors.size());
Expand All @@ -218,6 +244,114 @@ public void test_doParse_TwoTopLevelHierarchicalLoops() {
assertEquals("05755986", asnTx.getShipmentIdentification());
}

@Test
public void test_doParse_orderWithoutParentLoop() {
X12Group x12Group = new X12Group();

List<X12Segment> segments = new ArrayList<>();
// O without parent
segments.add(new X12Segment("ST*856*368090001"));
segments.add(new X12Segment("BSN*00*05755986*20190523*171543*0002"));
segments.add(new X12Segment("HL*1**S"));
segments.add(new X12Segment("HL*2**O"));
segments.add(new X12Segment("SE*296*368090001"));

X12TransactionSet txSet = txParser.doParse(segments, x12Group);
assertNotNull(txSet);

AsnTransactionSet asnTx = (AsnTransactionSet) txSet;

// looping check
List<X12ErrorDetail> loopErrors = asnTx.getLoopingErrors();
assertEquals(1, loopErrors.size());
assertEquals("Missing parent loop", loopErrors.get(0).getIssueText());
assertEquals("Missing parent loop on O loop", loopErrors.get(0).getInvalidValue());

// BSN
assertEquals("05755986", asnTx.getShipmentIdentification());
}

@Test
public void test_doParse_packWithoutParentLoop() {
X12Group x12Group = new X12Group();

List<X12Segment> segments = new ArrayList<>();
// P without parent
segments.add(new X12Segment("ST*856*368090001"));
segments.add(new X12Segment("BSN*00*05755986*20190523*171543*0002"));
segments.add(new X12Segment("HL*1**S"));
segments.add(new X12Segment("HL*2**P"));
segments.add(new X12Segment("SE*296*368090001"));

X12TransactionSet txSet = txParser.doParse(segments, x12Group);
assertNotNull(txSet);

AsnTransactionSet asnTx = (AsnTransactionSet) txSet;

// looping check
List<X12ErrorDetail> loopErrors = asnTx.getLoopingErrors();
assertEquals(1, loopErrors.size());
assertEquals("Missing parent loop", loopErrors.get(0).getIssueText());
assertEquals("Missing parent loop on P loop", loopErrors.get(0).getInvalidValue());

// BSN
assertEquals("05755986", asnTx.getShipmentIdentification());
}

@Test
public void test_doParse_tareWithoutParentLoop() {
X12Group x12Group = new X12Group();

List<X12Segment> segments = new ArrayList<>();
// T without parent
segments.add(new X12Segment("ST*856*368090001"));
segments.add(new X12Segment("BSN*00*05755986*20190523*171543*0002"));
segments.add(new X12Segment("HL*1**S"));
segments.add(new X12Segment("HL*2**T"));
segments.add(new X12Segment("SE*296*368090001"));

X12TransactionSet txSet = txParser.doParse(segments, x12Group);
assertNotNull(txSet);

AsnTransactionSet asnTx = (AsnTransactionSet) txSet;

// looping check
List<X12ErrorDetail> loopErrors = asnTx.getLoopingErrors();
assertEquals(1, loopErrors.size());
assertEquals("Missing parent loop", loopErrors.get(0).getIssueText());
assertEquals("Missing parent loop on T loop", loopErrors.get(0).getInvalidValue());

// BSN
assertEquals("05755986", asnTx.getShipmentIdentification());
}

@Test
public void test_doParse_itemWithoutParentLoop() {
X12Group x12Group = new X12Group();

List<X12Segment> segments = new ArrayList<>();
// I without parent
segments.add(new X12Segment("ST*856*368090001"));
segments.add(new X12Segment("BSN*00*05755986*20190523*171543*0002"));
segments.add(new X12Segment("HL*1**S"));
segments.add(new X12Segment("HL*2**I"));
segments.add(new X12Segment("SE*296*368090001"));

X12TransactionSet txSet = txParser.doParse(segments, x12Group);
assertNotNull(txSet);

AsnTransactionSet asnTx = (AsnTransactionSet) txSet;

// looping check
List<X12ErrorDetail> loopErrors = asnTx.getLoopingErrors();
assertEquals(1, loopErrors.size());
assertEquals("Missing parent loop", loopErrors.get(0).getIssueText());
assertEquals("Missing parent loop on I loop", loopErrors.get(0).getInvalidValue());

// BSN
assertEquals("05755986", asnTx.getShipmentIdentification());
}

@Test
public void test_doParse_UnexpectedSegmentBeforeHierarchicalLoops() {
X12Group x12Group = new X12Group();
Expand Down
Loading