View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.coap.resource;
    21
    22  import static org.junit.Assert.*;
    23
    24  import org.apache.mina.api.IoSession;
    25  import org.apache.mina.coap.CoapCode;
    26  import org.apache.mina.coap.CoapMessage;
    27  import org.apache.mina.coap.CoapOption;
    28  import org.apache.mina.coap.CoapOptionType;
    29  import org.apache.mina.coap.MessageType;
    30  import org.junit.Test;
    31
    32  /**
    33   *
    34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    35   */
    36  public class ResourceRegistryTest {
    37
    38      @Test
    39      public void generate_4_04_on_unknown_resources() {
    40          ResourceRegistry reg = new ResourceRegistry();
    41          CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 123, null,
    42                  new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, "test".getBytes()) }, new byte[] {});
    43          CoapMessage resp = reg.respond(msg, null);
    44          assertEquals(1, resp.getVersion());
    45          assertEquals(CoapCode.NOT_FOUND.getCode(), resp.getCode());
    46          assertArrayEquals(new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) },
    47                  resp.getOptions());
    48          assertArrayEquals("not found !".getBytes(), resp.getPayload());
    49      }
    50
    51      @Test
    52      public void call_resource_handler() {
    53          ResourceRegistry reg = new ResourceRegistry();
    54          ResourceHandler handler = new AbstractResourceHandler() {
    55
    56              @Override
    57              public CoapResponse handle(CoapMessage request, IoSession session) {
    58                  return new CoapResponse(12345, "bla bla".getBytes(), new CoapOption[] {});
    59              }
    60
    61              @Override
    62              public String getPath() {
    63                  return "myTest/Path";
    64              }
    65          };
    66          reg.register(handler);
    67
    68          // 4.04 on bad path
    69          CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 123, null,
    70                  new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, "meh".getBytes()) }, new byte[] {});
    71          CoapMessage resp = reg.respond(msg, null);
    72          assertEquals(CoapCode.NOT_FOUND.getCode(), resp.getCode());
    73
    74          // on correct path the 2.02 with the response
    75          msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 123, null, new CoapOption[] {
    76                                  new CoapOption(CoapOptionType.URI_PATH, "myTest".getBytes()),
    77                                  new CoapOption(CoapOptionType.URI_PATH, "Path".getBytes()) }, new byte[] {});
    78          resp = reg.respond(msg, null);
    79          assertEquals(1, resp.getVersion());
    80          assertEquals(12345, resp.getCode());
    81          assertArrayEquals(new CoapOption[] {}, resp.getOptions());
    82          assertArrayEquals("bla bla".getBytes(), resp.getPayload());
    83      }
    84  }