From 250c8e2e21907f1e3e705c44790b5b85567ffc18 Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Wed, 25 Jan 2023 18:04:11 +0100 Subject: [PATCH] GH-1387: Fix BootstrapDeleteRequest always delete all instances --- .../client/request/DefaultDownlinkReceiver.java | 14 +++++++++++++- .../leshan/integration/tests/BootstrapTest.java | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/leshan-client-core/src/main/java/org/eclipse/leshan/client/request/DefaultDownlinkReceiver.java b/leshan-client-core/src/main/java/org/eclipse/leshan/client/request/DefaultDownlinkReceiver.java index e388f600b3..2609b6806f 100644 --- a/leshan-client-core/src/main/java/org/eclipse/leshan/client/request/DefaultDownlinkReceiver.java +++ b/leshan-client-core/src/main/java/org/eclipse/leshan/client/request/DefaultDownlinkReceiver.java @@ -266,7 +266,19 @@ public void visit(BootstrapReadRequest request) { @Override public void visit(BootstrapDeleteRequest request) { - response = toSendableResponse(bootstrapHandler.delete(sender, request)); + if (request.getPath().isRoot()) { + response = toSendableResponse(bootstrapHandler.delete(sender, request)); + } else { + LwM2mObjectEnabler objectEnabler = getObjectEnabler(request); + if (objectEnabler == null) { + // Bootstrap delete operation does not support Not Found Response code : + // See + // http://www.openmobilealliance.org/release/LightweightM2M/V1_1_1-20190617-A/HTML-Version/OMA-TS-LightweightM2M_Transport-V1_1_1-20190617-A.html#Table-642-1-Operation-to-Method-and-URI-Mapping-Bootstrap-Interface + response = toSendableResponse(BootstrapDeleteResponse.badRequest("not found")); + } else { + response = toSendableResponse(objectEnabler.delete(sender, request)); + } + } } @Override diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/BootstrapTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/BootstrapTest.java index 099a60d439..64f447da7a 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/BootstrapTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/BootstrapTest.java @@ -23,6 +23,7 @@ import static org.hamcrest.CoreMatchers.hasItems; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -430,7 +431,7 @@ public void bootstrap_with_auto_id_for_security_object() { } @Test - public void bootstrapDeleteSecurity() { + public void bootstrap_delete_access_control_and_connectivity_statistics() { // Create DM Server without security & start it helper.createServer(); helper.server.start(); @@ -444,6 +445,7 @@ public void bootstrapDeleteSecurity() { ObjectsInitializer initializer = new TestObjectsInitializer(); initializer.setInstancesForObject(LwM2mId.ACCESS_CONTROL, new SimpleInstanceEnabler()); initializer.setInstancesForObject(LwM2mId.CONNECTIVITY_STATISTICS, new SimpleInstanceEnabler()); + initializer.setInstancesForObject(LwM2mId.LOCATION, new SimpleInstanceEnabler()); helper.createClient(helper.withoutSecurity(), initializer); helper.assertClientNotRegisterered(); @@ -460,6 +462,19 @@ public void bootstrapDeleteSecurity() { .read(ServerIdentity.SYSTEM, new ReadRequest(LwM2mId.CONNECTIVITY_STATISTICS)); assertTrue("Connectvity instance is not deleted", ((LwM2mObject) response.getContent()).getInstances().isEmpty()); + + // ensure other instances are not deleted. + response = helper.client.getObjectTree().getObjectEnabler(LwM2mId.DEVICE).read(ServerIdentity.SYSTEM, + new ReadRequest(LwM2mId.DEVICE)); + assertFalse("Device instance is deleted", ((LwM2mObject) response.getContent()).getInstances().isEmpty()); + + response = helper.client.getObjectTree().getObjectEnabler(LwM2mId.SECURITY).read(ServerIdentity.SYSTEM, + new ReadRequest(LwM2mId.SECURITY)); + assertFalse("Security instance is deleted", ((LwM2mObject) response.getContent()).getInstances().isEmpty()); + + response = helper.client.getObjectTree().getObjectEnabler(LwM2mId.LOCATION).read(ServerIdentity.SYSTEM, + new ReadRequest(LwM2mId.LOCATION)); + assertFalse("Location instance is deleted", ((LwM2mObject) response.getContent()).getInstances().isEmpty()); } @Test