Skip to content

Commit

Permalink
SendRequest constructor should send InvalidRequestException
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Aug 13, 2024
1 parent 583807b commit 8281f23
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.eclipse.leshan.core.node.TimestampedLwM2mNodes;
import org.eclipse.leshan.core.request.exception.InvalidRequestException;
import org.eclipse.leshan.core.response.SendResponse;
import org.eclipse.leshan.core.util.Validate;

/**
* The "Send" operation is used by the LwM2M Client to send data to the LwM2M Server without explicit request by that
Expand All @@ -42,6 +41,14 @@ public class SendRequest extends AbstractLwM2mRequest<SendResponse>
private final ContentFormat format;
private final TimestampedLwM2mNodes timestampedNodes;

private static final TimestampedLwM2mNodes mapToTimestampedNodes(Map<LwM2mPath, LwM2mNode> nodes) {
try {
return TimestampedLwM2mNodes.builder().addNodes(nodes).build();
} catch (Exception e) {
throw new InvalidRequestException(e, "Invalid nodes");
}
}

/**
* @param format {@link ContentFormat} used to encode data. It MUST be {@link ContentFormat#SENML_CBOR} or
* {@link ContentFormat#SENML_JSON}
Expand All @@ -53,7 +60,7 @@ public SendRequest(ContentFormat format, Map<LwM2mPath, LwM2mNode> nodes) {
}

public SendRequest(ContentFormat format, Map<LwM2mPath, LwM2mNode> nodes, Object coapRequest) {
this(format, TimestampedLwM2mNodes.builder().addNodes(nodes).build(), coapRequest);
this(format, mapToTimestampedNodes(nodes), coapRequest);
}

public SendRequest(ContentFormat format, TimestampedLwM2mNodes timestampedNodes, Object coapRequest) {
Expand All @@ -70,12 +77,19 @@ public SendRequest(ContentFormat format, TimestampedLwM2mNodes timestampedNodes,
}

private void validateNodes(Map<LwM2mPath, LwM2mNode> nodes) {
Validate.notEmpty(nodes);
if (nodes == null || nodes.size() == 0) {
throw new InvalidRequestException(
"SendRequest MUST NOT have empty payload (at least 1 node should be present)");
}
for (Entry<LwM2mPath, LwM2mNode> entry : nodes.entrySet()) {
LwM2mPath path = entry.getKey();
LwM2mNode node = entry.getValue();
Validate.notNull(path);
Validate.notNull(node);
if (path == null) {
throw new InvalidRequestException("Invalid key for entry (null, %s) : path MUST NOT be null", node);
}
if (node == null) {
throw new InvalidRequestException("Invalid value for entry (%s, null) : node MUST NOT be null ", path);
}

if (path.isObject() && node instanceof LwM2mObject)
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
* Copyright (c) 2024 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.request;

import static org.junit.jupiter.api.Assertions.assertThrowsExactly;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
import org.eclipse.leshan.core.request.exception.InvalidRequestException;
import org.junit.jupiter.api.Test;

public class SendRequestTest {

@Test
public void send_request_with_empty_nodes_should_fail() {
assertThrowsExactly(InvalidRequestException.class, () -> {
new SendRequest(ContentFormat.SENML_JSON, Collections.emptyMap());
});
}

@Test
public void send_request_with_node_without_path_should_fail() {
final Map<LwM2mPath, LwM2mNode> nodes = new HashMap<>();
nodes.put(null, LwM2mSingleResource.newBooleanResource(1, false));

assertThrowsExactly(InvalidRequestException.class, () -> {

new SendRequest(ContentFormat.SENML_JSON, nodes);
});
}

@Test
public void send_request_with_path_without_node_should_fail() {
final Map<LwM2mPath, LwM2mNode> nodes = new HashMap<>();
nodes.put(new LwM2mPath(3, 0, 0), null);

assertThrowsExactly(InvalidRequestException.class, () -> {

new SendRequest(ContentFormat.SENML_JSON, nodes);
});
}
}

0 comments on commit 8281f23

Please sign in to comment.