Search in sources :

Example 11 with FirmwareStatusInfo

use of org.openhab.core.thing.firmware.FirmwareStatusInfo 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 12 with FirmwareStatusInfo

use of org.openhab.core.thing.firmware.FirmwareStatusInfo in project openhab-core by openhab.

the class FirmwareUpdateServiceTest method testPrerequisiteVersionCheckIllegalVersion.

@Test
public void testPrerequisiteVersionCheckIllegalVersion() {
    when(firmwareRegistryMock.getFirmware(any(Thing.class), any(String.class))).thenAnswer(invocation -> {
        String firmwareVersion = (String) invocation.getArguments()[1];
        if (V111_FIX.equals(firmwareVersion)) {
            return FW111_FIX_EN;
        } else if (V113.equals(firmwareVersion)) {
            return FW113_EN;
        } else {
            return null;
        }
    });
    when(firmwareRegistryMock.getFirmwares(any(Thing.class))).thenAnswer(invocation -> {
        Thing thing = (Thing) invocation.getArguments()[0];
        if (THING_TYPE_UID_WITHOUT_FW.equals(thing.getThingTypeUID()) || THING_TYPE_UID2.equals(thing.getThingTypeUID())) {
            return Collections.emptySet();
        } else {
            Supplier<TreeSet<Firmware>> supplier = () -> new TreeSet<>();
            return Stream.of(FW111_FIX_EN, FW113_EN).collect(Collectors.toCollection(supplier));
        }
    });
    when(firmwareRegistryMock.getFirmware(any(Thing.class), eq(V113))).thenReturn(FW113_EN);
    final FirmwareStatusInfo updateExecutableInfoFw113 = createUpdateExecutableInfo(thing1.getUID(), V113);
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw113));
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> firmwareUpdateService.updateFirmware(THING1_UID, V113, null));
    assertThat(exception.getMessage(), is(String.format("Firmware %s is not suitable for thing with UID %s.", FW113_EN, THING1_UID)));
}
Also used : FirmwareStatusInfo(org.openhab.core.thing.firmware.FirmwareStatusInfo) TreeSet(java.util.TreeSet) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 13 with FirmwareStatusInfo

use of org.openhab.core.thing.firmware.FirmwareStatusInfo in project openhab-core by openhab.

the class FirmwareUpdateServiceTest method testGetFirmwareStatusInfoEventsOnlySentOnce.

@Test
public void testGetFirmwareStatusInfoEventsOnlySentOnce() {
    final FirmwareStatusInfo updateExecutableInfoFw112 = createUpdateExecutableInfo(thing1.getUID(), V112);
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112));
    // verify that the corresponding events are sent
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(eventPublisherMock, times(1)).post(eventCaptor.capture());
    assertFirmwareStatusInfoEvent(THING1_UID, eventCaptor.getAllValues().get(0), updateExecutableInfoFw112);
}
Also used : FirmwareStatusInfo(org.openhab.core.thing.firmware.FirmwareStatusInfo) 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) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 14 with FirmwareStatusInfo

use of org.openhab.core.thing.firmware.FirmwareStatusInfo in project openhab-core by openhab.

the class FirmwareUpdateServiceTest method testGetFirmwareStatusInfoNoFirmwareProvider.

@Test
public void testGetFirmwareStatusInfoNoFirmwareProvider() {
    final FirmwareStatusInfo unknownInfo = createUnknownInfo(thing3.getUID());
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING3_UID), is(unknownInfo));
    // verify that the corresponding events are sent
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(eventPublisherMock, times(1)).post(eventCaptor.capture());
    assertFirmwareStatusInfoEvent(THING3_UID, eventCaptor.getValue(), unknownInfo);
}
Also used : FirmwareStatusInfo(org.openhab.core.thing.firmware.FirmwareStatusInfo) 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) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Example 15 with FirmwareStatusInfo

use of org.openhab.core.thing.firmware.FirmwareStatusInfo in project openhab-core by openhab.

the class FirmwareUpdateServiceTest method testPrerequisiteVersionCheck.

