Skip to content

Commit

Permalink
MCPODS-6531 refactor and sample unit tests layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaneusz committed Oct 31, 2023
1 parent b37ea25 commit f4efe32
Show file tree
Hide file tree
Showing 25 changed files with 1,763 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ project(":here-naksha-lib-handlers") {
implementation(postgres)

testImplementation(json_assert)
testImplementation(mockito)
}
}
//} catch (ignore: UnknownProjectException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.UUID;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

Expand Down Expand Up @@ -79,7 +80,7 @@ private String getHeader(final HttpResponse<?> response, final String header) {
return (values == null) ? null : (values.size() > 1 ? values.toString() : values.get(0));
}

// @Test
@Test
void tc0001_testGetStorages() throws Exception {
// Test API : GET /hub/storages
// 1. Load test data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@
package com.here.naksha.lib.hub;

import static com.here.naksha.lib.core.exceptions.UncheckedException.unchecked;
import static com.here.naksha.lib.core.util.storage.RequestHelper.*;
import static com.here.naksha.lib.core.util.storage.RequestHelper.createFeatureRequest;
import static com.here.naksha.lib.core.util.storage.RequestHelper.readFeaturesByIdsRequest;

import com.here.naksha.lib.core.INaksha;
import com.here.naksha.lib.core.NakshaAdminCollection;
import com.here.naksha.lib.core.NakshaContext;
import com.here.naksha.lib.core.NakshaVersion;
import com.here.naksha.lib.core.models.naksha.Storage;
import com.here.naksha.lib.core.models.storage.*;
import com.here.naksha.lib.core.models.storage.EWriteOp;
import com.here.naksha.lib.core.models.storage.ErrorResult;
import com.here.naksha.lib.core.models.storage.IfConflict;
import com.here.naksha.lib.core.models.storage.IfExists;
import com.here.naksha.lib.core.models.storage.ReadResult;
import com.here.naksha.lib.core.models.storage.Result;
import com.here.naksha.lib.core.models.storage.StorageCollection;
import com.here.naksha.lib.core.models.storage.WriteCollections;
import com.here.naksha.lib.core.models.storage.WriteOp;
import com.here.naksha.lib.core.storage.IStorage;
import com.here.naksha.lib.core.storage.IWriteSession;
import com.here.naksha.lib.core.util.IoHelp;
Expand All @@ -45,21 +53,31 @@

public class NakshaHub implements INaksha {

/** The id of default NakshaHub Config feature object */
/**
* The id of default NakshaHub Config feature object
*/
public static final @NotNull String DEF_CFG_ID = "default-config";
/** The NakshaHub config. */
/**
* The NakshaHub config.
*/
protected final @NotNull NakshaHubConfig nakshaHubConfig;
/** Singleton instance of physical admin storage implementation */
/**
* Singleton instance of physical admin storage implementation
*/
protected final @NotNull IStorage psqlStorage;
/** Singleton instance of AdminStorage, which internally uses physical admin storage (i.e. PsqlStorage) */
/**
* Singleton instance of AdminStorage, which internally uses physical admin storage (i.e. PsqlStorage)
*/
protected final @NotNull IStorage adminStorageInstance;
/** Singleton instance of Space Storage, which is responsible to manage admin collections as spaces
* and support respective read/write operations on spaces */
/**
* Singleton instance of Space Storage, which is responsible to manage admin collections as spaces and support respective read/write
* operations on spaces
*/
protected final @NotNull IStorage spaceStorageInstance;

@ApiStatus.AvailableSince(NakshaVersion.v2_0_7)
public NakshaHub(
final @NotNull PsqlConfig config,
final @NotNull PsqlConfig config, // TODO Kuba - de facto adminDB config
final @Nullable NakshaHubConfig customCfg,
final @Nullable String configId) {
// this.dataSource = new PsqlDataSource(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

// TODO, Kuba: ta klasa nic nie wnosi - delegat na IStorage
public class NHAdminStorage implements IStorage {

/** Singleton instance of physical admin storage implementation */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

// TODO, Kuba: ta klasa nic nie wnosi, 1:1 delegacja IReadSession
public class NHAdminStorageReader implements IReadSession {

/** Current session, all read storage operations should be executed against */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

// TODO, Kuba: ta klasa nic nie wnosi, delegat wraper do IWriteSession
public class NHAdminStorageWriter extends NHAdminStorageReader implements IWriteSession {

/** Current session, all write storage operations should be executed against */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2017-2023 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.lib.hub2;

import com.here.naksha.lib.core.EventPipeline;

public interface EventPipelineFactory {

EventPipeline eventPipeline();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2017-2023 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.lib.hub2;

import com.here.naksha.lib.core.EventPipeline;
import com.here.naksha.lib.core.INaksha;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;

public class NakshaEventPipelineFactory implements EventPipelineFactory {

private final @NotNull INaksha naksha;

public NakshaEventPipelineFactory(@NotNull INaksha naksha) {
this.naksha = naksha;
}

@Override
public EventPipeline eventPipeline() {
return new EventPipeline(naksha);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
NakshaEventPipelineFactory that = (NakshaEventPipelineFactory) o;
return Objects.equals(naksha, that.naksha);
}

@Override
public int hashCode() {
return Objects.hash(naksha);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (C) 2017-2023 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.lib.hub2;

import static com.here.naksha.lib.core.exceptions.UncheckedException.unchecked;

import com.here.naksha.lib.core.IEventHandler;
import com.here.naksha.lib.core.INaksha;
import com.here.naksha.lib.core.NakshaAdminCollection;
import com.here.naksha.lib.core.storage.IStorage;
import com.here.naksha.lib.handlers.AuthorizationEventHandler;
import com.here.naksha.lib.handlers.IntHandlerForConfigs;
import com.here.naksha.lib.handlers.IntHandlerForEventHandlers;
import com.here.naksha.lib.handlers.IntHandlerForExtensions;
import com.here.naksha.lib.handlers.IntHandlerForSpaces;
import com.here.naksha.lib.handlers.IntHandlerForStorages;
import com.here.naksha.lib.handlers.IntHandlerForSubscriptions;
import com.here.naksha.lib.hub2.admin.AdminStorage;
import com.here.naksha.lib.hub2.space.SpaceStorage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;

public class NakshaHub2 implements INaksha {

private final @NotNull AdminStorage adminStorage;

private final @NotNull SpaceStorage spaceStorage;

NakshaHub2(@NotNull AdminStorage adminStorage, @NotNull SpaceStorage spaceStorage) {
this.adminStorage = adminStorage;
this.spaceStorage = spaceStorage;
spaceStorage.setVirtualSpaces(configureVirtualSpaces(this));
spaceStorage.setEventPipelineFactory(new NakshaEventPipelineFactory(this));
}

/**
* Returns a thin wrapper above the admin-database that adds authorization and internal event handling. Basically, this allows access to
* the admin collections.
*
* @return the admin-storage.
*/
@Override
public @NotNull IStorage getAdminStorage() {
return adminStorage;
}

/**
* Returns a virtual storage that maps spaces to collections and allows to execute requests in spaces.
*
* @return the virtual space-storage.
*/
@Override
public @NotNull IStorage getSpaceStorage() {
return spaceStorage;
}

/**
* Returns the user defined space storage instance based on storageId as per space collection defined in Naksha admin storage.
*
* @param storageId Id of the space storage
* @return the space-storage
*/
@Override
public @NotNull IStorage getStorageById(@NotNull String storageId) {
// TODO : Add logic to retrieve Storage from Admin DB and then instantiate respective IStorage implementation
return null;
}

private static @NotNull Map<String, List<IEventHandler>> configureVirtualSpaces(final @NotNull INaksha hub) {
final Map<String, List<IEventHandler>> adminSpaces = new HashMap<>();
// common auth handler
final IEventHandler authHandler = new AuthorizationEventHandler(hub);
// add event handlers for each admin space
for (final String spaceId : NakshaAdminCollection.ALL) {
adminSpaces.put(
spaceId,
switch (spaceId) {
case NakshaAdminCollection.CONFIGS -> List.of(authHandler, new IntHandlerForConfigs(hub));
case NakshaAdminCollection.SPACES -> List.of(authHandler, new IntHandlerForSpaces(hub));
case NakshaAdminCollection.SUBSCRIPTIONS -> List.of(
authHandler, new IntHandlerForSubscriptions(hub));
case NakshaAdminCollection.EVENT_HANDLERS -> List.of(
authHandler, new IntHandlerForEventHandlers(hub));
case NakshaAdminCollection.STORAGES -> List.of(authHandler, new IntHandlerForStorages(hub));
case NakshaAdminCollection.EXTENSIONS -> List.of(authHandler, new IntHandlerForExtensions(hub));
default -> throw unchecked(new Exception("Unsupported virtual space " + spaceId));
});
}
return adminSpaces;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2017-2023 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.lib.hub2;

import com.here.naksha.lib.core.NakshaContext;
import com.here.naksha.lib.hub2.admin.AdminStorage;
import com.here.naksha.lib.hub2.admin.AdminStorageConfiguration;
import com.here.naksha.lib.hub2.space.SpaceStorage;
import com.here.naksha.lib.hub2.space.SpaceStorageConfiguration;
import com.here.naksha.lib.psql.PsqlConfig;

public class NakshaHubConfiguration {
private NakshaHubConfiguration() {}

public static NakshaHub2 nakshaHub(PsqlConfig psqlConfig, NakshaContext nakshaContext) {
AdminStorage adminStorage = AdminStorageConfiguration.psqlAdminStorage(psqlConfig, nakshaContext);
SpaceStorage spaceStorage = SpaceStorageConfiguration.adminBasedSpaceStorage(adminStorage);
return new NakshaHub2(adminStorage, spaceStorage);
}
}
Loading

0 comments on commit f4efe32

Please sign in to comment.