JAL- 2835 support filter on nested attribute keys
[jalview.git] / test / jalview / util / matcher / KeyedMatcherSetTest.java
1 package jalview.util.matcher;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertSame;
6 import static org.testng.Assert.assertTrue;
7
8 import java.util.Iterator;
9 import java.util.function.Function;
10
11 import org.testng.annotations.Test;
12
13 public class KeyedMatcherSetTest
14 {
15   @Test(groups = "Functional")
16   public void testMatches()
17   {
18     /*
19      * a numeric matcher - MatcherTest covers more conditions
20      */
21     KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
22     KeyedMatcherSetI kms = new KeyedMatcherSet();
23     kms.and(km);
24     assertTrue(kms.matches(key -> "-2"));
25     assertTrue(kms.matches(key -> "-1"));
26     assertFalse(kms.matches(key -> "-3"));
27     assertFalse(kms.matches(key -> ""));
28     assertFalse(kms.matches(key -> "junk"));
29     assertFalse(kms.matches(key -> null));
30
31     /*
32      * a string pattern matcher
33      */
34     km = new KeyedMatcher(Condition.Contains, "Cat", "AF");
35     kms = new KeyedMatcherSet();
36     kms.and(km);
37     assertTrue(kms
38             .matches(key -> "AF".equals(key[0]) ? "raining cats and dogs"
39             : "showers"));
40   }
41
42   @Test(groups = "Functional")
43   public void testAnd()
44   {
45     // condition1: AF value contains "dog" (matches)
46     KeyedMatcherI km1 = new KeyedMatcher(Condition.Contains, "dog", "AF");
47     // condition 2: CSQ value does not contain "how" (does not match)
48     KeyedMatcherI km2 = new KeyedMatcher(Condition.NotContains, "how",
49             "CSQ");
50
51     Function<String[], String> vp = key -> "AF".equals(key[0])
52             ? "raining cats and dogs"
53             : "showers";
54     assertTrue(km1.matches(vp));
55     assertFalse(km2.matches(vp));
56
57     KeyedMatcherSetI kms = new KeyedMatcherSet();
58     assertTrue(kms.matches(vp)); // if no conditions, then 'all' pass
59     kms.and(km1);
60     assertTrue(kms.matches(vp));
61     kms.and(km2);
62     assertFalse(kms.matches(vp));
63   }
64
65   @Test(groups = "Functional")
66   public void testToString()
67   {
68     KeyedMatcherI km1 = new KeyedMatcher(Condition.LT, 1.2f, "AF");
69     assertEquals(km1.toString(), "AF < 1.2");
70
71     KeyedMatcher km2 = new KeyedMatcher(Condition.NotContains, "path",
72             "CLIN_SIG");
73     assertEquals(km2.toString(), "CLIN_SIG Does not contain 'PATH'");
74
75     /*
76      * AND them
77      */
78     KeyedMatcherSetI kms = new KeyedMatcherSet();
79     assertEquals(kms.toString(), "");
80     kms.and(km1);
81     assertEquals(kms.toString(), "(AF < 1.2)");
82     kms.and(km2);
83     assertEquals(kms.toString(),
84             "(AF < 1.2) AND (CLIN_SIG Does not contain 'PATH')");
85
86     /*
87      * OR them
88      */
89     kms = new KeyedMatcherSet();
90     assertEquals(kms.toString(), "");
91     kms.or(km1);
92     assertEquals(kms.toString(), "(AF < 1.2)");
93     kms.or(km2);
94     assertEquals(kms.toString(),
95             "(AF < 1.2) OR (CLIN_SIG Does not contain 'PATH')");
96   }
97
98   @Test(groups = "Functional")
99   public void testOr()
100   {
101     // condition1: AF value contains "dog" (matches)
102     KeyedMatcherI km1 = new KeyedMatcher(Condition.Contains, "dog", "AF");
103     // condition 2: CSQ value does not contain "how" (does not match)
104     KeyedMatcherI km2 = new KeyedMatcher(Condition.NotContains, "how",
105             "CSQ");
106
107     Function<String[], String> vp = key -> "AF".equals(key[0])
108             ? "raining cats and dogs"
109             : "showers";
110     assertTrue(km1.matches(vp));
111     assertFalse(km2.matches(vp));
112
113     KeyedMatcherSetI kms = new KeyedMatcherSet();
114     kms.or(km2);
115     assertFalse(kms.matches(vp));
116     kms.or(km1);
117     assertTrue(kms.matches(vp));
118   }
119
120   @Test(groups = "Functional")
121   public void testIsEmpty()
122   {
123     KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "AF");
124     KeyedMatcherSetI kms = new KeyedMatcherSet();
125     assertTrue(kms.isEmpty());
126     kms.and(km);
127     assertFalse(kms.isEmpty());
128   }
129
130   @Test(groups = "Functional")
131   public void testGetMatchers()
132   {
133     KeyedMatcherSetI kms = new KeyedMatcherSet();
134
135     /*
136      * empty iterable:
137      */
138     Iterator<KeyedMatcherI> iterator = kms.getMatchers().iterator();
139     assertFalse(iterator.hasNext());
140
141     /*
142      * one matcher:
143      */
144     KeyedMatcherI km1 = new KeyedMatcher(Condition.GE, -2F, "AF");
145     kms.and(km1);
146     iterator = kms.getMatchers().iterator();
147     assertSame(km1, iterator.next());
148     assertFalse(iterator.hasNext());
149
150     /*
151      * two matchers:
152      */
153     KeyedMatcherI km2 = new KeyedMatcher(Condition.LT, 8F, "AF");
154     kms.and(km2);
155     iterator = kms.getMatchers().iterator();
156     assertSame(km1, iterator.next());
157     assertSame(km2, iterator.next());
158     assertFalse(iterator.hasNext());
159   }
160
161   /**
162    * Tests for the 'compound attribute' key i.e. where first key's value is a map
163    * from which we take the value for the second key, e.g. CSQ : Consequence
164    */
165   @Test(groups = "Functional")
166   public void testMatches_compoundKey()
167   {
168     /*
169      * a numeric matcher - MatcherTest covers more conditions
170      */
171     KeyedMatcherI km = new KeyedMatcher(Condition.GE, -2F, "CSQ",
172             "Consequence");
173     KeyedMatcherSetI kms = new KeyedMatcherSet();
174     kms.and(km);
175     assertTrue(kms.matches(key -> "-2"));
176     assertTrue(kms.matches(key -> "-1"));
177     assertFalse(kms.matches(key -> "-3"));
178     assertFalse(kms.matches(key -> ""));
179     assertFalse(kms.matches(key -> "junk"));
180     assertFalse(kms.matches(key -> null));
181   
182     /*
183      * a string pattern matcher
184      */
185     km = new KeyedMatcher(Condition.Contains, "Cat", "CSQ", "Consequence");
186     kms = new KeyedMatcherSet();
187     kms.and(km);
188     assertTrue(kms.matches(key -> "csq".equalsIgnoreCase(key[0])
189             && "Consequence".equalsIgnoreCase(key[1])
190                     ? "raining cats and dogs"
191                     : "showers"));
192   }
193 }