use of org.openhab.core.thing.binding.firmware.ProgressCallback in project openhab-core by openhab.
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(handler1Mock).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
// getFirmwareStatusInfo() method will internally generate and post one FirmwareStatusInfoEvent event.
final FirmwareStatusInfo updateExecutableInfoFw112 = createUpdateExecutableInfo(thing1.getUID(), V112);
assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
firmwareUpdateService.updateFirmware(THING1_UID, FW112_EN.getVersion(), 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(eventPublisherMock, 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.getProgressInfo().getThingUID(), is(THING1_UID));
assertThat(event.getProgressInfo().getProgressStep(), is(SEQUENCE[i]));
}
}
use of org.openhab.core.thing.binding.firmware.ProgressCallback in project openhab-core by openhab.
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(handler1Mock).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
// Execute update and cancel it immediately
firmwareUpdateService.updateFirmware(THING1_UID, V112, null);
firmwareUpdateService.cancelFirmwareUpdate(THING1_UID);
// Be sure that the cancel is executed before the completion of the update
waitForAssert(() -> {
verify(handler1Mock, times(1)).cancel();
}, stepsTime * numberOfSteps, stepsTime);
assertThat(isUpdateFinished.get(), is(false));
}
use of org.openhab.core.thing.binding.firmware.ProgressCallback in project openhab-core by openhab.
the class FirmwareUpdateServiceTest method testUpdateFirmwareError.
@Test
public void testUpdateFirmwareError() {
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(handler1Mock).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
final FirmwareStatusInfo updateExecutableInfoFw112 = createUpdateExecutableInfo(thing1.getUID(), V112);
assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
assertResultInfoEvent(THING1_UID, FW112_EN, "unexpected-handler-error", Locale.ENGLISH, "english", 1);
assertResultInfoEvent(THING1_UID, FW112_EN, "unexpected-handler-error", Locale.GERMAN, "deutsch", 2);
assertThat(thing1.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V111.toString()));
assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
}
use of org.openhab.core.thing.binding.firmware.ProgressCallback in project openhab-core by openhab.
the class FirmwareUpdateServiceTest method testUpdateFirmwareCustomError.
@Test
public void testUpdateFirmwareCustomError() {
doAnswer(invocation -> {
ProgressCallback progressCallback = (ProgressCallback) invocation.getArguments()[1];
progressCallback.failed("test-error");
return null;
}).when(handler1Mock).updateFirmware(any(Firmware.class), any(ProgressCallback.class));
final FirmwareStatusInfo updateExecutableInfoFw112 = createUpdateExecutableInfo(thing1.getUID(), V112);
assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
assertResultInfoEvent(THING1_UID, FW112_EN, "test-error", Locale.ENGLISH, "english", 1);
assertResultInfoEvent(THING1_UID, FW112_EN, "test-error", Locale.GERMAN, "deutsch", 2);
assertThat(thing1.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V111.toString()));
assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
}
Aggregations