use of org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback in project smarthome by eclipse.
the class FirmwareUpdateServiceTest method testUpdateFirmware_error.
@Test
public void testUpdateFirmware_error() {
doAnswer(invocation -> {
ProgressCallback progressCallback = (ProgressCallback) invocation.getArguments()[1];
progressCallback.defineSequence(SEQUENCE);
progressCallback.next();
progressCallback.next();
progressCallback.next();
progressCallback.next();
try {
progressCallback.next();
} catch (NoSuchElementException e) {
fail("Unexcepted exception thrown");
}
return null;
}).when(handler1).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
assertResultInfoEvent(THING1_UID, FW112_EN, "unexpected-handler-error", null, "english", 1);
assertResultInfoEvent(THING1_UID, FW112_EN, "unexpected-handler-error", Locale.ENGLISH, "english", 2);
assertResultInfoEvent(THING1_UID, FW112_EN, "unexpected-handler-error", Locale.GERMAN, "deutsch", 3);
assertThat(thing1.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V111.toString()));
assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
}
use of org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback in project smarthome by eclipse.
the class FirmwareUpdateServiceTest method testUpdateFirmware_customError.
@Test
public void testUpdateFirmware_customError() {
doAnswer(invocation -> {
ProgressCallback progressCallback = (ProgressCallback) invocation.getArguments()[1];
progressCallback.failed("test-error");
return null;
}).when(handler1).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
assertResultInfoEvent(THING1_UID, FW112_EN, "test-error", null, "english", 1);
assertResultInfoEvent(THING1_UID, FW112_EN, "test-error", Locale.ENGLISH, "english", 2);
assertResultInfoEvent(THING1_UID, FW112_EN, "test-error", Locale.GERMAN, "deutsch", 3);
assertThat(thing1.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V111.toString()));
assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
}
use of org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback in project smarthome by eclipse.
the class FirmwareUpdateServiceTest method testEvents.
@Test
public void testEvents() {
doAnswer(invocation -> {
Firmware firmware = (Firmware) invocation.getArguments()[0];
ProgressCallback progressCallback = (ProgressCallback) invocation.getArguments()[1];
progressCallback.defineSequence(SEQUENCE);
progressCallback.next();
progressCallback.next();
progressCallback.next();
progressCallback.next();
thing1.setProperty(Thing.PROPERTY_FIRMWARE_VERSION, firmware.getVersion());
return null;
}).when(handler1).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
// getFirmwareStatusInfo() method will internally generate and post one FirmwareStatusInfoEvent event.
assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
firmwareUpdateService.updateFirmware(THING1_UID, FW112_EN.getUID(), null);
AtomicReference<List<Event>> events = new AtomicReference<>(new ArrayList<>());
ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
waitForAssert(() -> {
// Wait for four FirmwareUpdateProgressInfoEvents plus one FirmwareStatusInfoEvent event.
verify(mockPublisher, atLeast(SEQUENCE.length + 1)).post(eventCaptor.capture());
});
events.get().addAll(eventCaptor.getAllValues());
List<Event> list = events.get().stream().filter(event -> event instanceof FirmwareUpdateProgressInfoEvent).collect(Collectors.toList());
assertTrue(list.size() >= SEQUENCE.length);
for (int i = 0; i < SEQUENCE.length; i++) {
FirmwareUpdateProgressInfoEvent event = (FirmwareUpdateProgressInfoEvent) list.get(i);
assertThat(event.getTopic(), containsString(THING1_UID.getAsString()));
assertThat(event.getThingUID(), is(THING1_UID));
assertThat(event.getProgressInfo().getProgressStep(), is(SEQUENCE[i]));
}
}
use of org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback in project smarthome by eclipse.
the class FirmwareUpdateServiceTest method testCancelFirmwareUpdateIntheMiddleOfUpdate.
@Test
public void testCancelFirmwareUpdateIntheMiddleOfUpdate() {
final long stepsTime = 10;
final int numberOfSteps = SEQUENCE.length;
final AtomicBoolean isUpdateFinished = new AtomicBoolean(false);
doAnswer(invocation -> {
ProgressCallback progressCallback = (ProgressCallback) invocation.getArguments()[1];
progressCallback.defineSequence(SEQUENCE);
// Simulate update steps with delay
for (int updateStepsCount = 0; updateStepsCount < numberOfSteps; updateStepsCount++) {
progressCallback.next();
Thread.sleep(stepsTime);
}
progressCallback.success();
isUpdateFinished.set(true);
return null;
}).when(handler1).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
// Execute update and cancel it immediately
firmwareUpdateService.updateFirmware(THING1_UID, FW112_EN.getUID(), null);
firmwareUpdateService.cancelFirmwareUpdate(THING1_UID);
// Be sure that the cancel is executed before the completion of the update
waitForAssert(() -> {
verify(handler1, times(1)).cancel();
}, stepsTime * numberOfSteps, stepsTime);
assertThat(isUpdateFinished.get(), is(false));
}
use of org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback in project smarthome by eclipse.
the class FirmwareUpdateService method cancelFirmwareUpdate.
/**
* Cancels the firmware update of the thing having the given thing UID by invoking the operation
* {@link FirmwareUpdateHandler#cancel()} of the thingĀ“s firmware update handler.
*
* @param thingUID the thing UID (must not be null)
*/
public void cancelFirmwareUpdate(final ThingUID thingUID) {
Objects.requireNonNull(thingUID, "Thing UID must not be null.");
final FirmwareUpdateHandler firmwareUpdateHandler = getFirmwareUpdateHandler(thingUID);
if (firmwareUpdateHandler == null) {
throw new IllegalArgumentException(String.format("There is no firmware update handler for thing with UID %s.", thingUID));
}
final ProgressCallbackImpl progressCallback = getProgressCallback(thingUID);
logger.debug("Cancelling firmware update for thing with UID {}.", thingUID);
safeCaller.create(firmwareUpdateHandler, FirmwareUpdateHandler.class).withTimeout(timeout).withAsync().onTimeout(() -> {
logger.error("Timeout occurred while cancelling firmware update of thing with UID {}.", thingUID);
progressCallback.failedInternal("timeout-error-during-cancel");
}).onException(e -> {
logger.error("Unexpected exception occurred while cancelling firmware update of thing with UID {}.", thingUID, e.getCause());
progressCallback.failedInternal("unexpected-handler-error-during-cancel");
}).withIdentifier(new Object()).build().cancel();
}
Aggregations