Search in sources :

Example 1 with ProfileCallback

use of org.openhab.core.thing.profiles.ProfileCallback in project openhab-addons by openhab.

the class ModbusGainOffsetProfileTest method testOnUpdateFromHandlerGeneric.

/**
 * Test profile behaviour when handler updates the state
 *
 * @param preGainOffset profile pre-gain-offset offset
 * @param gain profile gain
 * @param updateFromHandlerObj state update from handler. String representing QuantityType or State/Command
 * @param expectedUpdateTowardsItemObj expected state/command update towards item. String representing QuantityType
 *            or
 *            State
 * @param stateUpdateFromHandler whether there is state update from handler. Otherwise command
 */
@SuppressWarnings("rawtypes")
private void testOnUpdateFromHandlerGeneric(String preGainOffset, String gain, Object updateFromHandlerObj, Object expectedUpdateTowardsItemObj, boolean stateUpdateFromHandler) {
    ProfileCallback callback = mock(ProfileCallback.class);
    ModbusGainOffsetProfile profile = createProfile(callback, gain, preGainOffset);
    final Type actualStateUpdateTowardsItem;
    if (stateUpdateFromHandler) {
        final State updateFromHandler;
        if (updateFromHandlerObj instanceof String) {
            updateFromHandler = new QuantityType((String) updateFromHandlerObj);
        } else {
            assertTrue(updateFromHandlerObj instanceof State);
            updateFromHandler = (State) updateFromHandlerObj;
        }
        profile.onStateUpdateFromHandler(updateFromHandler);
        ArgumentCaptor<State> capture = ArgumentCaptor.forClass(State.class);
        verify(callback, times(1)).sendUpdate(capture.capture());
        actualStateUpdateTowardsItem = capture.getValue();
    } else {
        final Command updateFromHandler;
        if (updateFromHandlerObj instanceof String) {
            updateFromHandler = new QuantityType((String) updateFromHandlerObj);
        } else {
            assertTrue(updateFromHandlerObj instanceof State);
            updateFromHandler = (Command) updateFromHandlerObj;
        }
        profile.onCommandFromHandler(updateFromHandler);
        ArgumentCaptor<Command> capture = ArgumentCaptor.forClass(Command.class);
        verify(callback, times(1)).sendCommand(capture.capture());
        actualStateUpdateTowardsItem = capture.getValue();
    }
    Type expectedStateUpdateTowardsItem = (expectedUpdateTowardsItemObj instanceof String) ? new QuantityType((String) expectedUpdateTowardsItemObj) : (Type) expectedUpdateTowardsItemObj;
    assertEquals(expectedStateUpdateTowardsItem, actualStateUpdateTowardsItem);
    verifyNoMoreInteractions(callback);
}
Also used : ProfileCallback(org.openhab.core.thing.profiles.ProfileCallback) OnOffType(org.openhab.core.library.types.OnOffType) Type(org.openhab.core.types.Type) DecimalType(org.openhab.core.library.types.DecimalType) QuantityType(org.openhab.core.library.types.QuantityType) UnDefType(org.openhab.core.types.UnDefType) RefreshType(org.openhab.core.types.RefreshType) QuantityType(org.openhab.core.library.types.QuantityType) Command(org.openhab.core.types.Command) State(org.openhab.core.types.State)

Example 2 with ProfileCallback

use of org.openhab.core.thing.profiles.ProfileCallback in project openhab-addons by openhab.

the class ModbusGainOffsetProfileTest method testInvalidInit.

@Test
public void testInvalidInit() {
    // preGainOffset must be dimensionless
    ProfileCallback callback = mock(ProfileCallback.class);
    ModbusGainOffsetProfile<?> profile = createProfile(callback, "1.0", "0.0 K");
    assertFalse(profile.isValid());
}
Also used : ProfileCallback(org.openhab.core.thing.profiles.ProfileCallback) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ProfileCallback

use of org.openhab.core.thing.profiles.ProfileCallback in project openhab-addons by openhab.

