Search in sources :

Example 11 with LwM2mPath

use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.

the class ObservationServiceTest method cancel_by_path.

@Test
public void cancel_by_path() {
    // create some observations
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 13));
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
    givenAnObservation("anotherClient", new LwM2mPath(3, 0, 12));
    // check its presence
    Set<Observation> observations = observationService.getObservations(support.registration);
    Assert.assertEquals(2, observations.size());
    // cancel it
    int nbCancelled = observationService.cancelObservations(support.registration, "/3/0/12");
    Assert.assertEquals(1, nbCancelled);
    // check its absence
    observations = observationService.getObservations(support.registration);
    Assert.assertEquals(1, observations.size());
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) Test(org.junit.Test)

Example 12 with LwM2mPath

use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.

the class ObservationServiceTest method cancel_by_client.

@Test
public void cancel_by_client() {
    // create some observations
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 13));
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
    givenAnObservation("anotherClient", new LwM2mPath(3, 0, 12));
    // check its presence
    Set<Observation> observations = observationService.getObservations(support.registration);
    Assert.assertEquals(2, observations.size());
    // cancel it
    int nbCancelled = observationService.cancelObservations(support.registration);
    Assert.assertEquals(2, nbCancelled);
    // check its absence
    observations = observationService.getObservations(support.registration);
    Assert.assertTrue(observations.isEmpty());
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) Test(org.junit.Test)

Example 13 with LwM2mPath

use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.

the class ObserveTest method can_handle_error_on_notification.

@Test
public void can_handle_error_on_notification() throws InterruptedException {
    TestObservationListener listener = new TestObservationListener();
    helper.server.getObservationService().addListener(listener);
    // observe device timezone
    ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3, 0, 15));
    assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
    assertNotNull(observeResponse.getCoapResponse());
    assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
    // an observation response should have been sent
    Observation observation = observeResponse.getObservation();
    assertEquals("/3/0/15", observation.getPath().toString());
    assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
    Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
    assertTrue("We should have only on observation", observations.size() == 1);
    assertTrue("New observation is not there", observations.contains(observation));
    // *** HACK send a notification with unsupported content format *** //
    byte[] payload = LwM2mNodeJsonEncoder.encode(LwM2mSingleResource.newStringResource(15, "Paris"), new LwM2mPath("/3/0/15"), new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
    Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
    // 666 is not a supported //
    sendNotification(getConnector(helper.client), payload, firstCoapResponse, 666);
    // contentFormat.
    // *** Hack End *** //
    // verify result
    listener.waitForNotification(2000);
    assertTrue(listener.receivedNotify().get());
    assertNotNull(listener.getError());
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) DefaultLwM2mValueConverter(org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) Test(org.junit.Test)

Example 14 with LwM2mPath

use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.

the class RegistrationTest method register_observe_deregister_observe.

@Test
public void register_observe_deregister_observe() throws NonUniqueSecurityInfoException, InterruptedException {
    // Check client is not registered
    helper.assertClientNotRegisterered();
    // Start it and wait for registration
    helper.client.start();
    helper.waitForRegistration(1);
    // Check client is well registered
    helper.assertClientRegisterered();
    // observe device timezone
    ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3, 0));
    assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
    assertNotNull(observeResponse.getCoapResponse());
    assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
    // check observation registry is not null
    Registration currentRegistration = helper.getCurrentRegistration();
    Set<Observation> observations = helper.server.getObservationService().getObservations(currentRegistration);
    assertEquals(1, observations.size());
    Observation obs = observations.iterator().next();
    assertEquals(currentRegistration.getId(), obs.getRegistrationId());
    assertEquals(new LwM2mPath(3, 0), obs.getPath());
    // Check de-registration
    helper.client.stop(true);
    helper.waitForDeregistration(1);
    helper.assertClientNotRegisterered();
    observations = helper.server.getObservationService().getObservations(currentRegistration);
    assertTrue(observations.isEmpty());
    // try to send a new observation
    observeResponse = helper.server.send(currentRegistration, new ObserveRequest(3, 0), 50);
    assertNull(observeResponse);
    // check observationStore is empty
    observations = helper.server.getObservationService().getObservations(currentRegistration);
    assertTrue(observations.isEmpty());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) Test(org.junit.Test)

Example 15 with LwM2mPath

use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.

the class LwM2mNodeEncoderTest method json_encode_device_object_instance.

@Test
public void json_encode_device_object_instance() {
    LwM2mObjectInstance oInstance = new LwM2mObjectInstance(0, getDeviceResources());
    byte[] encoded = encoder.encode(oInstance, ContentFormat.JSON, new LwM2mPath("/3/0"), model);
    StringBuilder b = new StringBuilder();
    b.append("{\"bn\":\"/3/0\",\"e\":[");
    b.append("{\"n\":\"0\",\"sv\":\"Open Mobile Alliance\"},");
    b.append("{\"n\":\"1\",\"sv\":\"Lightweight M2M Client\"},");
    b.append("{\"n\":\"2\",\"sv\":\"345000123\"},");
    b.append("{\"n\":\"3\",\"sv\":\"1.0\"},");
    b.append("{\"n\":\"6/0\",\"v\":1},");
    b.append("{\"n\":\"6/1\",\"v\":5},");
    b.append("{\"n\":\"7/0\",\"v\":3800},");
    b.append("{\"n\":\"7/1\",\"v\":5000},");
    b.append("{\"n\":\"8/0\",\"v\":125},");
    b.append("{\"n\":\"8/1\",\"v\":900},");
    b.append("{\"n\":\"9\",\"v\":100},");
    b.append("{\"n\":\"10\",\"v\":15},");
    b.append("{\"n\":\"11/0\",\"v\":0},");
    b.append("{\"n\":\"13\",\"v\":1.367491215E9},");
    b.append("{\"n\":\"14\",\"sv\":\"+02:00\"},");
    b.append("{\"n\":\"16\",\"sv\":\"U\"}]}");
    String expected = b.toString();
    Assert.assertEquals(expected, new String(encoded));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Test(org.junit.Test)

Aggregations

LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)70 Test (org.junit.Test)51 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)25 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)13 Observation (org.eclipse.leshan.core.observation.Observation)11 ArrayList (java.util.ArrayList)10 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)10 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)9 Tlv (org.eclipse.leshan.tlv.Tlv)8 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)7 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)6 CodecException (org.eclipse.leshan.core.node.codec.CodecException)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)6 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)6 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)5 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)5 HashMap (java.util.HashMap)4 Response (org.eclipse.californium.core.coap.Response)4 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4