Search in sources :

Example 1 with ProgressCallback

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]));
    }
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ProgressCallback(org.openhab.core.thing.binding.firmware.ProgressCallback) FirmwareUpdateResultInfoEvent(org.openhab.core.thing.firmware.FirmwareUpdateResultInfoEvent) SafeCaller(org.openhab.core.common.SafeCaller) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Locale(java.util.Locale) Map(java.util.Map) FirmwareUpdateService(org.openhab.core.thing.firmware.FirmwareUpdateService) ThingBuilder(org.openhab.core.thing.binding.builder.ThingBuilder) Bundle(org.osgi.framework.Bundle) EventPublisher(org.openhab.core.events.EventPublisher) FirmwareRegistry(org.openhab.core.thing.firmware.FirmwareRegistry) BundleResolver(org.openhab.core.util.BundleResolver) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) FirmwareUpdateHandler(org.openhab.core.thing.binding.firmware.FirmwareUpdateHandler) Set(java.util.Set) FirmwareUpdateBackgroundTransferHandler(org.openhab.core.thing.binding.firmware.FirmwareUpdateBackgroundTransferHandler) TranslationProvider(org.openhab.core.i18n.TranslationProvider) Collectors(java.util.stream.Collectors) Firmware(org.openhab.core.thing.binding.firmware.Firmware) Test(org.junit.jupiter.api.Test) List(java.util.List) LocaleProvider(org.openhab.core.i18n.LocaleProvider) Stream(java.util.stream.Stream) FirmwareStatusInfoEvent(org.openhab.core.thing.firmware.FirmwareStatusInfoEvent) FirmwareUpdateProgressInfoEvent(org.openhab.core.thing.firmware.FirmwareUpdateProgressInfoEvent) FirmwareStatusInfo(org.openhab.core.thing.firmware.FirmwareStatusInfo) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Strictness(org.mockito.quality.Strictness) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) Mock(org.mockito.Mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ConfigDescriptionValidator(org.openhab.core.config.core.validation.ConfigDescriptionValidator) Answer(org.mockito.stubbing.Answer) Thing(org.openhab.core.thing.Thing) ArgumentCaptor(org.mockito.ArgumentCaptor) Map.entry(java.util.Map.entry) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NoSuchElementException(java.util.NoSuchElementException) FirmwareUpdateResult(org.openhab.core.thing.firmware.FirmwareUpdateResult) ThingUID(org.openhab.core.thing.ThingUID) Event(org.openhab.core.events.Event) Constants(org.openhab.core.thing.firmware.Constants) IOException(java.io.IOException) ProgressStep(org.openhab.core.thing.binding.firmware.ProgressStep) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest) Assertions(org.junit.jupiter.api.Assertions) Collections(java.util.Collections) FirmwareStatusInfo(org.openhab.core.thing.firmware.FirmwareStatusInfo) ProgressCallback(org.openhab.core.thing.binding.firmware.ProgressCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) FirmwareUpdateResultInfoEvent(org.openhab.core.thing.firmware.FirmwareUpdateResultInfoEvent) FirmwareStatusInfoEvent(org.openhab.core.thing.firmware.FirmwareStatusInfoEvent) FirmwareUpdateProgressInfoEvent(org.openhab.core.thing.firmware.FirmwareUpdateProgressInfoEvent) Event(org.openhab.core.events.Event) FirmwareUpdateProgressInfoEvent(org.openhab.core.thing.firmware.FirmwareUpdateProgressInfoEvent) List(java.util.List) ArrayList(java.util.ArrayList) Firmware(org.openhab.core.thing.binding.firmware.Firmware) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 2 with ProgressCallback

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

Example 3 with ProgressCallback

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

Example 4 with ProgressCallback

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

Aggregations

Test (org.junit.jupiter.api.Test)4 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)4 Firmware (org.openhab.core.thing.binding.firmware.Firmware)4 ProgressCallback (org.openhab.core.thing.binding.firmware.ProgressCallback)4 NoSuchElementException (java.util.NoSuchElementException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 FirmwareStatusInfo (org.openhab.core.thing.firmware.FirmwareStatusInfo)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Map.entry (java.util.Map.entry)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Supplier (java.util.function.Supplier)1