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