Skip to content

Commit 7a65c8d

Browse files
author
Mike Davis
committed
Add missing unit tests.
1 parent 650d6a2 commit 7a65c8d

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

core-api/src/main/java/com/optimizely/ab/config/audience/match/ExactMatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Boolean eval(Object conditionValue, Object attributeValue) throws Unexpec
3838
throw new UnexpectedValueTypeException();
3939
}
4040

41-
if (attributeValue.getClass() != conditionValue.getClass()) {
41+
if (attributeValue == null || attributeValue.getClass() != conditionValue.getClass()) {
4242
return null;
4343
}
4444

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
*
3+
* Copyright 2020, Optimizely and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.optimizely.ab.config.audience.match;
18+
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
import java.util.Arrays;
23+
import java.util.Collections;
24+
import java.util.List;
25+
26+
import static org.junit.Assert.*;
27+
28+
public class ExactMatchTest {
29+
30+
private ExactMatch match;
31+
private static final List<Object> INVALIDS = Collections.unmodifiableList(Arrays.asList(new byte[0], new Object(), null));
32+
33+
@Before
34+
public void setUp() {
35+
match = new ExactMatch();
36+
}
37+
38+
@Test
39+
public void testInvalidConditionValues() {
40+
for (Object invalid : INVALIDS) {
41+
try {
42+
match.eval(invalid, "valid");
43+
fail("should have raised exception");
44+
} catch (UnexpectedValueTypeException e) {
45+
//pass
46+
}
47+
}
48+
}
49+
50+
@Test
51+
public void testMismatchClasses() throws Exception {
52+
assertNull(match.eval(false, "false"));
53+
assertNull(match.eval("false", null));
54+
}
55+
56+
@Test
57+
public void testStringMatch() throws Exception {
58+
assertEquals(Boolean.TRUE, match.eval("", ""));
59+
assertEquals(Boolean.TRUE, match.eval("true", "true"));
60+
assertEquals(Boolean.FALSE, match.eval("true", "false"));
61+
}
62+
63+
@Test
64+
public void testBooleanMatch() throws Exception {
65+
assertEquals(Boolean.TRUE, match.eval(true, true));
66+
assertEquals(Boolean.TRUE, match.eval(false, false));
67+
assertEquals(Boolean.FALSE, match.eval(true, false));
68+
}
69+
70+
@Test
71+
public void testNumberMatch() throws UnexpectedValueTypeException {
72+
assertEquals(Boolean.TRUE, match.eval(1, 1));
73+
assertEquals(Boolean.TRUE, match.eval(1L, 1L));
74+
assertEquals(Boolean.TRUE, match.eval(1.0, 1.0));
75+
assertEquals(Boolean.TRUE, match.eval(1, 1.0));
76+
assertEquals(Boolean.TRUE, match.eval(1L, 1.0));
77+
78+
assertEquals(Boolean.FALSE, match.eval(1, 2));
79+
assertEquals(Boolean.FALSE, match.eval(1L, 2L));
80+
assertEquals(Boolean.FALSE, match.eval(1.0, 2.0));
81+
assertEquals(Boolean.FALSE, match.eval(1, 1.1));
82+
assertEquals(Boolean.FALSE, match.eval(1L, 1.1));
83+
}
84+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
*
3+
* Copyright 2020, Optimizely and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.optimizely.ab.config.audience.match;
18+
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
import java.util.Arrays;
23+
import java.util.Collections;
24+
import java.util.List;
25+
26+
import static org.junit.Assert.*;
27+
28+
public class SubstringMatchTest {
29+
30+
private SubstringMatch match;
31+
private static final List<Object> INVALIDS = Collections.unmodifiableList(Arrays.asList(new byte[0], new Object(), null));
32+
33+
@Before
34+
public void setUp() {
35+
match = new SubstringMatch();
36+
}
37+
38+
@Test
39+
public void testInvalidConditionValues() {
40+
for (Object invalid : INVALIDS) {
41+
try {
42+
match.eval(invalid, "valid");
43+
fail("should have raised exception");
44+
} catch (UnexpectedValueTypeException e) {
45+
//pass
46+
}
47+
}
48+
}
49+
50+
@Test
51+
public void testInvalidAttributesValues() throws UnexpectedValueTypeException {
52+
for (Object invalid : INVALIDS) {
53+
assertNull(match.eval("valid", invalid));
54+
}
55+
}
56+
57+
@Test
58+
public void testStringMatch() throws Exception {
59+
assertEquals(Boolean.TRUE, match.eval("", "any"));
60+
assertEquals(Boolean.TRUE, match.eval("same", "same"));
61+
assertEquals(Boolean.TRUE, match.eval("a", "ab"));
62+
assertEquals(Boolean.FALSE, match.eval("ab", "a"));
63+
assertEquals(Boolean.FALSE, match.eval("a", "b"));
64+
}
65+
}

0 commit comments

Comments
 (0)