From 43e1238701cd4c323791b8f4e4ab37f457d4bf92 Mon Sep 17 00:00:00 2001 From: LM Date: Wed, 18 Dec 2019 17:37:50 -0800 Subject: [PATCH 1/2] Add ReminderInfo --- .../io/dapr/actors/runtime/ReminderInfo.java | 103 ++++++++++++++++++ .../dapr/actors/runtime/ReminderInfoTest.java | 60 ++++++++++ 2 files changed, 163 insertions(+) create mode 100644 sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java create mode 100644 sdk/src/test/java/io/dapr/actors/runtime/ReminderInfoTest.java diff --git a/sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java b/sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java new file mode 100644 index 0000000000..524e6426ce --- /dev/null +++ b/sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java @@ -0,0 +1,103 @@ +// ------------------------------------------------------------ +// 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(); + + final ObjectMapper mapper = new ObjectMapper(); + final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class); + final Map data = mapper.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); + } + } +} diff --git a/sdk/src/test/java/io/dapr/actors/runtime/ReminderInfoTest.java b/sdk/src/test/java/io/dapr/actors/runtime/ReminderInfoTest.java new file mode 100644 index 0000000000..4f56ba404a --- /dev/null +++ b/sdk/src/test/java/io/dapr/actors/runtime/ReminderInfoTest.java @@ -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); + } +} From bdeef453629c384166e26b45faa2df1d8d7b3df3 Mon Sep 17 00:00:00 2001 From: LM Date: Wed, 18 Dec 2019 17:41:27 -0800 Subject: [PATCH 2/2] remove extra var --- sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java b/sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java index 524e6426ce..4fd1cb685c 100644 --- a/sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java +++ b/sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java @@ -63,10 +63,8 @@ String serialize() throws IOException { static ReminderInfo deserialize(byte[] stream) throws IOException { try { ObjectMapper om = new ObjectMapper(); - - final ObjectMapper mapper = new ObjectMapper(); - final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class); - final Map data = mapper.readValue(stream, type); + MapType type = om.getTypeFactory().constructMapType(Map.class, String.class, Object.class); + Map data = om.readValue(stream, type); String d = (String)data.getOrDefault("dueTime", ""); Duration dueTime = ConverterUtils.ConvertDurationFromDaprFormat(d);