|
15 | 15 | * You should have received a copy of the GNU Affero General Public License
|
16 | 16 | * along with CoAnSys. If not, see <http://www.gnu.org/licenses/>.
|
17 | 17 | */
|
18 |
| - |
19 | 18 | package pl.edu.icm.coansys.statisticsgenerator.filters;
|
20 | 19 |
|
21 | 20 | import java.util.HashMap;
|
|
24 | 23 | import static org.testng.Assert.*;
|
25 | 24 | import pl.edu.icm.coansys.models.StatisticsProtos;
|
26 | 25 | import pl.edu.icm.coansys.statisticsgenerator.operationcomponents.FilterComponent;
|
| 26 | +import pl.edu.icm.coansys.statisticsgenerator.operationcomponents.FilterContains; |
27 | 27 | import pl.edu.icm.coansys.statisticsgenerator.operationcomponents.FilterEq;
|
28 | 28 | import pl.edu.icm.coansys.statisticsgenerator.operationcomponents.FilterNotEmpty;
|
| 29 | +import pl.edu.icm.coansys.statisticsgenerator.operationcomponents.FilterTwoFieldsEqual; |
29 | 30 |
|
30 | 31 | /**
|
31 | 32 | *
|
|
34 | 35 | public class LogicalEvaluatorTest {
|
35 | 36 |
|
36 | 37 | @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 { |
48 | 39 |
|
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 |
| - |
56 | 40 | StatisticsProtos.InputEntry.Builder ieb = StatisticsProtos.InputEntry.newBuilder();
|
57 | 41 | 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) { |
58 | 63 | StatisticsProtos.KeyValue.Builder kvb = StatisticsProtos.KeyValue.newBuilder();
|
59 |
| - kvb.setKey("bookId"); |
60 |
| - kvb.setValue("book-03426"); |
| 64 | + kvb.setKey(key); |
| 65 | + kvb.setValue(value); |
61 | 66 | ieb.addField(kvb);
|
62 |
| - StatisticsProtos.InputEntry ie = ieb.build(); |
| 67 | + } |
63 | 68 |
|
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); |
70 | 74 | }
|
71 | 75 |
|
| 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 | + } |
72 | 85 | }
|
0 commit comments