Skip to content

Commit

Permalink
Add Unknown LWM2M responseCode
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Nov 10, 2017
1 parent 6a620f6 commit ea6dadb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 12 deletions.
6 changes: 6 additions & 0 deletions leshan-core-cf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Contributors:
<groupId>org.eclipse.californium</groupId>
<artifactId>scandium</artifactId>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}
}
18 changes: 16 additions & 2 deletions leshan-core/src/main/java/org/eclipse/leshan/ResponseCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit ea6dadb

Please sign in to comment.