2 * Copyright (c) 2010-2022 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 org.junit.jupiter.api.AfterEach;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Disabled;
30 import org.junit.jupiter.api.Test;
31 import org.openhab.binding.systeminfo.internal.SysteminfoBindingConstants;
32 import org.openhab.binding.systeminfo.internal.SysteminfoHandlerFactory;
33 import org.openhab.binding.systeminfo.internal.discovery.SysteminfoDiscoveryService;
34 import org.openhab.binding.systeminfo.internal.handler.SysteminfoHandler;
35 import org.openhab.binding.systeminfo.internal.model.DeviceNotFoundException;
36 import org.openhab.binding.systeminfo.internal.model.SysteminfoInterface;
37 import org.openhab.core.config.core.Configuration;
38 import org.openhab.core.config.discovery.DiscoveryResult;
39 import org.openhab.core.config.discovery.DiscoveryService;
40 import org.openhab.core.config.discovery.inbox.Inbox;
41 import org.openhab.core.config.discovery.inbox.InboxPredicates;
42 import org.openhab.core.items.GenericItem;
43 import org.openhab.core.items.ItemNotFoundException;
44 import org.openhab.core.items.ItemRegistry;
45 import org.openhab.core.library.items.NumberItem;
46 import org.openhab.core.library.items.StringItem;
47 import org.openhab.core.library.types.DecimalType;
48 import org.openhab.core.library.types.PercentType;
49 import org.openhab.core.library.types.StringType;
50 import org.openhab.core.test.java.JavaOSGiTest;
51 import org.openhab.core.test.storage.VolatileStorageService;
52 import org.openhab.core.thing.Channel;
53 import org.openhab.core.thing.ChannelUID;
54 import org.openhab.core.thing.ManagedThingProvider;
55 import org.openhab.core.thing.Thing;
56 import org.openhab.core.thing.ThingProvider;
57 import org.openhab.core.thing.ThingRegistry;
58 import org.openhab.core.thing.ThingStatus;
59 import org.openhab.core.thing.ThingStatusDetail;
60 import org.openhab.core.thing.ThingTypeUID;
61 import org.openhab.core.thing.ThingUID;
62 import org.openhab.core.thing.binding.ThingHandler;
63 import org.openhab.core.thing.binding.ThingHandlerFactory;
64 import org.openhab.core.thing.binding.builder.ChannelBuilder;
65 import org.openhab.core.thing.binding.builder.ThingBuilder;
66 import org.openhab.core.thing.link.ItemChannelLink;
67 import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
68 import org.openhab.core.thing.type.ChannelKind;
69 import org.openhab.core.thing.type.ChannelTypeUID;
70 import org.openhab.core.types.State;
71 import org.openhab.core.types.UnDefType;
74 * OSGi tests for the {@link SysteminfoHandler}
76 * @author Svilen Valkanov - Initial contribution
77 * @author Lyubomir Papazov - Created a mock systeminfo object. This way, access to the user's OS will not be required,
78 * but mock data will be used instead, avoiding potential errors from the OS queries.
79 * @author Wouter Born - Migrate Groovy to Java tests
81 public class SysteminfoOSGiTest extends JavaOSGiTest {
82 private static final String DEFAULT_TEST_THING_NAME = "work";
83 private static final String DEFAULT_TEST_ITEM_NAME = "test";
84 private static final String DEFAULT_CHANNEL_TEST_PRIORITY = "High";
85 private static final int DEFAULT_CHANNEL_PID = -1;
86 private static final String DEFAULT_TEST_CHANNEL_ID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
87 private static final int DEFAULT_DEVICE_INDEX = 0;
90 * Refresh time in seconds for tasks with priority High.
91 * Default value for the parameter interval_high in the thing configuration
93 private static final int DEFAULT_TEST_INTERVAL_HIGH = 1;
96 * Refresh time in seconds for tasks with priority Medium.
98 private static final int DEFAULT_TEST_INTERVAL_MEDIUM = 3;
100 private Thing systemInfoThing;
101 private SysteminfoHandler systemInfoHandler;
102 private GenericItem testItem;
104 private SysteminfoInterface mockedSystemInfo;
105 private ManagedThingProvider managedThingProvider;
106 private ThingRegistry thingRegistry;
107 private ItemRegistry itemRegistry;
108 private SysteminfoHandlerFactory systeminfoHandlerFactory;
111 public void setUp() {
112 VolatileStorageService volatileStorageService = new VolatileStorageService();
113 registerService(volatileStorageService);
115 // Preparing the mock with OS properties, that are used in the initialize method of SysteminfoHandler
116 mockedSystemInfo = mock(SysteminfoInterface.class);
117 when(mockedSystemInfo.getCpuLogicalCores()).thenReturn(new DecimalType(2));
118 when(mockedSystemInfo.getCpuPhysicalCores()).thenReturn(new DecimalType(2));
119 when(mockedSystemInfo.getOsFamily()).thenReturn(new StringType("Mock OS"));
120 when(mockedSystemInfo.getOsManufacturer()).thenReturn(new StringType("Mock OS Manufacturer"));
121 when(mockedSystemInfo.getOsVersion()).thenReturn(new StringType("Mock Os Version"));
123 systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
124 SysteminfoInterface oshiSystemInfo = getService(SysteminfoInterface.class);
126 // Unbind oshiSystemInfo service and bind the mock service to make the systeminfobinding tests independent of
127 // the external OSHI library
128 if (oshiSystemInfo != null) {
129 systeminfoHandlerFactory.unbindSystemInfo(oshiSystemInfo);
131 systeminfoHandlerFactory.bindSystemInfo(mockedSystemInfo);
133 managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
134 assertThat(managedThingProvider, is(notNullValue()));
136 thingRegistry = getService(ThingRegistry.class);
137 assertThat(thingRegistry, is(notNullValue()));
139 itemRegistry = getService(ItemRegistry.class);
140 assertThat(itemRegistry, is(notNullValue()));
144 public void tearDown() {
145 if (systemInfoThing != null) {
146 // Remove the systeminfo thing. The handler will be also disposed automatically
147 Thing removedThing = thingRegistry.forceRemove(systemInfoThing.getUID());
148 assertThat("The systeminfo thing cannot be deleted", removedThing, is(notNullValue()));
150 waitForAssert(() -> {
151 ThingHandler systemInfoHandler = systemInfoThing.getHandler();
152 assertThat(systemInfoHandler, is(nullValue()));
155 if (testItem != null) {
156 itemRegistry.remove(DEFAULT_TEST_ITEM_NAME);
160 private void initializeThingWithChannelAndPID(String channelID, String acceptedItemType, int pid) {
161 Configuration thingConfig = new Configuration();
162 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
163 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
164 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
165 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
166 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
168 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
171 private void initializeThingWithChannelAndPriority(String channelID, String acceptedItemType, String priority) {
172 Configuration thingConfig = new Configuration();
173 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
174 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
175 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
176 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
177 int pid = DEFAULT_CHANNEL_PID;
179 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
182 private void initializeThingWithConfiguration(Configuration config) {
183 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
184 String channelID = DEFAULT_TEST_CHANNEL_ID;
185 String acceptedItemType = "String";
186 int pid = DEFAULT_CHANNEL_PID;
188 initializeThing(config, channelID, acceptedItemType, priority, pid);
191 private void initializeThingWithChannel(String channelID, String acceptedItemType) {
192 Configuration thingConfig = new Configuration();
193 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
194 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
195 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
196 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
198 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
199 int pid = DEFAULT_CHANNEL_PID;
200 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
203 private void initializeThing(Configuration thingConfiguration, String channelID, String acceptedItemType,
204 String priority, int pid) {
205 ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
206 ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
208 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
209 ChannelTypeUID channelTypeUID = new ChannelTypeUID(SysteminfoBindingConstants.BINDING_ID,
210 channelUID.getIdWithoutGroup());
211 Configuration channelConfig = new Configuration();
212 channelConfig.put("priority", priority);
213 channelConfig.put("pid", new BigDecimal(pid));
214 Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
215 .withKind(ChannelKind.STATE).withConfiguration(channelConfig).build();
217 systemInfoThing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(thingConfiguration)
218 .withChannel(channel).build();
220 managedThingProvider.add(systemInfoThing);
222 waitForAssert(() -> {
223 systemInfoHandler = (SysteminfoHandler) systemInfoThing.getHandler();
224 assertThat(systemInfoHandler, is(notNullValue()));
227 waitForAssert(() -> {
228 assertThat("Thing is not initilized, before an Item is created", systemInfoThing.getStatus(),
229 anyOf(equalTo(ThingStatus.OFFLINE), equalTo(ThingStatus.ONLINE)));
232 intializeItem(channelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
235 private void assertItemState(String acceptedItemType, String itemName, String priority, State expectedState) {
236 waitForAssert(() -> {
237 ThingStatusDetail thingStatusDetail = systemInfoThing.getStatusInfo().getStatusDetail();
238 String description = systemInfoThing.getStatusInfo().getDescription();
239 assertThat("Thing status detail is " + thingStatusDetail + " with description " + description,
240 systemInfoThing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
242 // The binding starts all refresh tasks in SysteminfoHandler.scheduleUpdates() after this delay !
244 sleep(SysteminfoHandler.WAIT_TIME_CHANNEL_ITEM_LINK_INIT * 1000);
245 } catch (InterruptedException e) {
246 throw new AssertionError("Interrupted while sleeping");
251 item = (GenericItem) itemRegistry.getItem(itemName);
252 } catch (ItemNotFoundException e) {
253 throw new AssertionError("Item not found in registry");
257 if (priority.equals("High")) {
258 waitTime = DEFAULT_TEST_INTERVAL_HIGH * 1000;
259 } else if (priority.equals("Medium")) {
260 waitTime = DEFAULT_TEST_INTERVAL_MEDIUM * 1000;
265 waitForAssert(() -> {
266 State itemState = item.getState();
267 assertThat(itemState, is(equalTo(expectedState)));
268 }, waitTime, DFL_SLEEP_TIME);
271 private void intializeItem(ChannelUID channelUID, String itemName, String acceptedItemType) {
272 if (acceptedItemType.equals("Number")) {
273 testItem = new NumberItem(itemName);
274 } else if (acceptedItemType.equals("String")) {
275 testItem = new StringItem(itemName);
277 itemRegistry.add(testItem);
279 ManagedItemChannelLinkProvider itemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
280 assertThat(itemChannelLinkProvider, is(notNullValue()));
282 itemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
286 public void assertInvalidThingConfigurationValuesAreHandled() {
287 Configuration configuration = new Configuration();
289 // invalid value - must be positive
290 int refreshIntervalHigh = -5;
291 configuration.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME, new BigDecimal(refreshIntervalHigh));
293 int refreshIntervalMedium = 3;
294 configuration.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
295 new BigDecimal(refreshIntervalMedium));
296 initializeThingWithConfiguration(configuration);
298 testInvalidConfiguration();
301 private void testInvalidConfiguration() {
302 waitForAssert(() -> {
303 assertThat("Invalid configuratuin is used !", systemInfoThing.getStatus(),
304 is(equalTo(ThingStatus.OFFLINE)));
305 assertThat(systemInfoThing.getStatusInfo().getStatusDetail(),
306 is(equalTo(ThingStatusDetail.HANDLER_INITIALIZING_ERROR)));
307 assertThat(systemInfoThing.getStatusInfo().getDescription(), is(equalTo("Thing cannot be initialized!")));
312 public void assertThingStatusIsUninitializedWhenThereIsNoSysteminfoServiceProvided() {
313 // Unbind the mock service to verify the systeminfo thing will not be initialized when no systeminfo service is
315 systeminfoHandlerFactory.unbindSystemInfo(mockedSystemInfo);
317 ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
318 ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
320 systemInfoThing = ThingBuilder.create(thingTypeUID, thingUID).build();
321 managedThingProvider.add(systemInfoThing);
323 waitForAssert(() -> {
324 assertThat("The thing status is uninitialized when systeminfo service is missing",
325 systemInfoThing.getStatus(), equalTo(ThingStatus.UNINITIALIZED));
330 public void assertMediumPriorityChannelIsUpdated() {
331 String channnelID = DEFAULT_TEST_CHANNEL_ID;
332 String acceptedItemType = "Number";
333 String priority = "Medium";
335 initializeThingWithChannelAndPriority(channnelID, acceptedItemType, priority);
336 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, priority, UnDefType.UNDEF);
340 public void assertStateOfSecondDeviceIsUpdated() {
341 // This test assumes that at least 2 network interfaces are present on the test platform
343 String channnelID = "network" + deviceIndex + "#mac";
344 String acceptedItemType = "String";
346 initializeThingWithChannel(channnelID, acceptedItemType);
347 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, UnDefType.UNDEF);
351 public void assertChannelCpuLoadIsUpdated() {
352 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
353 String acceptedItemType = "Number";
355 PercentType mockedCpuLoadValue = new PercentType(9);
356 when(mockedSystemInfo.getSystemCpuLoad()).thenReturn(mockedCpuLoadValue);
358 initializeThingWithChannel(channnelID, acceptedItemType);
359 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoadValue);
363 public void assertChannelCpuLoad1IsUpdated() {
364 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_1;
365 String acceptedItemType = "Number";
367 DecimalType mockedCpuLoad1Value = new DecimalType(1.1);
368 when(mockedSystemInfo.getCpuLoad1()).thenReturn(mockedCpuLoad1Value);
370 initializeThingWithChannel(channnelID, acceptedItemType);
371 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad1Value);
375 public void assertChannelCpuLoad5IsUpdated() {
376 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_5;
377 String acceptedItemType = "Number";
379 DecimalType mockedCpuLoad5Value = new DecimalType(5.5);
380 when(mockedSystemInfo.getCpuLoad5()).thenReturn(mockedCpuLoad5Value);
382 initializeThingWithChannel(channnelID, acceptedItemType);
383 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad5Value);
387 public void assertChannelCpuLoad15IsUpdated() {
388 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_15;
389 String acceptedItemType = "Number";
391 DecimalType mockedCpuLoad15Value = new DecimalType(15.15);
392 when(mockedSystemInfo.getCpuLoad15()).thenReturn(mockedCpuLoad15Value);
394 initializeThingWithChannel(channnelID, acceptedItemType);
395 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad15Value);
399 public void assertChannelCpuThreadsIsUpdated() {
400 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_THREADS;
401 String acceptedItemType = "Number";
403 DecimalType mockedCpuThreadsValue = new DecimalType(16);
404 when(mockedSystemInfo.getCpuThreads()).thenReturn(mockedCpuThreadsValue);
406 initializeThingWithChannel(channnelID, acceptedItemType);
407 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuThreadsValue);
411 public void assertChannelCpuUptimeIsUpdated() {
412 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_UPTIME;
413 String acceptedItemType = "Number";
415 DecimalType mockedCpuUptimeValue = new DecimalType(100);
416 when(mockedSystemInfo.getCpuUptime()).thenReturn(mockedCpuUptimeValue);
418 initializeThingWithChannel(channnelID, acceptedItemType);
419 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuUptimeValue);
423 public void assertChannelCpuDescriptionIsUpdated() {
424 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_DESCRIPTION;
425 String acceptedItemType = "String";
427 StringType mockedCpuDescriptionValue = new StringType("Mocked Cpu Descr");
428 when(mockedSystemInfo.getCpuDescription()).thenReturn(mockedCpuDescriptionValue);
430 initializeThingWithChannel(channnelID, acceptedItemType);
431 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
432 mockedCpuDescriptionValue);
436 public void assertChannelCpuNameIsUpdated() {
437 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_NAME;
438 String acceptedItemType = "String";
440 StringType mockedCpuNameValue = new StringType("Mocked Cpu Name");
441 when(mockedSystemInfo.getCpuName()).thenReturn(mockedCpuNameValue);
443 initializeThingWithChannel(channnelID, acceptedItemType);
444 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuNameValue);
448 public void assertChannelMemoryAvailableIsUpdated() {
449 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE;
450 String acceptedItemType = "Number";
452 DecimalType mockedMemoryAvailableValue = new DecimalType(1000);
453 when(mockedSystemInfo.getMemoryAvailable()).thenReturn(mockedMemoryAvailableValue);
455 initializeThingWithChannel(channnelID, acceptedItemType);
456 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
457 mockedMemoryAvailableValue);
461 public void assertChannelMemoryUsedIsUpdated() {
462 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_USED;
463 String acceptedItemType = "Number";
465 DecimalType mockedMemoryUsedValue = new DecimalType(24);
466 when(mockedSystemInfo.getMemoryUsed()).thenReturn(mockedMemoryUsedValue);
468 initializeThingWithChannel(channnelID, acceptedItemType);
469 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedMemoryUsedValue);
473 public void assertChannelMemoryTotalIsUpdated() {
474 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_TOTAL;
475 String acceptedItemType = "Number";
477 DecimalType mockedMemoryTotalValue = new DecimalType(1024);
478 when(mockedSystemInfo.getMemoryTotal()).thenReturn(mockedMemoryTotalValue);
480 initializeThingWithChannel(channnelID, acceptedItemType);
481 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
482 mockedMemoryTotalValue);
486 public void assertChannelMemoryAvailablePercentIsUpdated() {
487 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE_PERCENT;
488 String acceptedItemType = "Number";
490 DecimalType mockedMemoryAvailablePercentValue = new DecimalType(97);
491 when(mockedSystemInfo.getMemoryAvailablePercent()).thenReturn(mockedMemoryAvailablePercentValue);
493 initializeThingWithChannel(channnelID, acceptedItemType);
494 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
495 mockedMemoryAvailablePercentValue);
499 public void assertChannelSwapAvailableIsUpdated() {
500 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE;
501 String acceptedItemType = "Number";
503 DecimalType mockedSwapAvailableValue = new DecimalType(482);
504 when(mockedSystemInfo.getSwapAvailable()).thenReturn(mockedSwapAvailableValue);
506 initializeThingWithChannel(channnelID, acceptedItemType);
507 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
508 mockedSwapAvailableValue);
512 public void assertChannelSwapUsedIsUpdated() {
513 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_USED;
514 String acceptedItemType = "Number";
516 DecimalType mockedSwapUsedValue = new DecimalType(30);
517 when(mockedSystemInfo.getSwapUsed()).thenReturn(mockedSwapUsedValue);
519 initializeThingWithChannel(channnelID, acceptedItemType);
520 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapUsedValue);
524 public void assertChannelSwapTotalIsUpdated() {
525 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_TOTAL;
526 String acceptedItemType = "Number";
528 DecimalType mockedSwapTotalValue = new DecimalType(512);
529 when(mockedSystemInfo.getSwapTotal()).thenReturn(mockedSwapTotalValue);
531 initializeThingWithChannel(channnelID, acceptedItemType);
532 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapTotalValue);
536 public void assertChannelSwapAvailablePercentIsUpdated() {
537 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE_PERCENT;
538 String acceptedItemType = "Number";
540 DecimalType mockedSwapAvailablePercentValue = new DecimalType(94);
541 when(mockedSystemInfo.getSwapAvailablePercent()).thenReturn(mockedSwapAvailablePercentValue);
543 initializeThingWithChannel(channnelID, acceptedItemType);
544 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
545 mockedSwapAvailablePercentValue);
549 public void assertChannelStorageNameIsUpdated() throws DeviceNotFoundException {
550 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_NAME;
551 String acceptedItemType = "String";
553 StringType mockedStorageName = new StringType("Mocked Storage Name");
554 when(mockedSystemInfo.getStorageName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageName);
556 initializeThingWithChannel(channnelID, acceptedItemType);
557 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageName);
561 public void assertChannelStorageTypeIsUpdated() throws DeviceNotFoundException {
562 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TYPE;
563 String acceptedItemType = "String";
565 StringType mockedStorageType = new StringType("Mocked Storage Type");
566 when(mockedSystemInfo.getStorageType(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageType);
568 initializeThingWithChannel(channnelID, acceptedItemType);
569 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageType);
573 public void assertChannelStorageDescriptionIsUpdated() throws DeviceNotFoundException {
574 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_DESCRIPTION;
575 String acceptedItemType = "String";
577 StringType mockedStorageDescription = new StringType("Mocked Storage Description");
578 when(mockedSystemInfo.getStorageDescription(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageDescription);
580 initializeThingWithChannel(channnelID, acceptedItemType);
581 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
582 mockedStorageDescription);
586 public void assertChannelStorageAvailableIsUpdated() throws DeviceNotFoundException {
587 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE;
588 String acceptedItemType = "Number";
590 DecimalType mockedStorageAvailableValue = new DecimalType(2000);
591 when(mockedSystemInfo.getStorageAvailable(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageAvailableValue);
593 initializeThingWithChannel(channnelID, acceptedItemType);
594 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
595 mockedStorageAvailableValue);
599 public void assertChannelStorageUsedIsUpdated() throws DeviceNotFoundException {
600 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_USED;
601 String acceptedItemType = "Number";
603 DecimalType mockedStorageUsedValue = new DecimalType(500);
604 when(mockedSystemInfo.getStorageUsed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageUsedValue);
606 initializeThingWithChannel(channnelID, acceptedItemType);
607 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
608 mockedStorageUsedValue);
612 public void assertChannelStorageTotalIsUpdated() throws DeviceNotFoundException {
613 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TOTAL;
614 String acceptedItemType = "Number";
616 DecimalType mockedStorageTotalValue = new DecimalType(2500);
617 when(mockedSystemInfo.getStorageTotal(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageTotalValue);
619 initializeThingWithChannel(channnelID, acceptedItemType);
620 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
621 mockedStorageTotalValue);
625 public void assertChannelStorageAvailablePercentIsUpdated() throws DeviceNotFoundException {
626 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE_PERCENT;
627 String acceptedItemType = "Number";
629 DecimalType mockedStorageAvailablePercent = new DecimalType(20);
630 when(mockedSystemInfo.getStorageAvailablePercent(DEFAULT_DEVICE_INDEX))
631 .thenReturn(mockedStorageAvailablePercent);
633 initializeThingWithChannel(channnelID, acceptedItemType);
634 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
635 mockedStorageAvailablePercent);
639 public void assertChannelDriveNameIsUpdated() throws DeviceNotFoundException {
640 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_NAME;
641 String acceptedItemType = "String";
643 StringType mockedDriveNameValue = new StringType("Mocked Drive Name");
644 when(mockedSystemInfo.getDriveName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveNameValue);
646 initializeThingWithChannel(channelID, acceptedItemType);
647 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveNameValue);
651 public void assertChannelDriveModelIsUpdated() throws DeviceNotFoundException {
652 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_MODEL;
653 String acceptedItemType = "String";
655 StringType mockedDriveModelValue = new StringType("Mocked Drive Model");
656 when(mockedSystemInfo.getDriveModel(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveModelValue);
658 initializeThingWithChannel(channelID, acceptedItemType);
659 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveModelValue);
663 public void assertChannelDriveSerialIsUpdated() throws DeviceNotFoundException {
664 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_SERIAL;
665 String acceptedItemType = "String";
667 StringType mockedDriveSerialNumber = new StringType("Mocked Drive Serial Number");
668 when(mockedSystemInfo.getDriveSerialNumber(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveSerialNumber);
670 initializeThingWithChannel(channelID, acceptedItemType);
671 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
672 mockedDriveSerialNumber);
676 // There is a bug opened for this issue - https://github.com/dblock/oshi/issues/185
678 public void assertChannelSensorsCpuTempIsUpdated() {
679 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_CPU_TEMPERATURE;
680 String acceptedItemType = "Number";
682 DecimalType mockedSensorsCpuTemperatureValue = new DecimalType(60);
683 when(mockedSystemInfo.getSensorsCpuTemperature()).thenReturn(mockedSensorsCpuTemperatureValue);
685 initializeThingWithChannel(channnelID, acceptedItemType);
686 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
687 mockedSensorsCpuTemperatureValue);
691 public void assertChannelSensorsCpuVoltageIsUpdated() {
692 String channnelID = SysteminfoBindingConstants.CHANNEL_SENOSRS_CPU_VOLTAGE;
693 String acceptedItemType = "Number";
695 DecimalType mockedSensorsCpuVoltageValue = new DecimalType(1000);
696 when(mockedSystemInfo.getSensorsCpuVoltage()).thenReturn(mockedSensorsCpuVoltageValue);
698 initializeThingWithChannel(channnelID, acceptedItemType);
699 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
700 mockedSensorsCpuVoltageValue);
704 public void assertChannelSensorsFanSpeedIsUpdated() throws DeviceNotFoundException {
705 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_FAN_SPEED;
706 String acceptedItemType = "Number";
708 DecimalType mockedSensorsCpuFanSpeedValue = new DecimalType(180);
709 when(mockedSystemInfo.getSensorsFanSpeed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedSensorsCpuFanSpeedValue);
711 initializeThingWithChannel(channnelID, acceptedItemType);
712 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
713 mockedSensorsCpuFanSpeedValue);
717 public void assertChannelBatteryNameIsUpdated() throws DeviceNotFoundException {
718 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_NAME;
719 String acceptedItemType = "String";
721 StringType mockedBatteryName = new StringType("Mocked Battery Name");
722 when(mockedSystemInfo.getBatteryName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryName);
724 initializeThingWithChannel(channnelID, acceptedItemType);
725 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedBatteryName);
729 public void assertChannelBatteryRemainingCapacityIsUpdated() throws DeviceNotFoundException {
730 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_CAPACITY;
731 String acceptedItemType = "Number";
733 DecimalType mockedBatteryRemainingCapacity = new DecimalType(200);
734 when(mockedSystemInfo.getBatteryRemainingCapacity(DEFAULT_DEVICE_INDEX))
735 .thenReturn(mockedBatteryRemainingCapacity);
737 initializeThingWithChannel(channnelID, acceptedItemType);
738 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
739 mockedBatteryRemainingCapacity);
743 public void assertChannelBatteryRemainingTimeIsUpdated() throws DeviceNotFoundException {
744 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_TIME;
745 String acceptedItemType = "Number";
747 DecimalType mockedBatteryRemainingTime = new DecimalType(3600);
748 when(mockedSystemInfo.getBatteryRemainingTime(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryRemainingTime);
750 initializeThingWithChannel(channnelID, acceptedItemType);
751 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
752 mockedBatteryRemainingTime);
756 public void assertChannelDisplayInformationIsUpdated() throws DeviceNotFoundException {
757 String channnelID = SysteminfoBindingConstants.CHANNEL_DISPLAY_INFORMATION;
758 String acceptedItemType = "String";
760 StringType mockedDisplayInfo = new StringType("Mocked Display Information");
761 when(mockedSystemInfo.getDisplayInformation(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDisplayInfo);
763 initializeThingWithChannel(channnelID, acceptedItemType);
764 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDisplayInfo);
768 public void assertChannelNetworkIpIsUpdated() throws DeviceNotFoundException {
769 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_IP;
770 String acceptedItemType = "String";
772 StringType mockedNetworkIp = new StringType("192.168.1.0");
773 when(mockedSystemInfo.getNetworkIp(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkIp);
775 initializeThingWithChannel(channnelID, acceptedItemType);
776 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkIp);
780 public void assertChannelNetworkMacIsUpdated() throws DeviceNotFoundException {
781 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_MAC;
782 String acceptedItemType = "String";
784 StringType mockedNetworkMacValue = new StringType("AB-10-11-12-13-14");
785 when(mockedSystemInfo.getNetworkMac(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkMacValue);
787 initializeThingWithChannel(channnelID, acceptedItemType);
788 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkMacValue);
792 public void assertChannelNetworkDataSentIsUpdated() throws DeviceNotFoundException {
793 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_SENT;
794 String acceptedItemType = "Number";
796 DecimalType mockedNetworkDataSent = new DecimalType(1000);
797 when(mockedSystemInfo.getNetworkDataSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataSent);
799 initializeThingWithChannel(channnelID, acceptedItemType);
800 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkDataSent);
804 public void assertChannelNetworkDataReceivedIsUpdated() throws DeviceNotFoundException {
805 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_RECEIVED;
806 String acceptedItemType = "Number";
808 DecimalType mockedNetworkDataReceiveed = new DecimalType(800);
809 when(mockedSystemInfo.getNetworkDataReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataReceiveed);
811 initializeThingWithChannel(channnelID, acceptedItemType);
812 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
813 mockedNetworkDataReceiveed);
817 public void assertChannelNetworkPacketsSentIsUpdated() throws DeviceNotFoundException {
818 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_SENT;
819 String acceptedItemType = "Number";
821 DecimalType mockedNetworkPacketsSent = new DecimalType(50);
822 when(mockedSystemInfo.getNetworkPacketsSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsSent);
824 initializeThingWithChannel(channnelID, acceptedItemType);
825 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
826 mockedNetworkPacketsSent);
830 public void assertChannelNetworkPacketsReceivedIsUpdated() throws DeviceNotFoundException {
831 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_RECEIVED;
832 String acceptedItemType = "Number";
834 DecimalType mockedNetworkPacketsReceived = new DecimalType(48);
835 when(mockedSystemInfo.getNetworkPacketsReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsReceived);
837 initializeThingWithChannel(channnelID, acceptedItemType);
838 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
839 mockedNetworkPacketsReceived);
843 public void assertChannelNetworkNetworkNameIsUpdated() throws DeviceNotFoundException {
844 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_NAME;
845 String acceptedItemType = "String";
847 StringType mockedNetworkName = new StringType("MockN-AQ34");
848 when(mockedSystemInfo.getNetworkName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkName);
850 initializeThingWithChannel(channnelID, acceptedItemType);
851 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkName);
855 public void assertChannelNetworkNetworkDisplayNameIsUpdated() throws DeviceNotFoundException {
856 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_ADAPTER_NAME;
857 String acceptedItemType = "String";
859 StringType mockedNetworkAdapterName = new StringType("Mocked Network Adapter Name");
860 when(mockedSystemInfo.getNetworkDisplayName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkAdapterName);
862 initializeThingWithChannel(channnelID, acceptedItemType);
863 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
864 mockedNetworkAdapterName);
867 class SysteminfoDiscoveryServiceMock extends SysteminfoDiscoveryService {
870 SysteminfoDiscoveryServiceMock(String hostname) {
872 this.hostname = hostname;
876 protected String getHostName() throws UnknownHostException {
877 if (hostname.equals("unresolved")) {
878 throw new UnknownHostException();
884 public void startScan() {
890 public void testDiscoveryWithInvalidHostname() {
891 String hostname = "Hilo.fritz.box";
892 String expectedHostname = "Hilo_fritz_box";
894 testDiscoveryService(expectedHostname, hostname);
898 public void testDiscoveryWithValidHostname() {
899 String hostname = "MyComputer";
900 String expectedHostname = "MyComputer";
902 testDiscoveryService(expectedHostname, hostname);
906 public void testDiscoveryWithUnresolvedHostname() {
907 String hostname = "unresolved";
908 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
910 testDiscoveryService(expectedHostname, hostname);
914 public void testDiscoveryWithEmptyHostnameString() {
915 String hostname = "";
916 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
918 testDiscoveryService(expectedHostname, hostname);
921 private void testDiscoveryService(String expectedHostname, String hostname) {
922 SysteminfoDiscoveryService discoveryService = getService(DiscoveryService.class,
923 SysteminfoDiscoveryService.class);
924 waitForAssert(() -> {
925 assertThat(discoveryService, is(notNullValue()));
927 SysteminfoDiscoveryServiceMock discoveryServiceMock = new SysteminfoDiscoveryServiceMock(hostname);
928 if (discoveryService != null) {
929 unregisterService(DiscoveryService.class);
931 registerService(discoveryServiceMock, DiscoveryService.class.getName(), new Hashtable<>());
933 ThingTypeUID computerType = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
934 ThingUID computerUID = new ThingUID(computerType, expectedHostname);
936 discoveryServiceMock.startScan();
938 Inbox inbox = getService(Inbox.class);
939 assertThat(inbox, is(notNullValue()));
941 waitForAssert(() -> {
942 List<DiscoveryResult> results = inbox.stream().filter(InboxPredicates.forThingUID(computerUID))
944 assertFalse(results.isEmpty(), "No Thing with UID " + computerUID.getAsString() + " in inbox");
947 inbox.approve(computerUID, SysteminfoDiscoveryService.DEFAULT_THING_LABEL, null);
949 waitForAssert(() -> {
950 systemInfoThing = thingRegistry.get(computerUID);
951 assertThat(systemInfoThing, is(notNullValue()));
954 waitForAssert(() -> {
955 assertThat("Thing is not initialized.", systemInfoThing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
960 public void assertChannelProcessThreadsIsUpdatedWithPIDse() throws DeviceNotFoundException {
961 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_THREADS;
962 String acceptedItemType = "Number";
963 // The pid of the System idle process in Windows
966 DecimalType mockedProcessThreadsCount = new DecimalType(4);
967 when(mockedSystemInfo.getProcessThreads(pid)).thenReturn(mockedProcessThreadsCount);
969 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
970 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
971 mockedProcessThreadsCount);
975 public void assertChannelProcessPathIsUpdatedWithPIDset() throws DeviceNotFoundException {
976 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_PATH;
977 String acceptedItemType = "String";
978 // The pid of the System idle process in Windows
981 StringType mockedProcessPath = new StringType("C:\\Users\\MockedUser\\Process");
982 when(mockedSystemInfo.getProcessPath(pid)).thenReturn(mockedProcessPath);
984 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
985 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessPath);
989 public void assertChannelProcessNameIsUpdatedWithPIDset() throws DeviceNotFoundException {
990 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_NAME;
991 String acceptedItemType = "String";
992 // The pid of the System idle process in Windows
995 StringType mockedProcessName = new StringType("MockedProcess.exe");
996 when(mockedSystemInfo.getProcessName(pid)).thenReturn(mockedProcessName);
998 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
999 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessName);
1003 public void assertChannelProcessMemoryIsUpdatedWithPIDset() throws DeviceNotFoundException {
1004 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_MEMORY;
1005 String acceptedItemType = "Number";
1006 // The pid of the System idle process in Windows
1009 DecimalType mockedProcessMemory = new DecimalType(450);
1010 when(mockedSystemInfo.getProcessMemoryUsage(pid)).thenReturn(mockedProcessMemory);
1012 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1013 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessMemory);
1017 public void assertChannelProcessLoadIsUpdatedWithPIDset() throws DeviceNotFoundException {
1018 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_LOAD;
1019 String acceptedItemType = "Number";
1020 // The pid of the System idle process in Windows
1023 PercentType mockedProcessLoad = new PercentType(3);
1024 when(mockedSystemInfo.getProcessCpuUsage(pid)).thenReturn(mockedProcessLoad);
1026 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1027 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessLoad);
1031 public void testThingHandlesChannelPriorityChange() {
1032 String priorityKey = "priority";
1033 String pidKey = "pid";
1034 String initialPriority = DEFAULT_CHANNEL_TEST_PRIORITY; // Evaluates to High
1035 String newPriority = "Low";
1037 String acceptedItemType = "Number";
1038 initializeThingWithChannel(DEFAULT_TEST_CHANNEL_ID, acceptedItemType);
1040 Channel channel = systemInfoThing.getChannel(DEFAULT_TEST_CHANNEL_ID);
1041 if (channel == null) {
1042 throw new AssertionError("Channel '" + DEFAULT_TEST_CHANNEL_ID + "' is null");
1045 waitForAssert(() -> {
1046 assertThat("The initial priority of channel " + channel.getUID() + " is not as expected.",
1047 channel.getConfiguration().get(priorityKey), is(equalTo(initialPriority)));
1048 assertThat(systemInfoHandler.getHighPriorityChannels().contains(channel.getUID()), is(true));
1051 // Change the priority of a channel, keep the pid
1052 Configuration updatedConfig = new Configuration();
1053 updatedConfig.put(priorityKey, newPriority);
1054 updatedConfig.put(pidKey, channel.getConfiguration().get(pidKey));
1055 Channel updatedChannel = ChannelBuilder.create(channel.getUID(), channel.getAcceptedItemType())
1056 .withType(channel.getChannelTypeUID()).withKind(channel.getKind()).withConfiguration(updatedConfig)
1059 Thing updatedThing = ThingBuilder.create(systemInfoThing.getThingTypeUID(), systemInfoThing.getUID())
1060 .withConfiguration(systemInfoThing.getConfiguration()).withChannel(updatedChannel).build();
1062 systemInfoHandler.thingUpdated(updatedThing);
1064 waitForAssert(() -> {
1065 assertThat("The prority of the channel was not updated: ", channel.getConfiguration().get(priorityKey),
1066 is(equalTo(newPriority)));
1067 assertThat(systemInfoHandler.getLowPriorityChannels().contains(channel.getUID()), is(true));