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