From 202dcf3885ce54b4f1a4e286cbf9e23d3b724f7e Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Wed, 24 Jun 2020 14:55:56 +0200 Subject: [PATCH] Fix TEXT format can be used to Write OPAQUE resource. (see #675) --- .../leshan/core/request/WriteRequest.java | 34 ++++++++----------- .../leshan/integration/tests/WriteTest.java | 5 +++ 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/request/WriteRequest.java b/leshan-core/src/main/java/org/eclipse/leshan/core/request/WriteRequest.java index 94c05c706c..b8c1aa3c1c 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/request/WriteRequest.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/request/WriteRequest.java @@ -40,16 +40,16 @@ public class WriteRequest extends AbstractDownlinkRequest { * Define the behavior of a write request. */ public enum Mode { - /** - * Replaces the Object Instance or the Resource(s) with the new value provided in the “Write” operation. (see - * section 5.3.3 of the LW M2M spec). - */ - REPLACE, - /** - * Adds or updates Resources provided in the new value and leaves other existing Resources unchanged. (see section - * 5.3.3 of the LW M2M spec). - */ - UPDATE + /** + * Replaces the Object Instance or the Resource(s) with the new value provided in the “Write” operation. (see + * section 5.3.3 of the LW M2M spec). + */ + REPLACE, + /** + * Adds or updates Resources provided in the new value and leaves other existing Resources unchanged. (see + * section 5.3.3 of the LW M2M spec). + */ + UPDATE } private final LwM2mNode node; @@ -332,16 +332,10 @@ private WriteRequest(Mode mode, ContentFormat format, LwM2mPath target, LwM2mNod throw new InvalidRequestException( "Invalid format for path %s: format must be used only for single resources", target, format); - } else { - if (resource.getType() == Type.OPAQUE && format == ContentFormat.TEXT) { - throw new InvalidRequestException( - "Invalid format for %s: TEXT format must not be used for byte array single resources", - target); - } else if (resource.getType() != Type.OPAQUE && format == ContentFormat.OPAQUE) { - throw new InvalidRequestException( - "Invalid format for %s: OPAQUE format must be used only for byte array single resources", - target); - } + } else if (resource.getType() != Type.OPAQUE && format == ContentFormat.OPAQUE) { + throw new InvalidRequestException( + "Invalid format for %s: OPAQUE format must be used only for byte array single resources", + target); } } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/WriteTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/WriteTest.java index fcf3c0c801..9158d6c9d9 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/WriteTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/WriteTest.java @@ -304,6 +304,11 @@ public void can_write_opaque_resource_in_json() throws InterruptedException { write_opaque_resource(ContentFormat.JSON); } + @Test + public void can_write_opaque_resource_in_text() throws InterruptedException { + write_opaque_resource(ContentFormat.TEXT); + } + @Test public void can_write_opaque_resource_in_old_json() throws InterruptedException { write_opaque_resource(ContentFormat.fromCode(ContentFormat.OLD_JSON_CODE));