2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.systeminfo.test;
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.*;
22 import java.math.BigDecimal;
23 import java.net.UnknownHostException;
24 import java.util.Hashtable;
25 import java.util.List;
27 import javax.measure.quantity.ElectricPotential;
28 import javax.measure.quantity.Temperature;
29 import javax.measure.quantity.Time;
31 import org.eclipse.jdt.annotation.NonNullByDefault;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.jupiter.MockitoExtension;
39 import org.mockito.junit.jupiter.MockitoSettings;
40 import org.mockito.quality.Strictness;
41 import org.openhab.binding.systeminfo.internal.SysteminfoBindingConstants;
42 import org.openhab.binding.systeminfo.internal.SysteminfoHandlerFactory;
43 import org.openhab.binding.systeminfo.internal.discovery.SysteminfoDiscoveryService;
44 import org.openhab.binding.systeminfo.internal.handler.SysteminfoHandler;
45 import org.openhab.binding.systeminfo.internal.model.DeviceNotFoundException;
46 import org.openhab.binding.systeminfo.internal.model.OSHISysteminfo;
47 import org.openhab.binding.systeminfo.internal.model.SysteminfoInterface;
48 import org.openhab.core.config.core.Configuration;
49 import org.openhab.core.config.discovery.DiscoveryResult;
50 import org.openhab.core.config.discovery.DiscoveryService;
51 import org.openhab.core.config.discovery.inbox.Inbox;
52 import org.openhab.core.config.discovery.inbox.InboxPredicates;
53 import org.openhab.core.i18n.UnitProvider;
54 import org.openhab.core.items.GenericItem;
55 import org.openhab.core.items.ItemNotFoundException;
56 import org.openhab.core.items.ItemRegistry;
57 import org.openhab.core.library.dimension.DataAmount;
58 import org.openhab.core.library.items.NumberItem;
59 import org.openhab.core.library.items.StringItem;
60 import org.openhab.core.library.types.DecimalType;
61 import org.openhab.core.library.types.PercentType;
62 import org.openhab.core.library.types.QuantityType;
63 import org.openhab.core.library.types.StringType;
64 import org.openhab.core.library.unit.SIUnits;
65 import org.openhab.core.library.unit.Units;
66 import org.openhab.core.test.java.JavaOSGiTest;
67 import org.openhab.core.test.storage.VolatileStorageService;
68 import org.openhab.core.thing.Channel;
69 import org.openhab.core.thing.ChannelUID;
70 import org.openhab.core.thing.ManagedThingProvider;
71 import org.openhab.core.thing.Thing;
72 import org.openhab.core.thing.ThingProvider;
73 import org.openhab.core.thing.ThingRegistry;
74 import org.openhab.core.thing.ThingStatus;
75 import org.openhab.core.thing.ThingStatusDetail;
76 import org.openhab.core.thing.ThingTypeUID;
77 import org.openhab.core.thing.ThingUID;
78 import org.openhab.core.thing.binding.ThingHandler;
79 import org.openhab.core.thing.binding.ThingHandlerFactory;
80 import org.openhab.core.thing.binding.builder.ChannelBuilder;
81 import org.openhab.core.thing.binding.builder.ThingBuilder;
82 import org.openhab.core.thing.link.ItemChannelLink;
83 import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
84 import org.openhab.core.thing.type.ChannelKind;
85 import org.openhab.core.thing.type.ChannelTypeUID;
86 import org.openhab.core.types.State;
87 import org.openhab.core.types.UnDefType;
90 * OSGi tests for the {@link SysteminfoHandler}
92 * @author Svilen Valkanov - Initial contribution
93 * @author Lyubomir Papazov - Created a mock systeminfo object. This way, access to the user's OS will not be required,
94 * but mock data will be used instead, avoiding potential errors from the OS queries.
95 * @author Wouter Born - Migrate Groovy to Java tests
98 @ExtendWith(MockitoExtension.class)
99 @MockitoSettings(strictness = Strictness.LENIENT)
100 public class SysteminfoOSGiTest extends JavaOSGiTest {
101 private static final String DEFAULT_TEST_THING_NAME = "work";
102 private static final String DEFAULT_TEST_ITEM_NAME = "test";
103 private static final String DEFAULT_CHANNEL_TEST_PRIORITY = "High";
104 private static final int DEFAULT_CHANNEL_PID = -1;
105 private static final String DEFAULT_TEST_CHANNEL_ID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
106 private static final int DEFAULT_DEVICE_INDEX = 0;
109 * Refresh time in seconds for tasks with priority High.
110 * Default value for the parameter interval_high in the thing configuration
112 private static final int DEFAULT_TEST_INTERVAL_HIGH = 1;
115 * Refresh time in seconds for tasks with priority Medium.
117 private static final int DEFAULT_TEST_INTERVAL_MEDIUM = 3;
119 private @Nullable Thing systeminfoThing;
120 private @Nullable GenericItem testItem;
122 private @Mock @NonNullByDefault({}) OSHISysteminfo mockedSystemInfo;
123 private @NonNullByDefault({}) SysteminfoHandlerFactory systeminfoHandlerFactory;
124 private @NonNullByDefault({}) ThingRegistry thingRegistry;
125 private @NonNullByDefault({}) ItemRegistry itemRegistry;
126 private @NonNullByDefault({}) ManagedThingProvider managedThingProvider;
127 private @NonNullByDefault({}) ManagedItemChannelLinkProvider itemChannelLinkProvider;
128 private @NonNullByDefault({}) UnitProvider unitProvider;
131 public void setUp() {
132 VolatileStorageService volatileStorageService = new VolatileStorageService();
133 registerService(volatileStorageService);
135 // Preparing the mock with OS properties, that are used in the initialize method of SysteminfoHandler
136 // Make this lenient because the assertInvalidThingConfigurationValuesAreHandled test does not require them
137 lenient().when(mockedSystemInfo.getCpuLogicalCores()).thenReturn(new DecimalType(2));
138 lenient().when(mockedSystemInfo.getCpuPhysicalCores()).thenReturn(new DecimalType(2));
139 lenient().when(mockedSystemInfo.getOsFamily()).thenReturn(new StringType("Mock OS"));
140 lenient().when(mockedSystemInfo.getOsManufacturer()).thenReturn(new StringType("Mock OS Manufacturer"));
141 lenient().when(mockedSystemInfo.getOsVersion()).thenReturn(new StringType("Mock Os Version"));
142 // Following mock method returns will make sure the thing does not get recreated with extra channels
143 lenient().when(mockedSystemInfo.getNetworkIFCount()).thenReturn(1);
144 lenient().when(mockedSystemInfo.getDisplayCount()).thenReturn(1);
145 lenient().when(mockedSystemInfo.getFileOSStoreCount()).thenReturn(1);
146 lenient().when(mockedSystemInfo.getPowerSourceCount()).thenReturn(1);
147 lenient().when(mockedSystemInfo.getDriveCount()).thenReturn(1);
148 lenient().when(mockedSystemInfo.getFanCount()).thenReturn(1);
150 registerService(mockedSystemInfo);
152 waitForAssert(() -> {
153 systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
154 assertThat(systeminfoHandlerFactory, is(notNullValue()));
157 if (systeminfoHandlerFactory != null) {
158 // Unbind oshiSystemInfo service and bind the mock service to make the systeminfo binding tests independent
159 // of the external OSHI library
160 SysteminfoInterface oshiSystemInfo = getService(SysteminfoInterface.class);
161 if (oshiSystemInfo != null) {
162 systeminfoHandlerFactory.unbindSystemInfo(oshiSystemInfo);
164 systeminfoHandlerFactory.bindSystemInfo(mockedSystemInfo);
167 waitForAssert(() -> {
168 systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
169 assertThat(systeminfoHandlerFactory, is(notNullValue()));
172 waitForAssert(() -> {
173 thingRegistry = getService(ThingRegistry.class);
174 assertThat(thingRegistry, is(notNullValue()));
177 waitForAssert(() -> {
178 itemRegistry = getService(ItemRegistry.class);
179 assertThat(itemRegistry, is(notNullValue()));
182 waitForAssert(() -> {
183 managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
184 assertThat(managedThingProvider, is(notNullValue()));
187 waitForAssert(() -> {
188 itemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
189 assertThat(itemChannelLinkProvider, is(notNullValue()));
192 waitForAssert(() -> {
193 unitProvider = getService(UnitProvider.class);
194 assertThat(unitProvider, is(notNullValue()));
199 public void tearDown() {
200 Thing thing = systeminfoThing;
202 // Remove the systeminfo thing. The handler will be also disposed automatically
203 Thing removedThing = thingRegistry.forceRemove(thing.getUID());
204 assertThat("The systeminfo thing cannot be deleted", removedThing, is(notNullValue()));
205 waitForAssert(() -> {
206 ThingHandler systemInfoHandler = thing.getHandler();
207 assertThat(systemInfoHandler, is(nullValue()));
211 if (testItem != null) {
212 itemRegistry.remove(DEFAULT_TEST_ITEM_NAME);
215 unregisterService(mockedSystemInfo);
218 private void initializeThingWithChannelAndPID(String channelID, String acceptedItemType, int pid) {
219 Configuration thingConfig = new Configuration();
220 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
221 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
222 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
223 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
224 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
226 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
229 private void initializeThingWithChannelAndPriority(String channelID, String acceptedItemType, String priority) {
230 Configuration thingConfig = new Configuration();
231 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
232 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
233 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
234 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
235 int pid = DEFAULT_CHANNEL_PID;
237 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
240 private void initializeThingWithConfiguration(Configuration config) {
241 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
242 String channelID = DEFAULT_TEST_CHANNEL_ID;
243 String acceptedItemType = "String";
244 int pid = DEFAULT_CHANNEL_PID;
246 initializeThing(config, channelID, acceptedItemType, priority, pid);
249 private void initializeThingWithChannel(String channelID, String acceptedItemType) {
250 Configuration thingConfig = new Configuration();
251 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
252 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
253 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
254 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
256 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
257 int pid = DEFAULT_CHANNEL_PID;
258 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
261 private void initializeThing(Configuration thingConfiguration, String channelID, String acceptedItemType,
262 String priority, int pid) {
263 ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
264 ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
266 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
267 String channelTypeId = channelUID.getIdWithoutGroup();
268 if ("load1".equals(channelTypeId) || "load5".equals(channelTypeId) || "load15".equals(channelTypeId)) {
269 channelTypeId = "loadAverage";
271 ChannelTypeUID channelTypeUID = new ChannelTypeUID(SysteminfoBindingConstants.BINDING_ID, channelTypeId);
272 Configuration channelConfig = new Configuration();
273 channelConfig.put("priority", priority);
274 channelConfig.put("pid", new BigDecimal(pid));
275 Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
276 .withKind(ChannelKind.STATE).withConfiguration(channelConfig).build();
278 Thing thing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(thingConfiguration)
279 .withChannel(channel).build();
280 systeminfoThing = thing;
282 managedThingProvider.add(thing);
284 waitForAssert(() -> {
285 SysteminfoHandler handler = (SysteminfoHandler) thing.getHandler();
286 assertThat(handler, is(notNullValue()));
289 waitForAssert(() -> {
290 assertThat("Thing is not initialized, before an Item is created", thing.getStatus(),
291 anyOf(equalTo(ThingStatus.OFFLINE), equalTo(ThingStatus.ONLINE)));
294 intializeItem(channelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
297 private void assertItemState(String acceptedItemType, String itemName, String priority, State expectedState) {
298 Thing thing = systeminfoThing;
300 throw new AssertionError("Thing is null");
302 waitForAssert(() -> {
303 ThingStatusDetail thingStatusDetail = thing.getStatusInfo().getStatusDetail();
304 String description = thing.getStatusInfo().getDescription();
305 assertThat("Thing status detail is " + thingStatusDetail + " with description " + description,
306 thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
308 // The binding starts all refresh tasks in SysteminfoHandler.scheduleUpdates() after this delay !
310 sleep(SysteminfoHandler.WAIT_TIME_CHANNEL_ITEM_LINK_INIT * 1000);
311 } catch (InterruptedException e) {
312 throw new AssertionError("Interrupted while sleeping");
317 item = (GenericItem) itemRegistry.getItem(itemName);
318 } catch (ItemNotFoundException e) {
319 throw new AssertionError("Item not found in registry");
323 if ("High".equals(priority)) {
324 waitTime = DEFAULT_TEST_INTERVAL_HIGH * 1000;
325 } else if ("Medium".equals(priority)) {
326 waitTime = DEFAULT_TEST_INTERVAL_MEDIUM * 1000;
331 waitForAssert(() -> {
332 State itemState = item.getState();
333 assertThat(itemState, is(equalTo(expectedState)));
334 }, waitTime, DFL_SLEEP_TIME);
337 private void intializeItem(ChannelUID channelUID, String itemName, String acceptedItemType) {
338 GenericItem item = null;
339 if (acceptedItemType.startsWith("Number")) {
340 item = new NumberItem(acceptedItemType, itemName, unitProvider);
341 } else if ("String".equals(acceptedItemType)) {
342 item = new StringItem(itemName);
345 throw new AssertionError("Item is null");
347 itemRegistry.add(item);
350 itemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
354 public void assertInvalidThingConfigurationValuesAreHandled() {
355 Configuration configuration = new Configuration();
357 // invalid value - must be positive
358 int refreshIntervalHigh = -5;
359 configuration.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME, new BigDecimal(refreshIntervalHigh));
361 int refreshIntervalMedium = 3;
362 configuration.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
363 new BigDecimal(refreshIntervalMedium));
364 initializeThingWithConfiguration(configuration);
366 testInvalidConfiguration();
369 private void testInvalidConfiguration() {
370 waitForAssert(() -> {
371 Thing thing = systeminfoThing;
373 assertThat("Invalid configuration is used !", thing.getStatus(), is(equalTo(ThingStatus.OFFLINE)));
374 assertThat(thing.getStatusInfo().getStatusDetail(),
375 is(equalTo(ThingStatusDetail.HANDLER_INITIALIZING_ERROR)));
376 assertThat(thing.getStatusInfo().getDescription(), is(equalTo("@text/offline.cannot-initialize")));
382 public void assertMediumPriorityChannelIsUpdated() {
383 String channnelID = DEFAULT_TEST_CHANNEL_ID;
384 String acceptedItemType = "Number";
385 String priority = "Medium";
387 initializeThingWithChannelAndPriority(channnelID, acceptedItemType, priority);
388 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, priority, UnDefType.UNDEF);
392 public void assertStateOfSecondDeviceIsUpdated() {
393 // This test assumes that at least 2 network interfaces are present on the test platform
395 String channnelID = "network" + deviceIndex + "#mac";
396 String acceptedItemType = "String";
398 initializeThingWithChannel(channnelID, acceptedItemType);
399 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, UnDefType.UNDEF);
403 public void assertChannelCpuLoadIsUpdated() {
404 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
405 String acceptedItemType = "Number";
407 PercentType mockedCpuLoadValue = new PercentType(9);
408 when(mockedSystemInfo.getSystemCpuLoad()).thenReturn(mockedCpuLoadValue);
410 initializeThingWithChannel(channnelID, acceptedItemType);
411 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoadValue);
415 public void assertChannelCpuLoad1IsUpdated() {
416 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_1;
417 String acceptedItemType = "Number";
419 DecimalType mockedCpuLoad1Value = new DecimalType(1.1);
420 when(mockedSystemInfo.getCpuLoad1()).thenReturn(mockedCpuLoad1Value);
422 initializeThingWithChannel(channnelID, acceptedItemType);
423 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad1Value);
427 public void assertChannelCpuLoad5IsUpdated() {
428 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_5;
429 String acceptedItemType = "Number";
431 DecimalType mockedCpuLoad5Value = new DecimalType(5.5);
432 when(mockedSystemInfo.getCpuLoad5()).thenReturn(mockedCpuLoad5Value);
434 initializeThingWithChannel(channnelID, acceptedItemType);
435 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad5Value);
439 public void assertChannelCpuLoad15IsUpdated() {
440 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_15;
441 String acceptedItemType = "Number";
443 DecimalType mockedCpuLoad15Value = new DecimalType(15.15);
444 when(mockedSystemInfo.getCpuLoad15()).thenReturn(mockedCpuLoad15Value);
446 initializeThingWithChannel(channnelID, acceptedItemType);
447 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad15Value);
451 public void assertChannelCpuThreadsIsUpdated() {
452 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_THREADS;
453 String acceptedItemType = "Number";
455 DecimalType mockedCpuThreadsValue = new DecimalType(16);
456 when(mockedSystemInfo.getCpuThreads()).thenReturn(mockedCpuThreadsValue);
458 initializeThingWithChannel(channnelID, acceptedItemType);
459 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuThreadsValue);
463 public void assertChannelCpuUptimeIsUpdated() {
464 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_UPTIME;
465 String acceptedItemType = "Number:Time";
467 QuantityType<Time> mockedCpuUptimeValue = new QuantityType<>(100, Units.MINUTE);
468 when(mockedSystemInfo.getCpuUptime()).thenReturn(mockedCpuUptimeValue);
470 initializeThingWithChannel(channnelID, acceptedItemType);
471 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuUptimeValue);
475 public void assertChannelCpuDescriptionIsUpdated() {
476 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_DESCRIPTION;
477 String acceptedItemType = "String";
479 StringType mockedCpuDescriptionValue = new StringType("Mocked Cpu Descr");
480 when(mockedSystemInfo.getCpuDescription()).thenReturn(mockedCpuDescriptionValue);
482 initializeThingWithChannel(channnelID, acceptedItemType);
483 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
484 mockedCpuDescriptionValue);
488 public void assertChannelCpuNameIsUpdated() {
489 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_NAME;
490 String acceptedItemType = "String";
492 StringType mockedCpuNameValue = new StringType("Mocked Cpu Name");
493 when(mockedSystemInfo.getCpuName()).thenReturn(mockedCpuNameValue);
495 initializeThingWithChannel(channnelID, acceptedItemType);
496 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuNameValue);
500 public void assertChannelMemoryAvailableIsUpdated() {
501 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE;
502 String acceptedItemType = "Number:DataAmount";
504 QuantityType<DataAmount> mockedMemoryAvailableValue = new QuantityType<>(1000, Units.MEBIBYTE);
505 when(mockedSystemInfo.getMemoryAvailable()).thenReturn(mockedMemoryAvailableValue);
507 initializeThingWithChannel(channnelID, acceptedItemType);
508 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
509 mockedMemoryAvailableValue);
513 public void assertChannelMemoryUsedIsUpdated() {
514 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_USED;
515 String acceptedItemType = "Number:DataAmount";
517 QuantityType<DataAmount> mockedMemoryUsedValue = new QuantityType<>(24, Units.MEBIBYTE);
518 when(mockedSystemInfo.getMemoryUsed()).thenReturn(mockedMemoryUsedValue);
520 initializeThingWithChannel(channnelID, acceptedItemType);
521 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedMemoryUsedValue);
525 public void assertChannelMemoryTotalIsUpdated() {
526 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_TOTAL;
527 String acceptedItemType = "Number:DataAmount";
529 QuantityType<DataAmount> mockedMemoryTotalValue = new QuantityType<>(1024, Units.MEBIBYTE);
530 when(mockedSystemInfo.getMemoryTotal()).thenReturn(mockedMemoryTotalValue);
532 initializeThingWithChannel(channnelID, acceptedItemType);
533 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
534 mockedMemoryTotalValue);
538 public void assertChannelMemoryAvailablePercentIsUpdated() {
539 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE_PERCENT;
540 String acceptedItemType = "Number";
542 PercentType mockedMemoryAvailablePercentValue = new PercentType(97);
543 when(mockedSystemInfo.getMemoryAvailablePercent()).thenReturn(mockedMemoryAvailablePercentValue);
545 initializeThingWithChannel(channnelID, acceptedItemType);
546 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
547 mockedMemoryAvailablePercentValue);
551 public void assertChannelSwapAvailableIsUpdated() {
552 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE;
553 String acceptedItemType = "Number:DataAmount";
555 QuantityType<DataAmount> mockedSwapAvailableValue = new QuantityType<>(482, Units.MEBIBYTE);
556 when(mockedSystemInfo.getSwapAvailable()).thenReturn(mockedSwapAvailableValue);
558 initializeThingWithChannel(channnelID, acceptedItemType);
559 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
560 mockedSwapAvailableValue);
564 public void assertChannelSwapUsedIsUpdated() {
565 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_USED;
566 String acceptedItemType = "Number:DataAmount";
568 QuantityType<DataAmount> mockedSwapUsedValue = new QuantityType<>(30, Units.MEBIBYTE);
569 when(mockedSystemInfo.getSwapUsed()).thenReturn(mockedSwapUsedValue);
571 initializeThingWithChannel(channnelID, acceptedItemType);
572 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapUsedValue);
576 public void assertChannelSwapTotalIsUpdated() {
577 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_TOTAL;
578 String acceptedItemType = "Number:DataAmount";
580 QuantityType<DataAmount> mockedSwapTotalValue = new QuantityType<>(512, Units.MEBIBYTE);
581 when(mockedSystemInfo.getSwapTotal()).thenReturn(mockedSwapTotalValue);
583 initializeThingWithChannel(channnelID, acceptedItemType);
584 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapTotalValue);
588 public void assertChannelSwapAvailablePercentIsUpdated() {
589 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE_PERCENT;
590 String acceptedItemType = "Number";
592 PercentType mockedSwapAvailablePercentValue = new PercentType(94);
593 when(mockedSystemInfo.getSwapAvailablePercent()).thenReturn(mockedSwapAvailablePercentValue);
595 initializeThingWithChannel(channnelID, acceptedItemType);
596 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
597 mockedSwapAvailablePercentValue);
601 public void assertChannelStorageNameIsUpdated() throws DeviceNotFoundException {
602 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_NAME;
603 String acceptedItemType = "String";
605 StringType mockedStorageName = new StringType("Mocked Storage Name");
606 when(mockedSystemInfo.getStorageName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageName);
608 initializeThingWithChannel(channnelID, acceptedItemType);
609 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageName);
613 public void assertChannelStorageTypeIsUpdated() throws DeviceNotFoundException {
614 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TYPE;
615 String acceptedItemType = "String";
617 StringType mockedStorageType = new StringType("Mocked Storage Type");
618 when(mockedSystemInfo.getStorageType(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageType);
620 initializeThingWithChannel(channnelID, acceptedItemType);
621 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageType);
625 public void assertChannelStorageDescriptionIsUpdated() throws DeviceNotFoundException {
626 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_DESCRIPTION;
627 String acceptedItemType = "String";
629 StringType mockedStorageDescription = new StringType("Mocked Storage Description");
630 when(mockedSystemInfo.getStorageDescription(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageDescription);
632 initializeThingWithChannel(channnelID, acceptedItemType);
633 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
634 mockedStorageDescription);
638 public void assertChannelStorageAvailableIsUpdated() throws DeviceNotFoundException {
639 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE;
640 String acceptedItemType = "Number:DataAmount";
642 QuantityType<DataAmount> mockedStorageAvailableValue = new QuantityType<>(2000, Units.MEBIBYTE);
643 when(mockedSystemInfo.getStorageAvailable(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageAvailableValue);
645 initializeThingWithChannel(channnelID, acceptedItemType);
646 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
647 mockedStorageAvailableValue);
651 public void assertChannelStorageUsedIsUpdated() throws DeviceNotFoundException {
652 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_USED;
653 String acceptedItemType = "Number:DataAmount";
655 QuantityType<DataAmount> mockedStorageUsedValue = new QuantityType<>(500, Units.MEBIBYTE);
656 when(mockedSystemInfo.getStorageUsed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageUsedValue);
658 initializeThingWithChannel(channnelID, acceptedItemType);
659 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
660 mockedStorageUsedValue);
664 public void assertChannelStorageTotalIsUpdated() throws DeviceNotFoundException {
665 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TOTAL;
666 String acceptedItemType = "Number:DataAmount";
668 QuantityType<DataAmount> mockedStorageTotalValue = new QuantityType<>(2500, Units.MEBIBYTE);
669 when(mockedSystemInfo.getStorageTotal(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageTotalValue);
671 initializeThingWithChannel(channnelID, acceptedItemType);
672 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
673 mockedStorageTotalValue);
677 public void assertChannelStorageAvailablePercentIsUpdated() throws DeviceNotFoundException {
678 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE_PERCENT;
679 String acceptedItemType = "Number";
681 PercentType mockedStorageAvailablePercent = new PercentType(20);
682 when(mockedSystemInfo.getStorageAvailablePercent(DEFAULT_DEVICE_INDEX))
683 .thenReturn(mockedStorageAvailablePercent);
685 initializeThingWithChannel(channnelID, acceptedItemType);
686 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
687 mockedStorageAvailablePercent);
691 public void assertChannelDriveNameIsUpdated() throws DeviceNotFoundException {
692 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_NAME;
693 String acceptedItemType = "String";
695 StringType mockedDriveNameValue = new StringType("Mocked Drive Name");
696 when(mockedSystemInfo.getDriveName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveNameValue);
698 initializeThingWithChannel(channelID, acceptedItemType);
699 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveNameValue);
703 public void assertChannelDriveModelIsUpdated() throws DeviceNotFoundException {
704 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_MODEL;
705 String acceptedItemType = "String";
707 StringType mockedDriveModelValue = new StringType("Mocked Drive Model");
708 when(mockedSystemInfo.getDriveModel(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveModelValue);
710 initializeThingWithChannel(channelID, acceptedItemType);
711 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveModelValue);
715 public void assertChannelDriveSerialIsUpdated() throws DeviceNotFoundException {
716 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_SERIAL;
717 String acceptedItemType = "String";
719 StringType mockedDriveSerialNumber = new StringType("Mocked Drive Serial Number");
720 when(mockedSystemInfo.getDriveSerialNumber(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveSerialNumber);
722 initializeThingWithChannel(channelID, acceptedItemType);
723 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
724 mockedDriveSerialNumber);
727 // Re-enable this previously disabled test, as it is not relying on hardware anymore, but a mocked object
728 // There is a bug opened for this issue - https://github.com/dblock/oshi/issues/185
730 public void assertChannelSensorsCpuTempIsUpdated() {
731 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_CPU_TEMPERATURE;
732 String acceptedItemType = "Number:Temperature";
734 QuantityType<Temperature> mockedSensorsCpuTemperatureValue = new QuantityType<>(60, SIUnits.CELSIUS);
735 when(mockedSystemInfo.getSensorsCpuTemperature()).thenReturn(mockedSensorsCpuTemperatureValue);
737 initializeThingWithChannel(channnelID, acceptedItemType);
738 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
739 mockedSensorsCpuTemperatureValue);
743 public void assertChannelSensorsCpuVoltageIsUpdated() {
744 String channnelID = SysteminfoBindingConstants.CHANNEL_SENOSRS_CPU_VOLTAGE;
745 String acceptedItemType = "Number:ElectricPotential";
747 QuantityType<ElectricPotential> mockedSensorsCpuVoltageValue = new QuantityType<>(1000, Units.VOLT);
748 when(mockedSystemInfo.getSensorsCpuVoltage()).thenReturn(mockedSensorsCpuVoltageValue);
750 initializeThingWithChannel(channnelID, acceptedItemType);
751 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
752 mockedSensorsCpuVoltageValue);
756 public void assertChannelSensorsFanSpeedIsUpdated() throws DeviceNotFoundException {
757 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_FAN_SPEED;
758 String acceptedItemType = "Number";
760 DecimalType mockedSensorsCpuFanSpeedValue = new DecimalType(180);
761 when(mockedSystemInfo.getSensorsFanSpeed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedSensorsCpuFanSpeedValue);
763 initializeThingWithChannel(channnelID, acceptedItemType);
764 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
765 mockedSensorsCpuFanSpeedValue);
769 public void assertChannelBatteryNameIsUpdated() throws DeviceNotFoundException {
770 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_NAME;
771 String acceptedItemType = "String";
773 StringType mockedBatteryName = new StringType("Mocked Battery Name");
774 when(mockedSystemInfo.getBatteryName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryName);
776 initializeThingWithChannel(channnelID, acceptedItemType);
777 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedBatteryName);
781 public void assertChannelBatteryRemainingCapacityIsUpdated() throws DeviceNotFoundException {
782 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_CAPACITY;
783 String acceptedItemType = "Number";
785 PercentType mockedBatteryRemainingCapacity = new PercentType(20);
786 when(mockedSystemInfo.getBatteryRemainingCapacity(DEFAULT_DEVICE_INDEX))
787 .thenReturn(mockedBatteryRemainingCapacity);
789 initializeThingWithChannel(channnelID, acceptedItemType);
790 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
791 mockedBatteryRemainingCapacity);
795 public void assertChannelBatteryRemainingTimeIsUpdated() throws DeviceNotFoundException {
796 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_TIME;
797 String acceptedItemType = "Number:Time";
799 QuantityType<Time> mockedBatteryRemainingTime = new QuantityType<>(3600, Units.MINUTE);
800 when(mockedSystemInfo.getBatteryRemainingTime(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryRemainingTime);
802 initializeThingWithChannel(channnelID, acceptedItemType);
803 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
804 mockedBatteryRemainingTime);
808 public void assertChannelDisplayInformationIsUpdated() throws DeviceNotFoundException {
809 String channnelID = SysteminfoBindingConstants.CHANNEL_DISPLAY_INFORMATION;
810 String acceptedItemType = "String";
812 StringType mockedDisplayInfo = new StringType("Mocked Display Information");
813 when(mockedSystemInfo.getDisplayInformation(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDisplayInfo);
815 initializeThingWithChannel(channnelID, acceptedItemType);
816 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDisplayInfo);
820 public void assertChannelNetworkIpIsUpdated() throws DeviceNotFoundException {
821 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_IP;
822 String acceptedItemType = "String";
824 StringType mockedNetworkIp = new StringType("192.168.1.0");
825 when(mockedSystemInfo.getNetworkIp(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkIp);
827 initializeThingWithChannel(channnelID, acceptedItemType);
828 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkIp);
832 public void assertChannelNetworkMacIsUpdated() throws DeviceNotFoundException {
833 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_MAC;
834 String acceptedItemType = "String";
836 StringType mockedNetworkMacValue = new StringType("AB-10-11-12-13-14");
837 when(mockedSystemInfo.getNetworkMac(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkMacValue);
839 initializeThingWithChannel(channnelID, acceptedItemType);
840 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkMacValue);
844 public void assertChannelNetworkDataSentIsUpdated() throws DeviceNotFoundException {
845 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_SENT;
846 String acceptedItemType = "Number:DataAmount";
848 QuantityType<DataAmount> mockedNetworkDataSent = new QuantityType<>(1000, Units.MEBIBYTE);
849 when(mockedSystemInfo.getNetworkDataSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataSent);
851 initializeThingWithChannel(channnelID, acceptedItemType);
852 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkDataSent);
856 public void assertChannelNetworkDataReceivedIsUpdated() throws DeviceNotFoundException {
857 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_RECEIVED;
858 String acceptedItemType = "Number:DataAmount";
860 QuantityType<DataAmount> mockedNetworkDataReceiveed = new QuantityType<>(800, Units.MEBIBYTE);
861 when(mockedSystemInfo.getNetworkDataReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataReceiveed);
863 initializeThingWithChannel(channnelID, acceptedItemType);
864 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
865 mockedNetworkDataReceiveed);
869 public void assertChannelNetworkPacketsSentIsUpdated() throws DeviceNotFoundException {
870 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_SENT;
871 String acceptedItemType = "Number";
873 DecimalType mockedNetworkPacketsSent = new DecimalType(50);
874 when(mockedSystemInfo.getNetworkPacketsSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsSent);
876 initializeThingWithChannel(channnelID, acceptedItemType);
877 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
878 mockedNetworkPacketsSent);
882 public void assertChannelNetworkPacketsReceivedIsUpdated() throws DeviceNotFoundException {
883 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_RECEIVED;
884 String acceptedItemType = "Number";
886 DecimalType mockedNetworkPacketsReceived = new DecimalType(48);
887 when(mockedSystemInfo.getNetworkPacketsReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsReceived);
889 initializeThingWithChannel(channnelID, acceptedItemType);
890 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
891 mockedNetworkPacketsReceived);
895 public void assertChannelNetworkNetworkNameIsUpdated() throws DeviceNotFoundException {
896 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_NAME;
897 String acceptedItemType = "String";
899 StringType mockedNetworkName = new StringType("MockN-AQ34");
900 when(mockedSystemInfo.getNetworkName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkName);
902 initializeThingWithChannel(channnelID, acceptedItemType);
903 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkName);
907 public void assertChannelNetworkNetworkDisplayNameIsUpdated() throws DeviceNotFoundException {
908 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_ADAPTER_NAME;
909 String acceptedItemType = "String";
911 StringType mockedNetworkAdapterName = new StringType("Mocked Network Adapter Name");
912 when(mockedSystemInfo.getNetworkDisplayName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkAdapterName);
914 initializeThingWithChannel(channnelID, acceptedItemType);
915 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
916 mockedNetworkAdapterName);
919 class SysteminfoDiscoveryServiceMock extends SysteminfoDiscoveryService {
922 SysteminfoDiscoveryServiceMock(String hostname) {
924 this.hostname = hostname;
928 protected String getHostName() throws UnknownHostException {
929 if ("unresolved".equals(hostname)) {
930 throw new UnknownHostException();
936 public void startScan() {
942 public void testDiscoveryWithInvalidHostname() {
943 String hostname = "Hilo.fritz.box";
944 String expectedHostname = "Hilo_fritz_box";
946 testDiscoveryService(expectedHostname, hostname);
950 public void testDiscoveryWithValidHostname() {
951 String hostname = "MyComputer";
952 String expectedHostname = "MyComputer";
954 testDiscoveryService(expectedHostname, hostname);
958 public void testDiscoveryWithUnresolvedHostname() {
959 String hostname = "unresolved";
960 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
962 testDiscoveryService(expectedHostname, hostname);
966 public void testDiscoveryWithEmptyHostnameString() {
967 String hostname = "";
968 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
970 testDiscoveryService(expectedHostname, hostname);
973 private void testDiscoveryService(String expectedHostname, String hostname) {
974 SysteminfoDiscoveryService discoveryService = getService(DiscoveryService.class,
975 SysteminfoDiscoveryService.class);
976 waitForAssert(() -> {
977 assertThat(discoveryService, is(notNullValue()));
979 SysteminfoDiscoveryServiceMock discoveryServiceMock = new SysteminfoDiscoveryServiceMock(hostname);
980 if (discoveryService != null) {
981 unregisterService(DiscoveryService.class);
983 registerService(discoveryServiceMock, DiscoveryService.class.getName(), new Hashtable<>());
985 ThingTypeUID computerType = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
986 ThingUID computerUID = new ThingUID(computerType, expectedHostname);
988 discoveryServiceMock.startScan();
990 Inbox inbox = getService(Inbox.class);
991 waitForAssert(() -> {
992 assertThat(inbox, is(notNullValue()));
999 waitForAssert(() -> {
1000 List<DiscoveryResult> results = inbox.stream().filter(InboxPredicates.forThingUID(computerUID))
1002 assertFalse(results.isEmpty(), "No Thing with UID " + computerUID.getAsString() + " in inbox");
1005 inbox.approve(computerUID, SysteminfoDiscoveryService.DEFAULT_THING_LABEL, null);
1007 waitForAssert(() -> {
1008 systeminfoThing = thingRegistry.get(computerUID);
1009 assertThat(systeminfoThing, is(notNullValue()));
1012 Thing thing = systeminfoThing;
1013 if (thing == null) {
1017 waitForAssert(() -> {
1018 assertThat("Thing is not initialized.", thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
1023 public void assertChannelProcessThreadsIsUpdatedWithPIDse() throws DeviceNotFoundException {
1024 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_THREADS;
1025 String acceptedItemType = "Number";
1026 // The pid of the System idle process in Windows
1029 DecimalType mockedProcessThreadsCount = new DecimalType(4);
1030 when(mockedSystemInfo.getProcessThreads(pid)).thenReturn(mockedProcessThreadsCount);
1032 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1033 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
1034 mockedProcessThreadsCount);
1038 public void assertChannelProcessPathIsUpdatedWithPIDset() throws DeviceNotFoundException {
1039 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_PATH;
1040 String acceptedItemType = "String";
1041 // The pid of the System idle process in Windows
1044 StringType mockedProcessPath = new StringType("C:\\Users\\MockedUser\\Process");
1045 when(mockedSystemInfo.getProcessPath(pid)).thenReturn(mockedProcessPath);
1047 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1048 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessPath);
1052 public void assertChannelProcessNameIsUpdatedWithPIDset() throws DeviceNotFoundException {
1053 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_NAME;
1054 String acceptedItemType = "String";
1055 // The pid of the System idle process in Windows
1058 StringType mockedProcessName = new StringType("MockedProcess.exe");
1059 when(mockedSystemInfo.getProcessName(pid)).thenReturn(mockedProcessName);
1061 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1062 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessName);
1066 public void assertChannelProcessMemoryIsUpdatedWithPIDset() throws DeviceNotFoundException {
1067 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_MEMORY;
1068 String acceptedItemType = "Number:DataAmount";
1069 // The pid of the System idle process in Windows
1072 QuantityType<DataAmount> mockedProcessMemory = new QuantityType<>(450, Units.MEBIBYTE);
1073 when(mockedSystemInfo.getProcessMemoryUsage(pid)).thenReturn(mockedProcessMemory);
1075 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1076 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessMemory);
1080 public void assertChannelProcessLoadIsUpdatedWithPIDset() throws DeviceNotFoundException {
1081 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_LOAD;
1082 String acceptedItemType = "Number";
1083 // The pid of the System idle process in Windows
1086 DecimalType mockedProcessLoad = new DecimalType(3);
1087 when(mockedSystemInfo.getProcessCpuUsage(pid)).thenReturn(mockedProcessLoad);
1089 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1090 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessLoad);
1094 public void testThingHandlesChannelPriorityChange() {
1095 String priorityKey = "priority";
1096 String pidKey = "pid";
1097 String initialPriority = DEFAULT_CHANNEL_TEST_PRIORITY; // Evaluates to High
1098 String newPriority = "Low";
1100 String acceptedItemType = "Number";
1101 initializeThingWithChannel(DEFAULT_TEST_CHANNEL_ID, acceptedItemType);
1103 Thing thing = systeminfoThing;
1104 if (thing == null) {
1105 throw new AssertionError("Thing is null");
1107 Channel channel = thing.getChannel(DEFAULT_TEST_CHANNEL_ID);
1108 if (channel == null) {
1109 throw new AssertionError("Channel '" + DEFAULT_TEST_CHANNEL_ID + "' is null");
1112 ThingHandler thingHandler = thing.getHandler();
1113 if (thingHandler == null) {
1114 throw new AssertionError("Thing handler is null");
1116 if (!(thingHandler.getClass().equals(SysteminfoHandler.class))) {
1117 throw new AssertionError("Thing handler not of class SysteminfoHandler");
1119 SysteminfoHandler handler = (SysteminfoHandler) thingHandler;
1120 waitForAssert(() -> {
1121 assertThat("The initial priority of channel " + channel.getUID() + " is not as expected.",
1122 channel.getConfiguration().get(priorityKey), is(equalTo(initialPriority)));
1123 assertThat(handler.getHighPriorityChannels().contains(channel.getUID()), is(true));
1126 // Change the priority of a channel, keep the pid
1127 Configuration updatedConfig = new Configuration();
1128 updatedConfig.put(priorityKey, newPriority);
1129 updatedConfig.put(pidKey, channel.getConfiguration().get(pidKey));
1130 Channel updatedChannel = ChannelBuilder.create(channel.getUID(), channel.getAcceptedItemType())
1131 .withType(channel.getChannelTypeUID()).withKind(channel.getKind()).withConfiguration(updatedConfig)
1134 Thing updatedThing = ThingBuilder.create(thing.getThingTypeUID(), thing.getUID())
1135 .withConfiguration(thing.getConfiguration()).withChannel(updatedChannel).build();
1137 handler.thingUpdated(updatedThing);
1139 waitForAssert(() -> {
1140 assertThat("The prority of the channel was not updated: ", channel.getConfiguration().get(priorityKey),
1141 is(equalTo(newPriority)));
1142 assertThat(handler.getLowPriorityChannels().contains(channel.getUID()), is(true));