From 3cea526d8c740018d9e72a011d07f48b86bc99d7 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Fri, 19 May 2023 13:57:45 +0200 Subject: [PATCH] more tests Signed-off-by: Jan N. Klug --- .../CommunicationManagerConversionTest.java | 141 ------------------ .../profiles/SystemDefaultProfileTest.java | 12 ++ 2 files changed, 12 insertions(+), 141 deletions(-) delete mode 100644 bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/CommunicationManagerConversionTest.java diff --git a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/CommunicationManagerConversionTest.java b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/CommunicationManagerConversionTest.java deleted file mode 100644 index f191b1b19da..00000000000 --- a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/CommunicationManagerConversionTest.java +++ /dev/null @@ -1,141 +0,0 @@ -/** - * Copyright (c) 2010-2023 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.core.thing; - -import static org.junit.jupiter.api.Assertions.*; - -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Stream; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; -import org.openhab.core.items.Item; -import org.openhab.core.library.items.CallItem; -import org.openhab.core.library.items.ColorItem; -import org.openhab.core.library.items.ContactItem; -import org.openhab.core.library.items.DateTimeItem; -import org.openhab.core.library.items.DimmerItem; -import org.openhab.core.library.items.ImageItem; -import org.openhab.core.library.items.LocationItem; -import org.openhab.core.library.items.PlayerItem; -import org.openhab.core.library.items.RollershutterItem; -import org.openhab.core.library.items.StringItem; -import org.openhab.core.library.types.DateTimeType; -import org.openhab.core.library.types.DecimalType; -import org.openhab.core.library.types.HSBType; -import org.openhab.core.library.types.IncreaseDecreaseType; -import org.openhab.core.library.types.NextPreviousType; -import org.openhab.core.library.types.OnOffType; -import org.openhab.core.library.types.OpenClosedType; -import org.openhab.core.library.types.PercentType; -import org.openhab.core.library.types.PlayPauseType; -import org.openhab.core.library.types.PointType; -import org.openhab.core.library.types.QuantityType; -import org.openhab.core.library.types.RawType; -import org.openhab.core.library.types.RewindFastforwardType; -import org.openhab.core.library.types.StringType; -import org.openhab.core.library.types.UpDownType; -import org.openhab.core.types.Command; -import org.openhab.core.types.State; -import org.openhab.core.types.Type; -import org.openhab.core.types.UnDefType; - -/** - * @author Jan N. Klug - Initial contribution - */ -@NonNullByDefault -public class CommunicationManagerConversionTest { - // TODO: remove test - only to show CommunicationManager is too complex - - private static final List> itemTypes = List.of(CallItem.class, ColorItem.class, - ContactItem.class, DateTimeItem.class, DimmerItem.class, ImageItem.class, LocationItem.class, - PlayerItem.class, RollershutterItem.class, StringItem.class); - - private static final List> types = List.of(DateTimeType.class, DecimalType.class, - HSBType.class, IncreaseDecreaseType.class, NextPreviousType.class, OnOffType.class, OpenClosedType.class, - PercentType.class, PlayPauseType.class, PointType.class, QuantityType.class, RawType.class, - RewindFastforwardType.class, StringType.class, UpDownType.class, UnDefType.class); - - private static Stream arguments() - throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { - List arguments = new ArrayList<>(); - for (Class itemType : itemTypes) { - Item item = itemType.getDeclaredConstructor(String.class).newInstance("testItem"); - for (Class type : types) { - if (type.isEnum()) { - arguments.add(Arguments.of(item, type.getEnumConstants()[0])); - } else if (type == RawType.class) { - arguments.add(Arguments.of(item, new RawType(new byte[] {}, "mimeType"))); - } else { - arguments.add(Arguments.of(item, type.getDeclaredConstructor().newInstance())); - } - } - } - return arguments.stream(); - } - - @Disabled - @MethodSource("arguments") - @ParameterizedTest - public void testCommand(Item item, Type originalType) { - Type returnType = null; - - List> acceptedTypes = item.getAcceptedCommandTypes(); - if (acceptedTypes.contains(originalType.getClass())) { - returnType = originalType; - } else { - // Look for class hierarchy and convert appropriately - for (Class typeClass : acceptedTypes) { - if (!typeClass.isEnum() && typeClass.isAssignableFrom(originalType.getClass()) // - && State.class.isAssignableFrom(typeClass) && originalType instanceof State state) { - returnType = state.as((Class) typeClass); - } - } - } - - if (returnType != null && !returnType.getClass().equals(originalType.getClass())) { - fail("CommunicationManager did a conversion for target item " + item.getType() + " from " - + originalType.getClass() + " to " + returnType.getClass()); - } - } - - @MethodSource("arguments") - @ParameterizedTest - public void testState(Item item, Type originalType) { - Type returnType = null; - - List> acceptedTypes = item.getAcceptedDataTypes(); - if (acceptedTypes.contains(originalType.getClass())) { - returnType = originalType; - } else { - // Look for class hierarchy and convert appropriately - for (Class typeClass : acceptedTypes) { - if (!typeClass.isEnum() && typeClass.isAssignableFrom(originalType.getClass()) // - && State.class.isAssignableFrom(typeClass) && originalType instanceof State state) { - returnType = state.as((Class) typeClass); - - } - } - } - - if (returnType != null && !returnType.equals(originalType)) { - fail("CommunicationManager did a conversion for target item " + item.getType() + " from " - + originalType.getClass() + " to " + returnType.getClass()); - } - } -} diff --git a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/profiles/SystemDefaultProfileTest.java b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/profiles/SystemDefaultProfileTest.java index 94952d03acf..720f0008515 100644 --- a/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/profiles/SystemDefaultProfileTest.java +++ b/bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/internal/profiles/SystemDefaultProfileTest.java @@ -22,6 +22,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.openhab.core.library.types.OnOffType; import org.openhab.core.thing.profiles.ProfileCallback; +import org.openhab.core.types.TimeSeries; /** * @@ -60,4 +61,15 @@ public void testPostCommand() { verify(callbackMock).sendCommand(eq(OnOffType.ON)); verifyNoMoreInteractions(callbackMock); } + + @Test + public void testSendTimeSeries() { + TimeSeries timeSeries = new TimeSeries(TimeSeries.Policy.ADD); + + SystemDefaultProfile profile = new SystemDefaultProfile(callbackMock); + profile.onTimeSeriesFromHandler(timeSeries); + + verify(callbackMock).sendTimeSeries(timeSeries); + verifyNoMoreInteractions(callbackMock); + } }