Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,41 @@ The service will respond with the inserted geo features:

# Testing locally

To run tests locally run Gradle `cleanAndTest` task:
Naksha tests can currently run in two modes:

1. Against a Postgres database that you start yourself (for example the containerized Postgres described above).
2. Against a Postgres container started automatically by the build via [Testcontainers](https://github.com/testcontainers).

Before running Gradle, set the environment variables for the mode you want.

### Option 1: Run tests against a local standalone Postgres

```bash
export NAKSHA_APP_SERVICE_TEST_CONTEXT=LOCAL_STANDALONE
export NAKSHA_TEST_STORAGE_ID=local_psql_test_storage
export HUB_ADMIN_STORAGE_ID=local_psql_test_storage
Comment thread
gunplar marked this conversation as resolved.

# For `here-naksha-lib-psql` tests.
export NAKSHA_TEST_PSQL_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password"
Comment thread
gunplar marked this conversation as resolved.

# For `here-naksha-app-service` admin storage tests.
export NAKSHA_TEST_ADMIN_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password"

# For `here-naksha-app-service` data storage tests.
# Kept separate from the admin DB URL to allow deployment-like test setups,
# where administrative entities are stored in a different database/storage than data entities.
export NAKSHA_TEST_DATA_DB_URL="jdbc:postgresql://localhost:5432/postgres?user=postgres&password=password"

./gradlew cleanAndTest
```

### Option 2: Run tests with Testcontainers-managed Postgres

```bash
export NAKSHA_APP_SERVICE_TEST_CONTEXT=TEST_CONTAINERS
export HUB_ADMIN_STORAGE_ID=local_psql_test_storage
export NAKSHA_TEST_STORAGE_ID=local_psql_test_storage

./gradlew cleanAndTest
```

Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ val allModules = mapOf(
Pair("naksha", Pair(CleanAndTest.OFF, PublishModule.CONFIG_ONLY)),
Pair("here-naksha-app-service", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Pair("here-naksha-common-http", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Pair("here-naksha-common-test", Pair(CleanAndTest.OFF, PublishModule.YES)),
Pair("here-naksha-handler-activitylog", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Pair("here-naksha-lib-auth", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Pair("here-naksha-lib-base", Pair(CleanAndTest.KOTLIN, PublishModule.YES)),
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Descriptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.api.Project
private val Descriptions = mapOf(
"here-naksha-app-service" to "TBD",
"here-naksha-common-http" to "TBD",
"here-naksha-common-test" to "TBD",
"here-naksha-handler-activitylog" to "Naksha handler, adds downward compatibility to XYZ-Hub activity-log.",
//"here-naksha-handler-http" to "TBD",
"here-naksha-lib-auth" to "Naksha library, provides helper classes to perform authorization against Wikvaya UPM (User Permission Management) authorization matrix.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.here.naksha.app.init;

import static com.here.naksha.lib.hub.NakshaHubAdminStorageIdentifiers.DEFAULT_HUB_ADMIN_STORAGE_ID;

import com.here.naksha.lib.core.util.IoHelp;
import com.here.naksha.lib.core.util.IoHelp.LoadedBytes;
import com.here.naksha.lib.hub.NakshaHubAdminStorageIdentifiers;
import java.nio.charset.StandardCharsets;
import naksha.model.NakshaVersion;
import naksha.psql.PgConfig;
Expand Down Expand Up @@ -34,19 +33,19 @@ public TestStorageConfig(String mapId, PgConfig pgConfig) {
final byte[] bytes = loadedBytes.getBytes();
String url = new String(bytes, StandardCharsets.UTF_8);
if (url.startsWith("jdbc:postgresql://")) {
PgConfig pgConfig = new PgConfig(DEFAULT_HUB_ADMIN_STORAGE_ID).withMasterUri(url);
PgConfig pgConfig = new PgConfig(NakshaHubAdminStorageIdentifiers.getHubAdminStorageId()).withMasterUri(url);
return new TestStorageConfig(mapId, pgConfig);
}
} catch (Exception ignore) {
}
String url = System.getenv(envName);
if (url != null && url.startsWith("jdbc:postgresql://")) {
PgConfig pgConfig = new PgConfig(DEFAULT_HUB_ADMIN_STORAGE_ID).withMasterUri(url);
PgConfig pgConfig = new PgConfig(NakshaHubAdminStorageIdentifiers.getHubAdminStorageId()).withMasterUri(url);
return new TestStorageConfig(mapId, pgConfig);
}
url = System.getenv("TEST_NAKSHA_PSQL_URL");
if (url != null && url.startsWith("jdbc:postgresql://")) {
PgConfig pgConfig = new PgConfig(DEFAULT_HUB_ADMIN_STORAGE_ID).withMasterUri(url);
PgConfig pgConfig = new PgConfig(NakshaHubAdminStorageIdentifiers.getHubAdminStorageId()).withMasterUri(url);
return new TestStorageConfig(mapId, pgConfig);
}

Expand All @@ -57,7 +56,7 @@ public TestStorageConfig(String mapId, PgConfig pgConfig) {
url = "jdbc:postgresql://localhost:5432/postgres?user=postgres&password=" + password
+ "&schema=" + mapId
+ "&app=" + "Naksha/v" + NakshaVersion.current;
PgConfig pgConfig = new PgConfig(DEFAULT_HUB_ADMIN_STORAGE_ID).withMasterUri(url);
PgConfig pgConfig = new PgConfig(NakshaHubAdminStorageIdentifiers.getHubAdminStorageId()).withMasterUri(url);
return new TestStorageConfig(mapId, pgConfig);
}
}
1 change: 1 addition & 0 deletions here-naksha-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ kotlin {
}
jvmTest {
dependencies {
implementation(project(":here-naksha-common-test"))
implementation(libs.bundles.testing)
implementation(libs.test.containers)
runtimeOnly(libs.junit.platform.launcher) // https://github.com/gradle/gradle/issues/34512
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,70 @@
package com.here.naksha.cli.copy.service.psql;

import com.here.naksha.cli.copy.service.*;
import static naksha.model.RandomFeatures.randomFeatures;
import static naksha.model.util.RequestHelper.createFeaturesRequest;
import static naksha.model.util.RequestHelper.createWriteCollectionsRequest;
import static naksha.model.util.ResultHelper.extractResponseItems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;

import com.here.naksha.cli.copy.service.CopyElement;
import com.here.naksha.cli.copy.service.CopyService;
import com.here.naksha.cli.copy.service.CopyServiceException;
import com.here.naksha.cli.copy.service.CopyServiceSuccessResultPayload;
import com.here.naksha.cli.copy.service.StorageProvider;
import com.here.naksha.cli.copy.service.executors.OneShotFeaturesWriteExecutor;
import com.here.naksha.cli.copy.service.executors.ParallelFeaturesWriteExecutor;
import com.here.naksha.cli.copy.service.executors.model.FeaturesWriteExecutor;
import com.here.naksha.cli.results.CommandResult;
import com.here.naksha.cli.results.CommandSuccess;
import com.here.naksha.cli.storages.GeneratingStorage;
import com.here.naksha.cli.storages.GeneratingStorageConfig;
import com.here.naksha.cli.testcontainers.TestContainersPsqlStorage;
import com.here.naksha.lib.core.models.geojson.WebMercatorTile;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;
import naksha.base.StringList;
import naksha.common.test.CommonTestConstants;
import naksha.model.IStorage;
import naksha.model.Naksha;
import naksha.model.NakshaContext;
import naksha.model.SessionOptions;
import naksha.model.objects.NakshaCollection;
import naksha.model.objects.NakshaFeature;
import naksha.model.objects.NakshaMap;
import naksha.model.request.*;
import naksha.model.objects.NakshaStorage;
import naksha.model.request.ReadFeatures;
import naksha.model.request.Request;
import naksha.model.request.RequestQuery;
import naksha.model.request.Response;
import naksha.model.request.SuccessResponse;
import naksha.model.request.Write;
import naksha.model.request.WriteRequest;
import naksha.model.request.query.SpIntersects;
import naksha.model.request.query.SpOr;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

import static naksha.model.RandomFeatures.randomFeatures;
import static naksha.model.util.RequestHelper.createFeaturesRequest;
import static naksha.model.util.RequestHelper.createWriteCollectionsRequest;
import static naksha.model.util.ResultHelper.extractResponseItems;
import static org.junit.jupiter.api.Assertions.*;

class PsqlCopyTest {
private final String srcCollectionId = "srccolid";
private final String targetCollectionId = "targetcolid";
private SessionOptions sessionOptions;
private static final NakshaStorage storageConfig = NakshaStorage.fromJSON("""
{
"id": "%s",
"className": "naksha.psql.PsqlTestStorage"
}
""".formatted(CommonTestConstants.getTestStorageId()));
private IStorage psqlStorage;

@BeforeEach
void beforeEach() {
NakshaContext.currentContext().withAppId("testapp");
sessionOptions = SessionOptions.from(NakshaContext.currentContext());
psqlStorage = Naksha.useStorage(storageConfig);
}

@ParameterizedTest
Expand All @@ -58,7 +78,7 @@ void shouldCopyFeaturesBetweenGeneratingStorageAndPostgres(FeaturesWriteExecutor
CopyElement source = copyElementForGeneratingStorage(sourceStorage);

// And: prepared target
IStorage targetStorage = TestContainersPsqlStorage.getInstance().getStorage();
IStorage targetStorage = psqlStorage;
Comment thread
gunplar marked this conversation as resolved.
CopyElement target = createMapWithEmptyCollection(targetStorage, targetCollectionId);

// And: copy service
Expand All @@ -79,7 +99,7 @@ void shouldCopyFeaturesBetweenGeneratingStorageAndPostgres(FeaturesWriteExecutor
@MethodSource("featuresWriteExecutors")
void shouldCopyFeaturesBetweenMapsOnTheSameStorage(FeaturesWriteExecutor featuresWriteExecutor) {
// Given: the same storage for source and target
IStorage storage = TestContainersPsqlStorage.getInstance().getStorage();
IStorage storage = psqlStorage;

// Given: prepared source
CopyElement source = createMapWithEmptyCollection(storage, srcCollectionId);
Expand Down Expand Up @@ -110,7 +130,7 @@ void shouldCopyFeaturesBetweenMapsOnTheSameStorage(FeaturesWriteExecutor feature
@MethodSource("featuresWriteExecutors")
void shouldCreateMapAndCollectionThenCopy(FeaturesWriteExecutor featuresWriteExecutor) {
// Given: the same storage for source and target
IStorage storage = TestContainersPsqlStorage.getInstance().getStorage();
IStorage storage = psqlStorage;

// Given: prepared source
CopyElement source = createMapWithEmptyCollection(storage, srcCollectionId);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

class JsonParserTest {
Expand All @@ -33,6 +34,7 @@ void shouldFailWithNoReadable(@TempDir Path dir) throws IOException {
Files.writeString(pathToFile, "{}");
File file = pathToFile.toFile();
assumeTrue(file.setReadable(false), "Can not set file as unreadable!");
assumeFalse(Files.isReadable(pathToFile), "File is still readable by the process, skipping test");
JsonParser jsonParser = new JsonParser();

// When & Then
Expand Down
16 changes: 16 additions & 0 deletions here-naksha-common-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
alias(libs.plugins.kotlin.multiplatform)
}

description = gatherDescription()

kotlin {
jvm {
compilerOptions {
jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11)
}
}
js(IR) {
nodejs()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package naksha.common.test

import kotlin.jvm.JvmStatic

object CommonTestConstants {
private const val DEFAULT_TEST_STORAGE_ID: String = "local_psql_test_storage"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I carried an understanding that use of different storageId was the problem, but I still find two storageId in use (i.e. local_psql_test_storage and naksha-hub-admin-storage). I wonder, how this works, when we still use the same Postgres DB instance.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The understanding is correct, we need a single storageId for the whole build.

Currently it’s local_psql_test_storage. The naksha-hub-admin-storage is internal to Hub only, and now it’s overridable - during the build it’s also set to local_psql_test_storage.
I recall we initially agreed on simply reusing naksha-hub-admin-storage everywhere, but if with minimal effort we could set it to any arbitrary value, I went with this approach.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that clarifies. And I see the README is also updated to avoid this confusion. Thanks.

private const val TEST_STORAGE_ID_ENV: String = "NAKSHA_TEST_STORAGE_ID"

@JvmStatic
fun getTestStorageId(): String =
currentEnvironment()[TEST_STORAGE_ID_ENV]?.takeUnless { it.isBlank() } ?: DEFAULT_TEST_STORAGE_ID
}

internal expect fun currentEnvironment(): Map<String, String>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package naksha.common.test

internal actual fun currentEnvironment(): Map<String, String> = emptyMap()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package naksha.common.test

internal actual fun currentEnvironment(): Map<String, String> = System.getenv()
Loading
Loading