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());
}
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());
}
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());
}
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());
}
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));
}
Aggregations