@Test
public void testPrerequisiteVersionCheck() {
    when(firmwareRegistryMock.getFirmware(any(Thing.class), any(String.class), any())).thenAnswer(invocation -> {
        String version = (String) invocation.getArguments()[1];
        if (V111_FIX.equals(version)) {
            return FW111_FIX_EN;
        } else if (V113.equals(version)) {
            return FW113_EN;
        } else {
            return null;
        }
    });
    Answer<?> firmwaresAnswer = invocation -> {
        Thing thing = (Thing) invocation.getArguments()[0];
        if (THING_TYPE_UID_WITHOUT_FW.equals(thing.getThingTypeUID()) || THING_TYPE_UID2.equals(thing.getThingTypeUID())) {
            return Collections.emptySet();
        } else {
            Supplier<TreeSet<Firmware>> supplier = () -> new TreeSet<>();
            return Stream.of(FW111_FIX_EN, FW113_EN).collect(Collectors.toCollection(supplier));
        }
    };
    when(firmwareRegistryMock.getFirmwares(any(Thing.class), any())).thenAnswer(firmwaresAnswer);
    when(firmwareRegistryMock.getFirmwares(any(Thing.class))).thenAnswer(firmwaresAnswer);
    when(firmwareRegistryMock.getFirmware(any(Thing.class), eq(V111_FIX))).thenReturn(FW111_FIX_EN);
    when(firmwareRegistryMock.getFirmware(any(Thing.class), eq(V113))).thenReturn(FW113_EN);
    final FirmwareStatusInfo updateExecutableInfoFw113 = createUpdateExecutableInfo(thing1.getUID(), V113);
    assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw113));
    firmwareUpdateService.updateFirmware(THING1_UID, FW111_FIX_EN.getVersion(), null);
    waitForAssert(() -> {
        assertThat(thing1.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V111_FIX));
        assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw113));
    });
    firmwareUpdateService.updateFirmware(THING1_UID, FW113_EN.getVersion(), null);
    final FirmwareStatusInfo upToDateInfo1 = createUpToDateInfo(thing1.getUID());
    final FirmwareStatusInfo updateExecutableInfoFw1132 = createUpdateExecutableInfo(thing2.getUID(), V113);
    waitForAssert(() -> {
        assertThat(thing1.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V113));
        assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(upToDateInfo1));
        assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING2_UID), is(updateExecutableInfoFw1132));
    });
    firmwareUpdateService.updateFirmware(THING2_UID, FW113_EN.getVersion(), null);
    final FirmwareStatusInfo upToDateInfo2 = createUpToDateInfo(thing2.getUID());
    waitForAssert(() -> {
        assertThat(thing2.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is(V113));
        assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING2_UID), is(upToDateInfo2));
    });
}
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) Supplier(java.util.function.Supplier) Firmware(org.openhab.core.thing.binding.firmware.Firmware) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaOSGiTest(org.openhab.core.test.java.JavaOSGiTest)

Aggregations

FirmwareStatusInfo (org.openhab.core.thing.firmware.FirmwareStatusInfo)18 Test (org.junit.jupiter.api.Test)16 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)16 Event (org.openhab.core.events.Event)11 FirmwareStatusInfoEvent (org.openhab.core.thing.firmware.FirmwareStatusInfoEvent)11 FirmwareUpdateProgressInfoEvent (org.openhab.core.thing.firmware.FirmwareUpdateProgressInfoEvent)11 FirmwareUpdateResultInfoEvent (org.openhab.core.thing.firmware.FirmwareUpdateResultInfoEvent)11 Firmware (org.openhab.core.thing.binding.firmware.Firmware)7 Thing (org.openhab.core.thing.Thing)6 ProgressCallback (org.openhab.core.thing.binding.firmware.ProgressCallback)6 HashMap (java.util.HashMap)3 NoSuchElementException (java.util.NoSuchElementException)3 TreeSet (java.util.TreeSet)3 FirmwareUpdateHandler (org.openhab.core.thing.binding.firmware.FirmwareUpdateHandler)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Locale (java.util.Locale)2 Map (java.util.Map)2