-
Notifications
You must be signed in to change notification settings - Fork 230
Add ReminderInfo #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+161
−0
Merged
Add ReminderInfo #54
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java
This file contains hidden or 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,101 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| package io.dapr.actors.runtime; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import com.fasterxml.jackson.databind.type.MapType; | ||
| import java.io.IOException; | ||
| import java.time.*; | ||
| import java.util.Base64; | ||
| import java.util.Map; | ||
|
|
||
| class ReminderInfo | ||
| { | ||
| private final Duration minTimePeriod = Duration.ofMillis(-1); | ||
|
|
||
| public Duration dueTime; | ||
| public Duration period; | ||
| public byte[] data; | ||
|
|
||
| public ReminderInfo() { | ||
| } | ||
|
|
||
| public ReminderInfo(byte[] state, Duration dueTime, Duration period) { | ||
| this.ValidateDueTime("DueTime", dueTime); | ||
| this.ValidatePeriod("Period", period); | ||
| this.data = state; | ||
| this.dueTime = dueTime; | ||
| this.period = period; | ||
| } | ||
|
|
||
| Duration getDueTime() { | ||
| return this.dueTime; | ||
| } | ||
|
|
||
| Duration getPeriod() { | ||
| return this.period; | ||
| } | ||
|
|
||
| byte[] getData() { | ||
| return this.data; | ||
| } | ||
|
|
||
| String serialize() throws IOException { | ||
| try { | ||
| ObjectMapper om = new ObjectMapper(); | ||
| ObjectNode objectNode = om.createObjectNode(); | ||
| objectNode.put("dueTime", ConverterUtils.ConvertDurationToDaprFormat(this.dueTime)); | ||
| objectNode.put("period", ConverterUtils.ConvertDurationToDaprFormat(this.period)); | ||
| if (this.data != null) { | ||
| objectNode.put("data", Base64.getEncoder().encodeToString(this.data)); | ||
| } | ||
|
|
||
| return om.writeValueAsString(objectNode); | ||
| } catch (IOException e) { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| static ReminderInfo deserialize(byte[] stream) throws IOException { | ||
| try { | ||
| ObjectMapper om = new ObjectMapper(); | ||
| MapType type = om.getTypeFactory().constructMapType(Map.class, String.class, Object.class); | ||
| Map<String, Object> data = om.readValue(stream, type); | ||
|
|
||
| String d = (String)data.getOrDefault("dueTime", ""); | ||
| Duration dueTime = ConverterUtils.ConvertDurationFromDaprFormat(d); | ||
|
|
||
| String p = (String)data.getOrDefault("period", ""); | ||
| Duration period = ConverterUtils.ConvertDurationFromDaprFormat(p); | ||
|
|
||
| String s = (String)data.getOrDefault("data", null); | ||
| byte[] state = (s == null) ? null : Base64.getDecoder().decode(s); | ||
|
|
||
| return new ReminderInfo(state, dueTime, period); | ||
| } catch (IOException e) { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| private void ValidateDueTime(String argName, Duration value) | ||
| { | ||
| if (value.compareTo(Duration.ZERO) < 0 ) | ||
| { | ||
| String message = String.format("argName: %s - Duration toMillis() - specified value must be greater than %s", argName, Duration.ZERO); | ||
| throw new IllegalArgumentException(message); | ||
| } | ||
| } | ||
|
|
||
| private void ValidatePeriod(String argName, Duration value) throws IllegalArgumentException | ||
| { | ||
| if (value.compareTo(this.minTimePeriod) < 0) | ||
| { | ||
| String message = String.format("argName: %s - Duration toMillis() - specified value must be greater than %s", argName, Duration.ZERO); | ||
| throw new IllegalArgumentException(message); | ||
| } | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
sdk/src/test/java/io/dapr/actors/runtime/ReminderInfoTest.java
This file contains hidden or 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 @@ | ||
| package io.dapr.actors.runtime; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import java.time.Duration; | ||
| import java.util.Arrays; | ||
|
|
||
| public class ReminderInfoTest { | ||
| @Test(expected = IllegalArgumentException.class) | ||
| public void outOfRangeDueTime() { | ||
| ReminderInfo info = new ReminderInfo(null, Duration.ZERO.plusSeconds(-10), Duration.ZERO.plusMinutes(1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void negativePeriod() { | ||
| // this is ok | ||
| ReminderInfo info = new ReminderInfo(null, Duration.ZERO.plusMinutes(1), Duration.ZERO.plusMillis(-1)); | ||
| } | ||
|
|
||
| @Test(expected = IllegalArgumentException.class) | ||
| public void outOfRangePeriod() { | ||
| ReminderInfo info = new ReminderInfo(null, Duration.ZERO.plusMinutes(1), Duration.ZERO.plusMinutes(-10)); | ||
| } | ||
|
|
||
| @Test | ||
| public void noState() { | ||
| ReminderInfo original = new ReminderInfo(null, Duration.ZERO.plusMinutes(2), Duration.ZERO.plusMinutes((5))); | ||
| ReminderInfo recreated = null; | ||
| try { | ||
| String serialized = original.serialize(); | ||
| recreated = ReminderInfo.deserialize(serialized.getBytes()); | ||
| } | ||
| catch(Exception e) { | ||
| System.out.println("The error is: " + e); | ||
| Assert.fail(); | ||
| } | ||
|
|
||
| Assert.assertEquals(original.data, recreated.data); | ||
| Assert.assertEquals(original.dueTime, recreated.dueTime); | ||
| Assert.assertEquals(original.period, recreated.period); | ||
| } | ||
|
|
||
| @Test | ||
| public void withState() { | ||
| ReminderInfo original = new ReminderInfo("maru".getBytes(), Duration.ZERO.plusMinutes(2), Duration.ZERO.plusMinutes((5))); | ||
| ReminderInfo recreated = null; | ||
| try { | ||
| String serialized = original.serialize(); | ||
| recreated = ReminderInfo.deserialize(serialized.getBytes()); | ||
| } | ||
| catch(Exception e) { | ||
| System.out.println("The error is: " + e); | ||
| Assert.fail(); | ||
| } | ||
|
|
||
| Assert.assertTrue(Arrays.equals(original.data, recreated.data)); | ||
| Assert.assertEquals(original.dueTime, recreated.dueTime); | ||
| Assert.assertEquals(original.period, recreated.period); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.