Skip to content

Commit

Permalink
integration test to ensure that 1 observation use same endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Nov 27, 2024
1 parent b9201e3 commit 14fc20e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.leshan.integration.tests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.leshan.core.ResponseCode.CONTENT;
import static org.eclipse.leshan.integration.tests.util.LeshanProxyBuilder.givenReverseProxyFor;
import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing;
import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing;
Expand All @@ -25,13 +26,21 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import org.eclipse.leshan.client.servers.LwM2mServer;
import org.eclipse.leshan.core.ResponseCode;
import org.eclipse.leshan.core.endpoint.Protocol;
import org.eclipse.leshan.core.link.LinkParseException;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
import org.eclipse.leshan.core.observation.Observation;
import org.eclipse.leshan.core.observation.SingleObservation;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.request.ObserveRequest;
import org.eclipse.leshan.core.request.WriteRequest;
import org.eclipse.leshan.core.response.ObserveResponse;
import org.eclipse.leshan.core.response.SendResponse;
import org.eclipse.leshan.integration.tests.util.Failure;
import org.eclipse.leshan.integration.tests.util.LeshanTestClient;
Expand Down Expand Up @@ -229,4 +238,78 @@ protected void register_then_send_on_different_endpoint(LeshanTestServerBuilder
Registration registrationAfterSend = server.getRegistrationFor(client);
assertThat(registrationAfterSend).isEqualTo(registrationBeforeSend);
}

@TestAllTransportLayer
public void observe_then_send_notification_on_different_endpoint(Protocol givenProtocol,
String givenClientEndpointProvider, String givenServerEndpointProvider)
throws LinkParseException, InterruptedException {

observe_then_send_notification_on_different_endpoint(//
givenServerWithTwoEndpoint(givenProtocol, givenServerEndpointProvider), //
givenProtocol, //
givenClientEndpointProvider);
}

@TestAllTransportLayer
public void observe_then_send_notification_on_different_endpoint_with_update_on_notification(Protocol givenProtocol,
String givenClientEndpointProvider, String givenServerEndpointProvider)
throws LinkParseException, InterruptedException {

observe_then_send_notification_on_different_endpoint(//
givenServerWithTwoEndpoint(givenProtocol, givenServerEndpointProvider).withUpdateOnNotification(), //
givenProtocol, //
givenClientEndpointProvider);
}

protected void observe_then_send_notification_on_different_endpoint(LeshanTestServerBuilder givenServer,
Protocol givenProtocol, String givenClientEndpointProvider)
throws LinkParseException, InterruptedException {

// set-up test
setupTestFor(//
givenServer, //
givenProtocol, //
givenClientEndpointProvider);

// Start it and wait for registration
client.start();
server.waitForNewRegistrationOf(client);
client.waitForRegistrationTo(server);

// Check client is well registered
assertThat(client).isRegisteredAt(server);
Registration registration = server.getRegistrationFor(client);

// observe device timezone
ObserveResponse observeResponse = server.send(registration, new ObserveRequest(3, 0, 15));
assertThat(observeResponse) //
.hasCode(CONTENT);

// an observation response should have been sent
SingleObservation observation = observeResponse.getObservation();
assertThat(observation.getPath()).asString().isEqualTo("/3/0/15");
assertThat(observation.getRegistrationId()).isEqualTo(registration.getId());
Set<Observation> observations = server.getObservationService().getObservations(registration);
assertThat(observations).containsExactly(observation);

// change value to trigger new notification
client.getObjectTree().getObjectEnabler(3).write(LwM2mServer.SYSTEM,
new WriteRequest(3, 0, 15, "Europe/London"));

// verify result
server.waitForNewObservation(observation);
ObserveResponse response = server.waitForNotificationOf(observation);
assertThat(response.getContent()).isEqualTo(LwM2mSingleResource.newStringResource(15, "Europe/London"));

// Send request from client to another server endpoint.
proxy.useNextServerAddress();

// change value to trigger new notification
Registration registrationBeforeNotification = server.getRegistrationFor(client);
client.getObjectTree().getObjectEnabler(3).write(LwM2mServer.SYSTEM,
new WriteRequest(3, 0, 15, "Europe/Paris"));
server.ensureNoNotification(observation, 1, TimeUnit.SECONDS);
Registration registrationAfterNotification = server.getRegistrationFor(client);
assertThat(registrationAfterNotification).isEqualTo(registrationBeforeNotification);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2024 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.integration.tests;

import org.eclipse.leshan.core.endpoint.Protocol;
import org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder;
import org.eclipse.leshan.integration.tests.util.RedisTestUtil;
import org.eclipse.leshan.server.redis.RedisRegistrationStore;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.util.Pool;

public class RedisMultiEndpointsTest extends MultiEndpointsTest {

@Override
public LeshanTestServerBuilder givenServerWithTwoEndpoint(Protocol givenProtocol,
String givenServerEndpointProvider) {
LeshanTestServerBuilder builder = super.givenServerWithTwoEndpoint(givenProtocol, givenServerEndpointProvider);

// Create redis store
Pool<Jedis> jedis = RedisTestUtil.createJedisPool();
// TODO use custom key when https://github.com/eclipse/leshan/issues/1249 will be available
builder.setRegistrationStore(new RedisRegistrationStore(jedis));

return builder;
}
}

0 comments on commit 14fc20e

Please sign in to comment.