some test for CoAP reosurce manager

Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/c4351ac4
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/c4351ac4
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/c4351ac4

Branch: refs/heads/trunk
Commit: c4351ac44436aa117bf4bd8677fbbc25a6044b11
Parents: db9d78e
Author: jvermillard <[email protected]>
Authored: Mon May 27 14:08:26 2013 +0200
Committer: jvermillard <[email protected]>
Committed: Mon May 27 14:08:26 2013 +0200

----------------------------------------------------------------------
 .../mina/coap/resource/ResourceRegistry.java       |    2 +-
 .../mina/coap/resource/ResourceRegistryTest.java   |   83 +++++++++++++++
 2 files changed, 84 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/c4351ac4/coap/src/main/java/org/apache/mina/coap/resource/ResourceRegistry.java
----------------------------------------------------------------------
diff --git 
a/coap/src/main/java/org/apache/mina/coap/resource/ResourceRegistry.java 
b/coap/src/main/java/org/apache/mina/coap/resource/ResourceRegistry.java
index 81ee4bb..dd14e09 100644
--- a/coap/src/main/java/org/apache/mina/coap/resource/ResourceRegistry.java
+++ b/coap/src/main/java/org/apache/mina/coap/resource/ResourceRegistry.java
@@ -91,7 +91,7 @@ public class ResourceRegistry {
                 // 4.04 !
                 return new CoapMessage(1, MessageType.ACK, 
CoapCode.NOT_FOUND.getCode(), request.getId(),
                         request.getToken(), new CoapOption[] { new 
CoapOption(CoapOptionType.CONTENT_FORMAT,
-                                new byte[] { 0 }) }, "meh !".getBytes());
+                                new byte[] { 0 }) }, "not found !".getBytes());
             } else {
                 CoapResponse response = handler.handle(request);
                 return new CoapMessage(1, request.getType() == 
MessageType.CONFIRMABLE ? MessageType.ACK

http://git-wip-us.apache.org/repos/asf/mina/blob/c4351ac4/coap/src/test/java/org/apache/mina/coap/resource/ResourceRegistryTest.java
----------------------------------------------------------------------
diff --git 
a/coap/src/test/java/org/apache/mina/coap/resource/ResourceRegistryTest.java 
b/coap/src/test/java/org/apache/mina/coap/resource/ResourceRegistryTest.java
new file mode 100644
index 0000000..767811a
--- /dev/null
+++ b/coap/src/test/java/org/apache/mina/coap/resource/ResourceRegistryTest.java
@@ -0,0 +1,83 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.coap.resource;
+
+import static org.junit.Assert.*;
+
+import org.apache.mina.coap.CoapCode;
+import org.apache.mina.coap.CoapMessage;
+import org.apache.mina.coap.CoapOption;
+import org.apache.mina.coap.CoapOptionType;
+import org.apache.mina.coap.MessageType;
+import org.junit.Test;
+
+/**
+ * 
+ * @author <a href="http://mina.apache.org";>Apache MINA Project</a>
+ */
+public class ResourceRegistryTest {
+
+    @Test
+    public void generate_4_04_on_unknown_resources() {
+        ResourceRegistry reg = new ResourceRegistry();
+        CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, 
CoapCode.GET.getCode(), 123, null,
+                new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, 
"test".getBytes()) }, new byte[] {});
+        CoapMessage resp = reg.respond(msg);
+        assertEquals(1, resp.getVersion());
+        assertEquals(CoapCode.NOT_FOUND.getCode(), resp.getCode());
+        assertArrayEquals(new CoapOption[] { new 
CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) },
+                resp.getOptions());
+        assertArrayEquals("not found !".getBytes(), resp.getPayload());
+    }
+
+    @Test
+    public void call_resource_handler() {
+        ResourceRegistry reg = new ResourceRegistry();
+        ResourceHandler handler = new AbstractResourceHandler() {
+
+            @Override
+            public CoapResponse handle(CoapMessage request) {
+                return new CoapResponse(12345, "bla bla".getBytes(), new 
CoapOption[] {});
+            }
+
+            @Override
+            public String getPath() {
+                return "myTest/Path";
+            }
+        };
+        reg.register(handler);
+
+        // 4.04 on bad path
+        CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, 
CoapCode.GET.getCode(), 123, null,
+                new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, 
"meh".getBytes()) }, new byte[] {});
+        CoapMessage resp = reg.respond(msg);
+        assertEquals(CoapCode.NOT_FOUND.getCode(), resp.getCode());
+
+        // on correct path the 2.02 with the response
+        msg = new CoapMessage(1, MessageType.CONFIRMABLE, 
CoapCode.GET.getCode(), 123, null, new CoapOption[] {
+                                new CoapOption(CoapOptionType.URI_PATH, 
"myTest".getBytes()),
+                                new CoapOption(CoapOptionType.URI_PATH, 
"Path".getBytes()) }, new byte[] {});
+        resp = reg.respond(msg);
+        assertEquals(1, resp.getVersion());
+        assertEquals(12345, resp.getCode());
+        assertArrayEquals(new CoapOption[] {}, resp.getOptions());
+        assertArrayEquals("bla bla".getBytes(), resp.getPayload());
+    }
+}
\ No newline at end of file

Reply via email to