From d9a0688244a632bdeafc17b0ae0509f6486dcf6e Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Thu, 23 Nov 2023 10:18:12 +0100 Subject: [PATCH] GH-1542: Fix regression about creating attributes with null value. --- .../core/link/attributes/AttributeModel.java | 7 +- .../PositiveDoubleAttributeModel.java | 2 +- .../PositiveLongAttributeModel.java | 2 +- .../lwm2m/attributes/LwM2mAttributesTest.java | 77 +++++++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 leshan-core/src/test/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributesTest.java diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/attributes/AttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/attributes/AttributeModel.java index 7fbe9f2a9f..a7591af33f 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/attributes/AttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/attributes/AttributeModel.java @@ -22,7 +22,7 @@ */ public abstract class AttributeModel { - private String name; + private final String name; public AttributeModel(String name) { this.name = name; @@ -55,4 +55,9 @@ public T createEmptyAttribute() throws UnsupportedOperationException { public boolean linkAttributeCanBeValueless() { return false; } + + @Override + public String toString() { + return getName(); + } } diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveDoubleAttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveDoubleAttributeModel.java index 0967908e35..cafdfb6ed7 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveDoubleAttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveDoubleAttributeModel.java @@ -31,7 +31,7 @@ public PositiveDoubleAttributeModel(String coRELinkParam, Attachment attachment, @Override public String getInvalidValueCause(Double value) { - if (value < 0) { + if (value != null && value < 0) { return String.format("'%s' attribute value must not be negative", getName()); } return null; diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveLongAttributeModel.java b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveLongAttributeModel.java index 306d6a79bf..334dbb67a5 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveLongAttributeModel.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/link/lwm2m/attributes/PositiveLongAttributeModel.java @@ -31,7 +31,7 @@ public PositiveLongAttributeModel(String coRELinkParam, Attachment attachment, @Override public String getInvalidValueCause(Long value) { - if (value < 0) { + if (value != null && value < 0) { return String.format("'%s' attribute value must not be negative", getName()); } return null; diff --git a/leshan-core/src/test/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributesTest.java b/leshan-core/src/test/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributesTest.java new file mode 100644 index 0000000000..c6097ed886 --- /dev/null +++ b/leshan-core/src/test/java/org/eclipse/leshan/core/link/lwm2m/attributes/LwM2mAttributesTest.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2023 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.core.link.lwm2m.attributes; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.stream.Stream; + +import org.eclipse.leshan.core.link.attributes.InvalidAttributeException; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class LwM2mAttributesTest { + + private static Stream supportNullAttributes() throws InvalidAttributeException { + return Stream.of(// + Arguments.of(LwM2mAttributes.MINIMUM_PERIOD), // + Arguments.of(LwM2mAttributes.MAXIMUM_PERIOD), // + Arguments.of(LwM2mAttributes.EVALUATE_MINIMUM_PERIOD), // + Arguments.of(LwM2mAttributes.EVALUATE_MAXIMUM_PERIOD), // + Arguments.of(LwM2mAttributes.LESSER_THAN), // + Arguments.of(LwM2mAttributes.GREATER_THAN), // + Arguments.of(LwM2mAttributes.STEP) // + ); + } + + @ParameterizedTest(name = "Tests {index} : {0}") + @MethodSource("supportNullAttributes") + public void check_attribute_can_be_created_with_null_value(LwM2mAttributeModel model) + throws InvalidAttributeException { + LwM2mAttribute attribute = LwM2mAttributes.create(model); + assertNotNull(attribute); + assertFalse(attribute.hasValue()); + assertNull(attribute.getValue()); + attribute = LwM2mAttributes.create(model, null); + assertNotNull(attribute); + assertFalse(attribute.hasValue()); + assertNull(attribute.getValue()); + } + + private static Stream doesntSupportNullAttributes() throws InvalidAttributeException { + return Stream.of(// + Arguments.of(LwM2mAttributes.DIMENSION), // + Arguments.of(LwM2mAttributes.ENABLER_VERSION), // + Arguments.of(LwM2mAttributes.OBJECT_VERSION), // + Arguments.of(LwM2mAttributes.SHORT_SERVER_ID), // + Arguments.of(LwM2mAttributes.SERVER_URI)// + ); + } + + @Disabled + @ParameterizedTest(name = "Tests {index} : {0}") + @MethodSource("doesntSupportNullAttributes") + // TODO implement this correctly : https://github.com/eclipse-leshan/leshan/issues/1542 + public void check_attribute_can_not_be_created_with_null_value(LwM2mAttributeModel model) { + assertThrows(InvalidAttributeException.class, () -> LwM2mAttributes.create(model)); + assertThrows(InvalidAttributeException.class, () -> LwM2mAttributes.create(model, null)); + } +}