]> git.basschouten.com Git - openhab-addons.git/blob
6684b0771ea684e75fff6bf17c89a36420909803
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.webservice;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.*;
17
18 import java.util.Arrays;
19 import java.util.HashSet;
20
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;
25
26 /**
27  * @author Björn Lange - Initial contribution
28  */
29 @NonNullByDefault
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";
34
35     private final Device firstDevice = mock(Device.class);
36     private final Device secondDevice = mock(Device.class);
37     private final Device thirdDevice = mock(Device.class);
38
39     private final DeviceCache deviceCache = new DeviceCache();
40
41     @Test
42     public void testCacheIsEmptyAfterConstruction() {
43         // then:
44         assertEquals(0, deviceCache.getDeviceIds().size());
45     }
46
47     @Test
48     public void testReplaceAllDevicesClearsTheCacheAndPutsAllNewDevicesIntoTheCache() {
49         // given:
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);
55
56         // when:
57         deviceCache.replaceAllDevices(deviceCollection);
58
59         // then:
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());
64     }
65
66     @Test
67     public void testReplaceAllDevicesClearsTheCachePriorToCachingThePassedDevices() {
68         // given:
69         testReplaceAllDevicesClearsTheCacheAndPutsAllNewDevicesIntoTheCache();
70
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);
74
75         // when:
76         deviceCache.replaceAllDevices(deviceCollection);
77
78         // then:
79         assertEquals(new HashSet<>(Arrays.asList(THIRD_DEVICE_IDENTIFIER)), deviceCache.getDeviceIds());
80         assertEquals(thirdDevice, deviceCache.getDevice(THIRD_DEVICE_IDENTIFIER).get());
81     }
82
83     @Test
84     public void testClearClearsTheCachedDevices() {
85         // given:
86         testReplaceAllDevicesClearsTheCacheAndPutsAllNewDevicesIntoTheCache();
87
88         // when:
89         deviceCache.clear();
90
91         // then:
92         assertEquals(0, deviceCache.getDeviceIds().size());
93     }
94 }