Search in sources :

Example 1 with ProgressCallback

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));
}
Also used : ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) NoSuchElementException(java.util.NoSuchElementException) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 2 with ProgressCallback

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));
}
Also used : ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with ProgressCallback

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]));
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CoreMatchers(org.hamcrest.CoreMatchers) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) AtomicReference(java.util.concurrent.atomic.AtomicReference) FirmwareUpdateBackgroundTransferHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) FirmwareStatusInfo(org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo) SafeCaller(org.eclipse.smarthome.core.common.SafeCaller) Locale(java.util.Locale) FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) After(org.junit.After) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) NoSuchElementException(java.util.NoSuchElementException) Bundle(org.osgi.framework.Bundle) ExpectedException(org.junit.rules.ExpectedException) Hashtable(java.util.Hashtable) Before(org.junit.Before) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) TranslationProvider(org.eclipse.smarthome.core.i18n.TranslationProvider) ConfigDescriptionValidator(org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) IOException(java.io.IOException) Test(org.junit.Test) Constants(org.eclipse.smarthome.core.thing.firmware.Constants) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ProgressStep(org.eclipse.smarthome.core.thing.binding.firmware.ProgressStep) Assert(org.junit.Assert) Event(org.eclipse.smarthome.core.events.Event) Collections(java.util.Collections) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) Event(org.eclipse.smarthome.core.events.Event) ArrayList(java.util.ArrayList) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 4 with ProgressCallback

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));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 5 with ProgressCallback

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();
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) URISyntaxException(java.net.URISyntaxException) ProgressCallback(org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback) LoggerFactory(org.slf4j.LoggerFactory) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) FirmwareUpdateBackgroundTransferHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler) ThingStatusInfoChangedEvent(org.eclipse.smarthome.core.thing.events.ThingStatusInfoChangedEvent) Component(org.osgi.service.component.annotations.Component) FirmwareStatusInfo(org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo) SafeCaller(org.eclipse.smarthome.core.common.SafeCaller) Locale(java.util.Locale) FirmwareUID(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Activate(org.osgi.service.component.annotations.Activate) URI(java.net.URI) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler) ThreadPoolManager(org.eclipse.smarthome.core.common.ThreadPoolManager) TranslationProvider(org.eclipse.smarthome.core.i18n.TranslationProvider) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Deactivate(org.osgi.service.component.annotations.Deactivate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ConfigDescriptionValidator(org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) List(java.util.List) Modified(org.osgi.service.component.annotations.Modified) LocaleProvider(org.eclipse.smarthome.core.i18n.LocaleProvider) Event(org.eclipse.smarthome.core.events.Event) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) FirmwareUpdateHandler(org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUpdateHandler)

Aggregations

Firmware (org.eclipse.smarthome.core.thing.binding.firmware.Firmware)5 ProgressCallback (org.eclipse.smarthome.core.thing.binding.firmware.ProgressCallback)5 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)4 Test (org.junit.Test)4 Collections (java.util.Collections)2 List (java.util.List)2 Locale (java.util.Locale)2 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ConfigDescriptionValidator (org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator)2 SafeCaller (org.eclipse.smarthome.core.common.SafeCaller)2 Event (org.eclipse.smarthome.core.events.Event)2 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)2 LocaleProvider (org.eclipse.smarthome.core.i18n.LocaleProvider)2 TranslationProvider (org.eclipse.smarthome.core.i18n.TranslationProvider)2 Thing (org.eclipse.smarthome.core.thing.Thing)2 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)2 FirmwareUID (org.eclipse.smarthome.core.thing.binding.firmware.FirmwareUID)2