-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for MQTT message expiry interval
Signed-off-by: Dmitriy Barbul <dimabarbul@gmail.com>
- Loading branch information
1 parent
b80497d
commit 9d0011a
Showing
10 changed files
with
444 additions
and
8 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...g/eclipse/ditto/connectivity/model/mqtt/IllegalMessageExpiryIntervalSecondsException.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,35 @@ | ||
/* | ||
* Copyright (c) 2023 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.model.mqtt; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* This exception is thrown to indicate that the seconds of an MQTT | ||
* message expiry interval is outside its allowed range. | ||
*/ | ||
public final class IllegalMessageExpiryIntervalSecondsException extends Exception { | ||
|
||
private static final long serialVersionUID = -566567001721859949L; | ||
|
||
/** | ||
* Constructs a {@code IllegalMessageExpiryIntervalSecondsException} for the specified detail message argument. | ||
* | ||
* @param detailMessage the detail message of the exception. | ||
* @param cause the cause of the exception or {@code null} if unknown. | ||
*/ | ||
IllegalMessageExpiryIntervalSecondsException(final String detailMessage, @Nullable final Throwable cause) { | ||
super(detailMessage, cause); | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
.../model/src/main/java/org/eclipse/ditto/connectivity/model/mqtt/MessageExpiryInterval.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,86 @@ | ||
/* | ||
* Copyright (c) 2023 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.model.mqtt; | ||
|
||
import java.util.Objects; | ||
import java.util.OptionalLong; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* Representation of the MQTT 5 message expiry interval. | ||
* The minimum seconds is {@value MIN_INTERVAL_SECONDS}. | ||
* The maximum seconds is {@value MAX_INTERVAL_SECONDS}. | ||
*/ | ||
@Immutable | ||
public class MessageExpiryInterval { | ||
|
||
public static final long MIN_INTERVAL_SECONDS = 1L; | ||
|
||
public static final long MAX_INTERVAL_SECONDS = 4_294_967_295L; | ||
|
||
@Nullable private final Long seconds; | ||
|
||
private MessageExpiryInterval(@Nullable final Long seconds) { | ||
this.seconds = seconds; | ||
} | ||
|
||
public static MessageExpiryInterval of(final long seconds) | ||
throws IllegalMessageExpiryIntervalSecondsException { | ||
if (seconds < MIN_INTERVAL_SECONDS || seconds > MAX_INTERVAL_SECONDS) { | ||
throw new IllegalMessageExpiryIntervalSecondsException( | ||
String.format("Expected message expiry interval seconds to be within [%d, %d] but it was <%d>.", | ||
MIN_INTERVAL_SECONDS, | ||
MAX_INTERVAL_SECONDS, | ||
seconds), | ||
null); | ||
} | ||
|
||
return new MessageExpiryInterval(seconds); | ||
} | ||
|
||
public static MessageExpiryInterval empty() { | ||
return new MessageExpiryInterval(null); | ||
} | ||
|
||
public OptionalLong getAsOptionalLong() { | ||
return seconds != null ? | ||
OptionalLong.of(seconds) : | ||
OptionalLong.empty(); | ||
} | ||
|
||
@Override | ||
public boolean equals(@Nullable final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final MessageExpiryInterval that = (MessageExpiryInterval) o; | ||
return Objects.equals(seconds, that.seconds); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(seconds); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + " [" + | ||
"seconds=" + seconds + | ||
"]"; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...el/src/test/java/org/eclipse/ditto/connectivity/model/mqtt/MessageExpiryIntervalTest.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,76 @@ | ||
/* | ||
* Copyright (c) 2023 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.model.mqtt; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf; | ||
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable; | ||
|
||
import org.junit.Test; | ||
|
||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
|
||
/** | ||
* Unit test for {@link MessageExpiryInterval}. | ||
*/ | ||
public final class MessageExpiryIntervalTest { | ||
|
||
@Test | ||
public void assertImmutability() { | ||
assertInstancesOf(MessageExpiryInterval.class, areImmutable()); | ||
} | ||
|
||
@Test | ||
public void testHashCodeAndEquals() { | ||
EqualsVerifier.forClass(MessageExpiryInterval.class) | ||
.usingGetClass() | ||
.verify(); | ||
} | ||
|
||
@Test | ||
public void ofWithMaxValueReturnsExpected() | ||
throws IllegalMessageExpiryIntervalSecondsException { | ||
final MessageExpiryInterval underTest = MessageExpiryInterval.of(MessageExpiryInterval.MAX_INTERVAL_SECONDS); | ||
|
||
assertThat(underTest.getAsOptionalLong()).hasValue(MessageExpiryInterval.MAX_INTERVAL_SECONDS); | ||
} | ||
|
||
@Test | ||
public void ofWithNegativeOutOfBoundsValueThrowsException() { | ||
final long negativeOutOfBoundsSeconds = MessageExpiryInterval.MIN_INTERVAL_SECONDS - 1; | ||
|
||
assertThatExceptionOfType(IllegalMessageExpiryIntervalSecondsException.class) | ||
.isThrownBy(() -> MessageExpiryInterval.of(negativeOutOfBoundsSeconds)) | ||
.withMessageEndingWith("but it was <%d>.", negativeOutOfBoundsSeconds) | ||
.withNoCause(); | ||
} | ||
|
||
@Test | ||
public void ofWithPositiveOutOfBoundsValueThrowsException() { | ||
final long positiveOutOfBoundsSeconds = MessageExpiryInterval.MAX_INTERVAL_SECONDS + 1; | ||
|
||
assertThatExceptionOfType(IllegalMessageExpiryIntervalSecondsException.class) | ||
.isThrownBy(() -> MessageExpiryInterval.of(positiveOutOfBoundsSeconds)) | ||
.withMessageEndingWith("but it was <%d>.", positiveOutOfBoundsSeconds) | ||
.withNoCause(); | ||
} | ||
|
||
@Test | ||
public void emptyReturnsEmpty() { | ||
final MessageExpiryInterval underTest = MessageExpiryInterval.empty(); | ||
|
||
assertThat(underTest.getAsOptionalLong()).isEmpty(); | ||
} | ||
|
||
} |
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
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
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
Oops, something went wrong.