the class ModbusGainOffsetProfileTest method testInitGainDefault.

@ParameterizedTest
@NullSource
@EmptySource
public void testInitGainDefault(String gain) {
    ProfileCallback callback = mock(ProfileCallback.class);
    ModbusGainOffsetProfile<?> p = createProfile(callback, gain, "0.0");
    assertTrue(p.isValid());
    assertEquals(p.getGain(), Optional.of(QuantityType.ONE));
}
Also used : ProfileCallback(org.openhab.core.thing.profiles.ProfileCallback) EmptySource(org.junit.jupiter.params.provider.EmptySource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) NullSource(org.junit.jupiter.params.provider.NullSource)

Example 4 with ProfileCallback

use of org.openhab.core.thing.profiles.ProfileCallback in project openhab-core by openhab.

the class SystemOffsetProfileTest method testQuantityTypeOnCommandFromItem.

@Test
public void testQuantityTypeOnCommandFromItem() {
    ProfileCallback callback = mock(ProfileCallback.class);
    SystemOffsetProfile offsetProfile = createProfile(callback, "3°C");
    Command cmd = new QuantityType<>("23°C");
    offsetProfile.onCommandFromItem(cmd);
    ArgumentCaptor<Command> capture = ArgumentCaptor.forClass(Command.class);
    verify(callback, times(1)).handleCommand(capture.capture());
    Command result = capture.getValue();
    QuantityType<?> decResult = (QuantityType<?>) result;
    assertEquals(20, decResult.intValue());
    assertEquals(SIUnits.CELSIUS, decResult.getUnit());
}
Also used : ProfileCallback(org.openhab.core.thing.profiles.ProfileCallback) Command(org.openhab.core.types.Command) QuantityType(org.openhab.core.library.types.QuantityType) Test(org.junit.jupiter.api.Test)

Example 5 with ProfileCallback

use of org.openhab.core.thing.profiles.ProfileCallback in project openhab-core by openhab.

the class SystemOffsetProfileTest method testQuantityTypeOnCommandFromHandler.

@Test
public void testQuantityTypeOnCommandFromHandler() {
    ProfileCallback callback = mock(ProfileCallback.class);
    SystemOffsetProfile offsetProfile = createProfile(callback, "3°C");
    Command cmd = new QuantityType<>("23°C");
    offsetProfile.onCommandFromHandler(cmd);
    ArgumentCaptor<Command> capture = ArgumentCaptor.forClass(Command.class);
    verify(callback, times(1)).sendCommand(capture.capture());
    Command result = capture.getValue();
    QuantityType<?> decResult = (QuantityType<?>) result;
    assertEquals(26, decResult.intValue());
    assertEquals(SIUnits.CELSIUS, decResult.getUnit());
}
Also used : ProfileCallback(org.openhab.core.thing.profiles.ProfileCallback) Command(org.openhab.core.types.Command) QuantityType(org.openhab.core.library.types.QuantityType) Test(org.junit.jupiter.api.Test)

Aggregations

ProfileCallback (org.openhab.core.thing.profiles.ProfileCallback)38 Test (org.junit.jupiter.api.Test)31 Command (org.openhab.core.types.Command)17 State (org.openhab.core.types.State)17 DecimalType (org.openhab.core.library.types.DecimalType)16 QuantityType (org.openhab.core.library.types.QuantityType)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 Temperature (javax.measure.quantity.Temperature)6 DateTimeType (org.openhab.core.library.types.DateTimeType)6 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 ZonedDateTime (java.time.ZonedDateTime)2 EmptySource (org.junit.jupiter.params.provider.EmptySource)2 NullSource (org.junit.jupiter.params.provider.NullSource)2 JavaTest (org.openhab.core.test.java.JavaTest)2 OnOffType (org.openhab.core.library.types.OnOffType)1 TriggerProfile (org.openhab.core.thing.profiles.TriggerProfile)1 RefreshType (org.openhab.core.types.RefreshType)1 Type (org.openhab.core.types.Type)1 UnDefType (org.openhab.core.types.UnDefType)1