2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.webservice;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.*;
18 import java.util.Arrays;
19 import java.util.HashSet;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.mielecloud.internal.webservice.api.json.Device;
24 import org.openhab.binding.mielecloud.internal.webservice.api.json.DeviceCollection;
27 * @author Björn Lange - Initial contribution
30 public class DeviceCacheTest {
31 private static final String FIRST_DEVICE_IDENTIFIER = "000124430016";
32 private static final String SECOND_DEVICE_IDENTIFIER = "000124430017";
33 private static final String THIRD_DEVICE_IDENTIFIER = "400124430017";
35 private final Device firstDevice = mock(Device.class);
36 private final Device secondDevice = mock(Device.class);
37 private final Device thirdDevice = mock(Device.class);
39 private final DeviceCache deviceCache = new DeviceCache();
42 public void testCacheIsEmptyAfterConstruction() {
44 assertEquals(0, deviceCache.getDeviceIds().size());
48 public void testReplaceAllDevicesClearsTheCacheAndPutsAllNewDevicesIntoTheCache() {
50 DeviceCollection deviceCollection = mock(DeviceCollection.class);
51 when(deviceCollection.getDeviceIdentifiers())
52 .thenReturn(new HashSet<>(Arrays.asList(FIRST_DEVICE_IDENTIFIER, SECOND_DEVICE_IDENTIFIER)));
53 when(deviceCollection.getDevice(FIRST_DEVICE_IDENTIFIER)).thenReturn(firstDevice);
54 when(deviceCollection.getDevice(SECOND_DEVICE_IDENTIFIER)).thenReturn(secondDevice);
57 deviceCache.replaceAllDevices(deviceCollection);
60 assertEquals(new HashSet<>(Arrays.asList(FIRST_DEVICE_IDENTIFIER, SECOND_DEVICE_IDENTIFIER)),
61 deviceCache.getDeviceIds());
62 assertEquals(firstDevice, deviceCache.getDevice(FIRST_DEVICE_IDENTIFIER).get());
63 assertEquals(secondDevice, deviceCache.getDevice(SECOND_DEVICE_IDENTIFIER).get());
67 public void testReplaceAllDevicesClearsTheCachePriorToCachingThePassedDevices() {
69 testReplaceAllDevicesClearsTheCacheAndPutsAllNewDevicesIntoTheCache();
71 DeviceCollection deviceCollection = mock(DeviceCollection.class);
72 when(deviceCollection.getDeviceIdentifiers()).thenReturn(new HashSet<>(Arrays.asList(THIRD_DEVICE_IDENTIFIER)));
73 when(deviceCollection.getDevice(THIRD_DEVICE_IDENTIFIER)).thenReturn(thirdDevice);
76 deviceCache.replaceAllDevices(deviceCollection);
79 assertEquals(new HashSet<>(Arrays.asList(THIRD_DEVICE_IDENTIFIER)), deviceCache.getDeviceIds());
80 assertEquals(thirdDevice, deviceCache.getDevice(THIRD_DEVICE_IDENTIFIER).get());
84 public void testClearClearsTheCachedDevices() {
86 testReplaceAllDevicesClearsTheCacheAndPutsAllNewDevicesIntoTheCache();
92 assertEquals(0, deviceCache.getDeviceIds().size());