Skip to content

Commit

Permalink
MCPODS-6535 wiring test fix(?) & test db setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaneusz committed Dec 8, 2023
1 parent 2a5b3d4 commit 0b8c67f
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private static void printUsage() {

// Potentially we could override the app-name:
// NakshaHubConfig.APP_NAME = ?
return new NakshaApp(NakshaHubConfig.defaultAppName(), urlWithEnsuredSchema(url), cfgId, null);
return new NakshaApp(NakshaHubConfig.defaultAppName(), url, cfgId, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@

import com.here.naksha.app.service.NakshaApp;
import com.here.naksha.lib.hub.NakshaHubConfig;
import java.util.concurrent.atomic.AtomicReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class InitializedTestNakshaApp {
public class TestNakshaAppInitializer {

private static final String MOCK_CONFIG_ID = "mock-config";

Expand All @@ -34,20 +35,34 @@ public class InitializedTestNakshaApp {
private static final String TEST_SCHEMA = "naksha_test_schema";

public final @Nullable String testDbUrl;
public final @NotNull NakshaApp nakshaApp;
public final @NotNull AtomicReference<NakshaApp> nakshaApp;

private InitializedTestNakshaApp(@Nullable String testDbUrl, @NotNull NakshaApp nakshaApp) {
private TestNakshaAppInitializer(@Nullable String testDbUrl) {
this.testDbUrl = testDbUrl;
this.nakshaApp = nakshaApp;
this.nakshaApp = new AtomicReference<>();
}

public static InitializedTestNakshaApp initMockedNakshaApp() {
return new InitializedTestNakshaApp(null, newInstance(MOCK_CONFIG_ID));
public NakshaApp initNaksha() {
if (testDbUrl != null) {
nakshaApp.compareAndSet(null, newInstance(TEST_CONFIG_ID, testDbUrl));
} else {
nakshaApp.compareAndSet(null, newInstance(MOCK_CONFIG_ID));
}
return nakshaApp.get();
}

// null if not initialized
public @Nullable NakshaApp getNaksha() {
return nakshaApp.get();
}

public static TestNakshaAppInitializer mockedNakshaApp() {
return new TestNakshaAppInitializer(null);
}

public static InitializedTestNakshaApp initLocalPsqlBasedNakshaApp() {
public static TestNakshaAppInitializer localPsqlBasedNakshaApp() {
String dbUrl = dbUrl();
return new InitializedTestNakshaApp(dbUrl, newInstance(TEST_CONFIG_ID, dbUrl));
return new TestNakshaAppInitializer(dbUrl);
}

private static String dbUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
*/
package com.here.naksha.app.service;

import static com.here.naksha.app.common.InitializedTestNakshaApp.initLocalPsqlBasedNakshaApp;
import static com.here.naksha.app.common.TestNakshaAppInitializer.localPsqlBasedNakshaApp;
import static com.here.naksha.app.common.TestUtil.HDR_STREAM_ID;
import static com.here.naksha.app.common.TestUtil.getHeader;
import static com.here.naksha.app.common.TestUtil.loadFileOrFail;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.here.naksha.app.common.InitializedTestNakshaApp;
import com.here.naksha.app.common.NakshaTestWebClient;
import com.here.naksha.app.common.TestNakshaAppInitializer;
import com.here.naksha.lib.hub.NakshaHubConfig;
import com.here.naksha.lib.psql.PsqlStorage;
import java.net.http.HttpResponse;
Expand All @@ -43,7 +43,7 @@
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class NakshaAppTest {

static InitializedTestNakshaApp initializedTestNakshaApp;
static TestNakshaAppInitializer nakshaAppInitializer;
static NakshaHubConfig config;

static NakshaTestWebClient nakshaClient;
Expand All @@ -54,9 +54,9 @@ class NakshaAppTest {

@BeforeAll
static void prepare() throws InterruptedException, ExecutionException {
initializedTestNakshaApp =
initLocalPsqlBasedNakshaApp(); // to use mock, call NakshaAppInitializer.mockedNakshaApp()
NakshaApp app = initializedTestNakshaApp.nakshaApp;
nakshaAppInitializer = localPsqlBasedNakshaApp(); // to use mock, call NakshaAppInitializer.mockedNakshaApp()
cleanUpDb(nakshaAppInitializer.testDbUrl);
NakshaApp app = nakshaAppInitializer.initNaksha();
config = app.getHub().getConfig();
app.start();
Thread.sleep(5000); // wait for server to come up
Expand Down Expand Up @@ -719,12 +719,18 @@ void tc0506_testUpdateFeatureWithUuid() throws Exception {

@AfterAll
static void close() {
if (initializedTestNakshaApp != null) {
initializedTestNakshaApp.nakshaApp.stopInstance();
if (initializedTestNakshaApp.testDbUrl != null) {
try (PsqlStorage psqlStorage = new PsqlStorage(initializedTestNakshaApp.testDbUrl)) {
psqlStorage.dropSchema();
}
if (nakshaAppInitializer != null) {
NakshaApp app = nakshaAppInitializer.getNaksha();
if (app != null) {
app.stopInstance();
}
}
}

private static void cleanUpDb(String testUrl) {
if (testUrl != null && !testUrl.isBlank()) {
try (PsqlStorage psqlStorage = new PsqlStorage(testUrl)) {
psqlStorage.dropSchema();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public DefaultStorageHandler(
addStorageIdToStreamInfo(storageId, ctx);

// Obtain IStorage implementation using NakshaHub
// TODO: invocation eception start Jakub
final IStorage storageImpl = nakshaHub().getStorageById(storageId);

// Find collectionId from EventHandler, from Space, whichever is available first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.here.naksha.lib.core.models.storage.XyzFeatureCodec;
import com.here.naksha.lib.core.models.storage.XyzFeatureCodecFactory;
import com.here.naksha.lib.core.storage.IReadSession;
import com.here.naksha.lib.psql.EPsqlState;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -44,7 +45,6 @@
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.NotNull;
import org.postgresql.util.PSQLState;

public class NHAdminReaderMock implements IReadSession {

Expand Down Expand Up @@ -158,7 +158,8 @@ public void setLockTimeout(long timeout, @NotNull TimeUnit timeUnit) {}
for (final String collectionName : rf.getCollections()) {
if (mockCollection.get(collectionName) == null) {
throw unchecked(new SQLException(
"Collection " + collectionName + " not found!", PSQLState.UNDEFINED_TABLE.getState()));
"Collection " + collectionName + " not found!",
EPsqlState.COLLECTION_DOES_NOT_EXIST.toString()));
}
features.addAll(mockCollection.get(collectionName).values());
}
Expand All @@ -167,7 +168,8 @@ public void setLockTimeout(long timeout, @NotNull TimeUnit timeUnit) {}
for (final String collectionName : rf.getCollections()) {
if (mockCollection.get(collectionName) == null) {
throw unchecked(new SQLException(
"Collection " + collectionName + " not found!", PSQLState.UNDEFINED_TABLE.getState()));
"Collection " + collectionName + " not found!",
EPsqlState.COLLECTION_DOES_NOT_EXIST.toString()));
}
// if feature not found, return empty list
if (mockCollection.get(collectionName).get(pOp.getValue()) == null) {
Expand All @@ -189,7 +191,8 @@ public void setLockTimeout(long timeout, @NotNull TimeUnit timeUnit) {}
for (final String collectionName : rf.getCollections()) {
if (mockCollection.get(collectionName) == null) {
throw unchecked(new SQLException(
"Collection " + collectionName + " not found!", PSQLState.UNDEFINED_TABLE.getState()));
"Collection " + collectionName + " not found!",
EPsqlState.COLLECTION_DOES_NOT_EXIST.toString()));
}
features.addAll(ids.stream()
.map(id -> mockCollection.get(collectionName).get(id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.here.naksha.lib.core.models.storage.XyzFeatureCodecFactory;
import com.here.naksha.lib.core.storage.IStorageLock;
import com.here.naksha.lib.core.storage.IWriteSession;
import com.here.naksha.lib.psql.EPsqlState;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -99,7 +100,8 @@ public NHAdminWriterMock(final @NotNull Map<String, Map<String, Object>> mockCol
// Raise exception if collection doesn't exist already
if (mockCollection.get(wf.getCollectionId()) == null) {
throw unchecked(new SQLException(
"Collection " + wf.getCollectionId() + " doesn't exist.", PSQLState.UNDEFINED_TABLE.getState()));
"Collection " + wf.getCollectionId() + " doesn't exist.",
EPsqlState.COLLECTION_DOES_NOT_EXIST.toString()));
}
// Perform write operation for each feature
for (final XyzFeatureCodec featureCodec : wf.features) {
Expand Down Expand Up @@ -172,7 +174,8 @@ public NHAdminWriterMock(final @NotNull Map<String, Map<String, Object>> mockCol
} else {
// throw error if UUID mismatches
exception.set(new SQLException(
"Uuid " + uuidOf(ef) + " mismatch for id " + fId, PSQLState.UNIQUE_VIOLATION.getState()));
"Uuid " + uuidOf(ef) + " mismatch for id " + fId,
EPsqlState.COLLECTION_DOES_NOT_EXIST.toString()));
return oldF;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.argThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -64,6 +65,8 @@
import com.here.naksha.lib.hub.storages.NHAdminStorageReader;
import com.here.naksha.lib.hub.storages.NHAdminStorageWriter;
import com.here.naksha.lib.hub.storages.NHSpaceStorage;
import com.here.naksha.lib.psql.PsqlStorage;
import com.here.naksha.lib.psql.PsqlStorage.Params;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -95,9 +98,17 @@ public class NakshaHubWiringTest {

static NHSpaceStorage spaceStorage = null;

private static final String FEATURE_STORAGE_URL = "jdbc:postgresql://127.0.0.1/postgres?"
+ "user=postgres&"
+ "password=postgres&"
+ "schema=naksha_lib_hub_test_schema&"
+ "app=test-lib-hub-app-name&"
+ "id=naksha_wiring_test";

@BeforeEach
void setup() throws Exception {
MockitoAnnotations.openMocks(this);
cleanUpDb();
spyPipelineFactory = spy(new NakshaEventPipelineFactory(hub));
spaceStorage = new NHSpaceStorage(hub, spyPipelineFactory);
when(hub.getSpaceStorage()).thenReturn(spaceStorage);
Expand All @@ -116,7 +127,7 @@ private <T> T parseJson(final @NotNull String jsonStr, final @NotNull Class<T> t

@Test
@Order(1)
void testCreateStorageRequestWiring() throws Exception {
void testCreateStorageRequestWiring() {
// Given: Create Storage request
final Storage storage = parseJsonFileOrFail("create_storage.json", Storage.class);
final WriteXyzFeatures request =
Expand Down Expand Up @@ -181,7 +192,7 @@ void testGetStoragesRequestWiring() throws Exception {
assertTrue(reqCaptor.getValue() instanceof ReadFeatures);
}

// @Test TODO: fix psql setup ('function naksha_start_session(unknown, unknown, unknown, unknown) does not exist')
@Test
@Order(3)
void testCreateFeatureRequestWiring() throws Exception {
// Given: Storage, EventHandler and Space objects
Expand All @@ -191,6 +202,7 @@ void testCreateFeatureRequestWiring() throws Exception {
final Space space = parseJsonFileOrFail("createFeature/create_space.json", Space.class);
final IStorage storageImpl = PluginCache.getStorageConstructor(storage.getClassName(), Storage.class)
.call(storage);
storageImpl.initStorage(new Params().pg_hint_plan(false).pg_stat_statements(false));

// And: mock in place to return given Storage, EventHandler and Space objects, when requested from Admin Storage
final IStorage spyStorageImpl = spy(storageImpl);
Expand All @@ -212,11 +224,12 @@ void testCreateFeatureRequestWiring() throws Exception {
.thenReturn(spyStorageImpl);
// And: setup spy on Custom Storage Writer to intercept execute() method calls
final IWriteSession spyWriter = spy(spyStorageImpl.newWriteSession(newTestNakshaContext(), true));
when(spyStorageImpl.newWriteSession(any(), anyBoolean())).thenReturn(spyWriter);
doReturn(spyWriter).when(spyStorageImpl).newWriteSession(any(), anyBoolean());

// And: Create Feature request
final XyzFeature feature = parseJsonFileOrFail("createFeature/create_feature.json", XyzFeature.class);
final WriteXyzFeatures request = createFeatureRequest(space.getId(), feature, IfExists.FAIL, IfConflict.FAIL);
final WriteXyzFeatures request =
createFeatureRequest(space.getCollectionId(), feature, IfExists.FAIL, IfConflict.FAIL);

// And: spies and captors in place to return
final EventPipeline spyPipeline = spy(spyPipelineFactory.eventPipeline());
Expand Down Expand Up @@ -247,11 +260,12 @@ void testCreateFeatureRequestWiring() throws Exception {
verify(spyWriter, times(3)).execute(reqCaptor.capture());
assertTrue(reqCaptor.getValue() instanceof WriteFeatures);
final List<WriteRequest> requests = reqCaptor.getAllValues();
final String collectionId =
((Map) space.getProperties().get("xyzCollection")).get("id").toString();
final String collectionId = ((Map) space.getProperties().get("collection"))
.get("id")
.toString(); // TODO: this is ambiguous (see Space::getCollectionId), discuss
// Verify: WriteFeature into collection got called
assertTrue(
requests.get(0) instanceof WriteFeatures<?, ?, ?> wr
requests.get(0) instanceof WriteXyzFeatures wr
&& wr.getCollectionId().equals(collectionId),
"WriteFeature into collection request mismatch " + requests.get(0));
// Verify: WriteCollection got called (to create missing table)
Expand All @@ -271,4 +285,10 @@ private XyzFeatureCodec featureCodec(XyzFeature feature) {
.newInstance()
.withFeature(feature);
}

private static void cleanUpDb() {
try (PsqlStorage psqlStorage = new PsqlStorage(FEATURE_STORAGE_URL)) {
psqlStorage.dropSchema();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"active": true,
"extensionId": null,
"properties": {
"storageId": "local-mock"
"storageId": "test-storage"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"eventHandlerIds": [
"um-mod-dev-handler"
],
"collectionId": "um-mod-dev:topology",
"properties": {
"collection": {
"id": "um-mod-dev:topology",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
{
"id": "test-psql-storage",
"id": "test-storage",
"type": "Storage",
"title": "Test PSQL storage",
"description": "PSQL storage instance for testing purpose",
"className": "com.here.naksha.lib.psql.PsqlStorage",
"properties": {
"master": {
"host": "127.0.0.1",
"db": "postgres",
"user": "postgres",
"password": "postgres",
"readOnly": false
},
"appName": "test-lib-hub-app-name",
"schema": "naksha_lib_hub_test_schema"
}
"title": "Test mock storage",
"description": "Mock storage instance for testing purpose",
"className": "com.here.naksha.lib.hub.mock.NHAdminMock"
}

0 comments on commit 0b8c67f

Please sign in to comment.