/
DynamodbDocumentFetcherFactoryTest.java
233 lines (211 loc) · 13.7 KB
/
DynamodbDocumentFetcherFactoryTest.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.exasol.adapter.document.documentfetcher.dynamodb;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertAll;
import java.io.IOException;
import java.util.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.exasol.adapter.AdapterException;
import com.exasol.adapter.document.dynamodbmetadata.*;
import com.exasol.adapter.document.queryplanning.RemoteTableQuery;
import com.exasol.adapter.document.querypredicate.QueryPredicate;
import com.exasol.errorreporting.ExaError;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
class DynamodbDocumentFetcherFactoryTest {
private static final DynamodbTableMetadata tableMetadata = new DynamodbTableMetadata(
new DynamodbPrimaryIndex(BasicMappingSetup.PRIMARY_KEY_NAME, Optional.empty()), List.of(),
List.of(new DynamodbSecondaryIndex(BasicMappingSetup.INDEX_PARTITION_KEY,
Optional.of(BasicMappingSetup.INDEX_SORT_KEY), BasicMappingSetup.INDEX_NAME)));
private static BasicMappingSetup basicMappingSetup;
@BeforeAll
static void beforeAll() throws IOException, AdapterException {
basicMappingSetup = new BasicMappingSetup();
}
@Test
void testSelectAll() {
final RemoteTableQuery documentQuery = basicMappingSetup.getSelectAllQuery();
final DynamodbScanDocumentFetcher scanPlan = (DynamodbScanDocumentFetcher) new DynamodbDocumentFetcherFactory(
null).buildDocumentFetcherForQuery(documentQuery, tableMetadata, 1).getDocumentFetchers().get(0);
assertThat(scanPlan.getScanRequest().tableName(), equalTo(basicMappingSetup.tableMapping.getRemoteName()));
}
@Test
void testSelectAllWithProjection() {
final RemoteTableQuery documentQuery = basicMappingSetup.getSelectAllQueryWithNameColumnProjected();
final DynamodbScanDocumentFetcher scanPlan = (DynamodbScanDocumentFetcher) new DynamodbDocumentFetcherFactory(
null).buildDocumentFetcherForQuery(documentQuery, tableMetadata, 1).getDocumentFetchers().get(0);
assertAll(//
() -> assertThat(scanPlan.getScanRequest().tableName(),
equalTo(basicMappingSetup.tableMapping.getRemoteName())),
() -> assertThat(scanPlan.getScanRequest().projectionExpression(), equalTo("#0")),
() -> assertThat(scanPlan.getScanRequest().expressionAttributeNames(), equalTo(Map.of("#0", "name")))//
);
}
@Test
void testSecondaryIndexQuery() {
final String publisher = "jb books";
final RemoteTableQuery documentQuery = basicMappingSetup.getQueryForPublisher(publisher);
final DynamodbQueryDocumentFetcher queryPlan = (DynamodbQueryDocumentFetcher) new DynamodbDocumentFetcherFactory(
null).buildDocumentFetcherForQuery(documentQuery, tableMetadata, 1).getDocumentFetchers().get(0);
assertAll(//
() -> assertThat(queryPlan.getQueryRequest().tableName(),
equalTo(basicMappingSetup.tableMapping.getRemoteName())),
() -> assertThat(queryPlan.getQueryRequest().indexName(), equalTo("publisherIndex")),
() -> assertThat(queryPlan.getQueryRequest().keyConditionExpression(), equalTo("#0 = :0")),
() -> assertThat(queryPlan.getQueryRequest().expressionAttributeNames().get("#0"),
equalTo("publisher")),
() -> assertThat(queryPlan.getQueryRequest().expressionAttributeValues().get(":0").s(),
equalTo(publisher))//
);
}
@Test
void testRangeQueryWithPrimaryKey() {
final String publisher = "jb books";
final double price = 10.1;
final DynamodbTableMetadata tableMetadata = new DynamodbTableMetadata(
new DynamodbPrimaryIndex("publisher", Optional.of("price")), List.of(), List.of());
final RemoteTableQuery documentQuery = basicMappingSetup.getQueryForMinPriceAndPublisher(price, publisher);
final DynamodbQueryDocumentFetcher queryPlan = (DynamodbQueryDocumentFetcher) new DynamodbDocumentFetcherFactory(
null).buildDocumentFetcherForQuery(documentQuery, tableMetadata, 1).getDocumentFetchers().get(0);
final Map<String, String> attributeNames = queryPlan.getQueryRequest().expressionAttributeNames();
final Map<String, AttributeValue> attributeValues = queryPlan.getQueryRequest().expressionAttributeValues();
final String keyConditionExpression = fillPlaceholders(queryPlan.getQueryRequest().keyConditionExpression(),
attributeNames, attributeValues);
assertAll(//
() -> assertThat(queryPlan.getQueryRequest().tableName(),
equalTo(basicMappingSetup.tableMapping.getRemoteName())),
() -> assertThat(keyConditionExpression, anyOf(equalTo("(publisher = 'jb books' and (price > 10.1))"),
equalTo("(price > 10.1 and (publisher = 'jb books'))")))//
);
}
@Test
void testRangeQueryWithPrimaryKeyAndNot() {
final String publisher = "jb books";
final double price = 10.1;
final DynamodbTableMetadata tableMetadata = new DynamodbTableMetadata(
new DynamodbPrimaryIndex("publisher", Optional.of("price")), List.of(), List.of());
final RemoteTableQuery documentQuery = basicMappingSetup.getQueryForMaxPriceAndPublisher(price, publisher);
final DynamodbQueryDocumentFetcher queryPlan = (DynamodbQueryDocumentFetcher) new DynamodbDocumentFetcherFactory(
null).buildDocumentFetcherForQuery(documentQuery, tableMetadata, 1).getDocumentFetchers().get(0);
final Map<String, String> attributeNames = queryPlan.getQueryRequest().expressionAttributeNames();
final Map<String, AttributeValue> attributeValues = queryPlan.getQueryRequest().expressionAttributeValues();
final String keyConditionExpression = fillPlaceholders(queryPlan.getQueryRequest().keyConditionExpression(),
attributeNames, attributeValues);
assertAll(//
() -> assertThat(queryPlan.getQueryRequest().tableName(),
equalTo(basicMappingSetup.tableMapping.getRemoteName())),
() -> assertThat(keyConditionExpression, anyOf(equalTo("(publisher = 'jb books' and (price <= 10.1))"),
equalTo("(price <= 10.1 and (publisher = 'jb books'))")))//
);
}
@Test
void testRangeQueryWithoutPrimaryKey() {
final double price = 10.1;
final RemoteTableQuery documentQuery = basicMappingSetup.getQueryForMinPrice(price);
final DynamodbScanDocumentFetcher scanPlan = (DynamodbScanDocumentFetcher) new DynamodbDocumentFetcherFactory(
null).buildDocumentFetcherForQuery(documentQuery, tableMetadata, 1).getDocumentFetchers().get(0);
assertAll(//
() -> assertThat(scanPlan.getScanRequest().tableName(),
equalTo(basicMappingSetup.tableMapping.getRemoteName())),
() -> assertThat(scanPlan.getScanRequest().filterExpression(), equalTo("#0 > :0")),
() -> assertThat(scanPlan.getScanRequest().expressionAttributeNames().get("#0"), equalTo("price")),
() -> assertThat(scanPlan.getScanRequest().expressionAttributeValues().get(":0").n(),
equalTo(String.valueOf(price))));
}
@Test
void testQueryOnKeyAndNonKeyProperties() {
final String publisher = "jb books";
final String name = "test";
final DynamodbTableMetadata tableMetadata = new DynamodbTableMetadata(
new DynamodbPrimaryIndex("publisher", Optional.empty()), List.of(), List.of());
final RemoteTableQuery documentQuery = basicMappingSetup.getQueryForNameAndPublisher(name, publisher);
final DynamodbQueryDocumentFetcher queryPlan = (DynamodbQueryDocumentFetcher) new DynamodbDocumentFetcherFactory(
null).buildDocumentFetcherForQuery(documentQuery, tableMetadata, 1).getDocumentFetchers().get(0);
assertAll(//
() -> assertThat(queryPlan.getQueryRequest().tableName(),
equalTo(basicMappingSetup.tableMapping.getRemoteName())),
() -> assertThat(queryPlan.getQueryRequest().keyConditionExpression(), equalTo("#0 = :0")),
() -> assertThat(queryPlan.getQueryRequest().expressionAttributeValues().get(":0").s(),
equalTo(publisher)),
() -> assertThat(queryPlan.getQueryRequest().expressionAttributeNames().get("#0"),
equalTo("publisher")),
() -> assertThat(queryPlan.getQueryRequest().filterExpression(), equalTo("#1 = :1")),
() -> assertThat(queryPlan.getQueryRequest().expressionAttributeNames().get("#1"), equalTo("name")),
() -> assertThat(queryPlan.getQueryRequest().expressionAttributeValues().get(":1").s(), equalTo(name))//
);
}
@Test
void testExtractPrimaryKeyFromTwoPredicates() {
final String publisher = "jb books";
final String name1 = "name1";
final String name2 = "name2";
final DynamodbTableMetadata tableMetadata = new DynamodbTableMetadata(
new DynamodbPrimaryIndex("publisher", Optional.empty()), List.of(), List.of());
final RemoteTableQuery documentQuery = basicMappingSetup.getQueryForTwoNamesAndPublisher(name1, name2,
publisher);
final DynamodbQueryDocumentFetcher queryPlan = (DynamodbQueryDocumentFetcher) new DynamodbDocumentFetcherFactory(
null).buildDocumentFetcherForQuery(documentQuery, tableMetadata, 1).getDocumentFetchers().get(0);
final Map<String, String> attributeNames = queryPlan.getQueryRequest().expressionAttributeNames();
final Map<String, AttributeValue> attributeValues = queryPlan.getQueryRequest().expressionAttributeValues();
final String filterExpression = fillPlaceholders(queryPlan.getQueryRequest().filterExpression(), attributeNames,
attributeValues);
assertAll(//
() -> assertThat(queryPlan.getQueryRequest().tableName(),
equalTo(basicMappingSetup.tableMapping.getRemoteName())),
() -> assertThat(queryPlan.getQueryRequest().keyConditionExpression(), equalTo("#0 = :0")),
() -> assertThat(attributeValues.get(":0").s(), equalTo(publisher)),
() -> assertThat(attributeNames.get("#0"), equalTo("publisher")),
() -> assertThat(filterExpression, anyOf(equalTo("(name = 'name1' or (name = 'name2'))"),
equalTo("(name = 'name2' or (name = 'name1'))"))));
}
@Test
void testQueryOnKeyAndIndexProperties() {
final int price = 10;
final String publisher = "jb books";
final String isbn = "1234";
final RemoteTableQuery documentQuery = basicMappingSetup.getQueryForPriceAndPublisherAndIsbn(price, publisher,
isbn);
final DynamodbQueryDocumentFetcher queryPlan = (DynamodbQueryDocumentFetcher) new DynamodbDocumentFetcherFactory(
null).buildDocumentFetcherForQuery(documentQuery, tableMetadata, 1).getDocumentFetchers().get(0);
final Map<String, String> attributeNames = queryPlan.getQueryRequest().expressionAttributeNames();
final Map<String, AttributeValue> attributeValues = queryPlan.getQueryRequest().expressionAttributeValues();
final String filterExpression = fillPlaceholders(queryPlan.getQueryRequest().filterExpression(), attributeNames,
attributeValues);
final String keyConditionExpression = fillPlaceholders(queryPlan.getQueryRequest().keyConditionExpression(),
attributeNames, attributeValues);
assertAll(//
() -> assertThat(queryPlan.getQueryRequest().tableName(),
equalTo(basicMappingSetup.tableMapping.getRemoteName())),
() -> assertThat(keyConditionExpression, equalTo("isbn = '1234'")),
() -> assertThat(filterExpression, anyOf(equalTo("(publisher = 'jb books' and (price = 10.0))"),
equalTo("(price = 10.0 and (publisher = 'jb books'))")))//
);
}
@Test
void testPostSelectionIsExtracted() {
final RemoteTableQuery documentQuery = basicMappingSetup.getFilterSourceReferenceQuery();
final QueryPredicate postSelection = new DynamodbDocumentFetcherFactory(null)
.buildDocumentFetcherForQuery(documentQuery, tableMetadata, 1).getPostSelection();
assertThat(postSelection.toString(), containsString("SOURCE_REFERENCE"));
}
private String fillPlaceholders(final String expression, final Map<String, String> names,
final Map<String, AttributeValue> values) {
String result = expression;
for (final Map.Entry<String, String> namePlaceholder : names.entrySet()) {
result = result.replace(namePlaceholder.getKey(), namePlaceholder.getValue());
}
for (final Map.Entry<String, AttributeValue> valuePlaceholder : values.entrySet()) {
final AttributeValue value = valuePlaceholder.getValue();
if (value.s() != null) {
result = result.replace(valuePlaceholder.getKey(), "'" + value.s() + "'");
} else if (value.n() != null) {
result = result.replace(valuePlaceholder.getKey(), value.n());
} else {
throw new IllegalStateException(ExaError.messageBuilder("E-VSDY-6").message(
"Filling in a placeholder of this type is not yet implemented. Value for the placeholder: {{value}}.",
value).toString());
}
}
return result;
}
}