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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mavenPassword=YourPassword
# here-naksha-lib-psql/src/commonMain/kotlin/naksha/psql/LibPsql.kt (adminVersion property)
# Warning: Only update LibPsql version, if there is a change in SQL functions!
# The reason is, that this version is encoded in the database, and when updated, forced an upgrade!
version=3.0.0-beta.35
version=3.0.0-beta.36

org.gradle.jvmargs=-Xmx12g
kotlin.code.style=official
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ servers:
info:
title: "Naskha Hub-API"
description: "Naksha Hub-API is a REST API to provide simple access to geo data."
version: "3.0.0-beta.35"
version: "3.0.0-beta.36"

security:
- AccessToken: [ ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class NakshaVersion(
* The current version as string to constant usage cases.
* @since 3.0
*/
const val CURRENT = "3.0.0-beta.35"
const val CURRENT = "3.0.0-beta.36"
// WARNING: Do not update this property manually, it is automatically modified when building!
// Edit version only in `gradle.properties` file, which is used as well to create artifacts!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,7 @@ protected void initStorage(@NotNull NakshaStorage config, @Nullable Boolean crea
if (httpStorageProperties == null || httpStorageProperties.getUrl() == null) {
throw new IllegalArgumentException("A HTTP storage must have properties containing a 'url'");
}
this.defaultKeyProperties = new KeyProperties(
config.getId(),
httpStorageProperties.getUrl(),
httpStorageProperties.getHeaders(),
httpStorageProperties.getConnectTimeout(),
httpStorageProperties.getSocketTimeout(),
httpStorageProperties.getMaxRetries()
);
this.defaultKeyProperties = KeyProperties.fromHttpStorageProperties(config.getId(), httpStorageProperties);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.here.naksha.storage.http;

import naksha.base.JvmMapProxy;
import naksha.model.NakshaError;
import naksha.model.NakshaException;
import naksha.model.NakshaVersion;
Expand All @@ -35,6 +36,16 @@
@AvailableSince(NakshaVersion.v2_0_12)
public class HttpStorageProperties extends NakshaProperties {

static final class HeaderMap extends JvmMapProxy<String, String> {
private HeaderMap() {
super(String.class, String.class);
}

private void putHeaders(@NotNull Map<String, String> headers) {
headers.forEach(this::put);
}
}

public static final Integer DEF_CONNECTION_TIMEOUT_SEC = 20;
public static final Integer DEF_SOCKET_TIMEOUT_SEC = 90;
public static final Integer DEF_MAX_RETRIES = 1;
Expand Down Expand Up @@ -105,11 +116,29 @@ public void setMaxRetries(final @Nullable Integer maxRetries) {
* By default: 'Content-Type: application/json' and 'Accept-Encoding: gzip'
*/
public @NotNull Map<String, String> getHeaders() {
return getOrSet(HEADERS, DEFAULT_HEADERS);
final Object raw = get(HEADERS);
if (raw instanceof HeaderMap) {
return (HeaderMap) raw;
}
if (raw instanceof Map<?, ?>) {
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.

IntelliJ complains that it is always false. Meaning, it will never to Map<?, ?> . Which seems true. Pls revisit and remove this condition if not needed.
In that case, the earlier check of raw instanceof HeaderMap is also not needed. It will either be null or of type HeaderMap, so just checking for null should be sufficient?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Personally, I vote for keep these checks. The Kotlin Multiplatform build is not the most straightforward when it comes to IDE errors/warnings as I have seen.
In this case for example I can wrongly do setRaw(HEADERS, "12"); somewhere, and that string would still be stored. So correcting the headers here at getter looks to be the only solution without interfering with the underlying MapProxy (ideally I prefer to somehow catch this right when it was set).

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.

The way I am thinking, creates a doubt - if there is a possibility where setRaw(HEADERS, "12"); can happen during runtime? In the code, I don't see such case. Help with one example?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One example would be new HttpStorageProperties().setRaw(<<any Object>>), by mistake somewhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think you both are correct. with setRaw() and getRaw() we lose encapsulation but it's design of proxy model. Also Map check looks like a lot of boilerplate, and in normal flow it would be unnecessary, also Intelij is sometimes confused by proxy model. What is happening in that case: In most used flow in Storage, Handler etc jsons. When we want access properties we use JvmBoxingUtil.box(eventHandlerConfig.getProperties(), DefaultStorageHandlerProperties.class)); and in our case HttpStorageProperties storageProperties = JvmBoxingUtil.box(storage.getProperties(), HttpStorageProperties.class); Now when properties has nested objects like in our case Map. In boxing process it's become AnyObject by proxy model. In normal use case we then just call gets on our properties, if we don't have Map<?,?> check in get. It would fail HeaderMap check and create and return default headers, so we need step to translate our AnyObject to our wrapper in this case HeaderMap extends JvmMapProxy<String, String> . I personally think and from I remember Kuba agreed, that proxy model it's a bit complicated and confusing to use for new users, also creating what normally would be POJO becomes complicated and time consuming tasks, like in this example of which we're talking right now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also added test shouldPreserveHeadersWhenBoxingStoragePropertiesFromJson to help understand what is going on.

HeaderMap headers = toHeaderMap((Map<?, ?>) raw);
setRaw(HEADERS, headers);
return headers;
}
HeaderMap headers = new HeaderMap();
headers.putHeaders(DEFAULT_HEADERS);
setRaw(HEADERS, headers);
return headers;
}

public void setHeaders(final @Nullable Map<String, String> headers) {
setRaw(HEADERS, headers);
if (headers == null) {
setRaw(HEADERS, null);
return;
}
HeaderMap headerMap = new HeaderMap();
headerMap.putHeaders(headers);
setRaw(HEADERS, headerMap);
}

public @NotNull HttpInterface getProtocol() {
Expand All @@ -134,4 +163,14 @@ public void setHeaders(final @Nullable Map<String, String> headers) {

public void setProtocol(final HttpInterface protocol) {setRaw(HTTP_INTERFACE, protocol);}

private @NotNull HeaderMap toHeaderMap(@NotNull Map<?, ?> rawHeaders) {
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.

If the earlier comment about "Map<? , ?> not being used" is accepted, then this function also can be removed?

HeaderMap headers = new HeaderMap();
rawHeaders.forEach((key, value) -> {
if (key instanceof String && value instanceof String) {
headers.put((String) key, (String) value);
}
});
return headers;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ public KeyProperties(
this.maxRetries = maxRetries;
}

public static @NotNull KeyProperties fromHttpStorageProperties(
@NotNull String name,
@NotNull HttpStorageProperties properties) {
return new KeyProperties(
name,
properties.getUrl(),
properties.getHeaders(),
properties.getConnectTimeout(),
properties.getSocketTimeout(),
properties.getMaxRetries()
);
}

public @NotNull String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package com.here.naksha.storage.http;

import org.junit.jupiter.api.Test;
import naksha.base.JvmBoxingUtil;
import naksha.base.JvmJsonUtil;
import naksha.base.Platform;
import naksha.model.objects.NakshaStorage;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

class HttpStoragePropertiesTest {

private static final String TEST_RESOURCE_DIR = "/unit_test_data/HttpStorageProperties/";

@Test
void shouldReturnDefaultValuesOnCreation() {
// Given: a new HttpStorageProperties object created with the default constructor
Expand Down Expand Up @@ -39,4 +51,160 @@ void should_set_and_get_all_properties_correctly() {
assertEquals(testSocketTimeout, properties.getSocketTimeout());
assertEquals(testMaxRetries, properties.getMaxRetries());
}
}

@Test
void shouldNormalizeRawHeadersMapFromBoxedProperties() {
final HttpStorageProperties properties = new HttpStorageProperties();
final Map<String, String> rawHeaders = new HashMap<>();
rawHeaders.put("Authorization", "Bearer <token>");
rawHeaders.put("Content-Type", "application/json");

// Simulate the v3 boxed/proxy state before the typed getter runs its normalization logic.
properties.setRaw("headers", rawHeaders);

final Map<String, String> headers = properties.getHeaders();
assertEquals("Bearer <token>", headers.get("Authorization"));
assertEquals("application/json", headers.get("Content-Type"));
assertEquals(2, headers.size());
assertEquals(headers, properties.getRaw("headers"));
assertTrue(headers instanceof HttpStorageProperties.HeaderMap);
assertInstanceOf(Map.class, properties.getRaw("headers"));
assertNotSame(rawHeaders, headers);
}

@Test
void shouldStoreHeadersAsProxyWhenUsingSetter() {
final HttpStorageProperties properties = new HttpStorageProperties();
final Map<String, String> headers = Map.of(
"Authorization", "Bearer exampleToken",
"Content-Type", "application/json"
);

properties.setHeaders(headers);

assertEquals(headers, properties.getHeaders());
assertTrue(properties.getHeaders() instanceof HttpStorageProperties.HeaderMap);
assertInstanceOf(Map.class, properties.getRaw("headers"));
assertNotSame(headers, properties.getRaw("headers"));
}

@Test
void shouldReturnDefaultsForInvalidRawValues() {
final HttpStorageProperties properties = new HttpStorageProperties();

properties.setRaw("headers", "invalid");

assertEquals(HttpStorageProperties.DEFAULT_HEADERS, properties.getHeaders());
}

@Test
void shouldDeserializeAllFieldsFromJson() {
final HttpStorageProperties properties = jsonResourceToPropertiesOrFail("t01_testConvertAllFields");

assertEquals("https://example.org", properties.getUrl());
assertEquals(60, properties.getConnectTimeout());
assertEquals(3600, properties.getSocketTimeout());

final Map<String, String> headers = properties.getHeaders();
assertEquals("Bearer <token>", headers.get("Authorization"));
assertEquals("application/json", headers.get("Content-Type"));
assertEquals(2, headers.size());
}

@Test
void shouldPreserveHeadersWhenBoxingStoragePropertiesFromJson() {
final String storageJson;
try {
storageJson = jsonResourceToStringOrFail("t05_testBoxStorageProperties");
} catch (IOException e) {
fail("Unable to convert json resource", e);
return;
}

final NakshaStorage storage = JvmBoxingUtil.box(Platform.fromJSON(storageJson), NakshaStorage.class);
assertNotNull(storage);

final HttpStorageProperties properties = JvmBoxingUtil.box(storage.getProperties(), HttpStorageProperties.class);
assertNotNull(properties);

final Object rawHeaders = properties.get("headers");
assertInstanceOf(Map.class, rawHeaders);
assertFalse(rawHeaders instanceof HttpStorageProperties.HeaderMap);

final Map<String, String> headers = properties.getHeaders();
assertEquals("Bearer boxed-token", headers.get("Authorization"));
assertEquals("demo", headers.get("X-Tenant"));
assertFalse(headers.containsKey("Accept-Encoding"));
assertEquals(2, headers.size());
assertTrue(headers instanceof HttpStorageProperties.HeaderMap);
}

@Test
void shouldDeserializeMissingValuesToDefaultsFromJson() {
final HttpStorageProperties properties = jsonResourceToPropertiesOrFail("t02_testConvertMissingToNull");

assertEquals("https://example.org", properties.getUrl());
assertEquals(HttpStorageProperties.DEF_CONNECTION_TIMEOUT_SEC, properties.getConnectTimeout());
assertEquals(HttpStorageProperties.DEF_SOCKET_TIMEOUT_SEC, properties.getSocketTimeout());
assertEquals(HttpStorageProperties.DEF_MAX_RETRIES, properties.getMaxRetries());
assertEquals(HttpStorageProperties.DEFAULT_HEADERS, properties.getHeaders());
}

@Test
void shouldIgnoreExcessFieldsInJson() {
assertDoesNotThrow(() -> jsonResourceToPropertiesOrFail("t03_testDontThrowOnExcessFields"));
}

@Test
void shouldDeserializeMissingUrlAsNullInJson() {
final HttpStorageProperties properties = jsonResourceToPropertiesOrFail("t04_testThrowOnMissingMandatory");

assertNull(properties.getUrl());
assertEquals(60, properties.getConnectTimeout());
assertEquals(3600, properties.getSocketTimeout());
assertEquals("Bearer <token>", properties.getHeaders().get("Authorization"));
assertEquals("application/json", properties.getHeaders().get("Content-Type"));
}

@Test
void shouldUseNormalizedHeadersMapInKeyProperties() {
final HttpStorageProperties properties = new HttpStorageProperties();
final Map<String, String> rawHeaders = new HashMap<>();
rawHeaders.put("Authorization", "Bearer <token>");
rawHeaders.put("Content-Type", "application/json");
properties.setUrl("https://example.org");
properties.setRaw("headers", rawHeaders);

final RequestSender.KeyProperties fromProperties = RequestSender.KeyProperties.fromHttpStorageProperties("test-storage", properties);

assertNotNull(fromProperties.getDefaultHeaders());
assertTrue(fromProperties.getDefaultHeaders() instanceof HttpStorageProperties.HeaderMap);
assertNotSame(rawHeaders, fromProperties.getDefaultHeaders());
assertEquals("Bearer <token>", fromProperties.getDefaultHeaders().get("Authorization"));
assertEquals("application/json", fromProperties.getDefaultHeaders().get("Content-Type"));
assertEquals(2, fromProperties.getDefaultHeaders().size());
}

private HttpStorageProperties jsonResourceToPropertiesOrFail(String fileName) {
try {
String json = jsonResourceToStringOrFail(fileName);
HttpStorageProperties properties = JvmJsonUtil.readJsonAs(json, HttpStorageProperties.class);
assertNotNull(properties);
return properties;
} catch (IOException e) {
fail("Unable to convert json resource", e);
return null;
}
}

private String jsonResourceToStringOrFail(String fileName) throws IOException {
String resource = TEST_RESOURCE_DIR + fileName + ".json";

try (InputStream testResourceStream = this.getClass().getResourceAsStream(resource)) {
if (testResourceStream == null) {
throw new IOException("Could not access " + resource + " resource");
}
return new String(testResourceStream.readAllBytes(), StandardCharsets.UTF_8);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "http-storage",
"type": "Storage",
"className": "com.here.naksha.storage.http.HttpStorage",
"properties": {
"url": "https://example.org",
"headers": {
"Authorization": "Bearer boxed-token",
"X-Tenant": "demo"
}
}
}
Loading