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;
26 import java.util.Locale;
28 import javax.measure.quantity.ElectricPotential;
29 import javax.measure.quantity.Temperature;
30 import javax.measure.quantity.Time;
32 import org.eclipse.jdt.annotation.NonNullByDefault;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.ArgumentMatchers;
39 import org.mockito.Mock;
40 import org.mockito.junit.jupiter.MockitoExtension;
41 import org.mockito.junit.jupiter.MockitoSettings;
42 import org.mockito.quality.Strictness;
43 import org.openhab.binding.systeminfo.internal.SysteminfoBindingConstants;
44 import org.openhab.binding.systeminfo.internal.SysteminfoHandlerFactory;
45 import org.openhab.binding.systeminfo.internal.SysteminfoThingTypeProvider;
46 import org.openhab.binding.systeminfo.internal.discovery.SysteminfoDiscoveryService;
47 import org.openhab.binding.systeminfo.internal.handler.SysteminfoHandler;
48 import org.openhab.binding.systeminfo.internal.model.DeviceNotFoundException;
49 import org.openhab.binding.systeminfo.internal.model.OSHISysteminfo;
50 import org.openhab.binding.systeminfo.internal.model.SysteminfoInterface;
51 import org.openhab.core.config.core.Configuration;
52 import org.openhab.core.config.discovery.DiscoveryResult;
53 import org.openhab.core.config.discovery.DiscoveryService;
54 import org.openhab.core.config.discovery.inbox.Inbox;
55 import org.openhab.core.config.discovery.inbox.InboxPredicates;
56 import org.openhab.core.items.GenericItem;
57 import org.openhab.core.items.ItemNotFoundException;
58 import org.openhab.core.items.ItemRegistry;
59 import org.openhab.core.library.dimension.DataAmount;
60 import org.openhab.core.library.items.NumberItem;
61 import org.openhab.core.library.items.StringItem;
62 import org.openhab.core.library.types.DecimalType;
63 import org.openhab.core.library.types.PercentType;
64 import org.openhab.core.library.types.QuantityType;
65 import org.openhab.core.library.types.StringType;
66 import org.openhab.core.library.unit.SIUnits;
67 import org.openhab.core.library.unit.Units;
68 import org.openhab.core.test.java.JavaOSGiTest;
69 import org.openhab.core.test.storage.VolatileStorageService;
70 import org.openhab.core.thing.Channel;
71 import org.openhab.core.thing.ChannelUID;
72 import org.openhab.core.thing.ManagedThingProvider;
73 import org.openhab.core.thing.Thing;
74 import org.openhab.core.thing.ThingProvider;
75 import org.openhab.core.thing.ThingRegistry;
76 import org.openhab.core.thing.ThingStatus;
77 import org.openhab.core.thing.ThingStatusDetail;
78 import org.openhab.core.thing.ThingTypeUID;
79 import org.openhab.core.thing.ThingUID;
80 import org.openhab.core.thing.binding.ThingHandler;
81 import org.openhab.core.thing.binding.ThingHandlerFactory;
82 import org.openhab.core.thing.binding.ThingTypeProvider;
83 import org.openhab.core.thing.binding.builder.ChannelBuilder;
84 import org.openhab.core.thing.binding.builder.ThingBuilder;
85 import org.openhab.core.thing.link.ItemChannelLink;
86 import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
87 import org.openhab.core.thing.type.ChannelKind;
88 import org.openhab.core.thing.type.ChannelType;
89 import org.openhab.core.thing.type.ChannelTypeProvider;
90 import org.openhab.core.thing.type.ChannelTypeUID;
91 import org.openhab.core.thing.type.ThingType;
92 import org.openhab.core.types.State;
93 import org.openhab.core.types.UnDefType;
96 * OSGi tests for the {@link SysteminfoHandler}
98 * @author Svilen Valkanov - Initial contribution
99 * @author Lyubomir Papazov - Created a mock systeminfo object. This way, access to the user's OS will not be required,
100 * but mock data will be used instead, avoiding potential errors from the OS queries.
101 * @author Wouter Born - Migrate Groovy to Java tests
104 @ExtendWith(MockitoExtension.class)
105 @MockitoSettings(strictness = Strictness.LENIENT)
106 public class SysteminfoOSGiTest extends JavaOSGiTest {
107 private static final String DEFAULT_TEST_THING_NAME = "work";
108 private static final String DEFAULT_TEST_ITEM_NAME = "test";
109 private static final String DEFAULT_CHANNEL_TEST_PRIORITY = "High";
110 private static final int DEFAULT_CHANNEL_PID = -1;
111 private static final String DEFAULT_TEST_CHANNEL_ID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
112 private static final int DEFAULT_DEVICE_INDEX = 0;
115 * Refresh time in seconds for tasks with priority High.
116 * Default value for the parameter interval_high in the thing configuration
118 private static final int DEFAULT_TEST_INTERVAL_HIGH = 1;
121 * Refresh time in seconds for tasks with priority Medium.
123 private static final int DEFAULT_TEST_INTERVAL_MEDIUM = 3;
125 private @Nullable Thing systemInfoThing;
126 private @Nullable GenericItem testItem;
128 private @Mock @NonNullByDefault({}) OSHISysteminfo mockedSystemInfo;
129 private @NonNullByDefault({}) SysteminfoHandlerFactory systeminfoHandlerFactory;
130 private @NonNullByDefault({}) ThingRegistry thingRegistry;
131 private @NonNullByDefault({}) ItemRegistry itemRegistry;
134 public void setUp() {
135 VolatileStorageService volatileStorageService = new VolatileStorageService();
136 registerService(volatileStorageService);
138 // Preparing the mock with OS properties, that are used in the initialize method of SysteminfoHandler
139 // Make this lenient because the assertInvalidThingConfigurationValuesAreHandled test does not require them
140 lenient().when(mockedSystemInfo.getCpuLogicalCores()).thenReturn(new DecimalType(2));
141 lenient().when(mockedSystemInfo.getCpuPhysicalCores()).thenReturn(new DecimalType(2));
142 lenient().when(mockedSystemInfo.getOsFamily()).thenReturn(new StringType("Mock OS"));
143 lenient().when(mockedSystemInfo.getOsManufacturer()).thenReturn(new StringType("Mock OS Manufacturer"));
144 lenient().when(mockedSystemInfo.getOsVersion()).thenReturn(new StringType("Mock Os Version"));
145 // Following mock method returns will make sure the thing does not get recreated with extra channels
146 lenient().when(mockedSystemInfo.getNetworkIFCount()).thenReturn(1);
147 lenient().when(mockedSystemInfo.getDisplayCount()).thenReturn(1);
148 lenient().when(mockedSystemInfo.getFileOSStoreCount()).thenReturn(1);
149 lenient().when(mockedSystemInfo.getPowerSourceCount()).thenReturn(1);
150 lenient().when(mockedSystemInfo.getDriveCount()).thenReturn(1);
151 lenient().when(mockedSystemInfo.getFanCount()).thenReturn(1);
153 registerService(mockedSystemInfo);
155 waitForAssert(() -> {
156 systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
157 assertThat(systeminfoHandlerFactory, is(notNullValue()));
159 if (systeminfoHandlerFactory != null) {
160 // Unbind oshiSystemInfo service and bind the mock service to make the systeminfo binding tests independent
161 // of the external OSHI library
162 SysteminfoInterface oshiSystemInfo = getService(SysteminfoInterface.class);
163 if (oshiSystemInfo != null) {
164 systeminfoHandlerFactory.unbindSystemInfo(oshiSystemInfo);
166 systeminfoHandlerFactory.bindSystemInfo(mockedSystemInfo);
169 waitForAssert(() -> {
170 ThingTypeProvider thingTypeProvider = getService(ThingTypeProvider.class,
171 SysteminfoThingTypeProvider.class);
172 assertThat(thingTypeProvider, is(notNullValue()));
174 waitForAssert(() -> {
175 SysteminfoThingTypeProvider systeminfoThingTypeProvider = getService(SysteminfoThingTypeProvider.class);
176 assertThat(systeminfoThingTypeProvider, is(notNullValue()));
179 waitForAssert(() -> {
180 thingRegistry = getService(ThingRegistry.class);
181 assertThat(thingRegistry, is(notNullValue()));
184 waitForAssert(() -> {
185 itemRegistry = getService(ItemRegistry.class);
186 assertThat(itemRegistry, is(notNullValue()));
191 public void tearDown() {
192 Thing thing = systemInfoThing;
194 // Remove the systeminfo thing. The handler will be also disposed automatically
195 Thing removedThing = thingRegistry.forceRemove(thing.getUID());
196 assertThat("The systeminfo thing cannot be deleted", removedThing, is(notNullValue()));
197 waitForAssert(() -> {
198 ThingHandler systemInfoHandler = thing.getHandler();
199 assertThat(systemInfoHandler, is(nullValue()));
203 if (testItem != null) {
204 itemRegistry.remove(DEFAULT_TEST_ITEM_NAME);
207 unregisterService(mockedSystemInfo);
210 private void initializeThingWithChannelAndPID(String channelID, String acceptedItemType, int pid) {
211 Configuration thingConfig = new Configuration();
212 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
213 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
214 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
215 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
216 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
218 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
221 private void initializeThingWithChannelAndPriority(String channelID, String acceptedItemType, String priority) {
222 Configuration thingConfig = new Configuration();
223 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
224 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
225 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
226 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
227 int pid = DEFAULT_CHANNEL_PID;
229 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
232 private void initializeThingWithConfiguration(Configuration config) {
233 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
234 String channelID = DEFAULT_TEST_CHANNEL_ID;
235 String acceptedItemType = "String";
236 int pid = DEFAULT_CHANNEL_PID;
238 initializeThing(config, channelID, acceptedItemType, priority, pid);
241 private void initializeThingWithChannel(String channelID, String acceptedItemType) {
242 Configuration thingConfig = new Configuration();
243 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
244 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
245 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
246 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
248 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
249 int pid = DEFAULT_CHANNEL_PID;
250 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
253 private void initializeThing(Configuration thingConfiguration, String channelID, String acceptedItemType,
254 String priority, int pid) {
255 ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
256 ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
258 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
259 ChannelTypeUID channelTypeUID = new ChannelTypeUID(SysteminfoBindingConstants.BINDING_ID,
260 channelUID.getIdWithoutGroup());
261 Configuration channelConfig = new Configuration();
262 channelConfig.put("priority", priority);
263 channelConfig.put("pid", new BigDecimal(pid));
264 Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
265 .withKind(ChannelKind.STATE).withConfiguration(channelConfig).build();
267 Thing thing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(thingConfiguration)
268 .withChannel(channel).build();
269 systemInfoThing = thing;
271 // TODO: This is a technically not correct work-around as the thing types are currently not made available by
272 // the binding. It should be properly fixes in the binding that thing-types are added to the registry. The
273 // "correct" solution here would be to wait until the thing manager initializes the thing with a missing thing
274 // type, but that would make each test take 120+ s
275 ThingTypeProvider thingTypeProviderMock = mock(ThingTypeProvider.class);
276 when(thingTypeProviderMock.getThingType(ArgumentMatchers.any(ThingTypeUID.class), nullable(Locale.class)))
277 .thenReturn(mock(ThingType.class));
278 registerService(thingTypeProviderMock);
280 ChannelType channelTypeMock = mock(ChannelType.class);
281 when(channelTypeMock.getKind()).thenReturn(ChannelKind.STATE);
282 ChannelTypeProvider channelTypeProviderMock = mock(ChannelTypeProvider.class);
283 when(channelTypeProviderMock.getChannelType(ArgumentMatchers.any(ChannelTypeUID.class), nullable(Locale.class)))
284 .thenReturn(channelTypeMock);
285 registerService(channelTypeProviderMock);
287 ManagedThingProvider managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
288 assertThat(managedThingProvider, is(notNullValue()));
290 if (managedThingProvider != null) {
291 managedThingProvider.add(thing);
294 waitForAssert(() -> {
295 SysteminfoHandler handler = (SysteminfoHandler) thing.getHandler();
296 assertThat(handler, is(notNullValue()));
299 waitForAssert(() -> {
300 assertThat("Thing is not initialized, before an Item is created", thing.getStatus(),
301 anyOf(equalTo(ThingStatus.OFFLINE), equalTo(ThingStatus.ONLINE)));
304 intializeItem(channelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
307 private void assertItemState(String acceptedItemType, String itemName, String priority, State expectedState) {
308 Thing thing = systemInfoThing;
310 throw new AssertionError("Thing is null");
312 waitForAssert(() -> {
313 ThingStatusDetail thingStatusDetail = thing.getStatusInfo().getStatusDetail();
314 String description = thing.getStatusInfo().getDescription();
315 assertThat("Thing status detail is " + thingStatusDetail + " with description " + description,
316 thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
318 // The binding starts all refresh tasks in SysteminfoHandler.scheduleUpdates() after this delay !
320 sleep(SysteminfoHandler.WAIT_TIME_CHANNEL_ITEM_LINK_INIT * 1000);
321 } catch (InterruptedException e) {
322 throw new AssertionError("Interrupted while sleeping");
327 item = (GenericItem) itemRegistry.getItem(itemName);
328 } catch (ItemNotFoundException e) {
329 throw new AssertionError("Item not found in registry");
333 if ("High".equals(priority)) {
334 waitTime = DEFAULT_TEST_INTERVAL_HIGH * 1000;
335 } else if ("Medium".equals(priority)) {
336 waitTime = DEFAULT_TEST_INTERVAL_MEDIUM * 1000;
341 waitForAssert(() -> {
342 State itemState = item.getState();
343 assertThat(itemState, is(equalTo(expectedState)));
344 }, waitTime, DFL_SLEEP_TIME);
347 private void intializeItem(ChannelUID channelUID, String itemName, String acceptedItemType) {
348 GenericItem item = null;
349 if (acceptedItemType.startsWith("Number")) {
350 item = new NumberItem(acceptedItemType, itemName);
351 } else if ("String".equals(acceptedItemType)) {
352 item = new StringItem(itemName);
355 throw new AssertionError("Item is null");
357 itemRegistry.add(item);
360 ManagedItemChannelLinkProvider itemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
361 assertThat(itemChannelLinkProvider, is(notNullValue()));
363 if (itemChannelLinkProvider == null) {
367 itemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
371 public void assertInvalidThingConfigurationValuesAreHandled() {
372 Configuration configuration = new Configuration();
374 // invalid value - must be positive
375 int refreshIntervalHigh = -5;
376 configuration.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME, new BigDecimal(refreshIntervalHigh));
378 int refreshIntervalMedium = 3;
379 configuration.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
380 new BigDecimal(refreshIntervalMedium));
381 initializeThingWithConfiguration(configuration);
383 testInvalidConfiguration();
386 private void testInvalidConfiguration() {
387 waitForAssert(() -> {
388 Thing thing = systemInfoThing;
390 assertThat("Invalid configuration is used !", thing.getStatus(), is(equalTo(ThingStatus.OFFLINE)));
391 assertThat(thing.getStatusInfo().getStatusDetail(),
392 is(equalTo(ThingStatusDetail.HANDLER_INITIALIZING_ERROR)));
393 assertThat(thing.getStatusInfo().getDescription(), is(equalTo("@text/offline.cannot-initialize")));
399 public void assertMediumPriorityChannelIsUpdated() {
400 String channnelID = DEFAULT_TEST_CHANNEL_ID;
401 String acceptedItemType = "Number";
402 String priority = "Medium";
404 initializeThingWithChannelAndPriority(channnelID, acceptedItemType, priority);
405 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, priority, UnDefType.UNDEF);
409 public void assertStateOfSecondDeviceIsUpdated() {
410 // This test assumes that at least 2 network interfaces are present on the test platform
412 String channnelID = "network" + deviceIndex + "#mac";
413 String acceptedItemType = "String";
415 initializeThingWithChannel(channnelID, acceptedItemType);
416 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, UnDefType.UNDEF);
420 public void assertChannelCpuLoadIsUpdated() {
421 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
422 String acceptedItemType = "Number";
424 PercentType mockedCpuLoadValue = new PercentType(9);
425 when(mockedSystemInfo.getSystemCpuLoad()).thenReturn(mockedCpuLoadValue);
427 initializeThingWithChannel(channnelID, acceptedItemType);
428 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoadValue);
432 public void assertChannelCpuLoad1IsUpdated() {
433 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_1;
434 String acceptedItemType = "Number";
436 DecimalType mockedCpuLoad1Value = new DecimalType(1.1);
437 when(mockedSystemInfo.getCpuLoad1()).thenReturn(mockedCpuLoad1Value);
439 initializeThingWithChannel(channnelID, acceptedItemType);
440 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad1Value);
444 public void assertChannelCpuLoad5IsUpdated() {
445 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_5;
446 String acceptedItemType = "Number";
448 DecimalType mockedCpuLoad5Value = new DecimalType(5.5);
449 when(mockedSystemInfo.getCpuLoad5()).thenReturn(mockedCpuLoad5Value);
451 initializeThingWithChannel(channnelID, acceptedItemType);
452 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad5Value);
456 public void assertChannelCpuLoad15IsUpdated() {
457 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_15;
458 String acceptedItemType = "Number";
460 DecimalType mockedCpuLoad15Value = new DecimalType(15.15);
461 when(mockedSystemInfo.getCpuLoad15()).thenReturn(mockedCpuLoad15Value);
463 initializeThingWithChannel(channnelID, acceptedItemType);
464 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad15Value);
468 public void assertChannelCpuThreadsIsUpdated() {
469 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_THREADS;
470 String acceptedItemType = "Number";
472 DecimalType mockedCpuThreadsValue = new DecimalType(16);
473 when(mockedSystemInfo.getCpuThreads()).thenReturn(mockedCpuThreadsValue);
475 initializeThingWithChannel(channnelID, acceptedItemType);
476 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuThreadsValue);
480 public void assertChannelCpuUptimeIsUpdated() {
481 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_UPTIME;
482 String acceptedItemType = "Number:Time";
484 QuantityType<Time> mockedCpuUptimeValue = new QuantityType<>(100, Units.MINUTE);
485 when(mockedSystemInfo.getCpuUptime()).thenReturn(mockedCpuUptimeValue);
487 initializeThingWithChannel(channnelID, acceptedItemType);
488 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuUptimeValue);
492 public void assertChannelCpuDescriptionIsUpdated() {
493 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_DESCRIPTION;
494 String acceptedItemType = "String";
496 StringType mockedCpuDescriptionValue = new StringType("Mocked Cpu Descr");
497 when(mockedSystemInfo.getCpuDescription()).thenReturn(mockedCpuDescriptionValue);
499 initializeThingWithChannel(channnelID, acceptedItemType);
500 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
501 mockedCpuDescriptionValue);
505 public void assertChannelCpuNameIsUpdated() {
506 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_NAME;
507 String acceptedItemType = "String";
509 StringType mockedCpuNameValue = new StringType("Mocked Cpu Name");
510 when(mockedSystemInfo.getCpuName()).thenReturn(mockedCpuNameValue);
512 initializeThingWithChannel(channnelID, acceptedItemType);
513 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuNameValue);
517 public void assertChannelMemoryAvailableIsUpdated() {
518 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE;
519 String acceptedItemType = "Number:DataAmount";
521 QuantityType<DataAmount> mockedMemoryAvailableValue = new QuantityType<>(1000, Units.MEBIBYTE);
522 when(mockedSystemInfo.getMemoryAvailable()).thenReturn(mockedMemoryAvailableValue);
524 initializeThingWithChannel(channnelID, acceptedItemType);
525 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
526 mockedMemoryAvailableValue);
530 public void assertChannelMemoryUsedIsUpdated() {
531 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_USED;
532 String acceptedItemType = "Number:DataAmount";
534 QuantityType<DataAmount> mockedMemoryUsedValue = new QuantityType<>(24, Units.MEBIBYTE);
535 when(mockedSystemInfo.getMemoryUsed()).thenReturn(mockedMemoryUsedValue);
537 initializeThingWithChannel(channnelID, acceptedItemType);
538 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedMemoryUsedValue);
542 public void assertChannelMemoryTotalIsUpdated() {
543 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_TOTAL;
544 String acceptedItemType = "Number:DataAmount";
546 QuantityType<DataAmount> mockedMemoryTotalValue = new QuantityType<>(1024, Units.MEBIBYTE);
547 when(mockedSystemInfo.getMemoryTotal()).thenReturn(mockedMemoryTotalValue);
549 initializeThingWithChannel(channnelID, acceptedItemType);
550 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
551 mockedMemoryTotalValue);
555 public void assertChannelMemoryAvailablePercentIsUpdated() {
556 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE_PERCENT;
557 String acceptedItemType = "Number";
559 PercentType mockedMemoryAvailablePercentValue = new PercentType(97);
560 when(mockedSystemInfo.getMemoryAvailablePercent()).thenReturn(mockedMemoryAvailablePercentValue);
562 initializeThingWithChannel(channnelID, acceptedItemType);
563 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
564 mockedMemoryAvailablePercentValue);
568 public void assertChannelSwapAvailableIsUpdated() {
569 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE;
570 String acceptedItemType = "Number:DataAmount";
572 QuantityType<DataAmount> mockedSwapAvailableValue = new QuantityType<>(482, Units.MEBIBYTE);
573 when(mockedSystemInfo.getSwapAvailable()).thenReturn(mockedSwapAvailableValue);
575 initializeThingWithChannel(channnelID, acceptedItemType);
576 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
577 mockedSwapAvailableValue);
581 public void assertChannelSwapUsedIsUpdated() {
582 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_USED;
583 String acceptedItemType = "Number:DataAmount";
585 QuantityType<DataAmount> mockedSwapUsedValue = new QuantityType<>(30, Units.MEBIBYTE);
586 when(mockedSystemInfo.getSwapUsed()).thenReturn(mockedSwapUsedValue);
588 initializeThingWithChannel(channnelID, acceptedItemType);
589 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapUsedValue);
593 public void assertChannelSwapTotalIsUpdated() {
594 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_TOTAL;
595 String acceptedItemType = "Number:DataAmount";
597 QuantityType<DataAmount> mockedSwapTotalValue = new QuantityType<>(512, Units.MEBIBYTE);
598 when(mockedSystemInfo.getSwapTotal()).thenReturn(mockedSwapTotalValue);
600 initializeThingWithChannel(channnelID, acceptedItemType);
601 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapTotalValue);
605 public void assertChannelSwapAvailablePercentIsUpdated() {
606 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE_PERCENT;
607 String acceptedItemType = "Number";
609 PercentType mockedSwapAvailablePercentValue = new PercentType(94);
610 when(mockedSystemInfo.getSwapAvailablePercent()).thenReturn(mockedSwapAvailablePercentValue);
612 initializeThingWithChannel(channnelID, acceptedItemType);
613 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
614 mockedSwapAvailablePercentValue);
618 public void assertChannelStorageNameIsUpdated() throws DeviceNotFoundException {
619 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_NAME;
620 String acceptedItemType = "String";
622 StringType mockedStorageName = new StringType("Mocked Storage Name");
623 when(mockedSystemInfo.getStorageName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageName);
625 initializeThingWithChannel(channnelID, acceptedItemType);
626 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageName);
630 public void assertChannelStorageTypeIsUpdated() throws DeviceNotFoundException {
631 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TYPE;
632 String acceptedItemType = "String";
634 StringType mockedStorageType = new StringType("Mocked Storage Type");
635 when(mockedSystemInfo.getStorageType(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageType);
637 initializeThingWithChannel(channnelID, acceptedItemType);
638 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageType);
642 public void assertChannelStorageDescriptionIsUpdated() throws DeviceNotFoundException {
643 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_DESCRIPTION;
644 String acceptedItemType = "String";
646 StringType mockedStorageDescription = new StringType("Mocked Storage Description");
647 when(mockedSystemInfo.getStorageDescription(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageDescription);
649 initializeThingWithChannel(channnelID, acceptedItemType);
650 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
651 mockedStorageDescription);
655 public void assertChannelStorageAvailableIsUpdated() throws DeviceNotFoundException {
656 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE;
657 String acceptedItemType = "Number:DataAmount";
659 QuantityType<DataAmount> mockedStorageAvailableValue = new QuantityType<>(2000, Units.MEBIBYTE);
660 when(mockedSystemInfo.getStorageAvailable(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageAvailableValue);
662 initializeThingWithChannel(channnelID, acceptedItemType);
663 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
664 mockedStorageAvailableValue);
668 public void assertChannelStorageUsedIsUpdated() throws DeviceNotFoundException {
669 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_USED;
670 String acceptedItemType = "Number:DataAmount";
672 QuantityType<DataAmount> mockedStorageUsedValue = new QuantityType<>(500, Units.MEBIBYTE);
673 when(mockedSystemInfo.getStorageUsed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageUsedValue);
675 initializeThingWithChannel(channnelID, acceptedItemType);
676 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
677 mockedStorageUsedValue);
681 public void assertChannelStorageTotalIsUpdated() throws DeviceNotFoundException {
682 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TOTAL;
683 String acceptedItemType = "Number:DataAmount";
685 QuantityType<DataAmount> mockedStorageTotalValue = new QuantityType<>(2500, Units.MEBIBYTE);
686 when(mockedSystemInfo.getStorageTotal(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageTotalValue);
688 initializeThingWithChannel(channnelID, acceptedItemType);
689 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
690 mockedStorageTotalValue);
694 public void assertChannelStorageAvailablePercentIsUpdated() throws DeviceNotFoundException {
695 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE_PERCENT;
696 String acceptedItemType = "Number";
698 PercentType mockedStorageAvailablePercent = new PercentType(20);
699 when(mockedSystemInfo.getStorageAvailablePercent(DEFAULT_DEVICE_INDEX))
700 .thenReturn(mockedStorageAvailablePercent);
702 initializeThingWithChannel(channnelID, acceptedItemType);
703 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
704 mockedStorageAvailablePercent);
708 public void assertChannelDriveNameIsUpdated() throws DeviceNotFoundException {
709 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_NAME;
710 String acceptedItemType = "String";
712 StringType mockedDriveNameValue = new StringType("Mocked Drive Name");
713 when(mockedSystemInfo.getDriveName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveNameValue);
715 initializeThingWithChannel(channelID, acceptedItemType);
716 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveNameValue);
720 public void assertChannelDriveModelIsUpdated() throws DeviceNotFoundException {
721 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_MODEL;
722 String acceptedItemType = "String";
724 StringType mockedDriveModelValue = new StringType("Mocked Drive Model");
725 when(mockedSystemInfo.getDriveModel(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveModelValue);
727 initializeThingWithChannel(channelID, acceptedItemType);
728 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveModelValue);
732 public void assertChannelDriveSerialIsUpdated() throws DeviceNotFoundException {
733 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_SERIAL;
734 String acceptedItemType = "String";
736 StringType mockedDriveSerialNumber = new StringType("Mocked Drive Serial Number");
737 when(mockedSystemInfo.getDriveSerialNumber(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveSerialNumber);
739 initializeThingWithChannel(channelID, acceptedItemType);
740 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
741 mockedDriveSerialNumber);
744 // Re-enable this previously disabled test, as it is not relying on hardware anymore, but a mocked object
745 // There is a bug opened for this issue - https://github.com/dblock/oshi/issues/185
747 public void assertChannelSensorsCpuTempIsUpdated() {
748 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_CPU_TEMPERATURE;
749 String acceptedItemType = "Number:Temperature";
751 QuantityType<Temperature> mockedSensorsCpuTemperatureValue = new QuantityType<>(60, SIUnits.CELSIUS);
752 when(mockedSystemInfo.getSensorsCpuTemperature()).thenReturn(mockedSensorsCpuTemperatureValue);
754 initializeThingWithChannel(channnelID, acceptedItemType);
755 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
756 mockedSensorsCpuTemperatureValue);
760 public void assertChannelSensorsCpuVoltageIsUpdated() {
761 String channnelID = SysteminfoBindingConstants.CHANNEL_SENOSRS_CPU_VOLTAGE;
762 String acceptedItemType = "Number:ElectricPotential";
764 QuantityType<ElectricPotential> mockedSensorsCpuVoltageValue = new QuantityType<>(1000, Units.VOLT);
765 when(mockedSystemInfo.getSensorsCpuVoltage()).thenReturn(mockedSensorsCpuVoltageValue);
767 initializeThingWithChannel(channnelID, acceptedItemType);
768 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
769 mockedSensorsCpuVoltageValue);
773 public void assertChannelSensorsFanSpeedIsUpdated() throws DeviceNotFoundException {
774 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_FAN_SPEED;
775 String acceptedItemType = "Number";
777 DecimalType mockedSensorsCpuFanSpeedValue = new DecimalType(180);
778 when(mockedSystemInfo.getSensorsFanSpeed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedSensorsCpuFanSpeedValue);
780 initializeThingWithChannel(channnelID, acceptedItemType);
781 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
782 mockedSensorsCpuFanSpeedValue);
786 public void assertChannelBatteryNameIsUpdated() throws DeviceNotFoundException {
787 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_NAME;
788 String acceptedItemType = "String";
790 StringType mockedBatteryName = new StringType("Mocked Battery Name");
791 when(mockedSystemInfo.getBatteryName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryName);
793 initializeThingWithChannel(channnelID, acceptedItemType);
794 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedBatteryName);
798 public void assertChannelBatteryRemainingCapacityIsUpdated() throws DeviceNotFoundException {
799 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_CAPACITY;
800 String acceptedItemType = "Number";
802 PercentType mockedBatteryRemainingCapacity = new PercentType(20);
803 when(mockedSystemInfo.getBatteryRemainingCapacity(DEFAULT_DEVICE_INDEX))
804 .thenReturn(mockedBatteryRemainingCapacity);
806 initializeThingWithChannel(channnelID, acceptedItemType);
807 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
808 mockedBatteryRemainingCapacity);
812 public void assertChannelBatteryRemainingTimeIsUpdated() throws DeviceNotFoundException {
813 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_TIME;
814 String acceptedItemType = "Number:Time";
816 QuantityType<Time> mockedBatteryRemainingTime = new QuantityType<>(3600, Units.MINUTE);
817 when(mockedSystemInfo.getBatteryRemainingTime(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryRemainingTime);
819 initializeThingWithChannel(channnelID, acceptedItemType);
820 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
821 mockedBatteryRemainingTime);
825 public void assertChannelDisplayInformationIsUpdated() throws DeviceNotFoundException {
826 String channnelID = SysteminfoBindingConstants.CHANNEL_DISPLAY_INFORMATION;
827 String acceptedItemType = "String";
829 StringType mockedDisplayInfo = new StringType("Mocked Display Information");
830 when(mockedSystemInfo.getDisplayInformation(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDisplayInfo);
832 initializeThingWithChannel(channnelID, acceptedItemType);
833 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDisplayInfo);
837 public void assertChannelNetworkIpIsUpdated() throws DeviceNotFoundException {
838 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_IP;
839 String acceptedItemType = "String";
841 StringType mockedNetworkIp = new StringType("192.168.1.0");
842 when(mockedSystemInfo.getNetworkIp(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkIp);
844 initializeThingWithChannel(channnelID, acceptedItemType);
845 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkIp);
849 public void assertChannelNetworkMacIsUpdated() throws DeviceNotFoundException {
850 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_MAC;
851 String acceptedItemType = "String";
853 StringType mockedNetworkMacValue = new StringType("AB-10-11-12-13-14");
854 when(mockedSystemInfo.getNetworkMac(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkMacValue);
856 initializeThingWithChannel(channnelID, acceptedItemType);
857 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkMacValue);
861 public void assertChannelNetworkDataSentIsUpdated() throws DeviceNotFoundException {
862 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_SENT;
863 String acceptedItemType = "Number:DataAmount";
865 QuantityType<DataAmount> mockedNetworkDataSent = new QuantityType<>(1000, Units.MEBIBYTE);
866 when(mockedSystemInfo.getNetworkDataSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataSent);
868 initializeThingWithChannel(channnelID, acceptedItemType);
869 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkDataSent);
873 public void assertChannelNetworkDataReceivedIsUpdated() throws DeviceNotFoundException {
874 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_RECEIVED;
875 String acceptedItemType = "Number:DataAmount";
877 QuantityType<DataAmount> mockedNetworkDataReceiveed = new QuantityType<>(800, Units.MEBIBYTE);
878 when(mockedSystemInfo.getNetworkDataReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataReceiveed);
880 initializeThingWithChannel(channnelID, acceptedItemType);
881 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
882 mockedNetworkDataReceiveed);
886 public void assertChannelNetworkPacketsSentIsUpdated() throws DeviceNotFoundException {
887 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_SENT;
888 String acceptedItemType = "Number";
890 DecimalType mockedNetworkPacketsSent = new DecimalType(50);
891 when(mockedSystemInfo.getNetworkPacketsSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsSent);
893 initializeThingWithChannel(channnelID, acceptedItemType);
894 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
895 mockedNetworkPacketsSent);
899 public void assertChannelNetworkPacketsReceivedIsUpdated() throws DeviceNotFoundException {
900 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_RECEIVED;
901 String acceptedItemType = "Number";
903 DecimalType mockedNetworkPacketsReceived = new DecimalType(48);
904 when(mockedSystemInfo.getNetworkPacketsReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsReceived);
906 initializeThingWithChannel(channnelID, acceptedItemType);
907 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
908 mockedNetworkPacketsReceived);
912 public void assertChannelNetworkNetworkNameIsUpdated() throws DeviceNotFoundException {
913 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_NAME;
914 String acceptedItemType = "String";
916 StringType mockedNetworkName = new StringType("MockN-AQ34");
917 when(mockedSystemInfo.getNetworkName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkName);
919 initializeThingWithChannel(channnelID, acceptedItemType);
920 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkName);
924 public void assertChannelNetworkNetworkDisplayNameIsUpdated() throws DeviceNotFoundException {
925 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_ADAPTER_NAME;
926 String acceptedItemType = "String";
928 StringType mockedNetworkAdapterName = new StringType("Mocked Network Adapter Name");
929 when(mockedSystemInfo.getNetworkDisplayName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkAdapterName);
931 initializeThingWithChannel(channnelID, acceptedItemType);
932 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
933 mockedNetworkAdapterName);
936 class SysteminfoDiscoveryServiceMock extends SysteminfoDiscoveryService {
939 SysteminfoDiscoveryServiceMock(String hostname) {
941 this.hostname = hostname;
945 protected String getHostName() throws UnknownHostException {
946 if ("unresolved".equals(hostname)) {
947 throw new UnknownHostException();
953 public void startScan() {
959 public void testDiscoveryWithInvalidHostname() {
960 String hostname = "Hilo.fritz.box";
961 String expectedHostname = "Hilo_fritz_box";
963 testDiscoveryService(expectedHostname, hostname);
967 public void testDiscoveryWithValidHostname() {
968 String hostname = "MyComputer";
969 String expectedHostname = "MyComputer";
971 testDiscoveryService(expectedHostname, hostname);
975 public void testDiscoveryWithUnresolvedHostname() {
976 String hostname = "unresolved";
977 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
979 testDiscoveryService(expectedHostname, hostname);
983 public void testDiscoveryWithEmptyHostnameString() {
984 String hostname = "";
985 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
987 testDiscoveryService(expectedHostname, hostname);
990 private void testDiscoveryService(String expectedHostname, String hostname) {
991 SysteminfoDiscoveryService discoveryService = getService(DiscoveryService.class,
992 SysteminfoDiscoveryService.class);
993 waitForAssert(() -> {
994 assertThat(discoveryService, is(notNullValue()));
996 SysteminfoDiscoveryServiceMock discoveryServiceMock = new SysteminfoDiscoveryServiceMock(hostname);
997 if (discoveryService != null) {
998 unregisterService(DiscoveryService.class);
1000 registerService(discoveryServiceMock, DiscoveryService.class.getName(), new Hashtable<>());
1002 ThingTypeUID computerType = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
1003 ThingUID computerUID = new ThingUID(computerType, expectedHostname);
1005 discoveryServiceMock.startScan();
1007 Inbox inbox = getService(Inbox.class);
1008 assertThat(inbox, is(notNullValue()));
1010 if (inbox == null) {
1014 waitForAssert(() -> {
1015 List<DiscoveryResult> results = inbox.stream().filter(InboxPredicates.forThingUID(computerUID))
1017 assertFalse(results.isEmpty(), "No Thing with UID " + computerUID.getAsString() + " in inbox");
1020 inbox.approve(computerUID, SysteminfoDiscoveryService.DEFAULT_THING_LABEL, null);
1022 waitForAssert(() -> {
1023 systemInfoThing = thingRegistry.get(computerUID);
1024 assertThat(systemInfoThing, is(notNullValue()));
1027 Thing thing = systemInfoThing;
1028 if (thing == null) {
1032 waitForAssert(() -> {
1033 assertThat("Thing is not initialized.", thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
1038 public void assertChannelProcessThreadsIsUpdatedWithPIDse() throws DeviceNotFoundException {
1039 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_THREADS;
1040 String acceptedItemType = "Number";
1041 // The pid of the System idle process in Windows
1044 DecimalType mockedProcessThreadsCount = new DecimalType(4);
1045 when(mockedSystemInfo.getProcessThreads(pid)).thenReturn(mockedProcessThreadsCount);
1047 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1048 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
1049 mockedProcessThreadsCount);
1053 public void assertChannelProcessPathIsUpdatedWithPIDset() throws DeviceNotFoundException {
1054 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_PATH;
1055 String acceptedItemType = "String";
1056 // The pid of the System idle process in Windows
1059 StringType mockedProcessPath = new StringType("C:\\Users\\MockedUser\\Process");
1060 when(mockedSystemInfo.getProcessPath(pid)).thenReturn(mockedProcessPath);
1062 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1063 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessPath);
1067 public void assertChannelProcessNameIsUpdatedWithPIDset() throws DeviceNotFoundException {
1068 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_NAME;
1069 String acceptedItemType = "String";
1070 // The pid of the System idle process in Windows
1073 StringType mockedProcessName = new StringType("MockedProcess.exe");
1074 when(mockedSystemInfo.getProcessName(pid)).thenReturn(mockedProcessName);
1076 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1077 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessName);
1081 public void assertChannelProcessMemoryIsUpdatedWithPIDset() throws DeviceNotFoundException {
1082 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_MEMORY;
1083 String acceptedItemType = "Number:DataAmount";
1084 // The pid of the System idle process in Windows
1087 QuantityType<DataAmount> mockedProcessMemory = new QuantityType<>(450, Units.MEBIBYTE);
1088 when(mockedSystemInfo.getProcessMemoryUsage(pid)).thenReturn(mockedProcessMemory);
1090 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1091 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessMemory);
1095 public void assertChannelProcessLoadIsUpdatedWithPIDset() throws DeviceNotFoundException {
1096 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_LOAD;
1097 String acceptedItemType = "Number";
1098 // The pid of the System idle process in Windows
1101 DecimalType mockedProcessLoad = new DecimalType(3);
1102 when(mockedSystemInfo.getProcessCpuUsage(pid)).thenReturn(mockedProcessLoad);
1104 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1105 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessLoad);
1109 public void testThingHandlesChannelPriorityChange() {
1110 String priorityKey = "priority";
1111 String pidKey = "pid";
1112 String initialPriority = DEFAULT_CHANNEL_TEST_PRIORITY; // Evaluates to High
1113 String newPriority = "Low";
1115 String acceptedItemType = "Number";
1116 initializeThingWithChannel(DEFAULT_TEST_CHANNEL_ID, acceptedItemType);
1118 Thing thing = systemInfoThing;
1119 if (thing == null) {
1120 throw new AssertionError("Thing is null");
1122 Channel channel = thing.getChannel(DEFAULT_TEST_CHANNEL_ID);
1123 if (channel == null) {
1124 throw new AssertionError("Channel '" + DEFAULT_TEST_CHANNEL_ID + "' is null");
1127 ThingHandler thingHandler = thing.getHandler();
1128 if (thingHandler == null) {
1129 throw new AssertionError("Thing handler is null");
1131 if (!(thingHandler.getClass().equals(SysteminfoHandler.class))) {
1132 throw new AssertionError("Thing handler not of class SysteminfoHandler");
1134 SysteminfoHandler handler = (SysteminfoHandler) thingHandler;
1135 waitForAssert(() -> {
1136 assertThat("The initial priority of channel " + channel.getUID() + " is not as expected.",
1137 channel.getConfiguration().get(priorityKey), is(equalTo(initialPriority)));
1138 assertThat(handler.getHighPriorityChannels().contains(channel.getUID()), is(true));
1141 // Change the priority of a channel, keep the pid
1142 Configuration updatedConfig = new Configuration();
1143 updatedConfig.put(priorityKey, newPriority);
1144 updatedConfig.put(pidKey, channel.getConfiguration().get(pidKey));
1145 Channel updatedChannel = ChannelBuilder.create(channel.getUID(), channel.getAcceptedItemType())
1146 .withType(channel.getChannelTypeUID()).withKind(channel.getKind()).withConfiguration(updatedConfig)
1149 Thing updatedThing = ThingBuilder.create(thing.getThingTypeUID(), thing.getUID())
1150 .withConfiguration(thing.getConfiguration()).withChannel(updatedChannel).build();
1152 handler.thingUpdated(updatedThing);
1154 waitForAssert(() -> {
1155 assertThat("The prority of the channel was not updated: ", channel.getConfiguration().get(priorityKey),
1156 is(equalTo(newPriority)));
1157 assertThat(handler.getLowPriorityChannels().contains(channel.getUID()), is(true));