/
RqlOptionsParserTest.java
executable file
·173 lines (148 loc) · 6.8 KB
/
RqlOptionsParserTest.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.model.thingsearchparser.options.rql;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.model.rql.ParserException;
import org.eclipse.ditto.model.thingsearch.CursorOption;
import org.eclipse.ditto.model.thingsearch.LimitOption;
import org.eclipse.ditto.model.thingsearch.Option;
import org.eclipse.ditto.model.thingsearch.SizeOption;
import org.eclipse.ditto.model.thingsearch.SortOption;
import org.eclipse.ditto.model.thingsearch.SortOptionEntry;
import org.eclipse.ditto.model.thingsearchparser.OptionParser;
import org.eclipse.ditto.model.thingsearchparser.RqlOptionParser;
import org.junit.Test;
/**
* Unit test for {@link RqlOptionParser}.
*/
public final class RqlOptionsParserTest {
private final OptionParser parser = new RqlOptionParser();
@Test
public void parseSortWithOnePropertyAscending() throws ParserException {
final List<Option> options = parser.parse("sort(+username)");
assertThat(options.size()).isEqualTo(1);
final SortOption sortOption = (SortOption) options.get(0);
assertThat(sortOption.getEntries().stream()
.map(SortOptionEntry::getPropertyPath)
.map(JsonPointer::toString)
.anyMatch("/username"::equals)
).isTrue();
assertThat(sortOption.getEntries().stream()
.filter(soe -> JsonPointer.of("/username").equals(soe.getPropertyPath()))
.map(SortOptionEntry::getOrder)
.anyMatch(SortOptionEntry.SortOrder.ASC::equals)
).isTrue();
}
@Test
public void parseSortWithOnePropertyDescending() throws ParserException {
final List<Option> options = parser.parse("sort(-attributes/username)");
assertThat(options.size()).isEqualTo(1);
final SortOption sortOption = (SortOption) options.get(0);
assertThat(sortOption.getEntries().stream()
.map(SortOptionEntry::getPropertyPath)
.map(JsonPointer::toString)
.anyMatch("/attributes/username"::equals)
).isTrue();
assertThat(sortOption.getEntries().stream()
.filter(soe -> JsonPointer.of("/attributes/username").equals(soe.getPropertyPath()))
.map(SortOptionEntry::getOrder)
.anyMatch(SortOptionEntry.SortOrder.DESC::equals)
).isTrue();
}
@Test
public void parseSortWithSeveralProperties() throws ParserException {
final int expectedCount = 20;
final StringBuilder sb = new StringBuilder();
sb.append("sort(");
for (int i = 0; i < expectedCount; i++) {
sb.append(i % 2 == 0 ? "+" : "-");
sb.append("field").append(i);
sb.append(",");
}
sb.replace(sb.length() - 1, sb.length(), ")");
final List<Option> options = parser.parse(sb.toString());
assertThat(options.size()).isEqualTo(1);
final SortOption sortOption = (SortOption) options.get(0);
assertThat(sortOption.getEntries().size()).isEqualTo(expectedCount);
for (int i = 0; i < expectedCount; i++) {
final int idx = i;
assertThat(sortOption.getEntries().stream()
.filter(soe -> JsonPointer.of("field" + idx).equals(soe.getPropertyPath()))
.map(SortOptionEntry::getOrder)
.anyMatch(so -> idx % 2 == 0 ? SortOptionEntry.SortOrder.ASC.equals(so) :
SortOptionEntry.SortOrder.DESC.equals(so))
).isTrue();
}
}
@Test(expected = ParserException.class)
public void invalidSortMissingOrder() throws ParserException {
parser.parse("sort(username)");
}
@Test(expected = ParserException.class)
public void invalidSortMissingOrderAtSecondProperty() throws ParserException {
parser.parse("sort(-username,owner)");
}
@Test
public void parseLimitSuccess() throws ParserException {
final List<Option> options = parser.parse("limit(0,1)");
assertThat(options.size()).isEqualTo(1);
final LimitOption limitOption = (LimitOption) options.get(0);
assertThat(limitOption.getCount()).isEqualTo(1);
assertThat(limitOption.getOffset()).isEqualTo(0);
}
@Test(expected = ParserException.class)
public void invalidLimitMissingNumber() throws ParserException {
parser.parse("limit(0)");
}
@Test(expected = ParserException.class)
public void invalidLimitTooManyArguments() throws ParserException {
parser.parse("limit(0,1,2)");
}
@Test
public void parseOptionCombinations() throws ParserException {
final List<Option> options = parser.parse("limit(0,1),sort(-attributes/username),cursor(ABC),size(463)");
assertThat(options.size()).isEqualTo(4);
final LimitOption limitOption = (LimitOption) options.get(0);
assertThat(limitOption.getCount()).isEqualTo(1);
assertThat(limitOption.getOffset()).isEqualTo(0);
final SortOption sortOption = (SortOption) options.get(1);
assertThat(sortOption.getEntries().stream()
.map(SortOptionEntry::getPropertyPath)
.map(JsonPointer::toString)
.anyMatch("/attributes/username"::equals)
).isTrue();
assertThat(sortOption.getEntries().stream()
.filter(soe -> JsonPointer.of("/attributes/username").equals(soe.getPropertyPath()))
.map(SortOptionEntry::getOrder)
.anyMatch(SortOptionEntry.SortOrder.DESC::equals)
).isTrue();
final CursorOption cursorOption = (CursorOption) options.get(2);
assertThat(cursorOption.getCursor()).isEqualTo("ABC");
final SizeOption sizeOption = (SizeOption) options.get(3);
assertThat(sizeOption.getSize()).isEqualTo(463);
}
@Test
public void parseAndUnparseAreInverseOfEachOther() throws ParserException {
final String input = "limit(0,1),sort(-attributes/username)";
final List<Option> parsed = parser.parse(input);
final String unparsed = RqlOptionParser.unparse(parsed);
final List<Option> reParsed = parser.parse(unparsed);
assertThat(reParsed).isEqualTo(parsed);
}
@Test(expected = ParserException.class)
public void invalidLimitArgumentsExceedsLong() throws ParserException {
parser.parse("limit(100000000000000000000,10)");
}
}