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.openhab.binding.systeminfo.internal.SysteminfoBindingConstants;
40 import org.openhab.binding.systeminfo.internal.SysteminfoHandlerFactory;
41 import org.openhab.binding.systeminfo.internal.SysteminfoThingTypeProvider;
42 import org.openhab.binding.systeminfo.internal.discovery.SysteminfoDiscoveryService;
43 import org.openhab.binding.systeminfo.internal.handler.SysteminfoHandler;
44 import org.openhab.binding.systeminfo.internal.model.DeviceNotFoundException;
45 import org.openhab.binding.systeminfo.internal.model.OSHISysteminfo;
46 import org.openhab.binding.systeminfo.internal.model.SysteminfoInterface;
47 import org.openhab.core.config.core.Configuration;
48 import org.openhab.core.config.discovery.DiscoveryResult;
49 import org.openhab.core.config.discovery.DiscoveryService;
50 import org.openhab.core.config.discovery.inbox.Inbox;
51 import org.openhab.core.config.discovery.inbox.InboxPredicates;
52 import org.openhab.core.items.GenericItem;
53 import org.openhab.core.items.ItemNotFoundException;
54 import org.openhab.core.items.ItemRegistry;
55 import org.openhab.core.library.dimension.DataAmount;
56 import org.openhab.core.library.items.NumberItem;
57 import org.openhab.core.library.items.StringItem;
58 import org.openhab.core.library.types.DecimalType;
59 import org.openhab.core.library.types.PercentType;
60 import org.openhab.core.library.types.QuantityType;
61 import org.openhab.core.library.types.StringType;
62 import org.openhab.core.library.unit.SIUnits;
63 import org.openhab.core.library.unit.Units;
64 import org.openhab.core.test.java.JavaOSGiTest;
65 import org.openhab.core.test.storage.VolatileStorageService;
66 import org.openhab.core.thing.Channel;
67 import org.openhab.core.thing.ChannelUID;
68 import org.openhab.core.thing.ManagedThingProvider;
69 import org.openhab.core.thing.Thing;
70 import org.openhab.core.thing.ThingProvider;
71 import org.openhab.core.thing.ThingRegistry;
72 import org.openhab.core.thing.ThingStatus;
73 import org.openhab.core.thing.ThingStatusDetail;
74 import org.openhab.core.thing.ThingTypeUID;
75 import org.openhab.core.thing.ThingUID;
76 import org.openhab.core.thing.binding.ThingHandler;
77 import org.openhab.core.thing.binding.ThingHandlerFactory;
78 import org.openhab.core.thing.binding.ThingTypeProvider;
79 import org.openhab.core.thing.binding.builder.ChannelBuilder;
80 import org.openhab.core.thing.binding.builder.ThingBuilder;
81 import org.openhab.core.thing.link.ItemChannelLink;
82 import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
83 import org.openhab.core.thing.type.ChannelKind;
84 import org.openhab.core.thing.type.ChannelTypeUID;
85 import org.openhab.core.types.State;
86 import org.openhab.core.types.UnDefType;
89 * OSGi tests for the {@link SysteminfoHandler}
91 * @author Svilen Valkanov - Initial contribution
92 * @author Lyubomir Papazov - Created a mock systeminfo object. This way, access to the user's OS will not be required,
93 * but mock data will be used instead, avoiding potential errors from the OS queries.
94 * @author Wouter Born - Migrate Groovy to Java tests
97 @ExtendWith(MockitoExtension.class)
98 public class SysteminfoOSGiTest extends JavaOSGiTest {
99 private static final String DEFAULT_TEST_THING_NAME = "work";
100 private static final String DEFAULT_TEST_ITEM_NAME = "test";
101 private static final String DEFAULT_CHANNEL_TEST_PRIORITY = "High";
102 private static final int DEFAULT_CHANNEL_PID = -1;
103 private static final String DEFAULT_TEST_CHANNEL_ID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
104 private static final int DEFAULT_DEVICE_INDEX = 0;
107 * Refresh time in seconds for tasks with priority High.
108 * Default value for the parameter interval_high in the thing configuration
110 private static final int DEFAULT_TEST_INTERVAL_HIGH = 1;
113 * Refresh time in seconds for tasks with priority Medium.
115 private static final int DEFAULT_TEST_INTERVAL_MEDIUM = 3;
117 private @Nullable Thing systemInfoThing;
118 private @Nullable GenericItem testItem;
120 private @Mock @NonNullByDefault({}) OSHISysteminfo mockedSystemInfo;
121 private @NonNullByDefault({}) SysteminfoHandlerFactory systeminfoHandlerFactory;
122 private @NonNullByDefault({}) ThingRegistry thingRegistry;
123 private @NonNullByDefault({}) ItemRegistry itemRegistry;
126 public void setUp() {
127 VolatileStorageService volatileStorageService = new VolatileStorageService();
128 registerService(volatileStorageService);
130 // Preparing the mock with OS properties, that are used in the initialize method of SysteminfoHandler
131 // Make this lenient because the assertInvalidThingConfigurationValuesAreHandled test does not require them
132 lenient().when(mockedSystemInfo.getCpuLogicalCores()).thenReturn(new DecimalType(2));
133 lenient().when(mockedSystemInfo.getCpuPhysicalCores()).thenReturn(new DecimalType(2));
134 lenient().when(mockedSystemInfo.getOsFamily()).thenReturn(new StringType("Mock OS"));
135 lenient().when(mockedSystemInfo.getOsManufacturer()).thenReturn(new StringType("Mock OS Manufacturer"));
136 lenient().when(mockedSystemInfo.getOsVersion()).thenReturn(new StringType("Mock Os Version"));
137 // Following mock method returns will make sure the thing does not get recreated with extra channels
138 lenient().when(mockedSystemInfo.getNetworkIFCount()).thenReturn(1);
139 lenient().when(mockedSystemInfo.getDisplayCount()).thenReturn(1);
140 lenient().when(mockedSystemInfo.getFileOSStoreCount()).thenReturn(1);
141 lenient().when(mockedSystemInfo.getPowerSourceCount()).thenReturn(1);
142 lenient().when(mockedSystemInfo.getDriveCount()).thenReturn(1);
143 lenient().when(mockedSystemInfo.getFanCount()).thenReturn(1);
145 registerService(mockedSystemInfo);
147 waitForAssert(() -> {
148 systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
149 assertThat(systeminfoHandlerFactory, is(notNullValue()));
151 if (systeminfoHandlerFactory != null) {
152 // Unbind oshiSystemInfo service and bind the mock service to make the systeminfo binding tests independent
153 // of the external OSHI library
154 SysteminfoInterface oshiSystemInfo = getService(SysteminfoInterface.class);
155 if (oshiSystemInfo != null) {
156 systeminfoHandlerFactory.unbindSystemInfo(oshiSystemInfo);
158 systeminfoHandlerFactory.bindSystemInfo(mockedSystemInfo);
161 waitForAssert(() -> {
162 ThingTypeProvider thingTypeProvider = getService(ThingTypeProvider.class,
163 SysteminfoThingTypeProvider.class);
164 assertThat(thingTypeProvider, is(notNullValue()));
166 waitForAssert(() -> {
167 SysteminfoThingTypeProvider systeminfoThingTypeProvider = getService(SysteminfoThingTypeProvider.class);
168 assertThat(systeminfoThingTypeProvider, is(notNullValue()));
171 waitForAssert(() -> {
172 thingRegistry = getService(ThingRegistry.class);
173 assertThat(thingRegistry, is(notNullValue()));
176 waitForAssert(() -> {
177 itemRegistry = getService(ItemRegistry.class);
178 assertThat(itemRegistry, is(notNullValue()));
183 public void tearDown() {
184 Thing thing = systemInfoThing;
186 // Remove the systeminfo thing. The handler will be also disposed automatically
187 Thing removedThing = thingRegistry.forceRemove(thing.getUID());
188 assertThat("The systeminfo thing cannot be deleted", removedThing, is(notNullValue()));
189 waitForAssert(() -> {
190 ThingHandler systemInfoHandler = thing.getHandler();
191 assertThat(systemInfoHandler, is(nullValue()));
195 if (testItem != null) {
196 itemRegistry.remove(DEFAULT_TEST_ITEM_NAME);
199 unregisterService(mockedSystemInfo);
202 private void initializeThingWithChannelAndPID(String channelID, String acceptedItemType, int pid) {
203 Configuration thingConfig = new Configuration();
204 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
205 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
206 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
207 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
208 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
210 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
213 private void initializeThingWithChannelAndPriority(String channelID, String acceptedItemType, String priority) {
214 Configuration thingConfig = new Configuration();
215 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
216 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
217 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
218 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
219 int pid = DEFAULT_CHANNEL_PID;
221 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
224 private void initializeThingWithConfiguration(Configuration config) {
225 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
226 String channelID = DEFAULT_TEST_CHANNEL_ID;
227 String acceptedItemType = "String";
228 int pid = DEFAULT_CHANNEL_PID;
230 initializeThing(config, channelID, acceptedItemType, priority, pid);
233 private void initializeThingWithChannel(String channelID, String acceptedItemType) {
234 Configuration thingConfig = new Configuration();
235 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
236 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
237 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
238 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
240 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
241 int pid = DEFAULT_CHANNEL_PID;
242 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
245 private void initializeThing(Configuration thingConfiguration, String channelID, String acceptedItemType,
246 String priority, int pid) {
247 ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
248 ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
250 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
251 ChannelTypeUID channelTypeUID = new ChannelTypeUID(SysteminfoBindingConstants.BINDING_ID,
252 channelUID.getIdWithoutGroup());
253 Configuration channelConfig = new Configuration();
254 channelConfig.put("priority", priority);
255 channelConfig.put("pid", new BigDecimal(pid));
256 Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
257 .withKind(ChannelKind.STATE).withConfiguration(channelConfig).build();
259 Thing thing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(thingConfiguration)
260 .withChannel(channel).build();
261 systemInfoThing = thing;
263 ManagedThingProvider managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
264 assertThat(managedThingProvider, is(notNullValue()));
266 if (managedThingProvider != null) {
267 managedThingProvider.add(thing);
270 waitForAssert(() -> {
271 SysteminfoHandler handler = (SysteminfoHandler) thing.getHandler();
272 assertThat(handler, is(notNullValue()));
275 waitForAssert(() -> {
276 assertThat("Thing is not initialized, before an Item is created", thing.getStatus(),
277 anyOf(equalTo(ThingStatus.OFFLINE), equalTo(ThingStatus.ONLINE)));
280 intializeItem(channelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
283 private void assertItemState(String acceptedItemType, String itemName, String priority, State expectedState) {
284 Thing thing = systemInfoThing;
286 throw new AssertionError("Thing is null");
288 waitForAssert(() -> {
289 ThingStatusDetail thingStatusDetail = thing.getStatusInfo().getStatusDetail();
290 String description = thing.getStatusInfo().getDescription();
291 assertThat("Thing status detail is " + thingStatusDetail + " with description " + description,
292 thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
294 // The binding starts all refresh tasks in SysteminfoHandler.scheduleUpdates() after this delay !
296 sleep(SysteminfoHandler.WAIT_TIME_CHANNEL_ITEM_LINK_INIT * 1000);
297 } catch (InterruptedException e) {
298 throw new AssertionError("Interrupted while sleeping");
303 item = (GenericItem) itemRegistry.getItem(itemName);
304 } catch (ItemNotFoundException e) {
305 throw new AssertionError("Item not found in registry");
309 if ("High".equals(priority)) {
310 waitTime = DEFAULT_TEST_INTERVAL_HIGH * 1000;
311 } else if ("Medium".equals(priority)) {
312 waitTime = DEFAULT_TEST_INTERVAL_MEDIUM * 1000;
317 waitForAssert(() -> {
318 State itemState = item.getState();
319 assertThat(itemState, is(equalTo(expectedState)));
320 }, waitTime, DFL_SLEEP_TIME);
323 private void intializeItem(ChannelUID channelUID, String itemName, String acceptedItemType) {
324 GenericItem item = null;
325 if (acceptedItemType.startsWith("Number")) {
326 item = new NumberItem(acceptedItemType, itemName);
327 } else if ("String".equals(acceptedItemType)) {
328 item = new StringItem(itemName);
331 throw new AssertionError("Item is null");
333 itemRegistry.add(item);
336 ManagedItemChannelLinkProvider itemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
337 assertThat(itemChannelLinkProvider, is(notNullValue()));
339 if (itemChannelLinkProvider == null) {
343 itemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
347 public void assertInvalidThingConfigurationValuesAreHandled() {
348 Configuration configuration = new Configuration();
350 // invalid value - must be positive
351 int refreshIntervalHigh = -5;
352 configuration.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME, new BigDecimal(refreshIntervalHigh));
354 int refreshIntervalMedium = 3;
355 configuration.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
356 new BigDecimal(refreshIntervalMedium));
357 initializeThingWithConfiguration(configuration);
359 testInvalidConfiguration();
362 private void testInvalidConfiguration() {
363 waitForAssert(() -> {
364 Thing thing = systemInfoThing;
366 assertThat("Invalid configuration is used !", thing.getStatus(), is(equalTo(ThingStatus.OFFLINE)));
367 assertThat(thing.getStatusInfo().getStatusDetail(),
368 is(equalTo(ThingStatusDetail.HANDLER_INITIALIZING_ERROR)));
369 assertThat(thing.getStatusInfo().getDescription(), is(equalTo("@text/offline.cannot-initialize")));
375 public void assertMediumPriorityChannelIsUpdated() {
376 String channnelID = DEFAULT_TEST_CHANNEL_ID;
377 String acceptedItemType = "Number";
378 String priority = "Medium";
380 initializeThingWithChannelAndPriority(channnelID, acceptedItemType, priority);
381 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, priority, UnDefType.UNDEF);
385 public void assertStateOfSecondDeviceIsUpdated() {
386 // This test assumes that at least 2 network interfaces are present on the test platform
388 String channnelID = "network" + deviceIndex + "#mac";
389 String acceptedItemType = "String";
391 initializeThingWithChannel(channnelID, acceptedItemType);
392 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, UnDefType.UNDEF);
396 public void assertChannelCpuLoadIsUpdated() {
397 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
398 String acceptedItemType = "Number";
400 PercentType mockedCpuLoadValue = new PercentType(9);
401 when(mockedSystemInfo.getSystemCpuLoad()).thenReturn(mockedCpuLoadValue);
403 initializeThingWithChannel(channnelID, acceptedItemType);
404 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoadValue);
408 public void assertChannelCpuLoad1IsUpdated() {
409 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_1;
410 String acceptedItemType = "Number";
412 DecimalType mockedCpuLoad1Value = new DecimalType(1.1);
413 when(mockedSystemInfo.getCpuLoad1()).thenReturn(mockedCpuLoad1Value);
415 initializeThingWithChannel(channnelID, acceptedItemType);
416 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad1Value);
420 public void assertChannelCpuLoad5IsUpdated() {
421 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_5;
422 String acceptedItemType = "Number";
424 DecimalType mockedCpuLoad5Value = new DecimalType(5.5);
425 when(mockedSystemInfo.getCpuLoad5()).thenReturn(mockedCpuLoad5Value);
427 initializeThingWithChannel(channnelID, acceptedItemType);
428 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad5Value);
432 public void assertChannelCpuLoad15IsUpdated() {
433 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_15;
434 String acceptedItemType = "Number";
436 DecimalType mockedCpuLoad15Value = new DecimalType(15.15);
437 when(mockedSystemInfo.getCpuLoad15()).thenReturn(mockedCpuLoad15Value);
439 initializeThingWithChannel(channnelID, acceptedItemType);
440 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad15Value);
444 public void assertChannelCpuThreadsIsUpdated() {
445 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_THREADS;
446 String acceptedItemType = "Number";
448 DecimalType mockedCpuThreadsValue = new DecimalType(16);
449 when(mockedSystemInfo.getCpuThreads()).thenReturn(mockedCpuThreadsValue);
451 initializeThingWithChannel(channnelID, acceptedItemType);
452 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuThreadsValue);
456 public void assertChannelCpuUptimeIsUpdated() {
457 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_UPTIME;
458 String acceptedItemType = "Number:Time";
460 QuantityType<Time> mockedCpuUptimeValue = new QuantityType<>(100, Units.MINUTE);
461 when(mockedSystemInfo.getCpuUptime()).thenReturn(mockedCpuUptimeValue);
463 initializeThingWithChannel(channnelID, acceptedItemType);
464 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuUptimeValue);
468 public void assertChannelCpuDescriptionIsUpdated() {
469 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_DESCRIPTION;
470 String acceptedItemType = "String";
472 StringType mockedCpuDescriptionValue = new StringType("Mocked Cpu Descr");
473 when(mockedSystemInfo.getCpuDescription()).thenReturn(mockedCpuDescriptionValue);
475 initializeThingWithChannel(channnelID, acceptedItemType);
476 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
477 mockedCpuDescriptionValue);
481 public void assertChannelCpuNameIsUpdated() {
482 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_NAME;
483 String acceptedItemType = "String";
485 StringType mockedCpuNameValue = new StringType("Mocked Cpu Name");
486 when(mockedSystemInfo.getCpuName()).thenReturn(mockedCpuNameValue);
488 initializeThingWithChannel(channnelID, acceptedItemType);
489 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuNameValue);
493 public void assertChannelMemoryAvailableIsUpdated() {
494 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE;
495 String acceptedItemType = "Number:DataAmount";
497 QuantityType<DataAmount> mockedMemoryAvailableValue = new QuantityType<>(1000, Units.MEBIBYTE);
498 when(mockedSystemInfo.getMemoryAvailable()).thenReturn(mockedMemoryAvailableValue);
500 initializeThingWithChannel(channnelID, acceptedItemType);
501 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
502 mockedMemoryAvailableValue);
506 public void assertChannelMemoryUsedIsUpdated() {
507 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_USED;
508 String acceptedItemType = "Number:DataAmount";
510 QuantityType<DataAmount> mockedMemoryUsedValue = new QuantityType<>(24, Units.MEBIBYTE);
511 when(mockedSystemInfo.getMemoryUsed()).thenReturn(mockedMemoryUsedValue);
513 initializeThingWithChannel(channnelID, acceptedItemType);
514 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedMemoryUsedValue);
518 public void assertChannelMemoryTotalIsUpdated() {
519 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_TOTAL;
520 String acceptedItemType = "Number:DataAmount";
522 QuantityType<DataAmount> mockedMemoryTotalValue = new QuantityType<>(1024, Units.MEBIBYTE);
523 when(mockedSystemInfo.getMemoryTotal()).thenReturn(mockedMemoryTotalValue);
525 initializeThingWithChannel(channnelID, acceptedItemType);
526 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
527 mockedMemoryTotalValue);
531 public void assertChannelMemoryAvailablePercentIsUpdated() {
532 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE_PERCENT;
533 String acceptedItemType = "Number";
535 PercentType mockedMemoryAvailablePercentValue = new PercentType(97);
536 when(mockedSystemInfo.getMemoryAvailablePercent()).thenReturn(mockedMemoryAvailablePercentValue);
538 initializeThingWithChannel(channnelID, acceptedItemType);
539 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
540 mockedMemoryAvailablePercentValue);
544 public void assertChannelSwapAvailableIsUpdated() {
545 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE;
546 String acceptedItemType = "Number:DataAmount";
548 QuantityType<DataAmount> mockedSwapAvailableValue = new QuantityType<>(482, Units.MEBIBYTE);
549 when(mockedSystemInfo.getSwapAvailable()).thenReturn(mockedSwapAvailableValue);
551 initializeThingWithChannel(channnelID, acceptedItemType);
552 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
553 mockedSwapAvailableValue);
557 public void assertChannelSwapUsedIsUpdated() {
558 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_USED;
559 String acceptedItemType = "Number:DataAmount";
561 QuantityType<DataAmount> mockedSwapUsedValue = new QuantityType<>(30, Units.MEBIBYTE);
562 when(mockedSystemInfo.getSwapUsed()).thenReturn(mockedSwapUsedValue);
564 initializeThingWithChannel(channnelID, acceptedItemType);
565 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapUsedValue);
569 public void assertChannelSwapTotalIsUpdated() {
570 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_TOTAL;
571 String acceptedItemType = "Number:DataAmount";
573 QuantityType<DataAmount> mockedSwapTotalValue = new QuantityType<>(512, Units.MEBIBYTE);
574 when(mockedSystemInfo.getSwapTotal()).thenReturn(mockedSwapTotalValue);
576 initializeThingWithChannel(channnelID, acceptedItemType);
577 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapTotalValue);
581 public void assertChannelSwapAvailablePercentIsUpdated() {
582 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE_PERCENT;
583 String acceptedItemType = "Number";
585 PercentType mockedSwapAvailablePercentValue = new PercentType(94);
586 when(mockedSystemInfo.getSwapAvailablePercent()).thenReturn(mockedSwapAvailablePercentValue);
588 initializeThingWithChannel(channnelID, acceptedItemType);
589 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
590 mockedSwapAvailablePercentValue);
594 public void assertChannelStorageNameIsUpdated() throws DeviceNotFoundException {
595 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_NAME;
596 String acceptedItemType = "String";
598 StringType mockedStorageName = new StringType("Mocked Storage Name");
599 when(mockedSystemInfo.getStorageName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageName);
601 initializeThingWithChannel(channnelID, acceptedItemType);
602 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageName);
606 public void assertChannelStorageTypeIsUpdated() throws DeviceNotFoundException {
607 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TYPE;
608 String acceptedItemType = "String";
610 StringType mockedStorageType = new StringType("Mocked Storage Type");
611 when(mockedSystemInfo.getStorageType(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageType);
613 initializeThingWithChannel(channnelID, acceptedItemType);
614 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageType);
618 public void assertChannelStorageDescriptionIsUpdated() throws DeviceNotFoundException {
619 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_DESCRIPTION;
620 String acceptedItemType = "String";
622 StringType mockedStorageDescription = new StringType("Mocked Storage Description");
623 when(mockedSystemInfo.getStorageDescription(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageDescription);
625 initializeThingWithChannel(channnelID, acceptedItemType);
626 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
627 mockedStorageDescription);
631 public void assertChannelStorageAvailableIsUpdated() throws DeviceNotFoundException {
632 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE;
633 String acceptedItemType = "Number:DataAmount";
635 QuantityType<DataAmount> mockedStorageAvailableValue = new QuantityType<>(2000, Units.MEBIBYTE);
636 when(mockedSystemInfo.getStorageAvailable(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageAvailableValue);
638 initializeThingWithChannel(channnelID, acceptedItemType);
639 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
640 mockedStorageAvailableValue);
644 public void assertChannelStorageUsedIsUpdated() throws DeviceNotFoundException {
645 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_USED;
646 String acceptedItemType = "Number:DataAmount";
648 QuantityType<DataAmount> mockedStorageUsedValue = new QuantityType<>(500, Units.MEBIBYTE);
649 when(mockedSystemInfo.getStorageUsed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageUsedValue);
651 initializeThingWithChannel(channnelID, acceptedItemType);
652 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
653 mockedStorageUsedValue);
657 public void assertChannelStorageTotalIsUpdated() throws DeviceNotFoundException {
658 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TOTAL;
659 String acceptedItemType = "Number:DataAmount";
661 QuantityType<DataAmount> mockedStorageTotalValue = new QuantityType<>(2500, Units.MEBIBYTE);
662 when(mockedSystemInfo.getStorageTotal(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageTotalValue);
664 initializeThingWithChannel(channnelID, acceptedItemType);
665 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
666 mockedStorageTotalValue);
670 public void assertChannelStorageAvailablePercentIsUpdated() throws DeviceNotFoundException {
671 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE_PERCENT;
672 String acceptedItemType = "Number";
674 PercentType mockedStorageAvailablePercent = new PercentType(20);
675 when(mockedSystemInfo.getStorageAvailablePercent(DEFAULT_DEVICE_INDEX))
676 .thenReturn(mockedStorageAvailablePercent);
678 initializeThingWithChannel(channnelID, acceptedItemType);
679 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
680 mockedStorageAvailablePercent);
684 public void assertChannelDriveNameIsUpdated() throws DeviceNotFoundException {
685 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_NAME;
686 String acceptedItemType = "String";
688 StringType mockedDriveNameValue = new StringType("Mocked Drive Name");
689 when(mockedSystemInfo.getDriveName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveNameValue);
691 initializeThingWithChannel(channelID, acceptedItemType);
692 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveNameValue);
696 public void assertChannelDriveModelIsUpdated() throws DeviceNotFoundException {
697 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_MODEL;
698 String acceptedItemType = "String";
700 StringType mockedDriveModelValue = new StringType("Mocked Drive Model");
701 when(mockedSystemInfo.getDriveModel(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveModelValue);
703 initializeThingWithChannel(channelID, acceptedItemType);
704 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveModelValue);
708 public void assertChannelDriveSerialIsUpdated() throws DeviceNotFoundException {
709 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_SERIAL;
710 String acceptedItemType = "String";
712 StringType mockedDriveSerialNumber = new StringType("Mocked Drive Serial Number");
713 when(mockedSystemInfo.getDriveSerialNumber(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveSerialNumber);
715 initializeThingWithChannel(channelID, acceptedItemType);
716 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
717 mockedDriveSerialNumber);
720 // Re-enable this previously disabled test, as it is not relying on hardware anymore, but a mocked object
721 // There is a bug opened for this issue - https://github.com/dblock/oshi/issues/185
723 public void assertChannelSensorsCpuTempIsUpdated() {
724 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_CPU_TEMPERATURE;
725 String acceptedItemType = "Number:Temperature";
727 QuantityType<Temperature> mockedSensorsCpuTemperatureValue = new QuantityType<>(60, SIUnits.CELSIUS);
728 when(mockedSystemInfo.getSensorsCpuTemperature()).thenReturn(mockedSensorsCpuTemperatureValue);
730 initializeThingWithChannel(channnelID, acceptedItemType);
731 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
732 mockedSensorsCpuTemperatureValue);
736 public void assertChannelSensorsCpuVoltageIsUpdated() {
737 String channnelID = SysteminfoBindingConstants.CHANNEL_SENOSRS_CPU_VOLTAGE;
738 String acceptedItemType = "Number:ElectricPotential";
740 QuantityType<ElectricPotential> mockedSensorsCpuVoltageValue = new QuantityType<>(1000, Units.VOLT);
741 when(mockedSystemInfo.getSensorsCpuVoltage()).thenReturn(mockedSensorsCpuVoltageValue);
743 initializeThingWithChannel(channnelID, acceptedItemType);
744 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
745 mockedSensorsCpuVoltageValue);
749 public void assertChannelSensorsFanSpeedIsUpdated() throws DeviceNotFoundException {
750 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_FAN_SPEED;
751 String acceptedItemType = "Number";
753 DecimalType mockedSensorsCpuFanSpeedValue = new DecimalType(180);
754 when(mockedSystemInfo.getSensorsFanSpeed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedSensorsCpuFanSpeedValue);
756 initializeThingWithChannel(channnelID, acceptedItemType);
757 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
758 mockedSensorsCpuFanSpeedValue);
762 public void assertChannelBatteryNameIsUpdated() throws DeviceNotFoundException {
763 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_NAME;
764 String acceptedItemType = "String";
766 StringType mockedBatteryName = new StringType("Mocked Battery Name");
767 when(mockedSystemInfo.getBatteryName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryName);
769 initializeThingWithChannel(channnelID, acceptedItemType);
770 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedBatteryName);
774 public void assertChannelBatteryRemainingCapacityIsUpdated() throws DeviceNotFoundException {
775 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_CAPACITY;
776 String acceptedItemType = "Number";
778 PercentType mockedBatteryRemainingCapacity = new PercentType(20);
779 when(mockedSystemInfo.getBatteryRemainingCapacity(DEFAULT_DEVICE_INDEX))
780 .thenReturn(mockedBatteryRemainingCapacity);
782 initializeThingWithChannel(channnelID, acceptedItemType);
783 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
784 mockedBatteryRemainingCapacity);
788 public void assertChannelBatteryRemainingTimeIsUpdated() throws DeviceNotFoundException {
789 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_TIME;
790 String acceptedItemType = "Number:Time";
792 QuantityType<Time> mockedBatteryRemainingTime = new QuantityType<>(3600, Units.MINUTE);
793 when(mockedSystemInfo.getBatteryRemainingTime(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryRemainingTime);
795 initializeThingWithChannel(channnelID, acceptedItemType);
796 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
797 mockedBatteryRemainingTime);
801 public void assertChannelDisplayInformationIsUpdated() throws DeviceNotFoundException {
802 String channnelID = SysteminfoBindingConstants.CHANNEL_DISPLAY_INFORMATION;
803 String acceptedItemType = "String";
805 StringType mockedDisplayInfo = new StringType("Mocked Display Information");
806 when(mockedSystemInfo.getDisplayInformation(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDisplayInfo);
808 initializeThingWithChannel(channnelID, acceptedItemType);
809 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDisplayInfo);
813 public void assertChannelNetworkIpIsUpdated() throws DeviceNotFoundException {
814 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_IP;
815 String acceptedItemType = "String";
817 StringType mockedNetworkIp = new StringType("192.168.1.0");
818 when(mockedSystemInfo.getNetworkIp(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkIp);
820 initializeThingWithChannel(channnelID, acceptedItemType);
821 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkIp);
825 public void assertChannelNetworkMacIsUpdated() throws DeviceNotFoundException {
826 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_MAC;
827 String acceptedItemType = "String";
829 StringType mockedNetworkMacValue = new StringType("AB-10-11-12-13-14");
830 when(mockedSystemInfo.getNetworkMac(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkMacValue);
832 initializeThingWithChannel(channnelID, acceptedItemType);
833 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkMacValue);
837 public void assertChannelNetworkDataSentIsUpdated() throws DeviceNotFoundException {
838 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_SENT;
839 String acceptedItemType = "Number:DataAmount";
841 QuantityType<DataAmount> mockedNetworkDataSent = new QuantityType<>(1000, Units.MEBIBYTE);
842 when(mockedSystemInfo.getNetworkDataSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataSent);
844 initializeThingWithChannel(channnelID, acceptedItemType);
845 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkDataSent);
849 public void assertChannelNetworkDataReceivedIsUpdated() throws DeviceNotFoundException {
850 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_RECEIVED;
851 String acceptedItemType = "Number:DataAmount";
853 QuantityType<DataAmount> mockedNetworkDataReceiveed = new QuantityType<>(800, Units.MEBIBYTE);
854 when(mockedSystemInfo.getNetworkDataReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataReceiveed);
856 initializeThingWithChannel(channnelID, acceptedItemType);
857 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
858 mockedNetworkDataReceiveed);
862 public void assertChannelNetworkPacketsSentIsUpdated() throws DeviceNotFoundException {
863 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_SENT;
864 String acceptedItemType = "Number";
866 DecimalType mockedNetworkPacketsSent = new DecimalType(50);
867 when(mockedSystemInfo.getNetworkPacketsSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsSent);
869 initializeThingWithChannel(channnelID, acceptedItemType);
870 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
871 mockedNetworkPacketsSent);
875 public void assertChannelNetworkPacketsReceivedIsUpdated() throws DeviceNotFoundException {
876 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_RECEIVED;
877 String acceptedItemType = "Number";
879 DecimalType mockedNetworkPacketsReceived = new DecimalType(48);
880 when(mockedSystemInfo.getNetworkPacketsReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsReceived);
882 initializeThingWithChannel(channnelID, acceptedItemType);
883 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
884 mockedNetworkPacketsReceived);
888 public void assertChannelNetworkNetworkNameIsUpdated() throws DeviceNotFoundException {
889 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_NAME;
890 String acceptedItemType = "String";
892 StringType mockedNetworkName = new StringType("MockN-AQ34");
893 when(mockedSystemInfo.getNetworkName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkName);
895 initializeThingWithChannel(channnelID, acceptedItemType);
896 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkName);
900 public void assertChannelNetworkNetworkDisplayNameIsUpdated() throws DeviceNotFoundException {
901 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_ADAPTER_NAME;
902 String acceptedItemType = "String";
904 StringType mockedNetworkAdapterName = new StringType("Mocked Network Adapter Name");
905 when(mockedSystemInfo.getNetworkDisplayName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkAdapterName);
907 initializeThingWithChannel(channnelID, acceptedItemType);
908 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
909 mockedNetworkAdapterName);
912 class SysteminfoDiscoveryServiceMock extends SysteminfoDiscoveryService {
915 SysteminfoDiscoveryServiceMock(String hostname) {
917 this.hostname = hostname;
921 protected String getHostName() throws UnknownHostException {
922 if ("unresolved".equals(hostname)) {
923 throw new UnknownHostException();
929 public void startScan() {
935 public void testDiscoveryWithInvalidHostname() {
936 String hostname = "Hilo.fritz.box";
937 String expectedHostname = "Hilo_fritz_box";
939 testDiscoveryService(expectedHostname, hostname);
943 public void testDiscoveryWithValidHostname() {
944 String hostname = "MyComputer";
945 String expectedHostname = "MyComputer";
947 testDiscoveryService(expectedHostname, hostname);
951 public void testDiscoveryWithUnresolvedHostname() {
952 String hostname = "unresolved";
953 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
955 testDiscoveryService(expectedHostname, hostname);
959 public void testDiscoveryWithEmptyHostnameString() {
960 String hostname = "";
961 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
963 testDiscoveryService(expectedHostname, hostname);
966 private void testDiscoveryService(String expectedHostname, String hostname) {
967 SysteminfoDiscoveryService discoveryService = getService(DiscoveryService.class,
968 SysteminfoDiscoveryService.class);
969 waitForAssert(() -> {
970 assertThat(discoveryService, is(notNullValue()));
972 SysteminfoDiscoveryServiceMock discoveryServiceMock = new SysteminfoDiscoveryServiceMock(hostname);
973 if (discoveryService != null) {
974 unregisterService(DiscoveryService.class);
976 registerService(discoveryServiceMock, DiscoveryService.class.getName(), new Hashtable<>());
978 ThingTypeUID computerType = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
979 ThingUID computerUID = new ThingUID(computerType, expectedHostname);
981 discoveryServiceMock.startScan();
983 Inbox inbox = getService(Inbox.class);
984 assertThat(inbox, is(notNullValue()));
990 waitForAssert(() -> {
991 List<DiscoveryResult> results = inbox.stream().filter(InboxPredicates.forThingUID(computerUID))
993 assertFalse(results.isEmpty(), "No Thing with UID " + computerUID.getAsString() + " in inbox");
996 inbox.approve(computerUID, SysteminfoDiscoveryService.DEFAULT_THING_LABEL, null);
998 waitForAssert(() -> {
999 systemInfoThing = thingRegistry.get(computerUID);
1000 assertThat(systemInfoThing, is(notNullValue()));
1003 Thing thing = systemInfoThing;
1004 if (thing == null) {
1008 waitForAssert(() -> {
1009 assertThat("Thing is not initialized.", thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
1014 public void assertChannelProcessThreadsIsUpdatedWithPIDse() throws DeviceNotFoundException {
1015 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_THREADS;
1016 String acceptedItemType = "Number";
1017 // The pid of the System idle process in Windows
1020 DecimalType mockedProcessThreadsCount = new DecimalType(4);
1021 when(mockedSystemInfo.getProcessThreads(pid)).thenReturn(mockedProcessThreadsCount);
1023 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1024 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
1025 mockedProcessThreadsCount);
1029 public void assertChannelProcessPathIsUpdatedWithPIDset() throws DeviceNotFoundException {
1030 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_PATH;
1031 String acceptedItemType = "String";
1032 // The pid of the System idle process in Windows
1035 StringType mockedProcessPath = new StringType("C:\\Users\\MockedUser\\Process");
1036 when(mockedSystemInfo.getProcessPath(pid)).thenReturn(mockedProcessPath);
1038 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1039 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessPath);
1043 public void assertChannelProcessNameIsUpdatedWithPIDset() throws DeviceNotFoundException {
1044 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_NAME;
1045 String acceptedItemType = "String";
1046 // The pid of the System idle process in Windows
1049 StringType mockedProcessName = new StringType("MockedProcess.exe");
1050 when(mockedSystemInfo.getProcessName(pid)).thenReturn(mockedProcessName);
1052 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1053 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessName);
1057 public void assertChannelProcessMemoryIsUpdatedWithPIDset() throws DeviceNotFoundException {
1058 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_MEMORY;
1059 String acceptedItemType = "Number:DataAmount";
1060 // The pid of the System idle process in Windows
1063 QuantityType<DataAmount> mockedProcessMemory = new QuantityType<>(450, Units.MEBIBYTE);
1064 when(mockedSystemInfo.getProcessMemoryUsage(pid)).thenReturn(mockedProcessMemory);
1066 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1067 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessMemory);
1071 public void assertChannelProcessLoadIsUpdatedWithPIDset() throws DeviceNotFoundException {
1072 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_LOAD;
1073 String acceptedItemType = "Number";
1074 // The pid of the System idle process in Windows
1077 DecimalType mockedProcessLoad = new DecimalType(3);
1078 when(mockedSystemInfo.getProcessCpuUsage(pid)).thenReturn(mockedProcessLoad);
1080 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1081 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessLoad);
1085 public void testThingHandlesChannelPriorityChange() {
1086 String priorityKey = "priority";
1087 String pidKey = "pid";
1088 String initialPriority = DEFAULT_CHANNEL_TEST_PRIORITY; // Evaluates to High
1089 String newPriority = "Low";
1091 String acceptedItemType = "Number";
1092 initializeThingWithChannel(DEFAULT_TEST_CHANNEL_ID, acceptedItemType);
1094 Thing thing = systemInfoThing;
1095 if (thing == null) {
1096 throw new AssertionError("Thing is null");
1098 Channel channel = thing.getChannel(DEFAULT_TEST_CHANNEL_ID);
1099 if (channel == null) {
1100 throw new AssertionError("Channel '" + DEFAULT_TEST_CHANNEL_ID + "' is null");
1103 ThingHandler thingHandler = thing.getHandler();
1104 if (thingHandler == null) {
1105 throw new AssertionError("Thing handler is null");
1107 if (!(thingHandler.getClass().equals(SysteminfoHandler.class))) {
1108 throw new AssertionError("Thing handler not of class SysteminfoHandler");
1110 SysteminfoHandler handler = (SysteminfoHandler) thingHandler;
1111 waitForAssert(() -> {
1112 assertThat("The initial priority of channel " + channel.getUID() + " is not as expected.",
1113 channel.getConfiguration().get(priorityKey), is(equalTo(initialPriority)));
1114 assertThat(handler.getHighPriorityChannels().contains(channel.getUID()), is(true));
1117 // Change the priority of a channel, keep the pid
1118 Configuration updatedConfig = new Configuration();
1119 updatedConfig.put(priorityKey, newPriority);
1120 updatedConfig.put(pidKey, channel.getConfiguration().get(pidKey));
1121 Channel updatedChannel = ChannelBuilder.create(channel.getUID(), channel.getAcceptedItemType())
1122 .withType(channel.getChannelTypeUID()).withKind(channel.getKind()).withConfiguration(updatedConfig)
1125 Thing updatedThing = ThingBuilder.create(thing.getThingTypeUID(), thing.getUID())
1126 .withConfiguration(thing.getConfiguration()).withChannel(updatedChannel).build();
1128 handler.thingUpdated(updatedThing);
1130 waitForAssert(() -> {
1131 assertThat("The prority of the channel was not updated: ", channel.getConfiguration().get(priorityKey),
1132 is(equalTo(newPriority)));
1133 assertThat(handler.getLowPriorityChannels().contains(channel.getUID()), is(true));