From ea6dadb861b0f5979bba1bcd0d269667e0bdde36 Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Wed, 4 Oct 2017 15:09:26 +0200 Subject: [PATCH] Add Unknown LWM2M responseCode --- leshan-core-cf/pom.xml | 6 ++ .../core/californium/ResponseCodeUtil.java | 12 +-- .../californium/ResponseCodeUtilTest.java | 84 +++++++++++++++++++ .../java/org/eclipse/leshan/ResponseCode.java | 18 +++- 4 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 leshan-core-cf/src/test/java/org/eclipse/leshan/core/californium/ResponseCodeUtilTest.java diff --git a/leshan-core-cf/pom.xml b/leshan-core-cf/pom.xml index 7e06d9cc5d..289490e524 100644 --- a/leshan-core-cf/pom.xml +++ b/leshan-core-cf/pom.xml @@ -40,6 +40,12 @@ Contributors: org.eclipse.californium scandium + + + junit + junit + test + diff --git a/leshan-core-cf/src/main/java/org/eclipse/leshan/core/californium/ResponseCodeUtil.java b/leshan-core-cf/src/main/java/org/eclipse/leshan/core/californium/ResponseCodeUtil.java index ded02b7b87..dc2fac26a4 100644 --- a/leshan-core-cf/src/main/java/org/eclipse/leshan/core/californium/ResponseCodeUtil.java +++ b/leshan-core-cf/src/main/java/org/eclipse/leshan/core/californium/ResponseCodeUtil.java @@ -28,11 +28,7 @@ public static int toLwM2mCode(org.eclipse.californium.core.coap.CoAP.ResponseCod public static ResponseCode toLwM2mResponseCode( org.eclipse.californium.core.coap.CoAP.ResponseCode coapResponseCode) { - ResponseCode lwm2mResponseCode = ResponseCode.fromCode(toLwM2mCode(coapResponseCode)); - if (lwm2mResponseCode == null) - throw new IllegalArgumentException("Invalid CoAP code for LWM2M response: " + coapResponseCode); - - return lwm2mResponseCode; + return ResponseCode.fromCode(toLwM2mCode(coapResponseCode)); } public static int toLwM2mCode(int coapCode) { @@ -42,11 +38,7 @@ public static int toLwM2mCode(int coapCode) { } public static ResponseCode toLwM2mResponseCode(int coapCode) { - ResponseCode lwm2mResponseCode = ResponseCode.fromCode(toLwM2mCode(coapCode)); - if (lwm2mResponseCode == null) - throw new IllegalArgumentException("Invalid CoAP code for LWM2M response: " + coapCode); - - return lwm2mResponseCode; + return ResponseCode.fromCode(toLwM2mCode(coapCode)); } public static int toCoapCode(int lwm2mCode) { diff --git a/leshan-core-cf/src/test/java/org/eclipse/leshan/core/californium/ResponseCodeUtilTest.java b/leshan-core-cf/src/test/java/org/eclipse/leshan/core/californium/ResponseCodeUtilTest.java new file mode 100644 index 0000000000..c3aeb5cdae --- /dev/null +++ b/leshan-core-cf/src/test/java/org/eclipse/leshan/core/californium/ResponseCodeUtilTest.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2017 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.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.californium; + +import org.eclipse.californium.core.coap.CoAP.ResponseCode; +import org.junit.Assert; +import org.junit.Test; + +public class ResponseCodeUtilTest { + + @Test + public void known_coap_code_to_known_lwm2m_Code() { + org.eclipse.leshan.ResponseCode lwM2mResponseCode = ResponseCodeUtil.toLwM2mResponseCode(ResponseCode.CREATED); + + Assert.assertEquals(org.eclipse.leshan.ResponseCode.CREATED, lwM2mResponseCode); + Assert.assertEquals("CREATED", lwM2mResponseCode.toString()); + } + + @Test + public void known_coap_code_to_unknown_lwm2m_code() { + org.eclipse.leshan.ResponseCode lwM2mResponseCode = ResponseCodeUtil + .toLwM2mResponseCode(ResponseCode.GATEWAY_TIMEOUT); + + Assert.assertEquals(504, lwM2mResponseCode.getCode()); + Assert.assertEquals(org.eclipse.leshan.ResponseCode.UNKNOWN, lwM2mResponseCode.getName()); + Assert.assertEquals("UNKNOWN(504)", lwM2mResponseCode.toString()); + } + + @Test + public void known_lwm2m_code_to_known_coap_code() { + ResponseCode coapResponseCode = ResponseCodeUtil + .toCoapResponseCode(org.eclipse.leshan.ResponseCode.BAD_REQUEST); + + Assert.assertEquals(ResponseCode.BAD_REQUEST, coapResponseCode); + } + + @Test + public void unknown_lwm2m_code_to_known_coap_code() { + ResponseCode coapResponseCode = ResponseCodeUtil.toCoapResponseCode(new org.eclipse.leshan.ResponseCode(503)); + + Assert.assertEquals(ResponseCode.SERVICE_UNAVAILABLE, coapResponseCode); + } + + @Test(expected = IllegalArgumentException.class) + public void unknown_lwm2m_code_to_invalid_coap_code() { + ResponseCodeUtil.toCoapResponseCode(new org.eclipse.leshan.ResponseCode(301)); + } + + @Test(expected = IllegalArgumentException.class) + public void unknown_lwm2m_code_to_invalid_coap_code2() { + ResponseCodeUtil.toCoapResponseCode(new org.eclipse.leshan.ResponseCode(441)); + } + + @Test + public void unknown_lwm2m_code_to_unknown_coap_code() { + // californium behavior is not really consistent + + // for success : code value is lost but we know we use an unknown code + ResponseCode coapResponseCode = ResponseCodeUtil.toCoapResponseCode(new org.eclipse.leshan.ResponseCode(206)); + Assert.assertEquals(ResponseCode._UNKNOWN_SUCCESS_CODE, coapResponseCode); + + // for client error,: unknown code is replace by BAD REQUEST ... + coapResponseCode = ResponseCodeUtil.toCoapResponseCode(new org.eclipse.leshan.ResponseCode(425)); + Assert.assertEquals(ResponseCode.BAD_REQUEST, coapResponseCode); + + // for server error : unknown code is replace by INTERNAL SERVER ERROR ... + coapResponseCode = ResponseCodeUtil.toCoapResponseCode(new org.eclipse.leshan.ResponseCode(509)); + Assert.assertEquals(ResponseCode.INTERNAL_SERVER_ERROR, coapResponseCode); + + } +} diff --git a/leshan-core/src/main/java/org/eclipse/leshan/ResponseCode.java b/leshan-core/src/main/java/org/eclipse/leshan/ResponseCode.java index c85361e5ac..ebc6b8ce60 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/ResponseCode.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/ResponseCode.java @@ -15,10 +15,16 @@ *******************************************************************************/ package org.eclipse.leshan; +import org.eclipse.leshan.util.Validate; + /** * Response codes defined for LWM2M enabler */ public class ResponseCode { + + /** Common name for unknown response code. That means "not explicitly" defined in the LWM2M specification */ + public final static String UNKNOWN = "UNKNOWN"; + /** Resource correctly created */ public final static int CREATED_CODE = 201; /** Resource correctly deleted */ @@ -68,10 +74,15 @@ public class ResponseCode { private String name; public ResponseCode(int code, String name) { + Validate.notNull(name); this.code = code; this.name = name; } + public ResponseCode(int code) { + this(code, UNKNOWN); + } + public int getCode() { return code; } @@ -99,11 +110,14 @@ public static ResponseCode fromCode(int code) { return c; } } - return null; + return new ResponseCode(code); } @Override public String toString() { - return name; + if (UNKNOWN.equals(name)) + return String.format("%s(%d)", name, code); + else + return name; } }