|
| 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 | +} |
0 commit comments