-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HttpPushSpecificConfig to enable custom idle timeout configuratio…
…n per connection Signed-off-by: David Schwilk <david.schwilk@bosch.io>
- Loading branch information
1 parent
2e1f6aa
commit efd663e
Showing
5 changed files
with
196 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...ava/org/eclipse/ditto/connectivity/service/messaging/httppush/HttpPushSpecificConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (c) 2022 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.connectivity.service.messaging.httppush; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
import org.eclipse.ditto.connectivity.model.Connection; | ||
import org.eclipse.ditto.connectivity.service.config.HttpPushConfig; | ||
|
||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
|
||
/** | ||
* Class providing access to HTTP push specific configuration. | ||
*/ | ||
@Immutable | ||
public final class HttpPushSpecificConfig { | ||
|
||
static final String IDLE_TIMEOUT = "idleTimeout"; | ||
|
||
private final Config specificConfig; | ||
|
||
private HttpPushSpecificConfig(final Config specificConfig) { | ||
this.specificConfig = specificConfig; | ||
} | ||
|
||
/** | ||
* Creates a new instance of HttpSpecificConfig based on the {@code specificConfig} of the passed | ||
* {@code connection}. | ||
* | ||
* @param connection the Connection to extract the {@code specificConfig} map from. | ||
* @param httpConfig the http config to create the default config from. | ||
* @return the new HttpSpecificConfig instance | ||
*/ | ||
public static HttpPushSpecificConfig fromConnection(final Connection connection, final HttpPushConfig httpConfig) { | ||
final Map<String, Object> defaultConfig = toDefaultConfig(httpConfig); | ||
final Config config = ConfigFactory.parseMap(connection.getSpecificConfig()) | ||
.withFallback(ConfigFactory.parseMap(defaultConfig)); | ||
|
||
return new HttpPushSpecificConfig(config); | ||
} | ||
|
||
private static Map<String, Object> toDefaultConfig(final HttpPushConfig httpConfig) { | ||
final Map<String, Object> defaultMap = new HashMap<>(); | ||
defaultMap.put(IDLE_TIMEOUT, httpConfig.getRequestTimeout()); | ||
return defaultMap; | ||
} | ||
|
||
/** | ||
* @return the idle timeout applied for HTTP push requests. | ||
*/ | ||
public Duration idleTimeout() { | ||
return specificConfig.getDuration(IDLE_TIMEOUT); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final HttpPushSpecificConfig that = (HttpPushSpecificConfig) o; | ||
return Objects.equals(specificConfig, that.specificConfig); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(specificConfig); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + " [" + | ||
"specificConfig=" + specificConfig + | ||
"]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...org/eclipse/ditto/connectivity/service/messaging/httppush/HttpPushSpecificConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2022 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.connectivity.service.messaging.httppush; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.eclipse.ditto.connectivity.model.Connection; | ||
import org.eclipse.ditto.connectivity.service.config.HttpPushConfig; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
public final class HttpPushSpecificConfigTest { | ||
|
||
private HttpPushConfig httpConfig; | ||
private Connection connection; | ||
|
||
@Before | ||
public void setup() { | ||
httpConfig = Mockito.mock(HttpPushConfig.class); | ||
when(httpConfig.getRequestTimeout()).thenReturn(Duration.ofSeconds(2)); | ||
connection = Mockito.mock(Connection.class); | ||
} | ||
|
||
@Test | ||
public void parseHttpSpecificConfig() { | ||
final Map<String, String> configuredSpecificConfig = new HashMap<>(); | ||
configuredSpecificConfig.put(HttpPushSpecificConfig.IDLE_TIMEOUT, "3s"); | ||
|
||
when(connection.getSpecificConfig()).thenReturn(configuredSpecificConfig); | ||
final var specificConfig = HttpPushSpecificConfig.fromConnection(connection, httpConfig); | ||
|
||
assertThat(specificConfig.idleTimeout()).isEqualTo(Duration.ofSeconds(3)); | ||
} | ||
|
||
@Test | ||
public void defaultConfig() { | ||
when(connection.getSpecificConfig()).thenReturn(Collections.emptyMap()); | ||
final HttpPushSpecificConfig specificConfig = HttpPushSpecificConfig.fromConnection(connection, httpConfig); | ||
|
||
assertThat(specificConfig.idleTimeout()).isEqualTo(Duration.ofSeconds(2)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters