Skip to content

Commit 0a474e2

Browse files
committed
Refactored and extended test of logical evaluator
1 parent 1a0fa85 commit 0a474e2

File tree

1 file changed

+41
-28
lines changed
  • statistics-generator/statistics-generator-impl/src/test/java/pl/edu/icm/coansys/statisticsgenerator/filters

1 file changed

+41
-28
lines changed

statistics-generator/statistics-generator-impl/src/test/java/pl/edu/icm/coansys/statisticsgenerator/filters/LogicalEvaluatorTest.java

+41-28
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* You should have received a copy of the GNU Affero General Public License
1616
* along with CoAnSys. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
1918
package pl.edu.icm.coansys.statisticsgenerator.filters;
2019

2120
import java.util.HashMap;
@@ -24,8 +23,10 @@
2423
import static org.testng.Assert.*;
2524
import pl.edu.icm.coansys.models.StatisticsProtos;
2625
import pl.edu.icm.coansys.statisticsgenerator.operationcomponents.FilterComponent;
26+
import pl.edu.icm.coansys.statisticsgenerator.operationcomponents.FilterContains;
2727
import pl.edu.icm.coansys.statisticsgenerator.operationcomponents.FilterEq;
2828
import pl.edu.icm.coansys.statisticsgenerator.operationcomponents.FilterNotEmpty;
29+
import pl.edu.icm.coansys.statisticsgenerator.operationcomponents.FilterTwoFieldsEqual;
2930

3031
/**
3132
*
@@ -34,39 +35,51 @@
3435
public class LogicalEvaluatorTest {
3536

3637
@Test
37-
public void testEvaluator() {
38-
Map<String, FilterComponent> components = new HashMap<String, FilterComponent>();
39-
FilterComponent fc1 = new FilterNotEmpty();
40-
fc1.setup("bookId");
41-
components.put("book", fc1);
42-
FilterComponent fc2 = new FilterEq();
43-
fc2.setup("bookId", "book-03426");
44-
components.put("book26", fc2);
45-
FilterComponent fc3 = new FilterEq();
46-
fc3.setup("bookId", "book-03427");
47-
components.put("book27", fc3);
38+
public void testEvaluator() throws InstantiationException, IllegalAccessException {
4839

49-
50-
InputFilter ifilter1 = new InputFilter("book", components);
51-
InputFilter ifilter2 = new InputFilter("book and book26", components);
52-
InputFilter ifilter3 = new InputFilter("book and book27", components);
53-
InputFilter ifilter4 = new InputFilter("not book or not book27", components);
54-
InputFilter ifilter5 = new InputFilter("(book27 and book26) or book", components);
55-
5640
StatisticsProtos.InputEntry.Builder ieb = StatisticsProtos.InputEntry.newBuilder();
5741
ieb.setLabel("testInputEntry");
42+
addKV(ieb, "bookId", "book-03426");
43+
addKV(ieb, "elId", "book-03426");
44+
addKV(ieb, "authorId", "author-571411");
45+
StatisticsProtos.InputEntry ie = ieb.build();
46+
47+
Map<String, FilterComponent> components = new HashMap<String, FilterComponent>();
48+
addFilterComponent(components, FilterNotEmpty.class, "book", "bookId");
49+
addFilterComponent(components, FilterEq.class, "book26", "bookId", "book-03426");
50+
addFilterComponent(components, FilterEq.class, "book27", "bookId", "book-03427");
51+
addFilterComponent(components, FilterContains.class, "auth1411", "authorId", "1411");
52+
addFilterComponent(components, FilterTwoFieldsEqual.class, "elbook", "elId", "bookId");
53+
54+
checkFilter("book", components, ie, true);
55+
checkFilter("book and book26", components, ie, true);
56+
checkFilter("book and book27", components, ie, false);
57+
checkFilter("not book or not book27", components, ie, true);
58+
checkFilter("(book27 and book26) or book", components, ie, true);
59+
checkFilter("not book or (auth1411 and elbook)", components, ie, true);
60+
}
61+
62+
private static void addKV(StatisticsProtos.InputEntry.Builder ieb, String key, String value) {
5863
StatisticsProtos.KeyValue.Builder kvb = StatisticsProtos.KeyValue.newBuilder();
59-
kvb.setKey("bookId");
60-
kvb.setValue("book-03426");
64+
kvb.setKey(key);
65+
kvb.setValue(value);
6166
ieb.addField(kvb);
62-
StatisticsProtos.InputEntry ie = ieb.build();
67+
}
6368

64-
65-
assertTrue(ifilter1.filter(ie));
66-
assertTrue(ifilter2.filter(ie));
67-
assertFalse(ifilter3.filter(ie));
68-
assertTrue(ifilter4.filter(ie));
69-
assertTrue(ifilter5.filter(ie));
69+
private static void addFilterComponent(Map<String, FilterComponent> components, Class<? extends FilterComponent> componentCls,
70+
String label, String... parameters) throws InstantiationException, IllegalAccessException {
71+
FilterComponent fc = componentCls.newInstance();
72+
fc.setup(parameters);
73+
components.put(label, fc);
7074
}
7175

76+
private static void checkFilter(String formula, Map<String, FilterComponent> components,
77+
StatisticsProtos.InputEntry ie, boolean expectedResult) {
78+
InputFilter ifilter = new InputFilter(formula, components);
79+
if (expectedResult) {
80+
assertTrue(ifilter.filter(ie));
81+
} else {
82+
assertFalse(ifilter.filter(ie));
83+
}
84+
}
7285
}

0 commit comments

Comments
 (0)