Skip to content

Commit

Permalink
#1026 Evaluation attributes support
Browse files Browse the repository at this point in the history
  • Loading branch information
intx82 committed Jun 17, 2021
1 parent 7c5a398 commit 09a2656
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class Attribute {
public static final String GREATER_THAN = "gt";
public static final String LESSER_THAN = "lt";
public static final String STEP = "st";
public static final String EVALUATE_MINIMUM_PERIOD = "epmin";
public static final String EVALUATE_MAXIMUM_PERIOD = "epmax";

/**
* Metadata container for LwM2m attributes
Expand Down Expand Up @@ -81,6 +83,14 @@ private AttributeModel(String coRELinkParam, Attachment attachment, Set<Assignat
EnumSet.of(AssignationLevel.RESOURCE), AccessMode.RW, Double.class));
modelMap.put(STEP, new AttributeModel(STEP, Attachment.RESOURCE, EnumSet.of(AssignationLevel.RESOURCE),
AccessMode.RW, Double.class));
modelMap.put(EVALUATE_MINIMUM_PERIOD,
new Attribute.AttributeModel(EVALUATE_MINIMUM_PERIOD, Attachment.RESOURCE,
EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.INSTANCE, AssignationLevel.RESOURCE),
AccessMode.RW, Long.class));
modelMap.put(EVALUATE_MAXIMUM_PERIOD,
new Attribute.AttributeModel(EVALUATE_MAXIMUM_PERIOD, Attachment.RESOURCE,
EnumSet.of(AssignationLevel.OBJECT, AssignationLevel.INSTANCE, AssignationLevel.RESOURCE),
AccessMode.RW, Long.class));
}

private final AttributeModel model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public void validate(AssignationLevel assignationLevel) {
throw new IllegalArgumentException(String.format("Cannot write attributes where '%s' > '%s'",
pmin.getCoRELinkParam(), pmax.getCoRELinkParam()));
}

Attribute epmin = attributeMap.get(Attribute.EVALUATE_MINIMUM_PERIOD);
Attribute epmax = attributeMap.get(Attribute.EVALUATE_MAXIMUM_PERIOD);
if ((epmin != null) && (epmax != null) && (Long) epmin.getValue() > (Long) epmax.getValue()) {
throw new IllegalArgumentException(String.format("Cannot write attributes where '%s' > '%s'",
epmin.getCoRELinkParam(), epmax.getCoRELinkParam()));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public class AttributeSetTest {
@Test
public void should_provide_query_params() {
AttributeSet sut = new AttributeSet(new Attribute(Attribute.OBJECT_VERSION, "1.1"),
new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L));
assertEquals("ver=1.1&pmin=5&pmax=60", sut.toString());
new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L),
new Attribute(Attribute.EVALUATE_MINIMUM_PERIOD, 30L),new Attribute(Attribute.EVALUATE_MAXIMUM_PERIOD, 45L));
assertEquals("ver=1.1&pmin=5&pmax=60&epmin=30&epmax=45", sut.toString());

AttributeSet res = AttributeSet.parse(sut.toString());
assertEquals(sut, res);
Expand All @@ -38,8 +39,9 @@ public void should_provide_query_params() {
@Test
public void no_value_to_unset() {
AttributeSet sut = new AttributeSet(new Attribute(Attribute.MINIMUM_PERIOD),
new Attribute(Attribute.MAXIMUM_PERIOD));
assertEquals("pmin&pmax", sut.toString());
new Attribute(Attribute.MAXIMUM_PERIOD), new Attribute(Attribute.EVALUATE_MINIMUM_PERIOD),
new Attribute(Attribute.EVALUATE_MAXIMUM_PERIOD));
assertEquals("pmin&pmax&epmin&epmax", sut.toString());

AttributeSet res = AttributeSet.parse(sut.toString());
assertEquals(sut, res);
Expand All @@ -48,11 +50,14 @@ public void no_value_to_unset() {
@Test
public void should_get_map() {
AttributeSet sut = new AttributeSet(new Attribute(Attribute.OBJECT_VERSION, "1.1"),
new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L));
new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L),
new Attribute(Attribute.EVALUATE_MINIMUM_PERIOD, 30L),new Attribute(Attribute.EVALUATE_MAXIMUM_PERIOD, 45L));
Map<String, Object> map = sut.getMap();
assertEquals("1.1", map.get("ver"));
assertEquals(5L, map.get("pmin"));
assertEquals(60L, map.get("pmax"));
assertEquals(45L, map.get("epmax"));
assertEquals(30L, map.get("epmin"));
}

@Test
Expand All @@ -79,9 +84,10 @@ public void should_merge() {
@Test
public void should_to_string() {
AttributeSet sut = new AttributeSet(new Attribute(Attribute.OBJECT_VERSION, "1.1"),
new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L));
new Attribute(Attribute.MINIMUM_PERIOD, 5L), new Attribute(Attribute.MAXIMUM_PERIOD, 60L),
new Attribute(Attribute.EVALUATE_MINIMUM_PERIOD, 30L),new Attribute(Attribute.EVALUATE_MAXIMUM_PERIOD, 45L));

assertEquals("ver=1.1&pmin=5&pmax=60", sut.toString());
assertEquals("ver=1.1&pmin=5&pmax=60&epmin=30&epmax=45", sut.toString());
}

@Test(expected = IllegalArgumentException.class)
Expand Down Expand Up @@ -116,4 +122,13 @@ public void should_throw_on_invalid_pmin_pmax() {
// pmin cannot be greater then pmax
sut.validate(AssignationLevel.RESOURCE);
}

@Test(expected = IllegalArgumentException.class)
public void should_throw_on_invalid_epmin_epmax() {
AttributeSet sut = new AttributeSet(new Attribute(Attribute.EVALUATE_MINIMUM_PERIOD, 50L),
new Attribute(Attribute.EVALUATE_MAXIMUM_PERIOD, 49L));

// pmin cannot be greater then pmax
sut.validate(AssignationLevel.RESOURCE);
}
}

0 comments on commit 09a2656

Please sign in to comment.