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