Occasional exception while retrieving stock card summaries

Description

An occasional issue occurs on the production server of the Angola instance after submitting physical inventory and trying to view Stock on Hand screen. The logs show the following with the GET /api/v2/stockCardSummaries as cause:

java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:899)
at java.util.TimSort.mergeAt(TimSort.java:516)
at java.util.TimSort.mergeForceCollapse(TimSort.java:457)
at java.util.TimSort.sort(TimSort.java:254)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:348)
at java.util.stream.Sink$ChainedReference.end(Sink.java:258)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.openlmis.stockmanagement.web.stockcardsummariesv2.StockCardSummariesV2DtoBuilder.build(StockCardSummariesV2DtoBuilder.java:73)
at org.openlmis.stockmanagement.web.stockcardsummariesv2.StockCardSummariesV2Controller.getStockCardSummaries(StockCardSummariesV2Controller.java:69)

 

While the reproduction steps are unknown, the cause of the problem is clear. The compareTo method of StockCardSummaryV2Dto violates the contract of the Comparable interface (more precisely sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y is not met for any canFulfillForMe of size 1 and higher).

We should fix the contract to avoid any issues with this endpoint. Additionally, automated tests should be written that verify the contract (chekc javadoc or here: https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html for contract details)

Environment

None

Assignee

Mateusz Kwiatkowski

Reporter

Mateusz Kwiatkowski

Time tracking

2h

Epic Link

Components

Sprint

None

Fix versions

Priority

Major
Configure