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