BootstrapHandler { public BootstrapResponse bootstrap(final Identity sender, final BootstrapRequest request) { final String endpoint = request.getEndpointName(); final BootstrapSession bsSession = this.bsSessionManager.begin(endpoint, sender); if (!bsSession.isAuthorized()) { return BootstrapResponse.badRequest("Unauthorized"); } final BootstrapConfig cfg = bsStore.getBootstrap(endpoint); if (cfg == null) { LOG.error("No bootstrap config for {}", endpoint); return BootstrapResponse.badRequest("no bootstrap config"); } e.execute(new Runnable() { @Override public void run() { sendDelete(bsSession, cfg); } }); return BootstrapResponse.success(); } BootstrapHandler(BootstrapStore store, LwM2mBootstrapRequestSender requestSender,
            BootstrapSessionManager bsSessionManager); protected  BootstrapHandler(BootstrapStore store, LwM2mBootstrapRequestSender requestSender,
            BootstrapSessionManager bsSessionManager, Executor executor); BootstrapResponse bootstrap(final Identity sender, final BootstrapRequest request);  }
@Test public void error_if_not_authorized() { final BootstrapSessionManager bsSessionManager = new MockBootstrapSessionManager(false); BootstrapHandler bsHandler = new BootstrapHandler(null, null, bsSessionManager); BootstrapResponse bootstrapResponse = bsHandler .bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), new BootstrapRequest("enpoint")); assertEquals(ResponseCode.BAD_REQUEST, bootstrapResponse.getCode()); }
@Test public void notifies_at_end_of_successful_bootstrap() { final MockBootstrapSessionManager bsSessionManager = new MockBootstrapSessionManager(true); final LwM2mBootstrapRequestSender requestSender = new MockRequestSender(true); final BootstrapStore bsStore = new BootstrapStore() { @Override public BootstrapConfig getBootstrap(String endpoint) { return new BootstrapConfig(); } }; BootstrapHandler bsHandler = new BootstrapHandler(bsStore, requestSender, bsSessionManager, new Executor() { @Override public void execute(Runnable command) { command.run(); } }); bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), new BootstrapRequest("enpoint")); assertTrue(bsSessionManager.endWasCalled()); }
@Test public void does_not_notifies_at_end_of_failed_bootstrap() { final MockBootstrapSessionManager bsSessionManager = new MockBootstrapSessionManager(true); final LwM2mBootstrapRequestSender requestSender = new MockRequestSender(false); final BootstrapStore bsStore = new BootstrapStore() { @Override public BootstrapConfig getBootstrap(String endpoint) { return new BootstrapConfig(); } }; BootstrapHandler bsHandler = new BootstrapHandler(bsStore, requestSender, bsSessionManager, new Executor() { @Override public void execute(Runnable command) { command.run(); } }); bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), new BootstrapRequest("enpoint")); assertFalse(bsSessionManager.endWasCalled()); }