Skip to content

Commit

Permalink
Do not create dummy object implicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Feb 4, 2019
1 parent cf4114b commit d4c09ad
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class Device extends BaseInstanceEnabler {
private String timezone = TimeZone.getDefault().getID();
private String utcOffset = new SimpleDateFormat("X").format(Calendar.getInstance().getTime());

public Device() {
// should never be used
}

public Device(String manufacturer, String modelNumber, String serialNumber, String supportedBinding) {
this.manufacturer = manufacturer;
this.modelNumber = modelNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@

public class ObjectsInitializer {

protected LwM2mInstanceEnablerFactory defaultFactory = new BaseInstanceEnablerFactory() {
@Override
public LwM2mInstanceEnabler create() {
return new SimpleInstanceEnabler();
}
};

protected Map<Integer, LwM2mInstanceEnablerFactory> factories = new HashMap<>();
protected Map<Integer, LwM2mInstanceEnabler[]> instances = new HashMap<>();
protected Map<Integer, ContentFormat> defaultContentFormat = new HashMap<>();
Expand Down Expand Up @@ -96,6 +89,42 @@ public void setInstancesForObject(int objectId, LwM2mInstanceEnabler... instance
this.instances.put(objectId, instances);
}

/**
* Add dummy instance for each given <code>objectId</code>. ObjectId can be repeated to create several dummy
* instances. A dummy instance is just a very simple instance implementation which respect the object model and
* return some random values. A good way to begin to test Leshan client but not adapted to production environment.
*
* @param objectId
*/
public void setDummyInstancesForObject(int... objectIds) {
// create a map (id => nb instances)
Map<Integer, Integer> idToNbInstance = new HashMap<>();
for (int objectid : objectIds) {
// get current number of instance
Integer nbInstance = idToNbInstance.get(objectid);
if (nbInstance == null)
nbInstance = 0;

// add a new instance
idToNbInstance.put(objectid, nbInstance + 1);
}

// create dummy instances for each object
for (Map.Entry<Integer, Integer> entry : idToNbInstance.entrySet()) {
int objectid = entry.getKey();

// create instance Array;
Integer nbInstances = entry.getValue();
SimpleInstanceEnabler[] instances = new SimpleInstanceEnabler[nbInstances];
for (int i = 0; i < instances.length; i++) {
instances[i] = new SimpleInstanceEnabler();
}

// set instances for current object id
setInstancesForObject(objectid, instances);
}
}

public void setDefaultContentFormat(int objectId, ContentFormat format) {
defaultContentFormat.put(objectId, format);
}
Expand Down Expand Up @@ -129,7 +158,7 @@ public List<LwM2mObjectEnabler> createAll() {
/**
* Create an {@link LwM2mObjectEnabler} for the given <code>objectId</code>.
*
* An "instances", "object class" or "factory" should have been associated before.
* An "instances", "object class" or "factory" MUST have been associated before.
*
* @return a LwM2MObjectEnabler
*
Expand All @@ -149,7 +178,7 @@ public LwM2mObjectEnabler create(int objectId) {
/**
* Create an {@link LwM2mObjectEnabler} for each given <code>objectId</code>.
*
* An "instances", "object class" or "factory" should have been associated before.
* An "instances", "object class" or "factory" MUST have been associated before.
*
* @return a list of LwM2MObjectEnabler
*
Expand Down Expand Up @@ -187,8 +216,9 @@ protected LwM2mInstanceEnablerFactory getFactoryFor(ObjectModel objectModel) {
}
}
}
// default class :
return defaultFactory;
throw new IllegalStateException(String.format(
"Unable to create factory for %s object (%d) : a factory, a class or an instance with a default constructor should be associated to",
objectModel.name, objectModel.id));
}

protected ObjectEnabler createNodeEnabler(ObjectModel objectModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void check_callback_on_delete() throws InterruptedException {
assertTrue("callback delete should have been called", instanceEnabler.waitForDelete(2, TimeUnit.SECONDS));
}

private class TestInstanceEnabler extends BaseInstanceEnabler {
public static class TestInstanceEnabler extends BaseInstanceEnabler {

CountDownLatch onDelete = new CountDownLatch(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.leshan.client.object.Security;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.resource.SimpleInstanceEnabler;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig;
Expand Down Expand Up @@ -185,8 +186,9 @@ private void createClient(Security security) {
initializer.setInstancesForObject(LwM2mId.SECURITY, security);
initializer.setInstancesForObject(LwM2mId.DEVICE,
new Device("Eclipse Leshan", IntegrationTestHelper.MODEL_NUMBER, "12345", "U"));
initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, SimpleInstanceEnabler.class);
initializer.setClassForObject(LwM2mId.SERVER, SimpleInstanceEnabler.class);
List<LwM2mObjectEnabler> objects = initializer.createAll();
objects.addAll(initializer.create(1, 2));

// Create Leshan Client
LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.leshan.client.request.ServerIdentity;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.resource.SimpleInstanceEnabler;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ObjectModel;
Expand Down Expand Up @@ -122,25 +123,37 @@ public void createClient() {
createClient(null);
}

public static class TestDevice extends Device {

public TestDevice() {
super();
}

public TestDevice(String manufacturer, String modelNumber, String serialNumber, String supportedBinding) {
super(manufacturer, modelNumber, serialNumber, supportedBinding);
}

@Override
public ExecuteResponse execute(ServerIdentity identity, int resourceid, String params) {
if (resourceid == 4) {
return ExecuteResponse.success();
} else {
return super.execute(identity, resourceid, params);
}
}
}

public void createClient(Map<String, String> additionalAttributes) {
// Create objects Enabler
ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(createObjectModels()));
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec(
"coap://" + server.getUnsecuredAddress().getHostString() + ":" + server.getUnsecuredAddress().getPort(),
12345));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U") {
@Override
public ExecuteResponse execute(ServerIdentity identity, int resourceid, String params) {
if (resourceid == 4) {
return ExecuteResponse.success();
} else {
return super.execute(identity, resourceid, params);
}
}
});
initializer.setInstancesForObject(LwM2mId.DEVICE, new TestDevice("Eclipse Leshan", MODEL_NUMBER, "12345", "U"));
initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, SimpleInstanceEnabler.class);
initializer.setClassForObject(2000, SimpleInstanceEnabler.class);
List<LwM2mObjectEnabler> objects = initializer.createAll();
objects.addAll(initializer.create(2, 2000));

// Build Client
LeshanClientBuilder builder = new LeshanClientBuilder(currentEndpointIdentifier.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@

import org.eclipse.leshan.LwM2mId;
import org.eclipse.leshan.client.californium.LeshanClientBuilder;
import org.eclipse.leshan.client.object.Device;
import org.eclipse.leshan.client.object.Security;
import org.eclipse.leshan.client.object.Server;
import org.eclipse.leshan.client.request.ServerIdentity;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.resource.SimpleInstanceEnabler;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.core.response.ExecuteResponse;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.server.californium.LeshanServerBuilder;
import org.eclipse.leshan.server.queue.StaticClientAwakeTimeProvider;
Expand Down Expand Up @@ -68,18 +66,11 @@ public void createClient() {
"coap://" + server.getUnsecuredAddress().getHostString() + ":" + server.getUnsecuredAddress().getPort(),
12345));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.UQ, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "UQ") {
@Override
public ExecuteResponse execute(ServerIdentity identity, int resourceid, String params) {
if (resourceid == 4) {
return ExecuteResponse.success();
} else {
return super.execute(identity, resourceid, params);
}
}
});
initializer.setInstancesForObject(LwM2mId.DEVICE,
new TestDevice("Eclipse Leshan", MODEL_NUMBER, "12345", "UQ"));
initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, SimpleInstanceEnabler.class);
initializer.setClassForObject(2000, SimpleInstanceEnabler.class);
List<LwM2mObjectEnabler> objects = initializer.createAll();
objects.addAll(initializer.create(2, 2000));

// Build Client
LeshanClientBuilder builder = new LeshanClientBuilder(currentEndpointIdentifier.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.eclipse.leshan.client.object.Server;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.resource.SimpleInstanceEnabler;
import org.eclipse.leshan.core.californium.EndpointFactory;
import org.eclipse.leshan.core.request.BindingMode;
import org.eclipse.leshan.server.californium.LeshanServerBuilder;
Expand Down Expand Up @@ -191,8 +192,8 @@ public void createPSKClient() {
12345, GOOD_PSK_ID.getBytes(StandardCharsets.UTF_8), GOOD_PSK_KEY));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U"));
initializer.setDummyInstancesForObject(LwM2mId.ACCESS_CONTROL);
List<LwM2mObjectEnabler> objects = initializer.createAll();
objects.add(initializer.create(2));

InetSocketAddress clientAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
Expand Down Expand Up @@ -248,8 +249,8 @@ public void createRPKClient(boolean useServerCertificate) {
useServerCertificate ? serverX509Cert.getPublicKey().getEncoded() : serverPublicKey.getEncoded()));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U"));
initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, SimpleInstanceEnabler.class);
List<LwM2mObjectEnabler> objects = initializer.createAll();
objects.add(initializer.create(2));

InetSocketAddress clientAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
Expand Down Expand Up @@ -286,8 +287,8 @@ public void createX509CertClient(Certificate clientCertificate, PrivateKey priva
serverCertificate.getEncoded()));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U"));
initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, SimpleInstanceEnabler.class);
List<LwM2mObjectEnabler> objects = initializer.createAll();
objects.add(initializer.create(2));

InetSocketAddress clientAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
Expand Down

0 comments on commit d4c09ad

Please sign in to comment.