Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
x0l08og committed Nov 8, 2024
1 parent 6c7315f commit 4e6d505
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 1 deletion.
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) {
// handled before as default
} 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

0 comments on commit 4e6d505

Please sign in to comment.