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