From b229d462e97b0fefec0fcd6e7af3ca489ade9093 Mon Sep 17 00:00:00 2001 From: Leon Mai Date: Thu, 19 Dec 2019 16:16:52 -0800 Subject: [PATCH] Add ReminderInfo (#54) * Add ReminderInfo * remove extra var --- .../io/dapr/actors/runtime/ReminderInfo.java | 101 ++++++++++++++++++ .../dapr/actors/runtime/ReminderInfoTest.java | 60 +++++++++++ 2 files changed, 161 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 000000000..4fd1cb685 --- /dev/null +++ b/sdk/src/main/java/io/dapr/actors/runtime/ReminderInfo.java @@ -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 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); + } + } +} 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 000000000..4f56ba404 --- /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); + } +}