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