1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package gov.nist.secauto.metaschema.core.metapath;
28
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30
31 import gov.nist.secauto.metaschema.core.metapath.IComparison.Operator;
32 import gov.nist.secauto.metaschema.core.metapath.item.IItem;
33 import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
34 import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem;
35
36 import org.jmock.Expectations;
37 import org.jmock.Mockery;
38 import org.junit.jupiter.params.ParameterizedTest;
39 import org.junit.jupiter.params.provider.Arguments;
40 import org.junit.jupiter.params.provider.MethodSource;
41
42 import java.util.stream.Stream;
43
44 class ValueComparisonTest
45 extends ExpressionTestBase {
46
47 private static Stream<Arguments> testValueComparison() {
48 return Stream.of(
49
50 Arguments.of(IStringItem.valueOf("AbC"), Operator.EQ, IStringItem.valueOf("AbC"), IBooleanItem.TRUE),
51 Arguments.of(IStringItem.valueOf("AbC"), Operator.EQ, IStringItem.valueOf("xYz"), IBooleanItem.FALSE),
52 Arguments.of(IStringItem.valueOf("A.1"), Operator.NE, IStringItem.valueOf("A.2"), IBooleanItem.TRUE),
53 Arguments.of(IStringItem.valueOf("A.1"), Operator.NE, IStringItem.valueOf("A.1"), IBooleanItem.FALSE),
54 Arguments.of(IStringItem.valueOf("A.3"), Operator.GE, IStringItem.valueOf("A.2"), IBooleanItem.TRUE),
55 Arguments.of(IStringItem.valueOf("B\\1"), Operator.GE, IStringItem.valueOf("B\\1"), IBooleanItem.TRUE),
56 Arguments.of(IStringItem.valueOf("A.1"), Operator.GE, IStringItem.valueOf("A.2"), IBooleanItem.FALSE),
57 Arguments.of(IStringItem.valueOf("A.1@"), Operator.GT, IStringItem.valueOf("A.1"), IBooleanItem.TRUE),
58 Arguments.of(IStringItem.valueOf("X.1"), Operator.GT, IStringItem.valueOf("X.1"), IBooleanItem.FALSE),
59 Arguments.of(IStringItem.valueOf("A"), Operator.LE, IStringItem.valueOf("A.2"), IBooleanItem.TRUE),
60 Arguments.of(IStringItem.valueOf("B\\1"), Operator.LE, IStringItem.valueOf("C\\1"), IBooleanItem.TRUE),
61 Arguments.of(IStringItem.valueOf("X#"), Operator.LE, IStringItem.valueOf("X"), IBooleanItem.FALSE),
62 Arguments.of(IStringItem.valueOf("A"), Operator.LT, IStringItem.valueOf("A.2"), IBooleanItem.TRUE),
63 Arguments.of(IStringItem.valueOf("X#"), Operator.LT, IStringItem.valueOf("X"), IBooleanItem.FALSE),
64
65 Arguments.of(IBooleanItem.TRUE, Operator.EQ, IBooleanItem.TRUE, IBooleanItem.TRUE),
66 Arguments.of(IBooleanItem.FALSE, Operator.EQ, IBooleanItem.FALSE, IBooleanItem.TRUE),
67 Arguments.of(IBooleanItem.TRUE, Operator.EQ, IBooleanItem.FALSE, IBooleanItem.FALSE),
68 Arguments.of(IBooleanItem.FALSE, Operator.EQ, IBooleanItem.TRUE, IBooleanItem.FALSE),
69 Arguments.of(IBooleanItem.TRUE, Operator.NE, IBooleanItem.FALSE, IBooleanItem.TRUE),
70 Arguments.of(IBooleanItem.TRUE, Operator.NE, IBooleanItem.FALSE, IBooleanItem.TRUE),
71 Arguments.of(IBooleanItem.TRUE, Operator.NE, IBooleanItem.TRUE, IBooleanItem.FALSE),
72 Arguments.of(IBooleanItem.FALSE, Operator.NE, IBooleanItem.FALSE, IBooleanItem.FALSE),
73 Arguments.of(IBooleanItem.TRUE, Operator.GE, IBooleanItem.TRUE, IBooleanItem.TRUE),
74 Arguments.of(IBooleanItem.TRUE, Operator.GT, IBooleanItem.TRUE, IBooleanItem.FALSE),
75 Arguments.of(IBooleanItem.TRUE, Operator.LE, IBooleanItem.TRUE, IBooleanItem.TRUE),
76 Arguments.of(IBooleanItem.TRUE, Operator.LT, IBooleanItem.TRUE, IBooleanItem.FALSE)
77
78 );
79 }
80
81 @SuppressWarnings("null")
82 @ParameterizedTest
83 @MethodSource
84 void testValueComparison(IItem leftItem, Operator operator, IItem rightItem, IBooleanItem expectedResult) {
85 DynamicContext dynamicContext = newDynamicContext();
86 Mockery context = getContext();
87
88 ISequence<?> focus = ISequence.empty();
89
90 IExpression exp1 = context.mock(IExpression.class, "exp1");
91 IExpression exp2 = context.mock(IExpression.class, "exp2");
92
93 context.checking(new Expectations() {
94 {
95 atMost(1).of(exp1).accept(dynamicContext, focus);
96 will(returnValue(ISequence.of(leftItem)));
97 atMost(1).of(exp2).accept(dynamicContext, focus);
98 will(returnValue(ISequence.of(rightItem)));
99 }
100 });
101
102 ValueComparison expr = new ValueComparison(exp1, operator, exp2);
103
104 ISequence<?> result = expr.accept(dynamicContext, focus);
105 assertEquals(ISequence.of(expectedResult), result, "Sequence does not match");
106 }
107 }