Skip to content

Commit

Permalink
GH-1542: Fix regression about creating attributes with null value.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Nov 23, 2023
1 parent 2afadfc commit d9a0688
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public abstract class AttributeModel<T extends Attribute> {

private String name;
private final String name;

public AttributeModel(String name) {
this.name = name;
Expand Down Expand Up @@ -55,4 +55,9 @@ public T createEmptyAttribute() throws UnsupportedOperationException {
public boolean linkAttributeCanBeValueless() {
return false;
}

@Override
public String toString() {
return getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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<Arguments> 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));
}
}

0 comments on commit d9a0688

Please sign in to comment.