From 7677d274428d08384cedd0536a23713b5de2d112 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Thu, 30 Aug 2018 13:08:59 +0200 Subject: [PATCH] [Fix] Fixed behavior of IsIn and Contains ReportFilters --- .../policy/GreaterThanReportFilter.java | 4 +- .../report/policy/IsInReportFilter.java | 44 ++++++++++++++++ .../report/policy/LessThanReportFilter.java | 4 +- .../report/policy/ReportFilterPolicy.java | 36 +++++-------- .../li/strolch/report/GenericReportTest.java | 51 +++++++++---------- .../reporttest/config/StrolchPolicies.xml | 3 ++ .../reporttest/data/StrolchModel.xml | 18 +++++-- 7 files changed, 103 insertions(+), 57 deletions(-) diff --git a/li.strolch.service/src/main/java/li/strolch/report/policy/GreaterThanReportFilter.java b/li.strolch.service/src/main/java/li/strolch/report/policy/GreaterThanReportFilter.java index 4a3531080..5b6553445 100644 --- a/li.strolch.service/src/main/java/li/strolch/report/policy/GreaterThanReportFilter.java +++ b/li.strolch.service/src/main/java/li/strolch/report/policy/GreaterThanReportFilter.java @@ -13,8 +13,8 @@ public class GreaterThanReportFilter extends ReportFilterPolicy { @Override protected boolean filter(Object left, Object right, boolean negate) { if (negate) - return ObjectHelper.compare(left, right, false) > 0; - else return ObjectHelper.compare(right, left, false) > 0; + else + return ObjectHelper.compare(left, right, false) > 0; } } diff --git a/li.strolch.service/src/main/java/li/strolch/report/policy/IsInReportFilter.java b/li.strolch.service/src/main/java/li/strolch/report/policy/IsInReportFilter.java index 887b0fa69..8ca7203c1 100644 --- a/li.strolch.service/src/main/java/li/strolch/report/policy/IsInReportFilter.java +++ b/li.strolch.service/src/main/java/li/strolch/report/policy/IsInReportFilter.java @@ -1,6 +1,13 @@ package li.strolch.report.policy; +import static java.util.stream.Collectors.toList; + +import java.util.Arrays; +import java.util.Date; + import li.strolch.agent.api.ComponentContainer; +import li.strolch.model.StrolchValueType; +import li.strolch.model.parameter.Parameter; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.utils.ObjectHelper; @@ -10,6 +17,43 @@ public class IsInReportFilter extends ReportFilterPolicy { super(container, tx); } + @Override + public boolean filter(Object value) { + + Object left; + if (value instanceof Date) { + + if (this.right == null) + this.right = Arrays.stream(this.filterValue.split(",")).map(String::trim) + .map(this::parseFilterValueToDate).collect(toList()); + + left = value; + + } else if (value instanceof Parameter) { + + Parameter parameter = (Parameter) value; + if (this.right == null) { + StrolchValueType valueType = parameter.getValueType(); + if (valueType == StrolchValueType.DATE) + this.right = Arrays.stream(this.filterValue.split(",")).map(String::trim) + .map(this::parseFilterValueToDate).collect(toList()); + else + this.right = Arrays.stream(this.filterValue.split(",")).map(String::trim) + .map(valueType::parseValue).collect(toList()); + } + + left = parameter.getValue(); + + } else { + if (this.right == null) + this.right = Arrays.stream(this.filterValue.split(",")).map(String::trim).collect(toList()); + + left = value.toString(); + } + + return filter(left, this.right, this.negate); + } + @Override protected boolean filter(Object left, Object right, boolean negate) { if (negate) diff --git a/li.strolch.service/src/main/java/li/strolch/report/policy/LessThanReportFilter.java b/li.strolch.service/src/main/java/li/strolch/report/policy/LessThanReportFilter.java index 8ca6c1696..62dc65fd3 100644 --- a/li.strolch.service/src/main/java/li/strolch/report/policy/LessThanReportFilter.java +++ b/li.strolch.service/src/main/java/li/strolch/report/policy/LessThanReportFilter.java @@ -13,8 +13,8 @@ public class LessThanReportFilter extends ReportFilterPolicy { @Override protected boolean filter(Object left, Object right, boolean negate) { if (negate) - return ObjectHelper.compare(left, right, false) < 0; - else return ObjectHelper.compare(right, left, false) < 0; + else + return ObjectHelper.compare(left, right, false) < 0; } } diff --git a/li.strolch.service/src/main/java/li/strolch/report/policy/ReportFilterPolicy.java b/li.strolch.service/src/main/java/li/strolch/report/policy/ReportFilterPolicy.java index 9360bbce2..e7e5c1404 100644 --- a/li.strolch.service/src/main/java/li/strolch/report/policy/ReportFilterPolicy.java +++ b/li.strolch.service/src/main/java/li/strolch/report/policy/ReportFilterPolicy.java @@ -21,7 +21,7 @@ public abstract class ReportFilterPolicy extends StrolchPolicy { protected boolean negate; protected String filterValue; - protected Object left; + protected Object right; public boolean isNegate() { return this.negate; @@ -35,14 +35,6 @@ public abstract class ReportFilterPolicy extends StrolchPolicy { this.filterValue = filterValue; } - public Object getLeft() { - return this.left; - } - - public void setLeft(Object left) { - this.left = left; - } - public void init(String value) { if (value.startsWith("!")) { setNegate(true); @@ -54,38 +46,38 @@ public abstract class ReportFilterPolicy extends StrolchPolicy { public boolean filter(Object value) { - Object right; + Object left; if (value instanceof Date) { - if (this.left == null) - this.left = parseFilterValueToDate(this.filterValue); + if (this.right == null) + this.right = parseFilterValueToDate(this.filterValue); - right = value; + left = value; } else if (value instanceof Parameter) { Parameter parameter = (Parameter) value; - if (this.left == null) { + if (this.right == null) { StrolchValueType valueType = parameter.getValueType(); if (valueType == StrolchValueType.DATE) - this.left = parseFilterValueToDate(this.filterValue); + this.right = parseFilterValueToDate(this.filterValue); else - this.left = valueType.parseValue(this.filterValue); + this.right = valueType.parseValue(this.filterValue); } - right = parameter.getValue(); + left = parameter.getValue(); } else { - if (this.left == null) - this.left = this.filterValue; + if (this.right == null) + this.right = this.filterValue; - right = value.toString(); + left = value.toString(); } - return filter(this.left, right, this.negate); + return filter(left, this.right, this.negate); } - private Date parseFilterValueToDate(String filterValue) { + protected Date parseFilterValueToDate(String filterValue) { DBC.INTERIM.assertNotEmpty("filterValue must not be empty for date comparisons!", filterValue); if (!filterValue.startsWith("now")) diff --git a/li.strolch.service/src/test/java/li/strolch/report/GenericReportTest.java b/li.strolch.service/src/test/java/li/strolch/report/GenericReportTest.java index 5a6dcc6d1..9685dc282 100644 --- a/li.strolch.service/src/test/java/li/strolch/report/GenericReportTest.java +++ b/li.strolch.service/src/test/java/li/strolch/report/GenericReportTest.java @@ -1,30 +1,26 @@ package li.strolch.report; +import static java.util.stream.Collectors.toList; import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import java.time.LocalDate; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.stream.Collectors; -import li.strolch.model.StrolchElement; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - import com.google.gson.JsonObject; - +import li.strolch.model.StrolchElement; import li.strolch.model.StrolchRootElement; import li.strolch.persistence.api.StrolchTransaction; import li.strolch.privilege.model.Certificate; import li.strolch.testbase.runtime.RuntimeMock; import li.strolch.utils.collections.DateRange; import li.strolch.utils.collections.MapOfSets; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; public class GenericReportTest { @@ -172,7 +168,7 @@ public class GenericReportTest { .filter("Product", "product01") // .dateRange(dateRange) // .doReportAsJson() // - .collect(Collectors.toList()); + .collect(toList()); assertTrue(result.isEmpty()); // expect 2 slots, as in date range @@ -182,7 +178,7 @@ public class GenericReportTest { .filter("Product", "product01") // .dateRange(dateRange) // .doReportAsJson() // - .collect(Collectors.toList()); + .collect(toList()); assertEquals(2, result.size()); } } @@ -199,15 +195,14 @@ public class GenericReportTest { // expect no orders as all not in date range Report report = new Report(tx, "fromStockReport"); List result = report.filter("Product", "product01").dateRange(dateRange).doReportAsJson() - .collect(Collectors.toList()); + .collect(toList()); assertTrue(result.isEmpty()); // expect 2 orders, as in date range to = new Date(LocalDate.of(2017, 3, 1).toEpochDay() * 86400000); dateRange = new DateRange().from(from, true).to(to, false); report = new Report(tx, "fromStockReport"); - result = report.filter("Product", "product01").dateRange(dateRange).doReportAsJson() - .collect(Collectors.toList()); + result = report.filter("Product", "product01").dateRange(dateRange).doReportAsJson().collect(toList()); assertEquals(2, result.size()); // expect 4 orders, as all in date range @@ -215,7 +210,7 @@ public class GenericReportTest { dateRange = new DateRange().from(from, true).to(to, false); report = new Report(tx, "fromStockReport"); result = report.filter("Product", "product01", "product02").dateRange(dateRange).doReportAsJson() - .collect(Collectors.toList()); + .collect(toList()); assertEquals(4, result.size()); } } @@ -228,16 +223,14 @@ public class GenericReportTest { Report report = new Report(tx, "stockReport"); MapOfSets filterCriteria = report.generateFilterCriteria(); + assertFalse(filterCriteria.containsSet("Location")); + assertFalse(filterCriteria.containsSet("Slot")); assertThat(filterCriteria.getSet("Product").stream().map(StrolchElement::getId).collect(Collectors.toSet()), containsInAnyOrder("product01", "product02")); - assertThat( - filterCriteria.getSet("Location").stream().map(StrolchElement::getId).collect(Collectors.toSet()), - containsInAnyOrder("location01", "location02")); assertThat(filterCriteria.getSet("Storage").stream().map(StrolchElement::getId).collect(Collectors.toSet()), containsInAnyOrder("storage01", "storage02")); assertThat(filterCriteria.getSet("Section").stream().map(StrolchElement::getId).collect(Collectors.toSet()), containsInAnyOrder("section001", "section002")); - assertFalse(filterCriteria.containsSet("Slot")); } } @@ -247,20 +240,24 @@ public class GenericReportTest { try (StrolchTransaction tx = runtimeMock.openUserTx(certificate)) { Report report = new Report(tx, "stockReport"); - MapOfSets filterCriteria = report // - .filter("Product", "product01") // + MapOfSets filterCriteria = report.filter("Product", "product01") .generateFilterCriteria(); + // assert sequence of filter criteria is correct + List types = new ArrayList<>(filterCriteria.keySet()); + assertEquals(3, types.size()); + assertEquals("Product", types.get(0)); + assertEquals("Storage", types.get(1)); + assertEquals("Section", types.get(2)); + + assertFalse(filterCriteria.containsSet("Location")); + assertFalse(filterCriteria.containsSet("Slot")); assertThat(filterCriteria.getSet("Product").stream().map(StrolchElement::getId).collect(Collectors.toSet()), containsInAnyOrder("product01")); - assertThat( - filterCriteria.getSet("Location").stream().map(StrolchElement::getId).collect(Collectors.toSet()), - containsInAnyOrder("location01", "location02")); assertThat(filterCriteria.getSet("Storage").stream().map(StrolchElement::getId).collect(Collectors.toSet()), containsInAnyOrder("storage01", "storage02")); assertThat(filterCriteria.getSet("Section").stream().map(StrolchElement::getId).collect(Collectors.toSet()), containsInAnyOrder("section001", "section002")); - assertFalse(filterCriteria.containsSet("Slot")); } } diff --git a/li.strolch.service/src/test/resources/reporttest/config/StrolchPolicies.xml b/li.strolch.service/src/test/resources/reporttest/config/StrolchPolicies.xml index 06b1dff97..ed4382e5d 100644 --- a/li.strolch.service/src/test/resources/reporttest/config/StrolchPolicies.xml +++ b/li.strolch.service/src/test/resources/reporttest/config/StrolchPolicies.xml @@ -1,6 +1,9 @@ + + + diff --git a/li.strolch.service/src/test/resources/reporttest/data/StrolchModel.xml b/li.strolch.service/src/test/resources/reporttest/data/StrolchModel.xml index 72cf4afb9..ab5dfd508 100644 --- a/li.strolch.service/src/test/resources/reporttest/data/StrolchModel.xml +++ b/li.strolch.service/src/test/resources/reporttest/data/StrolchModel.xml @@ -15,6 +15,10 @@ + + + + @@ -37,10 +41,10 @@ - - - - + + + + @@ -184,6 +188,7 @@ + @@ -195,6 +200,7 @@ + @@ -206,6 +212,7 @@ + @@ -217,6 +224,7 @@ + @@ -228,6 +236,7 @@ + @@ -239,6 +248,7 @@ +