]> git.basschouten.com Git - openhab-addons.git/blob
803128577bbb09cee968ab1b365f666b294717a9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.systeminfo.test;
14
15 import static java.lang.Thread.sleep;
16 import static java.util.stream.Collectors.toList;
17 import static org.hamcrest.CoreMatchers.*;
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.junit.jupiter.api.Assertions.assertFalse;
20 import static org.mockito.Mockito.*;
21
22 import java.math.BigDecimal;
23 import java.net.UnknownHostException;
24 import java.util.Hashtable;
25 import java.util.List;
26
27 import org.junit.jupiter.api.AfterEach;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Disabled;
30 import org.junit.jupiter.api.Test;
31 import org.openhab.binding.systeminfo.internal.SysteminfoBindingConstants;
32 import org.openhab.binding.systeminfo.internal.SysteminfoHandlerFactory;
33 import org.openhab.binding.systeminfo.internal.discovery.SysteminfoDiscoveryService;
34 import org.openhab.binding.systeminfo.internal.handler.SysteminfoHandler;
35 import org.openhab.binding.systeminfo.internal.model.DeviceNotFoundException;
36 import org.openhab.binding.systeminfo.internal.model.SysteminfoInterface;
37 import org.openhab.core.config.core.Configuration;
38 import org.openhab.core.config.discovery.DiscoveryResult;
39 import org.openhab.core.config.discovery.DiscoveryService;
40 import org.openhab.core.config.discovery.inbox.Inbox;
41 import org.openhab.core.config.discovery.inbox.InboxPredicates;
42 import org.openhab.core.items.GenericItem;
43 import org.openhab.core.items.ItemNotFoundException;
44 import org.openhab.core.items.ItemRegistry;
45 import org.openhab.core.library.items.NumberItem;
46 import org.openhab.core.library.items.StringItem;
47 import org.openhab.core.library.types.DecimalType;
48 import org.openhab.core.library.types.StringType;
49 import org.openhab.core.test.java.JavaOSGiTest;
50 import org.openhab.core.test.storage.VolatileStorageService;
51 import org.openhab.core.thing.Channel;
52 import org.openhab.core.thing.ChannelUID;
53 import org.openhab.core.thing.ManagedThingProvider;
54 import org.openhab.core.thing.Thing;
55 import org.openhab.core.thing.ThingProvider;
56 import org.openhab.core.thing.ThingRegistry;
57 import org.openhab.core.thing.ThingStatus;
58 import org.openhab.core.thing.ThingStatusDetail;
59 import org.openhab.core.thing.ThingTypeUID;
60 import org.openhab.core.thing.ThingUID;
61 import org.openhab.core.thing.binding.ThingHandler;
62 import org.openhab.core.thing.binding.ThingHandlerFactory;
63 import org.openhab.core.thing.binding.builder.ChannelBuilder;
64 import org.openhab.core.thing.binding.builder.ThingBuilder;
65 import org.openhab.core.thing.link.ItemChannelLink;
66 import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
67 import org.openhab.core.thing.type.ChannelKind;
68 import org.openhab.core.thing.type.ChannelTypeUID;
69 import org.openhab.core.types.State;
70 import org.openhab.core.types.UnDefType;
71
72 /**
73  * OSGi tests for the {@link SysteminfoHandler}
74  *
75  * @author Svilen Valkanov - Initial contribution
76  * @author Lyubomir Papazov - Created a mock systeminfo object. This way, access to the user's OS will not be required,
77  *         but mock data will be used instead, avoiding potential errors from the OS queries.
78  * @author Wouter Born - Migrate Groovy to Java tests
79  */
80 public class SysteminfoOSGiTest extends JavaOSGiTest {
81     private static final String DEFAULT_TEST_THING_NAME = "work";
82     private static final String DEFAULT_TEST_ITEM_NAME = "test";
83     private static final String DEFAULT_CHANNEL_TEST_PRIORITY = "High";
84     private static final int DEFAULT_CHANNEL_PID = -1;
85     private static final String DEFAULT_TEST_CHANNEL_ID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
86     private static final int DEFAULT_DEVICE_INDEX = 0;
87
88     /**
89      * Refresh time in seconds for tasks with priority High.
90      * Default value for the parameter interval_high in the thing configuration
91      */
92     private static final int DEFAULT_TEST_INTERVAL_HIGH = 1;
93
94     /**
95      * Refresh time in seconds for tasks with priority Medium.
96      */
97     private static final int DEFAULT_TEST_INTERVAL_MEDIUM = 3;
98
99     private Thing systemInfoThing;
100     private SysteminfoHandler systemInfoHandler;
101     private GenericItem testItem;
102
103     private SysteminfoInterface mockedSystemInfo;
104     private ManagedThingProvider managedThingProvider;
105     private ThingRegistry thingRegistry;
106     private ItemRegistry itemRegistry;
107     private SysteminfoHandlerFactory systeminfoHandlerFactory;
108
109     @BeforeEach
110     public void setUp() {
111         VolatileStorageService volatileStorageService = new VolatileStorageService();
112         registerService(volatileStorageService);
113
114         // Preparing the mock with OS properties, that are used in the initialize method of SysteminfoHandler
115         mockedSystemInfo = mock(SysteminfoInterface.class);
116         when(mockedSystemInfo.getCpuLogicalCores()).thenReturn(new DecimalType(2));
117         when(mockedSystemInfo.getCpuPhysicalCores()).thenReturn(new DecimalType(2));
118         when(mockedSystemInfo.getOsFamily()).thenReturn(new StringType("Mock OS"));
119         when(mockedSystemInfo.getOsManufacturer()).thenReturn(new StringType("Mock OS Manufacturer"));
120         when(mockedSystemInfo.getOsVersion()).thenReturn(new StringType("Mock Os Version"));
121
122         systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
123         SysteminfoInterface oshiSystemInfo = getService(SysteminfoInterface.class);
124
125         // Unbind oshiSystemInfo service and bind the mock service to make the systeminfobinding tests independent of
126         // the external OSHI library
127         if (oshiSystemInfo != null) {
128             systeminfoHandlerFactory.unbindSystemInfo(oshiSystemInfo);
129         }
130         systeminfoHandlerFactory.bindSystemInfo(mockedSystemInfo);
131
132         managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
133         assertThat(managedThingProvider, is(notNullValue()));
134
135         thingRegistry = getService(ThingRegistry.class);
136         assertThat(thingRegistry, is(notNullValue()));
137
138         itemRegistry = getService(ItemRegistry.class);
139         assertThat(itemRegistry, is(notNullValue()));
140     }
141
142     @AfterEach
143     public void tearDown() {
144         if (systemInfoThing != null) {
145             // Remove the systeminfo thing. The handler will be also disposed automatically
146             Thing removedThing = thingRegistry.forceRemove(systemInfoThing.getUID());
147             assertThat("The systeminfo thing cannot be deleted", removedThing, is(notNullValue()));
148         }
149         waitForAssert(() -> {
150             ThingHandler systemInfoHandler = systemInfoThing.getHandler();
151             assertThat(systemInfoHandler, is(nullValue()));
152         });
153
154         if (testItem != null) {
155             itemRegistry.remove(DEFAULT_TEST_ITEM_NAME);
156         }
157     }
158
159     private void initializeThingWithChannelAndPID(String channelID, String acceptedItemType, int pid) {
160         Configuration thingConfig = new Configuration();
161         thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
162                 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
163         thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
164                 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
165         String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
166
167         initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
168     }
169
170     private void initializeThingWithChannelAndPriority(String channelID, String acceptedItemType, String priority) {
171         Configuration thingConfig = new Configuration();
172         thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
173                 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
174         thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
175                 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
176         int pid = DEFAULT_CHANNEL_PID;
177
178         initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
179     }
180
181     private void initializeThingWithConfiguration(Configuration config) {
182         String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
183         String channelID = DEFAULT_TEST_CHANNEL_ID;
184         String acceptedItemType = "String";
185         int pid = DEFAULT_CHANNEL_PID;
186
187         initializeThing(config, channelID, acceptedItemType, priority, pid);
188     }
189
190     private void initializeThingWithChannel(String channelID, String acceptedItemType) {
191         Configuration thingConfig = new Configuration();
192         thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
193                 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
194         thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
195                 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
196
197         String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
198         int pid = DEFAULT_CHANNEL_PID;
199         initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
200     }
201
202     private void initializeThing(Configuration thingConfiguration, String channelID, String acceptedItemType,
203             String priority, int pid) {
204         ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
205         ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
206
207         ChannelUID channelUID = new ChannelUID(thingUID, channelID);
208         ChannelTypeUID channelTypeUID = new ChannelTypeUID(SysteminfoBindingConstants.BINDING_ID,
209                 channelUID.getIdWithoutGroup());
210         Configuration channelConfig = new Configuration();
211         channelConfig.put("priority", priority);
212         channelConfig.put("pid", new BigDecimal(pid));
213         Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
214                 .withKind(ChannelKind.STATE).withConfiguration(channelConfig).build();
215
216         systemInfoThing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(thingConfiguration)
217                 .withChannel(channel).build();
218
219         managedThingProvider.add(systemInfoThing);
220
221         waitForAssert(() -> {
222             systemInfoHandler = (SysteminfoHandler) systemInfoThing.getHandler();
223             assertThat(systemInfoHandler, is(notNullValue()));
224         });
225
226         waitForAssert(() -> {
227             assertThat("Thing is not initilized, before an Item is created", systemInfoThing.getStatus(),
228                     anyOf(equalTo(ThingStatus.OFFLINE), equalTo(ThingStatus.ONLINE)));
229         });
230
231         intializeItem(channelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
232     }
233
234     private void assertItemState(String acceptedItemType, String itemName, String priority, State expectedState) {
235         waitForAssert(() -> {
236             ThingStatusDetail thingStatusDetail = systemInfoThing.getStatusInfo().getStatusDetail();
237             String description = systemInfoThing.getStatusInfo().getDescription();
238             assertThat("Thing status detail is " + thingStatusDetail + " with description " + description,
239                     systemInfoThing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
240         });
241         // The binding starts all refresh tasks in SysteminfoHandler.scheduleUpdates() after this delay !
242         try {
243             sleep(SysteminfoHandler.WAIT_TIME_CHANNEL_ITEM_LINK_INIT * 1000);
244         } catch (InterruptedException e) {
245             throw new AssertionError("Interrupted while sleeping");
246         }
247
248         GenericItem item;
249         try {
250             item = (GenericItem) itemRegistry.getItem(itemName);
251         } catch (ItemNotFoundException e) {
252             throw new AssertionError("Item not found in registry");
253         }
254
255         int waitTime;
256         if (priority.equals("High")) {
257             waitTime = DEFAULT_TEST_INTERVAL_HIGH * 1000;
258         } else if (priority.equals("Medium")) {
259             waitTime = DEFAULT_TEST_INTERVAL_MEDIUM * 1000;
260         } else {
261             waitTime = 100;
262         }
263
264         waitForAssert(() -> {
265             State itemState = item.getState();
266             assertThat(itemState, is(equalTo(expectedState)));
267         }, waitTime, DFL_SLEEP_TIME);
268     }
269
270     private void intializeItem(ChannelUID channelUID, String itemName, String acceptedItemType) {
271         if (acceptedItemType.equals("Number")) {
272             testItem = new NumberItem(itemName);
273         } else if (acceptedItemType.equals("String")) {
274             testItem = new StringItem(itemName);
275         }
276         itemRegistry.add(testItem);
277
278         ManagedItemChannelLinkProvider itemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
279         assertThat(itemChannelLinkProvider, is(notNullValue()));
280
281         itemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
282     }
283
284     @Test
285     public void assertInvalidThingConfigurationValuesAreHandled() {
286         Configuration configuration = new Configuration();
287
288         // invalid value - must be positive
289         int refreshIntervalHigh = -5;
290         configuration.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME, new BigDecimal(refreshIntervalHigh));
291
292         int refreshIntervalMedium = 3;
293         configuration.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
294                 new BigDecimal(refreshIntervalMedium));
295         initializeThingWithConfiguration(configuration);
296
297         testInvalidConfiguration();
298     }
299
300     private void testInvalidConfiguration() {
301         waitForAssert(() -> {
302             assertThat("Invalid configuratuin is used !", systemInfoThing.getStatus(),
303                     is(equalTo(ThingStatus.OFFLINE)));
304             assertThat(systemInfoThing.getStatusInfo().getStatusDetail(),
305                     is(equalTo(ThingStatusDetail.HANDLER_INITIALIZING_ERROR)));
306             assertThat(systemInfoThing.getStatusInfo().getDescription(), is(equalTo("Thing cannot be initialized!")));
307         });
308     }
309
310     @Test
311     public void assertThingStatusIsUninitializedWhenThereIsNoSysteminfoServiceProvided() {
312         // Unbind the mock service to verify the systeminfo thing will not be initialized when no systeminfo service is
313         // provided
314         systeminfoHandlerFactory.unbindSystemInfo(mockedSystemInfo);
315
316         ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
317         ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
318
319         systemInfoThing = ThingBuilder.create(thingTypeUID, thingUID).build();
320         managedThingProvider.add(systemInfoThing);
321
322         waitForAssert(() -> {
323             assertThat("The thing status is uninitialized when systeminfo service is missing",
324                     systemInfoThing.getStatus(), equalTo(ThingStatus.UNINITIALIZED));
325         });
326     }
327
328     @Test
329     public void assertMediumPriorityChannelIsUpdated() {
330         String channnelID = DEFAULT_TEST_CHANNEL_ID;
331         String acceptedItemType = "Number";
332         String priority = "Medium";
333
334         initializeThingWithChannelAndPriority(channnelID, acceptedItemType, priority);
335         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, priority, UnDefType.UNDEF);
336     }
337
338     @Test
339     public void assertStateOfSecondDeviceIsUpdated() {
340         // This test assumes that at least 2 network interfaces are present on the test platform
341         int deviceIndex = 1;
342         String channnelID = "network" + deviceIndex + "#mac";
343         String acceptedItemType = "String";
344
345         initializeThingWithChannel(channnelID, acceptedItemType);
346         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, UnDefType.UNDEF);
347     }
348
349     @Test
350     public void assertChannelCpuLoad1IsUpdated() {
351         String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_1;
352         String acceptedItemType = "Number";
353
354         DecimalType mockedCpuLoad1Value = new DecimalType(1.1);
355         when(mockedSystemInfo.getCpuLoad1()).thenReturn(mockedCpuLoad1Value);
356
357         initializeThingWithChannel(channnelID, acceptedItemType);
358         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad1Value);
359     }
360
361     @Test
362     public void assertChannelCpuLoad5IsUpdated() {
363         String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_5;
364         String acceptedItemType = "Number";
365
366         DecimalType mockedCpuLoad5Value = new DecimalType(5.5);
367         when(mockedSystemInfo.getCpuLoad5()).thenReturn(mockedCpuLoad5Value);
368
369         initializeThingWithChannel(channnelID, acceptedItemType);
370         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad5Value);
371     }
372
373     @Test
374     public void assertChannelCpuLoad15IsUpdated() {
375         String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_15;
376         String acceptedItemType = "Number";
377
378         DecimalType mockedCpuLoad15Value = new DecimalType(15.15);
379         when(mockedSystemInfo.getCpuLoad15()).thenReturn(mockedCpuLoad15Value);
380
381         initializeThingWithChannel(channnelID, acceptedItemType);
382         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad15Value);
383     }
384
385     @Test
386     public void assertChannelCpuThreadsIsUpdated() {
387         String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_THREADS;
388         String acceptedItemType = "Number";
389
390         DecimalType mockedCpuThreadsValue = new DecimalType(16);
391         when(mockedSystemInfo.getCpuThreads()).thenReturn(mockedCpuThreadsValue);
392
393         initializeThingWithChannel(channnelID, acceptedItemType);
394         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuThreadsValue);
395     }
396
397     @Test
398     public void assertChannelCpuUptimeIsUpdated() {
399         String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_UPTIME;
400         String acceptedItemType = "Number";
401
402         DecimalType mockedCpuUptimeValue = new DecimalType(100);
403         when(mockedSystemInfo.getCpuUptime()).thenReturn(mockedCpuUptimeValue);
404
405         initializeThingWithChannel(channnelID, acceptedItemType);
406         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuUptimeValue);
407     }
408
409     @Test
410     public void assertChannelCpuDescriptionIsUpdated() {
411         String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_DESCRIPTION;
412         String acceptedItemType = "String";
413
414         StringType mockedCpuDescriptionValue = new StringType("Mocked Cpu Descr");
415         when(mockedSystemInfo.getCpuDescription()).thenReturn(mockedCpuDescriptionValue);
416
417         initializeThingWithChannel(channnelID, acceptedItemType);
418         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
419                 mockedCpuDescriptionValue);
420     }
421
422     @Test
423     public void assertChannelCpuNameIsUpdated() {
424         String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_NAME;
425         String acceptedItemType = "String";
426
427         StringType mockedCpuNameValue = new StringType("Mocked Cpu Name");
428         when(mockedSystemInfo.getCpuName()).thenReturn(mockedCpuNameValue);
429
430         initializeThingWithChannel(channnelID, acceptedItemType);
431         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuNameValue);
432     }
433
434     @Test
435     public void assertChannelMemoryAvailableIsUpdated() {
436         String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE;
437         String acceptedItemType = "Number";
438
439         DecimalType mockedMemoryAvailableValue = new DecimalType(1000);
440         when(mockedSystemInfo.getMemoryAvailable()).thenReturn(mockedMemoryAvailableValue);
441
442         initializeThingWithChannel(channnelID, acceptedItemType);
443         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
444                 mockedMemoryAvailableValue);
445     }
446
447     @Test
448     public void assertChannelMemoryUsedIsUpdated() {
449         String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_USED;
450         String acceptedItemType = "Number";
451
452         DecimalType mockedMemoryUsedValue = new DecimalType(24);
453         when(mockedSystemInfo.getMemoryUsed()).thenReturn(mockedMemoryUsedValue);
454
455         initializeThingWithChannel(channnelID, acceptedItemType);
456         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedMemoryUsedValue);
457     }
458
459     @Test
460     public void assertChannelMemoryTotalIsUpdated() {
461         String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_TOTAL;
462         String acceptedItemType = "Number";
463
464         DecimalType mockedMemoryTotalValue = new DecimalType(1024);
465         when(mockedSystemInfo.getMemoryTotal()).thenReturn(mockedMemoryTotalValue);
466
467         initializeThingWithChannel(channnelID, acceptedItemType);
468         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
469                 mockedMemoryTotalValue);
470     }
471
472     @Test
473     public void assertChannelMemoryAvailablePercentIsUpdated() {
474         String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE_PERCENT;
475         String acceptedItemType = "Number";
476
477         DecimalType mockedMemoryAvailablePercentValue = new DecimalType(97);
478         when(mockedSystemInfo.getMemoryAvailablePercent()).thenReturn(mockedMemoryAvailablePercentValue);
479
480         initializeThingWithChannel(channnelID, acceptedItemType);
481         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
482                 mockedMemoryAvailablePercentValue);
483     }
484
485     @Test
486     public void assertChannelSwapAvailableIsUpdated() {
487         String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE;
488         String acceptedItemType = "Number";
489
490         DecimalType mockedSwapAvailableValue = new DecimalType(482);
491         when(mockedSystemInfo.getSwapAvailable()).thenReturn(mockedSwapAvailableValue);
492
493         initializeThingWithChannel(channnelID, acceptedItemType);
494         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
495                 mockedSwapAvailableValue);
496     }
497
498     @Test
499     public void assertChannelSwapUsedIsUpdated() {
500         String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_USED;
501         String acceptedItemType = "Number";
502
503         DecimalType mockedSwapUsedValue = new DecimalType(30);
504         when(mockedSystemInfo.getSwapUsed()).thenReturn(mockedSwapUsedValue);
505
506         initializeThingWithChannel(channnelID, acceptedItemType);
507         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapUsedValue);
508     }
509
510     @Test
511     public void assertChannelSwapTotalIsUpdated() {
512         String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_TOTAL;
513         String acceptedItemType = "Number";
514
515         DecimalType mockedSwapTotalValue = new DecimalType(512);
516         when(mockedSystemInfo.getSwapTotal()).thenReturn(mockedSwapTotalValue);
517
518         initializeThingWithChannel(channnelID, acceptedItemType);
519         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapTotalValue);
520     }
521
522     @Test
523     public void assertChannelSwapAvailablePercentIsUpdated() {
524         String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE_PERCENT;
525         String acceptedItemType = "Number";
526
527         DecimalType mockedSwapAvailablePercentValue = new DecimalType(94);
528         when(mockedSystemInfo.getSwapAvailablePercent()).thenReturn(mockedSwapAvailablePercentValue);
529
530         initializeThingWithChannel(channnelID, acceptedItemType);
531         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
532                 mockedSwapAvailablePercentValue);
533     }
534
535     @Test
536     public void assertChannelStorageNameIsUpdated() throws DeviceNotFoundException {
537         String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_NAME;
538         String acceptedItemType = "String";
539
540         StringType mockedStorageName = new StringType("Mocked Storage Name");
541         when(mockedSystemInfo.getStorageName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageName);
542
543         initializeThingWithChannel(channnelID, acceptedItemType);
544         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageName);
545     }
546
547     @Test
548     public void assertChannelStorageTypeIsUpdated() throws DeviceNotFoundException {
549         String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TYPE;
550         String acceptedItemType = "String";
551
552         StringType mockedStorageType = new StringType("Mocked Storage Type");
553         when(mockedSystemInfo.getStorageType(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageType);
554
555         initializeThingWithChannel(channnelID, acceptedItemType);
556         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageType);
557     }
558
559     @Test
560     public void assertChannelStorageDescriptionIsUpdated() throws DeviceNotFoundException {
561         String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_DESCRIPTION;
562         String acceptedItemType = "String";
563
564         StringType mockedStorageDescription = new StringType("Mocked Storage Description");
565         when(mockedSystemInfo.getStorageDescription(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageDescription);
566
567         initializeThingWithChannel(channnelID, acceptedItemType);
568         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
569                 mockedStorageDescription);
570     }
571
572     @Test
573     public void assertChannelStorageAvailableIsUpdated() throws DeviceNotFoundException {
574         String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE;
575         String acceptedItemType = "Number";
576
577         DecimalType mockedStorageAvailableValue = new DecimalType(2000);
578         when(mockedSystemInfo.getStorageAvailable(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageAvailableValue);
579
580         initializeThingWithChannel(channnelID, acceptedItemType);
581         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
582                 mockedStorageAvailableValue);
583     }
584
585     @Test
586     public void assertChannelStorageUsedIsUpdated() throws DeviceNotFoundException {
587         String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_USED;
588         String acceptedItemType = "Number";
589
590         DecimalType mockedStorageUsedValue = new DecimalType(500);
591         when(mockedSystemInfo.getStorageUsed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageUsedValue);
592
593         initializeThingWithChannel(channnelID, acceptedItemType);
594         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
595                 mockedStorageUsedValue);
596     }
597
598     @Test
599     public void assertChannelStorageTotalIsUpdated() throws DeviceNotFoundException {
600         String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TOTAL;
601         String acceptedItemType = "Number";
602
603         DecimalType mockedStorageTotalValue = new DecimalType(2500);
604         when(mockedSystemInfo.getStorageTotal(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageTotalValue);
605
606         initializeThingWithChannel(channnelID, acceptedItemType);
607         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
608                 mockedStorageTotalValue);
609     }
610
611     @Test
612     public void assertChannelStorageAvailablePercentIsUpdated() throws DeviceNotFoundException {
613         String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE_PERCENT;
614         String acceptedItemType = "Number";
615
616         DecimalType mockedStorageAvailablePercent = new DecimalType(20);
617         when(mockedSystemInfo.getStorageAvailablePercent(DEFAULT_DEVICE_INDEX))
618                 .thenReturn(mockedStorageAvailablePercent);
619
620         initializeThingWithChannel(channnelID, acceptedItemType);
621         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
622                 mockedStorageAvailablePercent);
623     }
624
625     @Test
626     public void assertChannelDriveNameIsUpdated() throws DeviceNotFoundException {
627         String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_NAME;
628         String acceptedItemType = "String";
629
630         StringType mockedDriveNameValue = new StringType("Mocked Drive Name");
631         when(mockedSystemInfo.getDriveName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveNameValue);
632
633         initializeThingWithChannel(channelID, acceptedItemType);
634         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveNameValue);
635     }
636
637     @Test
638     public void assertChannelDriveModelIsUpdated() throws DeviceNotFoundException {
639         String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_MODEL;
640         String acceptedItemType = "String";
641
642         StringType mockedDriveModelValue = new StringType("Mocked Drive Model");
643         when(mockedSystemInfo.getDriveModel(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveModelValue);
644
645         initializeThingWithChannel(channelID, acceptedItemType);
646         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveModelValue);
647     }
648
649     @Test
650     public void assertChannelDriveSerialIsUpdated() throws DeviceNotFoundException {
651         String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_SERIAL;
652         String acceptedItemType = "String";
653
654         StringType mockedDriveSerialNumber = new StringType("Mocked Drive Serial Number");
655         when(mockedSystemInfo.getDriveSerialNumber(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveSerialNumber);
656
657         initializeThingWithChannel(channelID, acceptedItemType);
658         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
659                 mockedDriveSerialNumber);
660     }
661
662     @Disabled
663     // There is a bug opened for this issue - https://github.com/dblock/oshi/issues/185
664     @Test
665     public void assertChannelSensorsCpuTempIsUpdated() {
666         String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_CPU_TEMPERATURE;
667         String acceptedItemType = "Number";
668
669         DecimalType mockedSensorsCpuTemperatureValue = new DecimalType(60);
670         when(mockedSystemInfo.getSensorsCpuTemperature()).thenReturn(mockedSensorsCpuTemperatureValue);
671
672         initializeThingWithChannel(channnelID, acceptedItemType);
673         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
674                 mockedSensorsCpuTemperatureValue);
675     }
676
677     @Test
678     public void assertChannelSensorsCpuVoltageIsUpdated() {
679         String channnelID = SysteminfoBindingConstants.CHANNEL_SENOSRS_CPU_VOLTAGE;
680         String acceptedItemType = "Number";
681
682         DecimalType mockedSensorsCpuVoltageValue = new DecimalType(1000);
683         when(mockedSystemInfo.getSensorsCpuVoltage()).thenReturn(mockedSensorsCpuVoltageValue);
684
685         initializeThingWithChannel(channnelID, acceptedItemType);
686         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
687                 mockedSensorsCpuVoltageValue);
688     }
689
690     @Test
691     public void assertChannelSensorsFanSpeedIsUpdated() throws DeviceNotFoundException {
692         String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_FAN_SPEED;
693         String acceptedItemType = "Number";
694
695         DecimalType mockedSensorsCpuFanSpeedValue = new DecimalType(180);
696         when(mockedSystemInfo.getSensorsFanSpeed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedSensorsCpuFanSpeedValue);
697
698         initializeThingWithChannel(channnelID, acceptedItemType);
699         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
700                 mockedSensorsCpuFanSpeedValue);
701     }
702
703     @Test
704     public void assertChannelBatteryNameIsUpdated() throws DeviceNotFoundException {
705         String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_NAME;
706         String acceptedItemType = "String";
707
708         StringType mockedBatteryName = new StringType("Mocked Battery Name");
709         when(mockedSystemInfo.getBatteryName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryName);
710
711         initializeThingWithChannel(channnelID, acceptedItemType);
712         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedBatteryName);
713     }
714
715     @Test
716     public void assertChannelBatteryRemainingCapacityIsUpdated() throws DeviceNotFoundException {
717         String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_CAPACITY;
718         String acceptedItemType = "Number";
719
720         DecimalType mockedBatteryRemainingCapacity = new DecimalType(200);
721         when(mockedSystemInfo.getBatteryRemainingCapacity(DEFAULT_DEVICE_INDEX))
722                 .thenReturn(mockedBatteryRemainingCapacity);
723
724         initializeThingWithChannel(channnelID, acceptedItemType);
725         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
726                 mockedBatteryRemainingCapacity);
727     }
728
729     @Test
730     public void assertChannelBatteryRemainingTimeIsUpdated() throws DeviceNotFoundException {
731         String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_TIME;
732         String acceptedItemType = "Number";
733
734         DecimalType mockedBatteryRemainingTime = new DecimalType(3600);
735         when(mockedSystemInfo.getBatteryRemainingTime(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryRemainingTime);
736
737         initializeThingWithChannel(channnelID, acceptedItemType);
738         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
739                 mockedBatteryRemainingTime);
740     }
741
742     @Test
743     public void assertChannelDisplayInformationIsUpdated() throws DeviceNotFoundException {
744         String channnelID = SysteminfoBindingConstants.CHANNEL_DISPLAY_INFORMATION;
745         String acceptedItemType = "String";
746
747         StringType mockedDisplayInfo = new StringType("Mocked Display Information");
748         when(mockedSystemInfo.getDisplayInformation(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDisplayInfo);
749
750         initializeThingWithChannel(channnelID, acceptedItemType);
751         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDisplayInfo);
752     }
753
754     @Test
755     public void assertChannelNetworkIpIsUpdated() throws DeviceNotFoundException {
756         String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_IP;
757         String acceptedItemType = "String";
758
759         StringType mockedNetworkIp = new StringType("192.168.1.0");
760         when(mockedSystemInfo.getNetworkIp(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkIp);
761
762         initializeThingWithChannel(channnelID, acceptedItemType);
763         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkIp);
764     }
765
766     @Test
767     public void assertChannelNetworkMacIsUpdated() throws DeviceNotFoundException {
768         String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_MAC;
769         String acceptedItemType = "String";
770
771         StringType mockedNetworkMacValue = new StringType("AB-10-11-12-13-14");
772         when(mockedSystemInfo.getNetworkMac(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkMacValue);
773
774         initializeThingWithChannel(channnelID, acceptedItemType);
775         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkMacValue);
776     }
777
778     @Test
779     public void assertChannelNetworkDataSentIsUpdated() throws DeviceNotFoundException {
780         String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_SENT;
781         String acceptedItemType = "Number";
782
783         DecimalType mockedNetworkDataSent = new DecimalType(1000);
784         when(mockedSystemInfo.getNetworkDataSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataSent);
785
786         initializeThingWithChannel(channnelID, acceptedItemType);
787         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkDataSent);
788     }
789
790     @Test
791     public void assertChannelNetworkDataReceivedIsUpdated() throws DeviceNotFoundException {
792         String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_RECEIVED;
793         String acceptedItemType = "Number";
794
795         DecimalType mockedNetworkDataReceiveed = new DecimalType(800);
796         when(mockedSystemInfo.getNetworkDataReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataReceiveed);
797
798         initializeThingWithChannel(channnelID, acceptedItemType);
799         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
800                 mockedNetworkDataReceiveed);
801     }
802
803     @Test
804     public void assertChannelNetworkPacketsSentIsUpdated() throws DeviceNotFoundException {
805         String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_SENT;
806         String acceptedItemType = "Number";
807
808         DecimalType mockedNetworkPacketsSent = new DecimalType(50);
809         when(mockedSystemInfo.getNetworkPacketsSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsSent);
810
811         initializeThingWithChannel(channnelID, acceptedItemType);
812         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
813                 mockedNetworkPacketsSent);
814     }
815
816     @Test
817     public void assertChannelNetworkPacketsReceivedIsUpdated() throws DeviceNotFoundException {
818         String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_RECEIVED;
819         String acceptedItemType = "Number";
820
821         DecimalType mockedNetworkPacketsReceived = new DecimalType(48);
822         when(mockedSystemInfo.getNetworkPacketsReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsReceived);
823
824         initializeThingWithChannel(channnelID, acceptedItemType);
825         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
826                 mockedNetworkPacketsReceived);
827     }
828
829     @Test
830     public void assertChannelNetworkNetworkNameIsUpdated() throws DeviceNotFoundException {
831         String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_NAME;
832         String acceptedItemType = "String";
833
834         StringType mockedNetworkName = new StringType("MockN-AQ34");
835         when(mockedSystemInfo.getNetworkName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkName);
836
837         initializeThingWithChannel(channnelID, acceptedItemType);
838         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkName);
839     }
840
841     @Test
842     public void assertChannelNetworkNetworkDisplayNameIsUpdated() throws DeviceNotFoundException {
843         String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_ADAPTER_NAME;
844         String acceptedItemType = "String";
845
846         StringType mockedNetworkAdapterName = new StringType("Mocked Network Adapter Name");
847         when(mockedSystemInfo.getNetworkDisplayName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkAdapterName);
848
849         initializeThingWithChannel(channnelID, acceptedItemType);
850         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
851                 mockedNetworkAdapterName);
852     }
853
854     class SysteminfoDiscoveryServiceMock extends SysteminfoDiscoveryService {
855         String hostname;
856
857         SysteminfoDiscoveryServiceMock(String hostname) {
858             super();
859             this.hostname = hostname;
860         }
861
862         @Override
863         protected String getHostName() throws UnknownHostException {
864             if (hostname.equals("unresolved")) {
865                 throw new UnknownHostException();
866             }
867             return hostname;
868         }
869
870         @Override
871         public void startScan() {
872             super.startScan();
873         }
874     }
875
876     @Test
877     public void testDiscoveryWithInvalidHostname() {
878         String hostname = "Hilo.fritz.box";
879         String expectedHostname = "Hilo_fritz_box";
880
881         testDiscoveryService(expectedHostname, hostname);
882     }
883
884     @Test
885     public void testDiscoveryWithValidHostname() {
886         String hostname = "MyComputer";
887         String expectedHostname = "MyComputer";
888
889         testDiscoveryService(expectedHostname, hostname);
890     }
891
892     @Test
893     public void testDiscoveryWithUnresolvedHostname() {
894         String hostname = "unresolved";
895         String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
896
897         testDiscoveryService(expectedHostname, hostname);
898     }
899
900     @Test
901     public void testDiscoveryWithEmptyHostnameString() {
902         String hostname = "";
903         String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
904
905         testDiscoveryService(expectedHostname, hostname);
906     }
907
908     private void testDiscoveryService(String expectedHostname, String hostname) {
909         SysteminfoDiscoveryService discoveryService = getService(DiscoveryService.class,
910                 SysteminfoDiscoveryService.class);
911         waitForAssert(() -> {
912             assertThat(discoveryService, is(notNullValue()));
913         });
914         SysteminfoDiscoveryServiceMock discoveryServiceMock = new SysteminfoDiscoveryServiceMock(hostname);
915         if (discoveryService != null) {
916             unregisterService(DiscoveryService.class);
917         }
918         registerService(discoveryServiceMock, DiscoveryService.class.getName(), new Hashtable<>());
919
920         ThingTypeUID computerType = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
921         ThingUID computerUID = new ThingUID(computerType, expectedHostname);
922
923         discoveryServiceMock.startScan();
924
925         Inbox inbox = getService(Inbox.class);
926         assertThat(inbox, is(notNullValue()));
927
928         waitForAssert(() -> {
929             List<DiscoveryResult> results = inbox.stream().filter(InboxPredicates.forThingUID(computerUID))
930                     .collect(toList());
931             assertFalse(results.isEmpty(), "No Thing with UID " + computerUID.getAsString() + " in inbox");
932         });
933
934         inbox.approve(computerUID, SysteminfoDiscoveryService.DEFAULT_THING_LABEL, null);
935
936         waitForAssert(() -> {
937             systemInfoThing = thingRegistry.get(computerUID);
938             assertThat(systemInfoThing, is(notNullValue()));
939         });
940
941         waitForAssert(() -> {
942             assertThat("Thing is not initialized.", systemInfoThing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
943         });
944     }
945
946     @Test
947     public void assertChannelProcessThreadsIsUpdatedWithPIDse() throws DeviceNotFoundException {
948         String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_THREADS;
949         String acceptedItemType = "Number";
950         // The pid of the System idle process in Windows
951         int pid = 0;
952
953         DecimalType mockedProcessThreadsCount = new DecimalType(4);
954         when(mockedSystemInfo.getProcessThreads(pid)).thenReturn(mockedProcessThreadsCount);
955
956         initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
957         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
958                 mockedProcessThreadsCount);
959     }
960
961     @Test
962     public void assertChannelProcessPathIsUpdatedWithPIDset() throws DeviceNotFoundException {
963         String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_PATH;
964         String acceptedItemType = "String";
965         // The pid of the System idle process in Windows
966         int pid = 0;
967
968         StringType mockedProcessPath = new StringType("C:\\Users\\MockedUser\\Process");
969         when(mockedSystemInfo.getProcessPath(pid)).thenReturn(mockedProcessPath);
970
971         initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
972         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessPath);
973     }
974
975     @Test
976     public void assertChannelProcessNameIsUpdatedWithPIDset() throws DeviceNotFoundException {
977         String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_NAME;
978         String acceptedItemType = "String";
979         // The pid of the System idle process in Windows
980         int pid = 0;
981
982         StringType mockedProcessName = new StringType("MockedProcess.exe");
983         when(mockedSystemInfo.getProcessName(pid)).thenReturn(mockedProcessName);
984
985         initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
986         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessName);
987     }
988
989     @Test
990     public void assertChannelProcessMemoryIsUpdatedWithPIDset() throws DeviceNotFoundException {
991         String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_MEMORY;
992         String acceptedItemType = "Number";
993         // The pid of the System idle process in Windows
994         int pid = 0;
995
996         DecimalType mockedProcessMemory = new DecimalType(450);
997         when(mockedSystemInfo.getProcessMemoryUsage(pid)).thenReturn(mockedProcessMemory);
998
999         initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1000         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessMemory);
1001     }
1002
1003     @Test
1004     public void assertChannelProcessLoadIsUpdatedWithPIDset() throws DeviceNotFoundException {
1005         String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_LOAD;
1006         String acceptedItemType = "Number";
1007         // The pid of the System idle process in Windows
1008         int pid = 0;
1009
1010         DecimalType mockedProcessLoad = new DecimalType(3);
1011         when(mockedSystemInfo.getProcessCpuUsage(pid)).thenReturn(mockedProcessLoad);
1012
1013         initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1014         assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessLoad);
1015     }
1016
1017     @Test
1018     public void testThingHandlesChannelPriorityChange() {
1019         String priorityKey = "priority";
1020         String pidKey = "pid";
1021         String initialPriority = DEFAULT_CHANNEL_TEST_PRIORITY; // Evaluates to High
1022         String newPriority = "Low";
1023
1024         String acceptedItemType = "Number";
1025         initializeThingWithChannel(DEFAULT_TEST_CHANNEL_ID, acceptedItemType);
1026
1027         Channel channel = systemInfoThing.getChannel(DEFAULT_TEST_CHANNEL_ID);
1028         if (channel == null) {
1029             throw new AssertionError("Channel '" + DEFAULT_TEST_CHANNEL_ID + "' is null");
1030         }
1031
1032         waitForAssert(() -> {
1033             assertThat("The initial priority of channel " + channel.getUID() + " is not as expected.",
1034                     channel.getConfiguration().get(priorityKey), is(equalTo(initialPriority)));
1035             assertThat(systemInfoHandler.getHighPriorityChannels().contains(channel.getUID()), is(true));
1036         });
1037
1038         // Change the priority of a channel, keep the pid
1039         Configuration updatedConfig = new Configuration();
1040         updatedConfig.put(priorityKey, newPriority);
1041         updatedConfig.put(pidKey, channel.getConfiguration().get(pidKey));
1042         Channel updatedChannel = ChannelBuilder.create(channel.getUID(), channel.getAcceptedItemType())
1043                 .withType(channel.getChannelTypeUID()).withKind(channel.getKind()).withConfiguration(updatedConfig)
1044                 .build();
1045
1046         Thing updatedThing = ThingBuilder.create(systemInfoThing.getThingTypeUID(), systemInfoThing.getUID())
1047                 .withConfiguration(systemInfoThing.getConfiguration()).withChannel(updatedChannel).build();
1048
1049         systemInfoHandler.thingUpdated(updatedThing);
1050
1051         waitForAssert(() -> {
1052             assertThat("The prority of the channel was not updated: ", channel.getConfiguration().get(priorityKey),
1053                     is(equalTo(newPriority)));
1054             assertThat(systemInfoHandler.getLowPriorityChannels().contains(channel.getUID()), is(true));
1055         });
1056     }
1057 }