Java 17 features (itests) (#15583)

- replace Collections by List.of and Set.of
- instanceof
- SAT warnings

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich 2023-09-13 08:06:37 +02:00 committed by GitHub
parent ab58f4ffb4
commit b3e88ae29e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 45 additions and 49 deletions

View File

@ -40,7 +40,7 @@ import org.openhab.core.types.State;
* <p>
* This class tests the commands for the astro thing.
*
* @author Petar Valchev - Initial implementation
* @author Petar Valchev - Initial contribution
* @author Svilen Valkanov - Reworked to plain unit tests
* @author Christoph Weitkamp - Migrated tests to pure Java
*/

View File

@ -37,7 +37,7 @@ import org.openhab.core.thing.binding.ThingHandlerCallback;
* <p>
* This class tests the required configuration for the astro thing.
*
* @author Petar Valchev - Initial implementation
* @author Petar Valchev - Initial contribution
* @author Svilen Valkanov - Reworked to plain unit tests, removed irrelevant tests
* @author Christoph Weitkamp - Migrated tests to pure Java
*/

View File

@ -40,7 +40,7 @@ import org.openhab.core.types.State;
* Tests for the Astro Channels state
*
* @See {@link AstroParametrizedTestCases}
* @author Petar Valchev - Initial implementation
* @author Petar Valchev - Initial contribution
* @author Svilen Valkanov - Reworked to plain unit tests
* @author Erdoan Hadzhiyusein - Adapted the class to work with the new DateTimeType
* @author Christoph Weitkamp - Migrated tests to pure Java

View File

@ -17,7 +17,7 @@ import java.math.BigDecimal;
/**
* Contains some test data used across different tests
*
* @author Svilen Valkanov - Initial implementation
* @author Svilen Valkanov - Initial contribution
* @author Christoph Weitkamp - Migrated tests to pure Java
*/
public final class AstroBindingTestsData {

View File

@ -61,8 +61,8 @@ public class ConfigFlowTest extends AbstractConfigFlowTest {
Thing bridge = getThingRegistry().get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
assertNotNull(bridge);
ThingHandler handler = bridge.getHandler();
if (handler instanceof MieleBridgeHandler) {
((MieleBridgeHandler) handler).onConnectionAlive();
if (handler instanceof MieleBridgeHandler mieleBridgeHandler) {
mieleBridgeHandler.onConnectionAlive();
}
return null;
}).when(webservice).addConnectionStatusListener(any());

View File

@ -20,8 +20,8 @@ import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingInte
import static org.openhab.binding.mielecloud.internal.util.ReflectionUtil.setPrivate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@ -181,9 +181,8 @@ public abstract class AbstractMieleThingHandlerTest extends JavaOSGiTest {
.create(MieleCloudBindingConstants.THING_TYPE_BRIDGE,
MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID)
.withLabel("Miele@home Account")
.withConfiguration(
new Configuration(Collections.singletonMap(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
MieleCloudBindingIntegrationTestConstants.EMAIL)))
.withConfiguration(new Configuration(Map.of(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
MieleCloudBindingIntegrationTestConstants.EMAIL)))
.build();
assertNotNull(bridge);
@ -216,8 +215,8 @@ public abstract class AbstractMieleThingHandlerTest extends JavaOSGiTest {
List<Channel> channels = createChannelsForThingHandler(thingTypeUid, thingUid);
Thing thing = ThingBuilder.create(thingTypeUid, thingUid)
.withConfiguration(new Configuration(Collections
.singletonMap(MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER, deviceIdentifier)))
.withConfiguration(new Configuration(
Map.of(MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER, deviceIdentifier)))
.withBridge(getBridge().getUID()).withChannels(channels).withLabel("DA-6996")
.withProperty("thingTypeVersion", thingTypeVersion).build();
assertNotNull(thing);

View File

@ -17,7 +17,7 @@ import static org.mockito.Mockito.*;
import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants.*;
import static org.openhab.binding.mielecloud.internal.util.ReflectionUtil.*;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@ -117,9 +117,8 @@ public class MieleHandlerFactoryTest extends JavaOSGiTest {
Bridge bridge = BridgeBuilder
.create(MieleCloudBindingConstants.THING_TYPE_BRIDGE,
MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID)
.withConfiguration(
new Configuration(Collections.singletonMap(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
MieleCloudBindingIntegrationTestConstants.EMAIL)))
.withConfiguration(new Configuration(Map.of(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
MieleCloudBindingIntegrationTestConstants.EMAIL)))
.withLabel(MIELE_CLOUD_ACCOUNT_LABEL).build();
assertNotNull(bridge);
@ -141,9 +140,8 @@ public class MieleHandlerFactoryTest extends JavaOSGiTest {
Bridge bridge = BridgeBuilder
.create(MieleCloudBindingConstants.THING_TYPE_BRIDGE,
MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID)
.withConfiguration(
new Configuration(Collections.singletonMap(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
MieleCloudBindingIntegrationTestConstants.EMAIL)))
.withConfiguration(new Configuration(Map.of(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
MieleCloudBindingIntegrationTestConstants.EMAIL)))
.withLabel(MIELE_CLOUD_ACCOUNT_LABEL).build();
assertNotNull(bridge);
@ -194,8 +192,8 @@ public class MieleHandlerFactoryTest extends JavaOSGiTest {
// when:
Thing device = ThingBuilder.create(thingTypeUid, thingUid)
.withConfiguration(new Configuration(Collections
.singletonMap(MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER, DEVICE_IDENTIFIER)))
.withConfiguration(new Configuration(
Map.of(MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER, DEVICE_IDENTIFIER)))
.withLabel(label).withProperty("thingTypeVersion", thingTypeVersion).build();
assertNotNull(device);
@ -208,9 +206,8 @@ public class MieleHandlerFactoryTest extends JavaOSGiTest {
Bridge bridge = BridgeBuilder
.create(MieleCloudBindingConstants.THING_TYPE_BRIDGE,
MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID)
.withConfiguration(
new Configuration(Collections.singletonMap(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
MieleCloudBindingIntegrationTestConstants.EMAIL)))
.withConfiguration(new Configuration(Map.of(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
MieleCloudBindingIntegrationTestConstants.EMAIL)))
.withLabel(MIELE_CLOUD_ACCOUNT_LABEL).build();
assertNotNull(bridge);

View File

@ -15,7 +15,7 @@ package org.openhab.binding.mielecloud.internal.util;
import static org.junit.jupiter.api.Assertions.*;
import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants.MIELE_CLOUD_ACCOUNT_LABEL;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -78,9 +78,8 @@ public abstract class OpenHabOsgiTest extends JavaOSGiTest {
Bridge bridge = BridgeBuilder
.create(MieleCloudBindingConstants.THING_TYPE_BRIDGE,
MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID)
.withConfiguration(
new Configuration(Collections.singletonMap(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
MieleCloudBindingIntegrationTestConstants.EMAIL)))
.withConfiguration(new Configuration(Map.of(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL,
MieleCloudBindingIntegrationTestConstants.EMAIL)))
.withLabel(MIELE_CLOUD_ACCOUNT_LABEL).build();
assertNotNull(bridge);

View File

@ -21,7 +21,6 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
@ -96,7 +95,7 @@ public abstract class AbstractModbusOSGiTest extends JavaOSGiTest {
@Override
public Set<@NonNull String> getSubscribedEventTypes() {
return Collections.singleton(ItemStateEvent.TYPE);
return Set.of(ItemStateEvent.TYPE);
}
@Override

View File

@ -15,8 +15,8 @@ package org.openhab.binding.mqtt.homeassistant;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@ -86,8 +86,7 @@ public class DiscoverComponentsTest extends JavaOSGiTest {
DiscoverComponents discover = spy(new DiscoverComponents(ThingChannelConstants.TEST_HOME_ASSISTANT_THING,
scheduler, channelStateUpdateListener, availabilityTracker, gson, transformationServiceProvider));
HandlerConfiguration config = new HandlerConfiguration("homeassistant",
Collections.singletonList("switch/object"));
HandlerConfiguration config = new HandlerConfiguration("homeassistant", List.of("switch/object"));
Set<HaID> discoveryIds = new HashSet<>();
discoveryIds.addAll(HaID.fromConfig(config));

View File

@ -19,10 +19,10 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledExecutorService;
@ -166,12 +166,11 @@ public class HomeAssistantMQTTImplementationTest extends MqttOSGiTest {
// Start the discovery for 2000ms. Forced timeout after 4000ms.
HaID haID = new HaID(testObjectTopic + "/config");
CompletableFuture<Void> future = discover.startDiscovery(haConnection, 2000, Collections.singleton(haID), cd)
.thenRun(() -> {
}).exceptionally(e -> {
failure = e;
return null;
});
CompletableFuture<Void> future = discover.startDiscovery(haConnection, 2000, Set.of(haID), cd).thenRun(() -> {
}).exceptionally(e -> {
failure = e;
return null;
});
assertTrue(latch.await(4, TimeUnit.SECONDS));
future.get(5, TimeUnit.SECONDS);

View File

@ -385,9 +385,15 @@ public class RuuviGatewayTest extends MqttOSGiTest {
} else {
// test argument is only specifiying the data field in the json payload
// Fill rest of the fields with some valid values
jsonPayload = "{" + " \"gw_mac\": \"DE:AD:BE:EF:00\"," + " \"rssi\": -82," + " \"aoa\": [],"
+ " \"gwts\": \"1659365432\"," + " \"ts\": \"1659365222\"," + " \"data\": \"" + val + "\","
+ " \"coords\": \"\" }";
jsonPayload = """
{\
"gw_mac": "DE:AD:BE:EF:00",\
"rssi": -82,\
"aoa": [],\
"gwts": "1659365432",\
"ts": "1659365222",\
"data": "\
""" + val + "\"," + " \"coords\": \"\" }";
}
Thing ruuviThing = createRuuviThing("mygwid", topic, 100);
@ -525,7 +531,6 @@ public class RuuviGatewayTest extends MqttOSGiTest {
.forEach(channelId -> assertEquals(UnDefType.UNDEF, getItemState.apply(channelId)));
statusUpdateIndex.incrementAndGet();
});
}
// Another mqtt update (("minimum values" test vector from

View File

@ -14,7 +14,6 @@ package org.openhab.binding.mqtt.ruuvigateway;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -48,7 +47,7 @@ public class ThingStatusInfoChangedSubscriber implements EventSubscriber {
@Override
public Set<@NonNull String> getSubscribedEventTypes() {
return Collections.singleton(ThingStatusInfoChangedEvent.TYPE);
return Set.of(ThingStatusInfoChangedEvent.TYPE);
}
@Override

View File

@ -22,8 +22,8 @@ import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import org.junit.jupiter.api.AfterAll;
@ -483,7 +483,7 @@ public class NtpOSGiTest extends JavaOSGiTest {
initialize(configuration, channelID, acceptedItemType, null, null);
EventSubscriber eventSubscriberMock = mock(EventSubscriber.class);
when(eventSubscriberMock.getSubscribedEventTypes()).thenReturn(Collections.singleton(ItemStateEvent.TYPE));
when(eventSubscriberMock.getSubscribedEventTypes()).thenReturn(Set.of(ItemStateEvent.TYPE));
registerService(eventSubscriberMock);
if (updateEventType.equals(UpdateEventType.HANDLE_COMMAND)) {

View File

@ -107,7 +107,7 @@ public class WemoDiscoveryParticipantTest {
assertThat(result.getThingUID(), is(new ThingUID(thingTypeUid, DEVICE_UDN)));
assertThat(result.getThingTypeUID(), is(thingTypeUid));
assertThat(result.getBridgeUID(), is(nullValue()));
assertThat(result.getProperties().get(WemoBindingConstants.UDN), is(DEVICE_UDN.toString()));
assertThat(result.getProperties().get(WemoBindingConstants.UDN), is(DEVICE_UDN));
assertThat(result.getRepresentationProperty(), is(WemoBindingConstants.UDN));
}
}