Add ReminderInfo (#54)

* Add ReminderInfo

* remove extra var
This commit is contained in:
Leon Mai 2019-12-19 16:16:52 -08:00 committed by Artur Souza
parent 4bd9942b8c
commit b229d462e9
2 changed files with 161 additions and 0 deletions

View File

@ -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);
}
}
}

View File

@ -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);
}
}