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.SysteminfoThingTypeProvider;
44 import org.openhab.binding.systeminfo.internal.discovery.SysteminfoDiscoveryService;
45 import org.openhab.binding.systeminfo.internal.handler.SysteminfoHandler;
46 import org.openhab.binding.systeminfo.internal.model.DeviceNotFoundException;
47 import org.openhab.binding.systeminfo.internal.model.OSHISysteminfo;
48 import org.openhab.binding.systeminfo.internal.model.SysteminfoInterface;
49 import org.openhab.core.config.core.Configuration;
50 import org.openhab.core.config.discovery.DiscoveryResult;
51 import org.openhab.core.config.discovery.DiscoveryService;
52 import org.openhab.core.config.discovery.inbox.Inbox;
53 import org.openhab.core.config.discovery.inbox.InboxPredicates;
54 import org.openhab.core.items.GenericItem;
55 import org.openhab.core.items.ItemNotFoundException;
56 import org.openhab.core.items.ItemRegistry;
57 import org.openhab.core.library.dimension.DataAmount;
58 import org.openhab.core.library.items.NumberItem;
59 import org.openhab.core.library.items.StringItem;
60 import org.openhab.core.library.types.DecimalType;
61 import org.openhab.core.library.types.PercentType;
62 import org.openhab.core.library.types.QuantityType;
63 import org.openhab.core.library.types.StringType;
64 import org.openhab.core.library.unit.SIUnits;
65 import org.openhab.core.library.unit.Units;
66 import org.openhab.core.test.java.JavaOSGiTest;
67 import org.openhab.core.test.storage.VolatileStorageService;
68 import org.openhab.core.thing.Channel;
69 import org.openhab.core.thing.ChannelUID;
70 import org.openhab.core.thing.ManagedThingProvider;
71 import org.openhab.core.thing.Thing;
72 import org.openhab.core.thing.ThingProvider;
73 import org.openhab.core.thing.ThingRegistry;
74 import org.openhab.core.thing.ThingStatus;
75 import org.openhab.core.thing.ThingStatusDetail;
76 import org.openhab.core.thing.ThingTypeUID;
77 import org.openhab.core.thing.ThingUID;
78 import org.openhab.core.thing.binding.ThingHandler;
79 import org.openhab.core.thing.binding.ThingHandlerFactory;
80 import org.openhab.core.thing.binding.ThingTypeProvider;
81 import org.openhab.core.thing.binding.builder.ChannelBuilder;
82 import org.openhab.core.thing.binding.builder.ThingBuilder;
83 import org.openhab.core.thing.link.ItemChannelLink;
84 import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
85 import org.openhab.core.thing.type.ChannelKind;
86 import org.openhab.core.thing.type.ChannelTypeUID;
87 import org.openhab.core.types.State;
88 import org.openhab.core.types.UnDefType;
91 * OSGi tests for the {@link SysteminfoHandler}
93 * @author Svilen Valkanov - Initial contribution
94 * @author Lyubomir Papazov - Created a mock systeminfo object. This way, access to the user's OS will not be required,
95 * but mock data will be used instead, avoiding potential errors from the OS queries.
96 * @author Wouter Born - Migrate Groovy to Java tests
99 @ExtendWith(MockitoExtension.class)
100 @MockitoSettings(strictness = Strictness.LENIENT)
101 public class SysteminfoOSGiTest extends JavaOSGiTest {
102 private static final String DEFAULT_TEST_THING_NAME = "work";
103 private static final String DEFAULT_TEST_ITEM_NAME = "test";
104 private static final String DEFAULT_CHANNEL_TEST_PRIORITY = "High";
105 private static final int DEFAULT_CHANNEL_PID = -1;
106 private static final String DEFAULT_TEST_CHANNEL_ID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
107 private static final int DEFAULT_DEVICE_INDEX = 0;
110 * Refresh time in seconds for tasks with priority High.
111 * Default value for the parameter interval_high in the thing configuration
113 private static final int DEFAULT_TEST_INTERVAL_HIGH = 1;
116 * Refresh time in seconds for tasks with priority Medium.
118 private static final int DEFAULT_TEST_INTERVAL_MEDIUM = 3;
120 private @Nullable Thing systemInfoThing;
121 private @Nullable GenericItem testItem;
123 private @Mock @NonNullByDefault({}) OSHISysteminfo mockedSystemInfo;
124 private @NonNullByDefault({}) SysteminfoHandlerFactory systeminfoHandlerFactory;
125 private @NonNullByDefault({}) ThingRegistry thingRegistry;
126 private @NonNullByDefault({}) ItemRegistry itemRegistry;
127 private @NonNullByDefault({}) SysteminfoThingTypeProvider systemThingTypeProvider;
130 public void setUp() {
131 VolatileStorageService volatileStorageService = new VolatileStorageService();
132 registerService(volatileStorageService);
134 // Preparing the mock with OS properties, that are used in the initialize method of SysteminfoHandler
135 // Make this lenient because the assertInvalidThingConfigurationValuesAreHandled test does not require them
136 lenient().when(mockedSystemInfo.getCpuLogicalCores()).thenReturn(new DecimalType(2));
137 lenient().when(mockedSystemInfo.getCpuPhysicalCores()).thenReturn(new DecimalType(2));
138 lenient().when(mockedSystemInfo.getOsFamily()).thenReturn(new StringType("Mock OS"));
139 lenient().when(mockedSystemInfo.getOsManufacturer()).thenReturn(new StringType("Mock OS Manufacturer"));
140 lenient().when(mockedSystemInfo.getOsVersion()).thenReturn(new StringType("Mock Os Version"));
141 // Following mock method returns will make sure the thing does not get recreated with extra channels
142 lenient().when(mockedSystemInfo.getNetworkIFCount()).thenReturn(1);
143 lenient().when(mockedSystemInfo.getDisplayCount()).thenReturn(1);
144 lenient().when(mockedSystemInfo.getFileOSStoreCount()).thenReturn(1);
145 lenient().when(mockedSystemInfo.getPowerSourceCount()).thenReturn(1);
146 lenient().when(mockedSystemInfo.getDriveCount()).thenReturn(1);
147 lenient().when(mockedSystemInfo.getFanCount()).thenReturn(1);
149 registerService(mockedSystemInfo);
151 waitForAssert(() -> {
152 systemThingTypeProvider = getService(ThingTypeProvider.class, SysteminfoThingTypeProvider.class);
153 assertThat(systemThingTypeProvider, is(notNullValue()));
156 waitForAssert(() -> {
157 systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
158 assertThat(systeminfoHandlerFactory, is(notNullValue()));
161 if (systeminfoHandlerFactory != null) {
162 // Unbind oshiSystemInfo service and bind the mock service to make the systeminfo binding tests independent
163 // of the external OSHI library
164 SysteminfoInterface oshiSystemInfo = getService(SysteminfoInterface.class);
165 if (oshiSystemInfo != null) {
166 systeminfoHandlerFactory.unbindSystemInfo(oshiSystemInfo);
168 systeminfoHandlerFactory.bindSystemInfo(mockedSystemInfo);
171 waitForAssert(() -> {
172 systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
173 assertThat(systeminfoHandlerFactory, is(notNullValue()));
176 waitForAssert(() -> {
177 thingRegistry = getService(ThingRegistry.class);
178 assertThat(thingRegistry, is(notNullValue()));
181 waitForAssert(() -> {
182 itemRegistry = getService(ItemRegistry.class);
183 assertThat(itemRegistry, is(notNullValue()));
188 public void tearDown() {
189 Thing thing = systemInfoThing;
191 // Remove the systeminfo thing. The handler will be also disposed automatically
192 Thing removedThing = thingRegistry.forceRemove(thing.getUID());
193 assertThat("The systeminfo thing cannot be deleted", removedThing, is(notNullValue()));
194 waitForAssert(() -> {
195 ThingHandler systemInfoHandler = thing.getHandler();
196 assertThat(systemInfoHandler, is(nullValue()));
200 if (testItem != null) {
201 itemRegistry.remove(DEFAULT_TEST_ITEM_NAME);
204 unregisterService(mockedSystemInfo);
207 private void initializeThingWithChannelAndPID(String channelID, String acceptedItemType, int pid) {
208 Configuration thingConfig = new Configuration();
209 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
210 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
211 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
212 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
213 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
215 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
218 private void initializeThingWithChannelAndPriority(String channelID, String acceptedItemType, String priority) {
219 Configuration thingConfig = new Configuration();
220 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
221 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
222 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
223 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
224 int pid = DEFAULT_CHANNEL_PID;
226 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
229 private void initializeThingWithConfiguration(Configuration config) {
230 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
231 String channelID = DEFAULT_TEST_CHANNEL_ID;
232 String acceptedItemType = "String";
233 int pid = DEFAULT_CHANNEL_PID;
235 initializeThing(config, channelID, acceptedItemType, priority, pid);
238 private void initializeThingWithChannel(String channelID, String acceptedItemType) {
239 Configuration thingConfig = new Configuration();
240 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
241 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
242 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
243 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
245 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
246 int pid = DEFAULT_CHANNEL_PID;
247 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
250 private void initializeThing(Configuration thingConfiguration, String channelID, String acceptedItemType,
251 String priority, int pid) {
252 ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
253 ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
255 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
256 String channelTypeId = channelUID.getIdWithoutGroup();
257 if ("load1".equals(channelTypeId) || "load5".equals(channelTypeId) || "load15".equals(channelTypeId)) {
258 channelTypeId = "loadAverage";
260 ChannelTypeUID channelTypeUID = new ChannelTypeUID(SysteminfoBindingConstants.BINDING_ID, channelTypeId);
261 Configuration channelConfig = new Configuration();
262 channelConfig.put("priority", priority);
263 channelConfig.put("pid", new BigDecimal(pid));
264 Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
265 .withKind(ChannelKind.STATE).withConfiguration(channelConfig).build();
267 Thing thing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(thingConfiguration)
268 .withChannel(channel).build();
269 systemInfoThing = thing;
271 ManagedThingProvider managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
272 assertThat(managedThingProvider, is(notNullValue()));
274 if (managedThingProvider != null) {
275 managedThingProvider.add(thing);
278 waitForAssert(() -> {
279 SysteminfoHandler handler = (SysteminfoHandler) thing.getHandler();
280 assertThat(handler, is(notNullValue()));
283 waitForAssert(() -> {
284 assertThat("Thing is not initialized, before an Item is created", thing.getStatus(),
285 anyOf(equalTo(ThingStatus.OFFLINE), equalTo(ThingStatus.ONLINE)));
288 intializeItem(channelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
291 private void assertItemState(String acceptedItemType, String itemName, String priority, State expectedState) {
292 Thing thing = systemInfoThing;
294 throw new AssertionError("Thing is null");
296 waitForAssert(() -> {
297 ThingStatusDetail thingStatusDetail = thing.getStatusInfo().getStatusDetail();
298 String description = thing.getStatusInfo().getDescription();
299 assertThat("Thing status detail is " + thingStatusDetail + " with description " + description,
300 thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
302 // The binding starts all refresh tasks in SysteminfoHandler.scheduleUpdates() after this delay !
304 sleep(SysteminfoHandler.WAIT_TIME_CHANNEL_ITEM_LINK_INIT * 1000);
305 } catch (InterruptedException e) {
306 throw new AssertionError("Interrupted while sleeping");
311 item = (GenericItem) itemRegistry.getItem(itemName);
312 } catch (ItemNotFoundException e) {
313 throw new AssertionError("Item not found in registry");
317 if ("High".equals(priority)) {
318 waitTime = DEFAULT_TEST_INTERVAL_HIGH * 1000;
319 } else if ("Medium".equals(priority)) {
320 waitTime = DEFAULT_TEST_INTERVAL_MEDIUM * 1000;
325 waitForAssert(() -> {
326 State itemState = item.getState();
327 assertThat(itemState, is(equalTo(expectedState)));
328 }, waitTime, DFL_SLEEP_TIME);
331 private void intializeItem(ChannelUID channelUID, String itemName, String acceptedItemType) {
332 GenericItem item = null;
333 if (acceptedItemType.startsWith("Number")) {
334 item = new NumberItem(acceptedItemType, itemName);
335 } else if ("String".equals(acceptedItemType)) {
336 item = new StringItem(itemName);
339 throw new AssertionError("Item is null");
341 itemRegistry.add(item);
344 ManagedItemChannelLinkProvider itemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
345 assertThat(itemChannelLinkProvider, is(notNullValue()));
347 if (itemChannelLinkProvider == null) {
351 itemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
355 public void assertInvalidThingConfigurationValuesAreHandled() {
356 Configuration configuration = new Configuration();
358 // invalid value - must be positive
359 int refreshIntervalHigh = -5;
360 configuration.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME, new BigDecimal(refreshIntervalHigh));
362 int refreshIntervalMedium = 3;
363 configuration.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
364 new BigDecimal(refreshIntervalMedium));
365 initializeThingWithConfiguration(configuration);
367 testInvalidConfiguration();
370 private void testInvalidConfiguration() {
371 waitForAssert(() -> {
372 Thing thing = systemInfoThing;
374 assertThat("Invalid configuration is used !", thing.getStatus(), is(equalTo(ThingStatus.OFFLINE)));
375 assertThat(thing.getStatusInfo().getStatusDetail(),
376 is(equalTo(ThingStatusDetail.HANDLER_INITIALIZING_ERROR)));
377 assertThat(thing.getStatusInfo().getDescription(), is(equalTo("@text/offline.cannot-initialize")));
383 public void assertMediumPriorityChannelIsUpdated() {
384 String channnelID = DEFAULT_TEST_CHANNEL_ID;
385 String acceptedItemType = "Number";
386 String priority = "Medium";
388 initializeThingWithChannelAndPriority(channnelID, acceptedItemType, priority);
389 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, priority, UnDefType.UNDEF);
393 public void assertStateOfSecondDeviceIsUpdated() {
394 // This test assumes that at least 2 network interfaces are present on the test platform
396 String channnelID = "network" + deviceIndex + "#mac";
397 String acceptedItemType = "String";
399 initializeThingWithChannel(channnelID, acceptedItemType);
400 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, UnDefType.UNDEF);
404 public void assertChannelCpuLoadIsUpdated() {
405 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
406 String acceptedItemType = "Number";
408 PercentType mockedCpuLoadValue = new PercentType(9);
409 when(mockedSystemInfo.getSystemCpuLoad()).thenReturn(mockedCpuLoadValue);
411 initializeThingWithChannel(channnelID, acceptedItemType);
412 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoadValue);
416 public void assertChannelCpuLoad1IsUpdated() {
417 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_1;
418 String acceptedItemType = "Number";
420 DecimalType mockedCpuLoad1Value = new DecimalType(1.1);
421 when(mockedSystemInfo.getCpuLoad1()).thenReturn(mockedCpuLoad1Value);
423 initializeThingWithChannel(channnelID, acceptedItemType);
424 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad1Value);
428 public void assertChannelCpuLoad5IsUpdated() {
429 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_5;
430 String acceptedItemType = "Number";
432 DecimalType mockedCpuLoad5Value = new DecimalType(5.5);
433 when(mockedSystemInfo.getCpuLoad5()).thenReturn(mockedCpuLoad5Value);
435 initializeThingWithChannel(channnelID, acceptedItemType);
436 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad5Value);
440 public void assertChannelCpuLoad15IsUpdated() {
441 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_15;
442 String acceptedItemType = "Number";
444 DecimalType mockedCpuLoad15Value = new DecimalType(15.15);
445 when(mockedSystemInfo.getCpuLoad15()).thenReturn(mockedCpuLoad15Value);
447 initializeThingWithChannel(channnelID, acceptedItemType);
448 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad15Value);
452 public void assertChannelCpuThreadsIsUpdated() {
453 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_THREADS;
454 String acceptedItemType = "Number";
456 DecimalType mockedCpuThreadsValue = new DecimalType(16);
457 when(mockedSystemInfo.getCpuThreads()).thenReturn(mockedCpuThreadsValue);
459 initializeThingWithChannel(channnelID, acceptedItemType);
460 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuThreadsValue);
464 public void assertChannelCpuUptimeIsUpdated() {
465 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_UPTIME;
466 String acceptedItemType = "Number:Time";
468 QuantityType<Time> mockedCpuUptimeValue = new QuantityType<>(100, Units.MINUTE);
469 when(mockedSystemInfo.getCpuUptime()).thenReturn(mockedCpuUptimeValue);
471 initializeThingWithChannel(channnelID, acceptedItemType);
472 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuUptimeValue);
476 public void assertChannelCpuDescriptionIsUpdated() {
477 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_DESCRIPTION;
478 String acceptedItemType = "String";
480 StringType mockedCpuDescriptionValue = new StringType("Mocked Cpu Descr");
481 when(mockedSystemInfo.getCpuDescription()).thenReturn(mockedCpuDescriptionValue);
483 initializeThingWithChannel(channnelID, acceptedItemType);
484 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
485 mockedCpuDescriptionValue);
489 public void assertChannelCpuNameIsUpdated() {
490 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_NAME;
491 String acceptedItemType = "String";
493 StringType mockedCpuNameValue = new StringType("Mocked Cpu Name");
494 when(mockedSystemInfo.getCpuName()).thenReturn(mockedCpuNameValue);
496 initializeThingWithChannel(channnelID, acceptedItemType);
497 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuNameValue);
501 public void assertChannelMemoryAvailableIsUpdated() {
502 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE;
503 String acceptedItemType = "Number:DataAmount";
505 QuantityType<DataAmount> mockedMemoryAvailableValue = new QuantityType<>(1000, Units.MEBIBYTE);
506 when(mockedSystemInfo.getMemoryAvailable()).thenReturn(mockedMemoryAvailableValue);
508 initializeThingWithChannel(channnelID, acceptedItemType);
509 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
510 mockedMemoryAvailableValue);
514 public void assertChannelMemoryUsedIsUpdated() {
515 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_USED;
516 String acceptedItemType = "Number:DataAmount";
518 QuantityType<DataAmount> mockedMemoryUsedValue = new QuantityType<>(24, Units.MEBIBYTE);
519 when(mockedSystemInfo.getMemoryUsed()).thenReturn(mockedMemoryUsedValue);
521 initializeThingWithChannel(channnelID, acceptedItemType);
522 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedMemoryUsedValue);
526 public void assertChannelMemoryTotalIsUpdated() {
527 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_TOTAL;
528 String acceptedItemType = "Number:DataAmount";
530 QuantityType<DataAmount> mockedMemoryTotalValue = new QuantityType<>(1024, Units.MEBIBYTE);
531 when(mockedSystemInfo.getMemoryTotal()).thenReturn(mockedMemoryTotalValue);
533 initializeThingWithChannel(channnelID, acceptedItemType);
534 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
535 mockedMemoryTotalValue);
539 public void assertChannelMemoryAvailablePercentIsUpdated() {
540 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE_PERCENT;
541 String acceptedItemType = "Number";
543 PercentType mockedMemoryAvailablePercentValue = new PercentType(97);
544 when(mockedSystemInfo.getMemoryAvailablePercent()).thenReturn(mockedMemoryAvailablePercentValue);
546 initializeThingWithChannel(channnelID, acceptedItemType);
547 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
548 mockedMemoryAvailablePercentValue);
552 public void assertChannelSwapAvailableIsUpdated() {
553 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE;
554 String acceptedItemType = "Number:DataAmount";
556 QuantityType<DataAmount> mockedSwapAvailableValue = new QuantityType<>(482, Units.MEBIBYTE);
557 when(mockedSystemInfo.getSwapAvailable()).thenReturn(mockedSwapAvailableValue);
559 initializeThingWithChannel(channnelID, acceptedItemType);
560 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
561 mockedSwapAvailableValue);
565 public void assertChannelSwapUsedIsUpdated() {
566 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_USED;
567 String acceptedItemType = "Number:DataAmount";
569 QuantityType<DataAmount> mockedSwapUsedValue = new QuantityType<>(30, Units.MEBIBYTE);
570 when(mockedSystemInfo.getSwapUsed()).thenReturn(mockedSwapUsedValue);
572 initializeThingWithChannel(channnelID, acceptedItemType);
573 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapUsedValue);
577 public void assertChannelSwapTotalIsUpdated() {
578 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_TOTAL;
579 String acceptedItemType = "Number:DataAmount";
581 QuantityType<DataAmount> mockedSwapTotalValue = new QuantityType<>(512, Units.MEBIBYTE);
582 when(mockedSystemInfo.getSwapTotal()).thenReturn(mockedSwapTotalValue);
584 initializeThingWithChannel(channnelID, acceptedItemType);
585 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapTotalValue);
589 public void assertChannelSwapAvailablePercentIsUpdated() {
590 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE_PERCENT;
591 String acceptedItemType = "Number";
593 PercentType mockedSwapAvailablePercentValue = new PercentType(94);
594 when(mockedSystemInfo.getSwapAvailablePercent()).thenReturn(mockedSwapAvailablePercentValue);
596 initializeThingWithChannel(channnelID, acceptedItemType);
597 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
598 mockedSwapAvailablePercentValue);
602 public void assertChannelStorageNameIsUpdated() throws DeviceNotFoundException {
603 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_NAME;
604 String acceptedItemType = "String";
606 StringType mockedStorageName = new StringType("Mocked Storage Name");
607 when(mockedSystemInfo.getStorageName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageName);
609 initializeThingWithChannel(channnelID, acceptedItemType);
610 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageName);
614 public void assertChannelStorageTypeIsUpdated() throws DeviceNotFoundException {
615 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TYPE;
616 String acceptedItemType = "String";
618 StringType mockedStorageType = new StringType("Mocked Storage Type");
619 when(mockedSystemInfo.getStorageType(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageType);
621 initializeThingWithChannel(channnelID, acceptedItemType);
622 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageType);
626 public void assertChannelStorageDescriptionIsUpdated() throws DeviceNotFoundException {
627 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_DESCRIPTION;
628 String acceptedItemType = "String";
630 StringType mockedStorageDescription = new StringType("Mocked Storage Description");
631 when(mockedSystemInfo.getStorageDescription(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageDescription);
633 initializeThingWithChannel(channnelID, acceptedItemType);
634 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
635 mockedStorageDescription);
639 public void assertChannelStorageAvailableIsUpdated() throws DeviceNotFoundException {
640 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE;
641 String acceptedItemType = "Number:DataAmount";
643 QuantityType<DataAmount> mockedStorageAvailableValue = new QuantityType<>(2000, Units.MEBIBYTE);
644 when(mockedSystemInfo.getStorageAvailable(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageAvailableValue);
646 initializeThingWithChannel(channnelID, acceptedItemType);
647 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
648 mockedStorageAvailableValue);
652 public void assertChannelStorageUsedIsUpdated() throws DeviceNotFoundException {
653 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_USED;
654 String acceptedItemType = "Number:DataAmount";
656 QuantityType<DataAmount> mockedStorageUsedValue = new QuantityType<>(500, Units.MEBIBYTE);
657 when(mockedSystemInfo.getStorageUsed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageUsedValue);
659 initializeThingWithChannel(channnelID, acceptedItemType);
660 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
661 mockedStorageUsedValue);
665 public void assertChannelStorageTotalIsUpdated() throws DeviceNotFoundException {
666 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TOTAL;
667 String acceptedItemType = "Number:DataAmount";
669 QuantityType<DataAmount> mockedStorageTotalValue = new QuantityType<>(2500, Units.MEBIBYTE);
670 when(mockedSystemInfo.getStorageTotal(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageTotalValue);
672 initializeThingWithChannel(channnelID, acceptedItemType);
673 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
674 mockedStorageTotalValue);
678 public void assertChannelStorageAvailablePercentIsUpdated() throws DeviceNotFoundException {
679 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE_PERCENT;
680 String acceptedItemType = "Number";
682 PercentType mockedStorageAvailablePercent = new PercentType(20);
683 when(mockedSystemInfo.getStorageAvailablePercent(DEFAULT_DEVICE_INDEX))
684 .thenReturn(mockedStorageAvailablePercent);
686 initializeThingWithChannel(channnelID, acceptedItemType);
687 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
688 mockedStorageAvailablePercent);
692 public void assertChannelDriveNameIsUpdated() throws DeviceNotFoundException {
693 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_NAME;
694 String acceptedItemType = "String";
696 StringType mockedDriveNameValue = new StringType("Mocked Drive Name");
697 when(mockedSystemInfo.getDriveName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveNameValue);
699 initializeThingWithChannel(channelID, acceptedItemType);
700 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveNameValue);
704 public void assertChannelDriveModelIsUpdated() throws DeviceNotFoundException {
705 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_MODEL;
706 String acceptedItemType = "String";
708 StringType mockedDriveModelValue = new StringType("Mocked Drive Model");
709 when(mockedSystemInfo.getDriveModel(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveModelValue);
711 initializeThingWithChannel(channelID, acceptedItemType);
712 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveModelValue);
716 public void assertChannelDriveSerialIsUpdated() throws DeviceNotFoundException {
717 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_SERIAL;
718 String acceptedItemType = "String";
720 StringType mockedDriveSerialNumber = new StringType("Mocked Drive Serial Number");
721 when(mockedSystemInfo.getDriveSerialNumber(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveSerialNumber);
723 initializeThingWithChannel(channelID, acceptedItemType);
724 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
725 mockedDriveSerialNumber);
728 // Re-enable this previously disabled test, as it is not relying on hardware anymore, but a mocked object
729 // There is a bug opened for this issue - https://github.com/dblock/oshi/issues/185
731 public void assertChannelSensorsCpuTempIsUpdated() {
732 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_CPU_TEMPERATURE;
733 String acceptedItemType = "Number:Temperature";
735 QuantityType<Temperature> mockedSensorsCpuTemperatureValue = new QuantityType<>(60, SIUnits.CELSIUS);
736 when(mockedSystemInfo.getSensorsCpuTemperature()).thenReturn(mockedSensorsCpuTemperatureValue);
738 initializeThingWithChannel(channnelID, acceptedItemType);
739 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
740 mockedSensorsCpuTemperatureValue);
744 public void assertChannelSensorsCpuVoltageIsUpdated() {
745 String channnelID = SysteminfoBindingConstants.CHANNEL_SENOSRS_CPU_VOLTAGE;
746 String acceptedItemType = "Number:ElectricPotential";
748 QuantityType<ElectricPotential> mockedSensorsCpuVoltageValue = new QuantityType<>(1000, Units.VOLT);
749 when(mockedSystemInfo.getSensorsCpuVoltage()).thenReturn(mockedSensorsCpuVoltageValue);
751 initializeThingWithChannel(channnelID, acceptedItemType);
752 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
753 mockedSensorsCpuVoltageValue);
757 public void assertChannelSensorsFanSpeedIsUpdated() throws DeviceNotFoundException {
758 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_FAN_SPEED;
759 String acceptedItemType = "Number";
761 DecimalType mockedSensorsCpuFanSpeedValue = new DecimalType(180);
762 when(mockedSystemInfo.getSensorsFanSpeed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedSensorsCpuFanSpeedValue);
764 initializeThingWithChannel(channnelID, acceptedItemType);
765 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
766 mockedSensorsCpuFanSpeedValue);
770 public void assertChannelBatteryNameIsUpdated() throws DeviceNotFoundException {
771 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_NAME;
772 String acceptedItemType = "String";
774 StringType mockedBatteryName = new StringType("Mocked Battery Name");
775 when(mockedSystemInfo.getBatteryName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryName);
777 initializeThingWithChannel(channnelID, acceptedItemType);
778 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedBatteryName);
782 public void assertChannelBatteryRemainingCapacityIsUpdated() throws DeviceNotFoundException {
783 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_CAPACITY;
784 String acceptedItemType = "Number";
786 PercentType mockedBatteryRemainingCapacity = new PercentType(20);
787 when(mockedSystemInfo.getBatteryRemainingCapacity(DEFAULT_DEVICE_INDEX))
788 .thenReturn(mockedBatteryRemainingCapacity);
790 initializeThingWithChannel(channnelID, acceptedItemType);
791 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
792 mockedBatteryRemainingCapacity);
796 public void assertChannelBatteryRemainingTimeIsUpdated() throws DeviceNotFoundException {
797 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_TIME;
798 String acceptedItemType = "Number:Time";
800 QuantityType<Time> mockedBatteryRemainingTime = new QuantityType<>(3600, Units.MINUTE);
801 when(mockedSystemInfo.getBatteryRemainingTime(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryRemainingTime);
803 initializeThingWithChannel(channnelID, acceptedItemType);
804 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
805 mockedBatteryRemainingTime);
809 public void assertChannelDisplayInformationIsUpdated() throws DeviceNotFoundException {
810 String channnelID = SysteminfoBindingConstants.CHANNEL_DISPLAY_INFORMATION;
811 String acceptedItemType = "String";
813 StringType mockedDisplayInfo = new StringType("Mocked Display Information");
814 when(mockedSystemInfo.getDisplayInformation(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDisplayInfo);
816 initializeThingWithChannel(channnelID, acceptedItemType);
817 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDisplayInfo);
821 public void assertChannelNetworkIpIsUpdated() throws DeviceNotFoundException {
822 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_IP;
823 String acceptedItemType = "String";
825 StringType mockedNetworkIp = new StringType("192.168.1.0");
826 when(mockedSystemInfo.getNetworkIp(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkIp);
828 initializeThingWithChannel(channnelID, acceptedItemType);
829 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkIp);
833 public void assertChannelNetworkMacIsUpdated() throws DeviceNotFoundException {
834 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_MAC;
835 String acceptedItemType = "String";
837 StringType mockedNetworkMacValue = new StringType("AB-10-11-12-13-14");
838 when(mockedSystemInfo.getNetworkMac(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkMacValue);
840 initializeThingWithChannel(channnelID, acceptedItemType);
841 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkMacValue);
845 public void assertChannelNetworkDataSentIsUpdated() throws DeviceNotFoundException {
846 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_SENT;
847 String acceptedItemType = "Number:DataAmount";
849 QuantityType<DataAmount> mockedNetworkDataSent = new QuantityType<>(1000, Units.MEBIBYTE);
850 when(mockedSystemInfo.getNetworkDataSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataSent);
852 initializeThingWithChannel(channnelID, acceptedItemType);
853 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkDataSent);
857 public void assertChannelNetworkDataReceivedIsUpdated() throws DeviceNotFoundException {
858 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_RECEIVED;
859 String acceptedItemType = "Number:DataAmount";
861 QuantityType<DataAmount> mockedNetworkDataReceiveed = new QuantityType<>(800, Units.MEBIBYTE);
862 when(mockedSystemInfo.getNetworkDataReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataReceiveed);
864 initializeThingWithChannel(channnelID, acceptedItemType);
865 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
866 mockedNetworkDataReceiveed);
870 public void assertChannelNetworkPacketsSentIsUpdated() throws DeviceNotFoundException {
871 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_SENT;
872 String acceptedItemType = "Number";
874 DecimalType mockedNetworkPacketsSent = new DecimalType(50);
875 when(mockedSystemInfo.getNetworkPacketsSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsSent);
877 initializeThingWithChannel(channnelID, acceptedItemType);
878 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
879 mockedNetworkPacketsSent);
883 public void assertChannelNetworkPacketsReceivedIsUpdated() throws DeviceNotFoundException {
884 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_RECEIVED;
885 String acceptedItemType = "Number";
887 DecimalType mockedNetworkPacketsReceived = new DecimalType(48);
888 when(mockedSystemInfo.getNetworkPacketsReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsReceived);
890 initializeThingWithChannel(channnelID, acceptedItemType);
891 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
892 mockedNetworkPacketsReceived);
896 public void assertChannelNetworkNetworkNameIsUpdated() throws DeviceNotFoundException {
897 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_NAME;
898 String acceptedItemType = "String";
900 StringType mockedNetworkName = new StringType("MockN-AQ34");
901 when(mockedSystemInfo.getNetworkName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkName);
903 initializeThingWithChannel(channnelID, acceptedItemType);
904 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkName);
908 public void assertChannelNetworkNetworkDisplayNameIsUpdated() throws DeviceNotFoundException {
909 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_ADAPTER_NAME;
910 String acceptedItemType = "String";
912 StringType mockedNetworkAdapterName = new StringType("Mocked Network Adapter Name");
913 when(mockedSystemInfo.getNetworkDisplayName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkAdapterName);
915 initializeThingWithChannel(channnelID, acceptedItemType);
916 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
917 mockedNetworkAdapterName);
920 class SysteminfoDiscoveryServiceMock extends SysteminfoDiscoveryService {
923 SysteminfoDiscoveryServiceMock(String hostname) {
925 this.hostname = hostname;
929 protected String getHostName() throws UnknownHostException {
930 if ("unresolved".equals(hostname)) {
931 throw new UnknownHostException();
937 public void startScan() {
943 public void testDiscoveryWithInvalidHostname() {
944 String hostname = "Hilo.fritz.box";
945 String expectedHostname = "Hilo_fritz_box";
947 testDiscoveryService(expectedHostname, hostname);
951 public void testDiscoveryWithValidHostname() {
952 String hostname = "MyComputer";
953 String expectedHostname = "MyComputer";
955 testDiscoveryService(expectedHostname, hostname);
959 public void testDiscoveryWithUnresolvedHostname() {
960 String hostname = "unresolved";
961 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
963 testDiscoveryService(expectedHostname, hostname);
967 public void testDiscoveryWithEmptyHostnameString() {
968 String hostname = "";
969 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
971 testDiscoveryService(expectedHostname, hostname);
974 private void testDiscoveryService(String expectedHostname, String hostname) {
975 SysteminfoDiscoveryService discoveryService = getService(DiscoveryService.class,
976 SysteminfoDiscoveryService.class);
977 waitForAssert(() -> {
978 assertThat(discoveryService, is(notNullValue()));
980 SysteminfoDiscoveryServiceMock discoveryServiceMock = new SysteminfoDiscoveryServiceMock(hostname);
981 if (discoveryService != null) {
982 unregisterService(DiscoveryService.class);
984 registerService(discoveryServiceMock, DiscoveryService.class.getName(), new Hashtable<>());
986 ThingTypeUID computerType = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
987 ThingUID computerUID = new ThingUID(computerType, expectedHostname);
989 discoveryServiceMock.startScan();
991 Inbox inbox = getService(Inbox.class);
992 assertThat(inbox, is(notNullValue()));
998 waitForAssert(() -> {
999 List<DiscoveryResult> results = inbox.stream().filter(InboxPredicates.forThingUID(computerUID))
1001 assertFalse(results.isEmpty(), "No Thing with UID " + computerUID.getAsString() + " in inbox");
1004 inbox.approve(computerUID, SysteminfoDiscoveryService.DEFAULT_THING_LABEL, null);
1006 waitForAssert(() -> {
1007 systemInfoThing = thingRegistry.get(computerUID);
1008 assertThat(systemInfoThing, is(notNullValue()));
1011 Thing thing = systemInfoThing;
1012 if (thing == null) {
1016 waitForAssert(() -> {
1017 assertThat("Thing is not initialized.", thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
1022 public void assertChannelProcessThreadsIsUpdatedWithPIDse() throws DeviceNotFoundException {
1023 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_THREADS;
1024 String acceptedItemType = "Number";
1025 // The pid of the System idle process in Windows
1028 DecimalType mockedProcessThreadsCount = new DecimalType(4);
1029 when(mockedSystemInfo.getProcessThreads(pid)).thenReturn(mockedProcessThreadsCount);
1031 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1032 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
1033 mockedProcessThreadsCount);
1037 public void assertChannelProcessPathIsUpdatedWithPIDset() throws DeviceNotFoundException {
1038 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_PATH;
1039 String acceptedItemType = "String";
1040 // The pid of the System idle process in Windows
1043 StringType mockedProcessPath = new StringType("C:\\Users\\MockedUser\\Process");
1044 when(mockedSystemInfo.getProcessPath(pid)).thenReturn(mockedProcessPath);
1046 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1047 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessPath);
1051 public void assertChannelProcessNameIsUpdatedWithPIDset() throws DeviceNotFoundException {
1052 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_NAME;
1053 String acceptedItemType = "String";
1054 // The pid of the System idle process in Windows
1057 StringType mockedProcessName = new StringType("MockedProcess.exe");
1058 when(mockedSystemInfo.getProcessName(pid)).thenReturn(mockedProcessName);
1060 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1061 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessName);
1065 public void assertChannelProcessMemoryIsUpdatedWithPIDset() throws DeviceNotFoundException {
1066 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_MEMORY;
1067 String acceptedItemType = "Number:DataAmount";
1068 // The pid of the System idle process in Windows
1071 QuantityType<DataAmount> mockedProcessMemory = new QuantityType<>(450, Units.MEBIBYTE);
1072 when(mockedSystemInfo.getProcessMemoryUsage(pid)).thenReturn(mockedProcessMemory);
1074 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1075 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessMemory);
1079 public void assertChannelProcessLoadIsUpdatedWithPIDset() throws DeviceNotFoundException {
1080 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_LOAD;
1081 String acceptedItemType = "Number";
1082 // The pid of the System idle process in Windows
1085 DecimalType mockedProcessLoad = new DecimalType(3);
1086 when(mockedSystemInfo.getProcessCpuUsage(pid)).thenReturn(mockedProcessLoad);
1088 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1089 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessLoad);
1093 public void testThingHandlesChannelPriorityChange() {
1094 String priorityKey = "priority";
1095 String pidKey = "pid";
1096 String initialPriority = DEFAULT_CHANNEL_TEST_PRIORITY; // Evaluates to High
1097 String newPriority = "Low";
1099 String acceptedItemType = "Number";
1100 initializeThingWithChannel(DEFAULT_TEST_CHANNEL_ID, acceptedItemType);
1102 Thing thing = systemInfoThing;
1103 if (thing == null) {
1104 throw new AssertionError("Thing is null");
1106 Channel channel = thing.getChannel(DEFAULT_TEST_CHANNEL_ID);
1107 if (channel == null) {
1108 throw new AssertionError("Channel '" + DEFAULT_TEST_CHANNEL_ID + "' is null");
1111 ThingHandler thingHandler = thing.getHandler();
1112 if (thingHandler == null) {
1113 throw new AssertionError("Thing handler is null");
1115 if (!(thingHandler.getClass().equals(SysteminfoHandler.class))) {
1116 throw new AssertionError("Thing handler not of class SysteminfoHandler");
1118 SysteminfoHandler handler = (SysteminfoHandler) thingHandler;
1119 waitForAssert(() -> {
1120 assertThat("The initial priority of channel " + channel.getUID() + " is not as expected.",
1121 channel.getConfiguration().get(priorityKey), is(equalTo(initialPriority)));
1122 assertThat(handler.getHighPriorityChannels().contains(channel.getUID()), is(true));
1125 // Change the priority of a channel, keep the pid
1126 Configuration updatedConfig = new Configuration();
1127 updatedConfig.put(priorityKey, newPriority);
1128 updatedConfig.put(pidKey, channel.getConfiguration().get(pidKey));
1129 Channel updatedChannel = ChannelBuilder.create(channel.getUID(), channel.getAcceptedItemType())
1130 .withType(channel.getChannelTypeUID()).withKind(channel.getKind()).withConfiguration(updatedConfig)
1133 Thing updatedThing = ThingBuilder.create(thing.getThingTypeUID(), thing.getUID())
1134 .withConfiguration(thing.getConfiguration()).withChannel(updatedChannel).build();
1136 handler.thingUpdated(updatedThing);
1138 waitForAssert(() -> {
1139 assertThat("The prority of the channel was not updated: ", channel.getConfiguration().get(priorityKey),
1140 is(equalTo(newPriority)));
1141 assertThat(handler.getLowPriorityChannels().contains(channel.getUID()), is(true));