예제 #1
0
  @Test
  public void cannot_write_non_writable_resource() {
    // try to write unwritable resource like manufacturer on device
    final String manufacturer = "new manufacturer";
    WriteResponse response =
        helper.server.send(
            helper.getClient(), new WriteRequest(Mode.REPLACE, 3, 0, 0, manufacturer));

    // verify result
    assertEquals(ResponseCode.METHOD_NOT_ALLOWED, response.getCode());
  }
예제 #2
0
  @Test
  public void can_write_replace_resource_in_json() {
    // write device timezone
    final String timeZone = "Europe/Paris";
    WriteResponse response =
        helper.server.send(helper.getClient(), new WriteRequest(Mode.REPLACE, 3, 0, 15, timeZone));

    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());

    // read the timezone to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getClient(), new ReadRequest(3, 0, 15));
    LwM2mResource resource = (LwM2mResource) readResponse.getContent();
    assertEquals(timeZone, resource.getValue());
  }
예제 #3
0
 @Override
 public WriteResponse write(int resourceid, LwM2mResource value) {
   LwM2mResource previousValue = resources.get(resourceid);
   resources.put(resourceid, value);
   if (!value.equals(previousValue)) fireResourceChange(resourceid);
   return WriteResponse.success();
 }
예제 #4
0
  @Test
  public void can_write_object_instance() {
    // write device timezone and offset
    LwM2mResource utcOffset = LwM2mSingleResource.newStringResource(14, "+02");
    LwM2mResource timeZone = LwM2mSingleResource.newStringResource(15, "Europe/Paris");
    WriteResponse response =
        helper.server.send(
            helper.getClient(), new WriteRequest(Mode.REPLACE, 3, 0, utcOffset, timeZone));

    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());

    // read the timezone to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getClient(), new ReadRequest(3, 0));
    LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent();
    assertEquals(utcOffset, instance.getResource(14));
    assertEquals(timeZone, instance.getResource(15));
  }
예제 #5
0
  @Override
  protected WriteResponse doWrite(WriteRequest request) {
    LwM2mPath path = request.getPath();

    // Manage Instance case
    LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
    if (instance == null) return WriteResponse.notFound();

    if (path.getResourceId() == null) {
      for (LwM2mResource resource :
          ((LwM2mObjectInstance) request.getNode()).getResources().values()) {
        instance.write(resource.getId(), resource);
      }
      return WriteResponse.success();
    }

    // Manage Resource case
    return instance.write(path.getResourceId(), (LwM2mResource) request.getNode());
  }