Skip to content

Commit ac3abae

Browse files
committed
Add new tests and refactoring existing ones.
Signed-off-by: Maximilian Chrzan <maximilian.chrzan@here.com>
1 parent 8b2d3a2 commit ac3abae

16 files changed

+911
-578
lines changed

xyz-psql-connector/src/test/java/com/here/xyz/psql/PSQLAbstractIT.java

Lines changed: 68 additions & 416 deletions
Large diffs are not rendered by default.

xyz-psql-connector/src/test/java/com/here/xyz/psql/PSQLDeleteIT.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2021 HERE Europe B.V.
2+
* Copyright (C) 2017-2022 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import com.jayway.jsonpath.DocumentContext;
2323
import com.jayway.jsonpath.JsonPath;
2424
import org.junit.After;
25-
import org.junit.Before;
2625
import org.junit.BeforeClass;
2726
import org.junit.Test;
2827

@@ -39,25 +38,22 @@ public class PSQLDeleteIT extends PSQLAbstractIT {
3938
@BeforeClass
4039
public static void init() throws Exception { initEnv(null); }
4140

42-
@Before
43-
public void removeTestSpaces() throws Exception { deleteTestSpace(null); }
44-
4541
@After
46-
public void shutdown() throws Exception { shutdownEnv(null); }
42+
public void shutdown() throws Exception { invokeDeleteTestSpace(null); }
4743

4844
@Test
4945
public void testDeleteFeatures() throws Exception {
5046
// =========== INSERT ==========
5147
String insertJsonFile = "/events/InsertFeaturesEvent.json";
5248
String insertResponse = invokeLambdaFromFile(insertJsonFile);
53-
logger.info("RAW RESPONSE: " + insertResponse);
49+
LOGGER.info("RAW RESPONSE: " + insertResponse);
5450

55-
String insertRequest = IOUtils.toString(GSContext.class.getResourceAsStream(insertJsonFile));
51+
String insertRequest = IOUtils.toString(this.getClass().getResourceAsStream(insertJsonFile));
5652
assertRead(insertRequest, insertResponse, false);
5753

5854
final JsonPath jsonPathFeatureIds = JsonPath.compile("$.features..id");
5955
List<String> ids = jsonPathFeatureIds.read(insertResponse, jsonPathConf);
60-
logger.info("Preparation: Inserted features {}", ids);
56+
LOGGER.info("Preparation: Inserted features {}", ids);
6157

6258
// =========== DELETE ==========
6359
final DocumentContext modifyFeaturesEventDoc = getEventFromResource("/events/InsertFeaturesEvent.json");
@@ -70,7 +66,7 @@ public void testDeleteFeatures() throws Exception {
7066
String deleteEvent = modifyFeaturesEventDoc.jsonString();
7167
String deleteResponse = invokeLambda(deleteEvent);
7268
assertNoErrorInResponse(deleteResponse);
73-
logger.info("Modify features tested successfully");
69+
LOGGER.info("Modify features tested successfully");
7470
}
7571

7672
@Test
@@ -83,22 +79,22 @@ public void testDeleteFeaturesByTagDefault() throws Exception {
8379
testDeleteFeaturesByTag(false);
8480
}
8581

86-
private void testDeleteFeaturesByTag(boolean includeOldStates) throws Exception {
82+
protected void testDeleteFeaturesByTag(boolean includeOldStates) throws Exception {
8783
// =========== INSERT ==========
8884
String insertJsonFile = "/events/InsertFeaturesEvent.json";
8985
String insertResponse = invokeLambdaFromFile(insertJsonFile);
90-
logger.info("RAW RESPONSE: " + insertResponse);
91-
String insertRequest = IOUtils.toString(GSContext.class.getResourceAsStream(insertJsonFile));
86+
LOGGER.info("RAW RESPONSE: " + insertResponse);
87+
String insertRequest = IOUtils.toString(this.getClass().getResourceAsStream(insertJsonFile));
9288
assertRead(insertRequest, insertResponse, false);
93-
logger.info("Preparation: Insert features");
89+
LOGGER.info("Preparation: Insert features");
9490

9591
// =========== COUNT ==========
9692
String countResponse = invokeLambdaFromFile("/events/CountFeaturesEvent.json");
9793
Integer originalCount = JsonPath.read(countResponse, "$.count");
98-
logger.info("Preparation: feature count = {}", originalCount);
94+
LOGGER.info("Preparation: feature count = {}", originalCount);
9995

10096
// =========== DELETE SOME TAGGED FEATURES ==========
101-
logger.info("Delete tagged features");
97+
LOGGER.info("Delete tagged features");
10298
final DocumentContext deleteByTagEventDoc = getEventFromResource("/events/DeleteFeaturesByTagEvent.json");
10399
deleteByTagEventDoc.put("$", "params", Collections.singletonMap("includeOldStates", includeOldStates));
104100
String[][] tags = {{"yellow"}};
@@ -118,7 +114,7 @@ private void testDeleteFeaturesByTag(boolean includeOldStates) throws Exception
118114
countResponse = invokeLambdaFromFile("/events/CountFeaturesEvent.json");
119115
Integer count = JsonPath.read(countResponse, "$.count");
120116
assertTrue(originalCount > count);
121-
logger.info("Delete tagged features tested successfully");
117+
LOGGER.info("Delete tagged features tested successfully");
122118

123119
// =========== DELETE ALL FEATURES ==========
124120
deleteByTagEventDoc.put("$", "tags", null);
@@ -133,6 +129,6 @@ private void testDeleteFeaturesByTag(boolean includeOldStates) throws Exception
133129
countResponse = invokeLambdaFromFile("/events/CountFeaturesEvent.json");
134130
count = JsonPath.read(countResponse, "$.count");
135131
assertEquals(0, count.intValue());
136-
logger.info("Delete all features tested successfully");
132+
LOGGER.info("Delete all features tested successfully");
137133
}
138134
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* Copyright (C) 2017-2022 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
package com.here.xyz.psql;
20+
21+
import com.here.xyz.XyzSerializable;
22+
import com.here.xyz.events.ModifyFeaturesEvent;
23+
import com.here.xyz.events.ModifySpaceEvent;
24+
import com.here.xyz.models.geojson.implementation.Feature;
25+
import com.here.xyz.models.geojson.implementation.XyzNamespace;
26+
import com.here.xyz.models.hub.Space;
27+
import com.here.xyz.psql.config.ConnectorParameters;
28+
import com.here.xyz.psql.query.ModifySpace;
29+
import com.here.xyz.psql.tools.FeatureGenerator;
30+
import com.here.xyz.responses.SuccessResponse;
31+
import org.json.JSONObject;
32+
import org.junit.After;
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
36+
import java.sql.Connection;
37+
import java.sql.ResultSet;
38+
import java.sql.Statement;
39+
import java.util.ArrayList;
40+
import java.util.HashMap;
41+
import java.util.List;
42+
import java.util.Map;
43+
44+
import static org.junit.Assert.*;
45+
46+
public class PSQLExtendedSpacesIT extends PSQLAbstractIT {
47+
private static final String BASE = "base_test";
48+
private static final String DELTA1 = "delta1_test";
49+
private static final String DELTA2 = "delta2_test";
50+
51+
private static List<String> spaces = new ArrayList<String>(){{add(BASE);add(DELTA1);add(DELTA2);}};
52+
53+
protected static Map<String, Object> connectorParams = new HashMap<String,Object>(){
54+
{ put(ConnectorParameters.CONNECTOR_ID, "test-connector");
55+
put(ConnectorParameters.AUTO_INDEXING, true);
56+
put(ConnectorParameters.PROPERTY_SEARCH, true);
57+
}
58+
};
59+
60+
@BeforeClass
61+
public static void init() throws Exception {
62+
initEnv(connectorParams);
63+
generateTestSpaces();
64+
}
65+
66+
@After
67+
public void shutdown() throws Exception { invokeDeleteTestSpaces(connectorParams, spaces); }
68+
69+
@Test
70+
public void checkIDX() throws Exception {
71+
72+
checkIDXTable(new JSONObject("{\"sortableProperties\": [[\"sort_test\"]], \"searchableProperties\": {\"search_test\": true}}"));
73+
74+
Map<String,Boolean> searchableProperties = new HashMap();
75+
List<List<Object>> sortableProperties = new ArrayList<>();
76+
searchableProperties.put("search_test", false);
77+
searchableProperties.put("search_test2", true);
78+
79+
/** Update Searchable and SortableProperties in Base */
80+
ModifySpaceEvent modifySpaceEvent = new ModifySpaceEvent()
81+
.withSpace(BASE)
82+
.withOperation(ModifySpaceEvent.Operation.UPDATE)
83+
.withConnectorParams(connectorParams)
84+
.withSpaceDefinition(
85+
new Space()
86+
.withId(BASE)
87+
.withSearchableProperties(searchableProperties)
88+
.withSortableProperties(sortableProperties)
89+
);
90+
SuccessResponse response = XyzSerializable.deserialize(invokeLambda(modifySpaceEvent.serialize()));
91+
assertEquals("OK",response.getStatus());
92+
93+
/** Check if IDX-Table reflects this changes */
94+
checkIDXTable(new JSONObject("{\"searchableProperties\": {\"search_test\": false,\"search_test2\": true}}"));
95+
}
96+
97+
protected static void generateTestSpaces() throws Exception {
98+
/** Generate:
99+
* BASE
100+
* DELTA1 Extends BASE
101+
* DELTA2 Extends DELTA1
102+
* */
103+
for (String space : spaces ) {
104+
Map<String,Boolean> searchableProperties = new HashMap();
105+
List<List<Object>> sortableProperties = new ArrayList<>();
106+
Map<String, Object> params = new HashMap<>();
107+
Map<String, Object> extendsL2 = new HashMap<>();
108+
109+
switch (space){
110+
case BASE:
111+
searchableProperties.put("search_test", true);
112+
sortableProperties.add(new ArrayList<Object>(){{add("sort_test");}});
113+
break;
114+
case DELTA1:
115+
mockAutoIndexing();
116+
params.put("extends",new Space.Extension().withSpaceId(BASE));
117+
break;
118+
case DELTA2:
119+
extendsL2.put("spaceId",DELTA1);
120+
extendsL2.put("extends",new Space.Extension().withSpaceId(BASE));
121+
params.put("extends",extendsL2);
122+
}
123+
124+
ModifySpaceEvent modifySpaceEvent = new ModifySpaceEvent()
125+
.withSpace(space)
126+
.withOperation(ModifySpaceEvent.Operation.CREATE)
127+
.withConnectorParams(connectorParams)
128+
.withParams(params)
129+
.withSpaceDefinition(
130+
new Space()
131+
.withId(space)
132+
.withSearchableProperties(searchableProperties)
133+
.withSortableProperties(sortableProperties)
134+
);
135+
SuccessResponse response = XyzSerializable.deserialize(invokeLambda(modifySpaceEvent.serialize()));
136+
assertEquals("OK",response.getStatus());
137+
138+
final List<Feature> features = new ArrayList<Feature>(){{
139+
add(FeatureGenerator.generateFeature(new XyzNamespace().withSpace("foo").withCreatedAt(1517504700726L), null));
140+
}};
141+
142+
ModifyFeaturesEvent modifyFeaturesEvent = new ModifyFeaturesEvent()
143+
.withSpace(space)
144+
.withConnectorParams(connectorParams)
145+
.withTransaction(true)
146+
.withInsertFeatures(features);
147+
invokeLambda(modifyFeaturesEvent.serialize());
148+
}
149+
}
150+
151+
protected static void checkIDXTable(JSONObject idx_manual_ref) throws Exception{
152+
String q = "SELECT * FROM "+ ModifySpace.IDX_STATUS_TABLE+" WHERE spaceid IN ('"+BASE+"','"+DELTA1+"','"+DELTA2+"');";
153+
154+
try (final Connection connection = LAMBDA.dataSource.getConnection()) {
155+
Statement stmt = connection.createStatement();
156+
ResultSet resultSet = stmt.executeQuery(q);
157+
int i = 0;
158+
159+
while (resultSet.next()) {
160+
i++;
161+
String spaceId = resultSet.getString("spaceid");
162+
JSONObject idx_manual = new JSONObject(resultSet.getString("idx_manual"));
163+
String autoIndexing = resultSet.getString("auto_indexing");
164+
boolean idx_creation_finished = resultSet.getBoolean("idx_creation_finished");
165+
166+
switch (spaceId){
167+
case BASE:
168+
assertNull(autoIndexing);
169+
break;
170+
case DELTA1:
171+
case DELTA2:
172+
/** Inject mocked Auto-Index*/
173+
idx_manual_ref.put("searchableProperties",((JSONObject)idx_manual_ref.get("searchableProperties")).put("foo",true));
174+
assertEquals("f",autoIndexing);
175+
}
176+
System.out.println(spaceId+" "+idx_manual);
177+
System.out.println(spaceId+" "+idx_manual_ref);
178+
179+
assertFalse(idx_creation_finished);
180+
assertTrue(idx_manual_ref.similar(idx_manual));
181+
}
182+
/** Are all entries are present? */
183+
assertEquals(3, i);
184+
}
185+
}
186+
187+
protected static void mockAutoIndexing() throws Exception{
188+
String q = "CREATE INDEX IF NOT EXISTS idx_base_test_foo_a" +
189+
" ON public."+BASE+" USING btree" +
190+
" (((jsondata -> 'properties'::text) -> 'foo'::text));" +
191+
"COMMENT ON INDEX public.idx_base_test_foo_a" +
192+
" IS 'foo';";
193+
194+
try (final Connection connection = LAMBDA.dataSource.getConnection()) {
195+
Statement stmt = connection.createStatement();
196+
stmt.execute(q);
197+
}
198+
}
199+
}

xyz-psql-connector/src/test/java/com/here/xyz/psql/PSQLHashedSpaceIdIT.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2021 HERE Europe B.V.
2+
* Copyright (C) 2017-2022 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import com.here.xyz.models.geojson.implementation.Feature;
2828
import com.here.xyz.models.geojson.implementation.XyzNamespace;
2929
import com.here.xyz.psql.config.ConnectorParameters;
30+
import com.here.xyz.psql.tools.FeatureGenerator;
3031
import com.here.xyz.util.Hasher;
3132
import java.sql.Connection;
3233
import java.sql.ResultSet;
@@ -35,25 +36,24 @@
3536
import java.util.stream.Collectors;
3637
import org.apache.commons.codec.digest.DigestUtils;
3738
import org.junit.After;
38-
import org.junit.Before;
3939
import org.junit.BeforeClass;
4040
import org.junit.Test;
4141

4242
@SuppressWarnings("unused")
4343
public class PSQLHashedSpaceIdIT extends PSQLAbstractIT {
4444

45-
static Map<String, Object> connectorParams = new HashMap<String,Object>(){
46-
{put(ConnectorParameters.CONNECTOR_ID, "test-connector"); put(ConnectorParameters.ENABLE_HASHED_SPACEID, true); put(ConnectorParameters.AUTO_INDEXING, true);}};
47-
45+
protected static Map<String, Object> connectorParams = new HashMap<String,Object>(){
46+
{ put(ConnectorParameters.CONNECTOR_ID, "test-connector");
47+
put(ConnectorParameters.ENABLE_HASHED_SPACEID, true);
48+
put(ConnectorParameters.AUTO_INDEXING, true);
49+
}
50+
};
4851

4952
@BeforeClass
5053
public static void init() throws Exception { initEnv(connectorParams); }
5154

52-
@Before
53-
public void removeTestSpaces() throws Exception { deleteTestSpace(connectorParams); }
54-
5555
@After
56-
public void shutdown() throws Exception { shutdownEnv(connectorParams); }
56+
public void shutdown() throws Exception { invokeDeleteTestSpace(connectorParams); }
5757

5858
@Test
5959
public void testTableCreation() throws Exception {
@@ -62,7 +62,7 @@ public void testTableCreation() throws Exception {
6262
final XyzNamespace xyzNamespace = new XyzNamespace().withSpace("foo").withCreatedAt(1517504700726L);
6363

6464
final List<Feature> features = new ArrayList<Feature>(){{
65-
add(generateFeature(xyzNamespace, null));
65+
add(FeatureGenerator.generateFeature(xyzNamespace, null));
6666
}};
6767

6868
ModifyFeaturesEvent mfevent = new ModifyFeaturesEvent()
@@ -74,9 +74,9 @@ public void testTableCreation() throws Exception {
7474

7575
/** Needed to trigger update on pg_stat */
7676
try (
77-
final Connection connection = lambda.dataSource.getConnection();
78-
final Statement stmt = connection.createStatement();
79-
final ResultSet rs = stmt.executeQuery("SELECT table_name FROM information_schema.tables WHERE table_name='" + hashedSpaceId + "'");
77+
final Connection connection = LAMBDA.dataSource.getConnection();
78+
final Statement stmt = connection.createStatement();
79+
final ResultSet rs = stmt.executeQuery("SELECT table_name FROM information_schema.tables WHERE table_name='" + hashedSpaceId + "'");
8080
) {
8181
assertTrue(rs.next());
8282
assertEquals(hashedSpaceId, rs.getString("table_name"));
@@ -88,7 +88,7 @@ public void testAutoIndexing() throws Exception {
8888
final String spaceId = "foo";
8989
final String hashedSpaceId = Hasher.getHash(spaceId);
9090

91-
final List<Feature> features = get11kFeatureCollection().getFeatures();
91+
final List<Feature> features = FeatureGenerator.get11kFeatureCollection().getFeatures();
9292
ModifyFeaturesEvent mfevent = new ModifyFeaturesEvent()
9393
.withSpace(spaceId)
9494
.withConnectorParams(connectorParams)
@@ -98,7 +98,7 @@ public void testAutoIndexing() throws Exception {
9898
invokeLambda(mfevent.serialize());
9999

100100
/** Needed to trigger update on pg_stat */
101-
try (final Connection connection = lambda.dataSource.getConnection()) {
101+
try (final Connection connection = LAMBDA.dataSource.getConnection()) {
102102
Statement stmt = connection.createStatement();
103103
stmt.execute("DELETE FROM xyz_config.xyz_idxs_status WHERE spaceid='" + hashedSpaceId + "';");
104104
stmt.execute("ANALYZE \"" + hashedSpaceId + "\";");
@@ -107,7 +107,7 @@ public void testAutoIndexing() throws Exception {
107107
//Triggers dbMaintenance
108108
invokeLambdaFromFile("/events/HealthCheckWithEnableHashedSpaceIdEvent.json");
109109

110-
try (final Connection connection = lambda.dataSource.getConnection()) {
110+
try (final Connection connection = LAMBDA.dataSource.getConnection()) {
111111
Statement stmt = connection.createStatement();
112112
// check for the index status
113113
try (ResultSet rs = stmt.executeQuery("SELECT * FROM xyz_config.xyz_idxs_status where spaceid = '" + hashedSpaceId + "';")) {

0 commit comments

Comments
 (0)