2 * Copyright (c) 2010-2021 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.StringType;
49 import org.openhab.core.test.java.JavaOSGiTest;
50 import org.openhab.core.test.storage.VolatileStorageService;
51 import org.openhab.core.thing.Channel;
52 import org.openhab.core.thing.ChannelUID;
53 import org.openhab.core.thing.ManagedThingProvider;
54 import org.openhab.core.thing.Thing;
55 import org.openhab.core.thing.ThingProvider;
56 import org.openhab.core.thing.ThingRegistry;
57 import org.openhab.core.thing.ThingStatus;
58 import org.openhab.core.thing.ThingStatusDetail;
59 import org.openhab.core.thing.ThingTypeUID;
60 import org.openhab.core.thing.ThingUID;
61 import org.openhab.core.thing.binding.ThingHandler;
62 import org.openhab.core.thing.binding.ThingHandlerFactory;
63 import org.openhab.core.thing.binding.builder.ChannelBuilder;
64 import org.openhab.core.thing.binding.builder.ThingBuilder;
65 import org.openhab.core.thing.link.ItemChannelLink;
66 import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
67 import org.openhab.core.thing.type.ChannelKind;
68 import org.openhab.core.thing.type.ChannelTypeUID;
69 import org.openhab.core.types.State;
70 import org.openhab.core.types.UnDefType;
73 * OSGi tests for the {@link SysteminfoHandler}
75 * @author Svilen Valkanov - Initial contribution
76 * @author Lyubomir Papazov - Created a mock systeminfo object. This way, access to the user's OS will not be required,
77 * but mock data will be used instead, avoiding potential errors from the OS queries.
78 * @author Wouter Born - Migrate Groovy to Java tests
80 public class SysteminfoOSGiTest extends JavaOSGiTest {
81 private static final String DEFAULT_TEST_THING_NAME = "work";
82 private static final String DEFAULT_TEST_ITEM_NAME = "test";
83 private static final String DEFAULT_CHANNEL_TEST_PRIORITY = "High";
84 private static final int DEFAULT_CHANNEL_PID = -1;
85 private static final String DEFAULT_TEST_CHANNEL_ID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
86 private static final int DEFAULT_DEVICE_INDEX = 0;
89 * Refresh time in seconds for tasks with priority High.
90 * Default value for the parameter interval_high in the thing configuration
92 private static final int DEFAULT_TEST_INTERVAL_HIGH = 1;
95 * Refresh time in seconds for tasks with priority Medium.
97 private static final int DEFAULT_TEST_INTERVAL_MEDIUM = 3;
99 private Thing systemInfoThing;
100 private SysteminfoHandler systemInfoHandler;
101 private GenericItem testItem;
103 private SysteminfoInterface mockedSystemInfo;
104 private ManagedThingProvider managedThingProvider;
105 private ThingRegistry thingRegistry;
106 private ItemRegistry itemRegistry;
107 private SysteminfoHandlerFactory systeminfoHandlerFactory;
110 public void setUp() {
111 VolatileStorageService volatileStorageService = new VolatileStorageService();
112 registerService(volatileStorageService);
114 // Preparing the mock with OS properties, that are used in the initialize method of SysteminfoHandler
115 mockedSystemInfo = mock(SysteminfoInterface.class);
116 when(mockedSystemInfo.getCpuLogicalCores()).thenReturn(new DecimalType(2));
117 when(mockedSystemInfo.getCpuPhysicalCores()).thenReturn(new DecimalType(2));
118 when(mockedSystemInfo.getOsFamily()).thenReturn(new StringType("Mock OS"));
119 when(mockedSystemInfo.getOsManufacturer()).thenReturn(new StringType("Mock OS Manufacturer"));
120 when(mockedSystemInfo.getOsVersion()).thenReturn(new StringType("Mock Os Version"));
122 systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
123 SysteminfoInterface oshiSystemInfo = getService(SysteminfoInterface.class);
125 // Unbind oshiSystemInfo service and bind the mock service to make the systeminfobinding tests independent of
126 // the external OSHI library
127 if (oshiSystemInfo != null) {
128 systeminfoHandlerFactory.unbindSystemInfo(oshiSystemInfo);
130 systeminfoHandlerFactory.bindSystemInfo(mockedSystemInfo);
132 managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
133 assertThat(managedThingProvider, is(notNullValue()));
135 thingRegistry = getService(ThingRegistry.class);
136 assertThat(thingRegistry, is(notNullValue()));
138 itemRegistry = getService(ItemRegistry.class);
139 assertThat(itemRegistry, is(notNullValue()));
143 public void tearDown() {
144 if (systemInfoThing != null) {
145 // Remove the systeminfo thing. The handler will be also disposed automatically
146 Thing removedThing = thingRegistry.forceRemove(systemInfoThing.getUID());
147 assertThat("The systeminfo thing cannot be deleted", removedThing, is(notNullValue()));
149 waitForAssert(() -> {
150 ThingHandler systemInfoHandler = systemInfoThing.getHandler();
151 assertThat(systemInfoHandler, is(nullValue()));
154 if (testItem != null) {
155 itemRegistry.remove(DEFAULT_TEST_ITEM_NAME);
159 private void initializeThingWithChannelAndPID(String channelID, String acceptedItemType, int pid) {
160 Configuration thingConfig = new Configuration();
161 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
162 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
163 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
164 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
165 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
167 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
170 private void initializeThingWithChannelAndPriority(String channelID, String acceptedItemType, String priority) {
171 Configuration thingConfig = new Configuration();
172 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
173 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
174 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
175 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
176 int pid = DEFAULT_CHANNEL_PID;
178 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
181 private void initializeThingWithConfiguration(Configuration config) {
182 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
183 String channelID = DEFAULT_TEST_CHANNEL_ID;
184 String acceptedItemType = "String";
185 int pid = DEFAULT_CHANNEL_PID;
187 initializeThing(config, channelID, acceptedItemType, priority, pid);
190 private void initializeThingWithChannel(String channelID, String acceptedItemType) {
191 Configuration thingConfig = new Configuration();
192 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
193 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
194 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
195 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
197 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
198 int pid = DEFAULT_CHANNEL_PID;
199 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
202 private void initializeThing(Configuration thingConfiguration, String channelID, String acceptedItemType,
203 String priority, int pid) {
204 ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
205 ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
207 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
208 ChannelTypeUID channelTypeUID = new ChannelTypeUID(SysteminfoBindingConstants.BINDING_ID,
209 channelUID.getIdWithoutGroup());
210 Configuration channelConfig = new Configuration();
211 channelConfig.put("priority", priority);
212 channelConfig.put("pid", new BigDecimal(pid));
213 Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
214 .withKind(ChannelKind.STATE).withConfiguration(channelConfig).build();
216 systemInfoThing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(thingConfiguration)
217 .withChannel(channel).build();
219 managedThingProvider.add(systemInfoThing);
221 waitForAssert(() -> {
222 systemInfoHandler = (SysteminfoHandler) systemInfoThing.getHandler();
223 assertThat(systemInfoHandler, is(notNullValue()));
226 waitForAssert(() -> {
227 assertThat("Thing is not initilized, before an Item is created", systemInfoThing.getStatus(),
228 anyOf(equalTo(ThingStatus.OFFLINE), equalTo(ThingStatus.ONLINE)));
231 intializeItem(channelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
234 private void assertItemState(String acceptedItemType, String itemName, String priority, State expectedState) {
235 waitForAssert(() -> {
236 ThingStatusDetail thingStatusDetail = systemInfoThing.getStatusInfo().getStatusDetail();
237 String description = systemInfoThing.getStatusInfo().getDescription();
238 assertThat("Thing status detail is " + thingStatusDetail + " with description " + description,
239 systemInfoThing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
241 // The binding starts all refresh tasks in SysteminfoHandler.scheduleUpdates() after this delay !
243 sleep(SysteminfoHandler.WAIT_TIME_CHANNEL_ITEM_LINK_INIT * 1000);
244 } catch (InterruptedException e) {
245 throw new AssertionError("Interrupted while sleeping");
250 item = (GenericItem) itemRegistry.getItem(itemName);
251 } catch (ItemNotFoundException e) {
252 throw new AssertionError("Item not found in registry");
256 if (priority.equals("High")) {
257 waitTime = DEFAULT_TEST_INTERVAL_HIGH * 1000;
258 } else if (priority.equals("Medium")) {
259 waitTime = DEFAULT_TEST_INTERVAL_MEDIUM * 1000;
264 waitForAssert(() -> {
265 State itemState = item.getState();
266 assertThat(itemState, is(equalTo(expectedState)));
267 }, waitTime, DFL_SLEEP_TIME);
270 private void intializeItem(ChannelUID channelUID, String itemName, String acceptedItemType) {
271 if (acceptedItemType.equals("Number")) {
272 testItem = new NumberItem(itemName);
273 } else if (acceptedItemType.equals("String")) {
274 testItem = new StringItem(itemName);
276 itemRegistry.add(testItem);
278 ManagedItemChannelLinkProvider itemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
279 assertThat(itemChannelLinkProvider, is(notNullValue()));
281 itemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
285 public void assertInvalidThingConfigurationValuesAreHandled() {
286 Configuration configuration = new Configuration();
288 // invalid value - must be positive
289 int refreshIntervalHigh = -5;
290 configuration.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME, new BigDecimal(refreshIntervalHigh));
292 int refreshIntervalMedium = 3;
293 configuration.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
294 new BigDecimal(refreshIntervalMedium));
295 initializeThingWithConfiguration(configuration);
297 testInvalidConfiguration();
300 private void testInvalidConfiguration() {
301 waitForAssert(() -> {
302 assertThat("Invalid configuratuin is used !", systemInfoThing.getStatus(),
303 is(equalTo(ThingStatus.OFFLINE)));
304 assertThat(systemInfoThing.getStatusInfo().getStatusDetail(),
305 is(equalTo(ThingStatusDetail.HANDLER_INITIALIZING_ERROR)));
306 assertThat(systemInfoThing.getStatusInfo().getDescription(), is(equalTo("Thing cannot be initialized!")));
311 public void assertThingStatusIsUninitializedWhenThereIsNoSysteminfoServiceProvided() {
312 // Unbind the mock service to verify the systeminfo thing will not be initialized when no systeminfo service is
314 systeminfoHandlerFactory.unbindSystemInfo(mockedSystemInfo);
316 ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
317 ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
319 systemInfoThing = ThingBuilder.create(thingTypeUID, thingUID).build();
320 managedThingProvider.add(systemInfoThing);
322 waitForAssert(() -> {
323 assertThat("The thing status is uninitialized when systeminfo service is missing",
324 systemInfoThing.getStatus(), equalTo(ThingStatus.UNINITIALIZED));
329 public void assertMediumPriorityChannelIsUpdated() {
330 String channnelID = DEFAULT_TEST_CHANNEL_ID;
331 String acceptedItemType = "Number";
332 String priority = "Medium";
334 initializeThingWithChannelAndPriority(channnelID, acceptedItemType, priority);
335 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, priority, UnDefType.UNDEF);
339 public void assertStateOfSecondDeviceIsUpdated() {
340 // This test assumes that at least 2 network interfaces are present on the test platform
342 String channnelID = "network" + deviceIndex + "#mac";
343 String acceptedItemType = "String";
345 initializeThingWithChannel(channnelID, acceptedItemType);
346 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, UnDefType.UNDEF);
350 public void assertChannelCpuLoad1IsUpdated() {
351 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_1;
352 String acceptedItemType = "Number";
354 DecimalType mockedCpuLoad1Value = new DecimalType(1.1);
355 when(mockedSystemInfo.getCpuLoad1()).thenReturn(mockedCpuLoad1Value);
357 initializeThingWithChannel(channnelID, acceptedItemType);
358 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad1Value);
362 public void assertChannelCpuLoad5IsUpdated() {
363 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_5;
364 String acceptedItemType = "Number";
366 DecimalType mockedCpuLoad5Value = new DecimalType(5.5);
367 when(mockedSystemInfo.getCpuLoad5()).thenReturn(mockedCpuLoad5Value);
369 initializeThingWithChannel(channnelID, acceptedItemType);
370 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad5Value);
374 public void assertChannelCpuLoad15IsUpdated() {
375 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_15;
376 String acceptedItemType = "Number";
378 DecimalType mockedCpuLoad15Value = new DecimalType(15.15);
379 when(mockedSystemInfo.getCpuLoad15()).thenReturn(mockedCpuLoad15Value);
381 initializeThingWithChannel(channnelID, acceptedItemType);
382 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad15Value);
386 public void assertChannelCpuThreadsIsUpdated() {
387 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_THREADS;
388 String acceptedItemType = "Number";
390 DecimalType mockedCpuThreadsValue = new DecimalType(16);
391 when(mockedSystemInfo.getCpuThreads()).thenReturn(mockedCpuThreadsValue);
393 initializeThingWithChannel(channnelID, acceptedItemType);
394 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuThreadsValue);
398 public void assertChannelCpuUptimeIsUpdated() {
399 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_UPTIME;
400 String acceptedItemType = "Number";
402 DecimalType mockedCpuUptimeValue = new DecimalType(100);
403 when(mockedSystemInfo.getCpuUptime()).thenReturn(mockedCpuUptimeValue);
405 initializeThingWithChannel(channnelID, acceptedItemType);
406 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuUptimeValue);
410 public void assertChannelCpuDescriptionIsUpdated() {
411 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_DESCRIPTION;
412 String acceptedItemType = "String";
414 StringType mockedCpuDescriptionValue = new StringType("Mocked Cpu Descr");
415 when(mockedSystemInfo.getCpuDescription()).thenReturn(mockedCpuDescriptionValue);
417 initializeThingWithChannel(channnelID, acceptedItemType);
418 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
419 mockedCpuDescriptionValue);
423 public void assertChannelCpuNameIsUpdated() {
424 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_NAME;
425 String acceptedItemType = "String";
427 StringType mockedCpuNameValue = new StringType("Mocked Cpu Name");
428 when(mockedSystemInfo.getCpuName()).thenReturn(mockedCpuNameValue);
430 initializeThingWithChannel(channnelID, acceptedItemType);
431 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuNameValue);
435 public void assertChannelMemoryAvailableIsUpdated() {
436 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE;
437 String acceptedItemType = "Number";
439 DecimalType mockedMemoryAvailableValue = new DecimalType(1000);
440 when(mockedSystemInfo.getMemoryAvailable()).thenReturn(mockedMemoryAvailableValue);
442 initializeThingWithChannel(channnelID, acceptedItemType);
443 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
444 mockedMemoryAvailableValue);
448 public void assertChannelMemoryUsedIsUpdated() {
449 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_USED;
450 String acceptedItemType = "Number";
452 DecimalType mockedMemoryUsedValue = new DecimalType(24);
453 when(mockedSystemInfo.getMemoryUsed()).thenReturn(mockedMemoryUsedValue);
455 initializeThingWithChannel(channnelID, acceptedItemType);
456 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedMemoryUsedValue);
460 public void assertChannelMemoryTotalIsUpdated() {
461 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_TOTAL;
462 String acceptedItemType = "Number";
464 DecimalType mockedMemoryTotalValue = new DecimalType(1024);
465 when(mockedSystemInfo.getMemoryTotal()).thenReturn(mockedMemoryTotalValue);
467 initializeThingWithChannel(channnelID, acceptedItemType);
468 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
469 mockedMemoryTotalValue);
473 public void assertChannelMemoryAvailablePercentIsUpdated() {
474 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE_PERCENT;
475 String acceptedItemType = "Number";
477 DecimalType mockedMemoryAvailablePercentValue = new DecimalType(97);
478 when(mockedSystemInfo.getMemoryAvailablePercent()).thenReturn(mockedMemoryAvailablePercentValue);
480 initializeThingWithChannel(channnelID, acceptedItemType);
481 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
482 mockedMemoryAvailablePercentValue);
486 public void assertChannelSwapAvailableIsUpdated() {
487 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE;
488 String acceptedItemType = "Number";
490 DecimalType mockedSwapAvailableValue = new DecimalType(482);
491 when(mockedSystemInfo.getSwapAvailable()).thenReturn(mockedSwapAvailableValue);
493 initializeThingWithChannel(channnelID, acceptedItemType);
494 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
495 mockedSwapAvailableValue);
499 public void assertChannelSwapUsedIsUpdated() {
500 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_USED;
501 String acceptedItemType = "Number";
503 DecimalType mockedSwapUsedValue = new DecimalType(30);
504 when(mockedSystemInfo.getSwapUsed()).thenReturn(mockedSwapUsedValue);
506 initializeThingWithChannel(channnelID, acceptedItemType);
507 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapUsedValue);
511 public void assertChannelSwapTotalIsUpdated() {
512 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_TOTAL;
513 String acceptedItemType = "Number";
515 DecimalType mockedSwapTotalValue = new DecimalType(512);
516 when(mockedSystemInfo.getSwapTotal()).thenReturn(mockedSwapTotalValue);
518 initializeThingWithChannel(channnelID, acceptedItemType);
519 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapTotalValue);
523 public void assertChannelSwapAvailablePercentIsUpdated() {
524 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE_PERCENT;
525 String acceptedItemType = "Number";
527 DecimalType mockedSwapAvailablePercentValue = new DecimalType(94);
528 when(mockedSystemInfo.getSwapAvailablePercent()).thenReturn(mockedSwapAvailablePercentValue);
530 initializeThingWithChannel(channnelID, acceptedItemType);
531 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
532 mockedSwapAvailablePercentValue);
536 public void assertChannelStorageNameIsUpdated() throws DeviceNotFoundException {
537 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_NAME;
538 String acceptedItemType = "String";
540 StringType mockedStorageName = new StringType("Mocked Storage Name");
541 when(mockedSystemInfo.getStorageName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageName);
543 initializeThingWithChannel(channnelID, acceptedItemType);
544 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageName);
548 public void assertChannelStorageTypeIsUpdated() throws DeviceNotFoundException {
549 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TYPE;
550 String acceptedItemType = "String";
552 StringType mockedStorageType = new StringType("Mocked Storage Type");
553 when(mockedSystemInfo.getStorageType(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageType);
555 initializeThingWithChannel(channnelID, acceptedItemType);
556 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageType);
560 public void assertChannelStorageDescriptionIsUpdated() throws DeviceNotFoundException {
561 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_DESCRIPTION;
562 String acceptedItemType = "String";
564 StringType mockedStorageDescription = new StringType("Mocked Storage Description");
565 when(mockedSystemInfo.getStorageDescription(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageDescription);
567 initializeThingWithChannel(channnelID, acceptedItemType);
568 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
569 mockedStorageDescription);
573 public void assertChannelStorageAvailableIsUpdated() throws DeviceNotFoundException {
574 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE;
575 String acceptedItemType = "Number";
577 DecimalType mockedStorageAvailableValue = new DecimalType(2000);
578 when(mockedSystemInfo.getStorageAvailable(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageAvailableValue);
580 initializeThingWithChannel(channnelID, acceptedItemType);
581 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
582 mockedStorageAvailableValue);
586 public void assertChannelStorageUsedIsUpdated() throws DeviceNotFoundException {
587 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_USED;
588 String acceptedItemType = "Number";
590 DecimalType mockedStorageUsedValue = new DecimalType(500);
591 when(mockedSystemInfo.getStorageUsed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageUsedValue);
593 initializeThingWithChannel(channnelID, acceptedItemType);
594 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
595 mockedStorageUsedValue);
599 public void assertChannelStorageTotalIsUpdated() throws DeviceNotFoundException {
600 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TOTAL;
601 String acceptedItemType = "Number";
603 DecimalType mockedStorageTotalValue = new DecimalType(2500);
604 when(mockedSystemInfo.getStorageTotal(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageTotalValue);
606 initializeThingWithChannel(channnelID, acceptedItemType);
607 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
608 mockedStorageTotalValue);
612 public void assertChannelStorageAvailablePercentIsUpdated() throws DeviceNotFoundException {
613 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE_PERCENT;
614 String acceptedItemType = "Number";
616 DecimalType mockedStorageAvailablePercent = new DecimalType(20);
617 when(mockedSystemInfo.getStorageAvailablePercent(DEFAULT_DEVICE_INDEX))
618 .thenReturn(mockedStorageAvailablePercent);
620 initializeThingWithChannel(channnelID, acceptedItemType);
621 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
622 mockedStorageAvailablePercent);
626 public void assertChannelDriveNameIsUpdated() throws DeviceNotFoundException {
627 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_NAME;
628 String acceptedItemType = "String";
630 StringType mockedDriveNameValue = new StringType("Mocked Drive Name");
631 when(mockedSystemInfo.getDriveName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveNameValue);
633 initializeThingWithChannel(channelID, acceptedItemType);
634 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveNameValue);
638 public void assertChannelDriveModelIsUpdated() throws DeviceNotFoundException {
639 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_MODEL;
640 String acceptedItemType = "String";
642 StringType mockedDriveModelValue = new StringType("Mocked Drive Model");
643 when(mockedSystemInfo.getDriveModel(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveModelValue);
645 initializeThingWithChannel(channelID, acceptedItemType);
646 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveModelValue);
650 public void assertChannelDriveSerialIsUpdated() throws DeviceNotFoundException {
651 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_SERIAL;
652 String acceptedItemType = "String";
654 StringType mockedDriveSerialNumber = new StringType("Mocked Drive Serial Number");
655 when(mockedSystemInfo.getDriveSerialNumber(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveSerialNumber);
657 initializeThingWithChannel(channelID, acceptedItemType);
658 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
659 mockedDriveSerialNumber);
663 // There is a bug opened for this issue - https://github.com/dblock/oshi/issues/185
665 public void assertChannelSensorsCpuTempIsUpdated() {
666 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_CPU_TEMPERATURE;
667 String acceptedItemType = "Number";
669 DecimalType mockedSensorsCpuTemperatureValue = new DecimalType(60);
670 when(mockedSystemInfo.getSensorsCpuTemperature()).thenReturn(mockedSensorsCpuTemperatureValue);
672 initializeThingWithChannel(channnelID, acceptedItemType);
673 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
674 mockedSensorsCpuTemperatureValue);
678 public void assertChannelSensorsCpuVoltageIsUpdated() {
679 String channnelID = SysteminfoBindingConstants.CHANNEL_SENOSRS_CPU_VOLTAGE;
680 String acceptedItemType = "Number";
682 DecimalType mockedSensorsCpuVoltageValue = new DecimalType(1000);
683 when(mockedSystemInfo.getSensorsCpuVoltage()).thenReturn(mockedSensorsCpuVoltageValue);
685 initializeThingWithChannel(channnelID, acceptedItemType);
686 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
687 mockedSensorsCpuVoltageValue);
691 public void assertChannelSensorsFanSpeedIsUpdated() throws DeviceNotFoundException {
692 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_FAN_SPEED;
693 String acceptedItemType = "Number";
695 DecimalType mockedSensorsCpuFanSpeedValue = new DecimalType(180);
696 when(mockedSystemInfo.getSensorsFanSpeed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedSensorsCpuFanSpeedValue);
698 initializeThingWithChannel(channnelID, acceptedItemType);
699 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
700 mockedSensorsCpuFanSpeedValue);
704 public void assertChannelBatteryNameIsUpdated() throws DeviceNotFoundException {
705 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_NAME;
706 String acceptedItemType = "String";
708 StringType mockedBatteryName = new StringType("Mocked Battery Name");
709 when(mockedSystemInfo.getBatteryName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryName);
711 initializeThingWithChannel(channnelID, acceptedItemType);
712 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedBatteryName);
716 public void assertChannelBatteryRemainingCapacityIsUpdated() throws DeviceNotFoundException {
717 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_CAPACITY;
718 String acceptedItemType = "Number";
720 DecimalType mockedBatteryRemainingCapacity = new DecimalType(200);
721 when(mockedSystemInfo.getBatteryRemainingCapacity(DEFAULT_DEVICE_INDEX))
722 .thenReturn(mockedBatteryRemainingCapacity);
724 initializeThingWithChannel(channnelID, acceptedItemType);
725 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
726 mockedBatteryRemainingCapacity);
730 public void assertChannelBatteryRemainingTimeIsUpdated() throws DeviceNotFoundException {
731 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_TIME;
732 String acceptedItemType = "Number";
734 DecimalType mockedBatteryRemainingTime = new DecimalType(3600);
735 when(mockedSystemInfo.getBatteryRemainingTime(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryRemainingTime);
737 initializeThingWithChannel(channnelID, acceptedItemType);
738 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
739 mockedBatteryRemainingTime);
743 public void assertChannelDisplayInformationIsUpdated() throws DeviceNotFoundException {
744 String channnelID = SysteminfoBindingConstants.CHANNEL_DISPLAY_INFORMATION;
745 String acceptedItemType = "String";
747 StringType mockedDisplayInfo = new StringType("Mocked Display Information");
748 when(mockedSystemInfo.getDisplayInformation(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDisplayInfo);
750 initializeThingWithChannel(channnelID, acceptedItemType);
751 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDisplayInfo);
755 public void assertChannelNetworkIpIsUpdated() throws DeviceNotFoundException {
756 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_IP;
757 String acceptedItemType = "String";
759 StringType mockedNetworkIp = new StringType("192.168.1.0");
760 when(mockedSystemInfo.getNetworkIp(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkIp);
762 initializeThingWithChannel(channnelID, acceptedItemType);
763 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkIp);
767 public void assertChannelNetworkMacIsUpdated() throws DeviceNotFoundException {
768 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_MAC;
769 String acceptedItemType = "String";
771 StringType mockedNetworkMacValue = new StringType("AB-10-11-12-13-14");
772 when(mockedSystemInfo.getNetworkMac(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkMacValue);
774 initializeThingWithChannel(channnelID, acceptedItemType);
775 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkMacValue);
779 public void assertChannelNetworkDataSentIsUpdated() throws DeviceNotFoundException {
780 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_SENT;
781 String acceptedItemType = "Number";
783 DecimalType mockedNetworkDataSent = new DecimalType(1000);
784 when(mockedSystemInfo.getNetworkDataSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataSent);
786 initializeThingWithChannel(channnelID, acceptedItemType);
787 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkDataSent);
791 public void assertChannelNetworkDataReceivedIsUpdated() throws DeviceNotFoundException {
792 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_RECEIVED;
793 String acceptedItemType = "Number";
795 DecimalType mockedNetworkDataReceiveed = new DecimalType(800);
796 when(mockedSystemInfo.getNetworkDataReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataReceiveed);
798 initializeThingWithChannel(channnelID, acceptedItemType);
799 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
800 mockedNetworkDataReceiveed);
804 public void assertChannelNetworkPacketsSentIsUpdated() throws DeviceNotFoundException {
805 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_SENT;
806 String acceptedItemType = "Number";
808 DecimalType mockedNetworkPacketsSent = new DecimalType(50);
809 when(mockedSystemInfo.getNetworkPacketsSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsSent);
811 initializeThingWithChannel(channnelID, acceptedItemType);
812 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
813 mockedNetworkPacketsSent);
817 public void assertChannelNetworkPacketsReceivedIsUpdated() throws DeviceNotFoundException {
818 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_RECEIVED;
819 String acceptedItemType = "Number";
821 DecimalType mockedNetworkPacketsReceived = new DecimalType(48);
822 when(mockedSystemInfo.getNetworkPacketsReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsReceived);
824 initializeThingWithChannel(channnelID, acceptedItemType);
825 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
826 mockedNetworkPacketsReceived);
830 public void assertChannelNetworkNetworkNameIsUpdated() throws DeviceNotFoundException {
831 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_NAME;
832 String acceptedItemType = "String";
834 StringType mockedNetworkName = new StringType("MockN-AQ34");
835 when(mockedSystemInfo.getNetworkName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkName);
837 initializeThingWithChannel(channnelID, acceptedItemType);
838 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkName);
842 public void assertChannelNetworkNetworkDisplayNameIsUpdated() throws DeviceNotFoundException {
843 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_ADAPTER_NAME;
844 String acceptedItemType = "String";
846 StringType mockedNetworkAdapterName = new StringType("Mocked Network Adapter Name");
847 when(mockedSystemInfo.getNetworkDisplayName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkAdapterName);
849 initializeThingWithChannel(channnelID, acceptedItemType);
850 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
851 mockedNetworkAdapterName);
854 class SysteminfoDiscoveryServiceMock extends SysteminfoDiscoveryService {
857 SysteminfoDiscoveryServiceMock(String hostname) {
859 this.hostname = hostname;
863 protected String getHostName() throws UnknownHostException {
864 if (hostname.equals("unresolved")) {
865 throw new UnknownHostException();
871 public void startScan() {
877 public void testDiscoveryWithInvalidHostname() {
878 String hostname = "Hilo.fritz.box";
879 String expectedHostname = "Hilo_fritz_box";
881 testDiscoveryService(expectedHostname, hostname);
885 public void testDiscoveryWithValidHostname() {
886 String hostname = "MyComputer";
887 String expectedHostname = "MyComputer";
889 testDiscoveryService(expectedHostname, hostname);
893 public void testDiscoveryWithUnresolvedHostname() {
894 String hostname = "unresolved";
895 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
897 testDiscoveryService(expectedHostname, hostname);
901 public void testDiscoveryWithEmptyHostnameString() {
902 String hostname = "";
903 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
905 testDiscoveryService(expectedHostname, hostname);
908 private void testDiscoveryService(String expectedHostname, String hostname) {
909 SysteminfoDiscoveryService discoveryService = getService(DiscoveryService.class,
910 SysteminfoDiscoveryService.class);
911 waitForAssert(() -> {
912 assertThat(discoveryService, is(notNullValue()));
914 SysteminfoDiscoveryServiceMock discoveryServiceMock = new SysteminfoDiscoveryServiceMock(hostname);
915 if (discoveryService != null) {
916 unregisterService(DiscoveryService.class);
918 registerService(discoveryServiceMock, DiscoveryService.class.getName(), new Hashtable<>());
920 ThingTypeUID computerType = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
921 ThingUID computerUID = new ThingUID(computerType, expectedHostname);
923 discoveryServiceMock.startScan();
925 Inbox inbox = getService(Inbox.class);
926 assertThat(inbox, is(notNullValue()));
928 waitForAssert(() -> {
929 List<DiscoveryResult> results = inbox.stream().filter(InboxPredicates.forThingUID(computerUID))
931 assertFalse(results.isEmpty(), "No Thing with UID " + computerUID.getAsString() + " in inbox");
934 inbox.approve(computerUID, SysteminfoDiscoveryService.DEFAULT_THING_LABEL, null);
936 waitForAssert(() -> {
937 systemInfoThing = thingRegistry.get(computerUID);
938 assertThat(systemInfoThing, is(notNullValue()));
941 waitForAssert(() -> {
942 assertThat("Thing is not initialized.", systemInfoThing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
947 public void assertChannelProcessThreadsIsUpdatedWithPIDse() throws DeviceNotFoundException {
948 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_THREADS;
949 String acceptedItemType = "Number";
950 // The pid of the System idle process in Windows
953 DecimalType mockedProcessThreadsCount = new DecimalType(4);
954 when(mockedSystemInfo.getProcessThreads(pid)).thenReturn(mockedProcessThreadsCount);
956 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
957 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
958 mockedProcessThreadsCount);
962 public void assertChannelProcessPathIsUpdatedWithPIDset() throws DeviceNotFoundException {
963 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_PATH;
964 String acceptedItemType = "String";
965 // The pid of the System idle process in Windows
968 StringType mockedProcessPath = new StringType("C:\\Users\\MockedUser\\Process");
969 when(mockedSystemInfo.getProcessPath(pid)).thenReturn(mockedProcessPath);
971 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
972 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessPath);
976 public void assertChannelProcessNameIsUpdatedWithPIDset() throws DeviceNotFoundException {
977 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_NAME;
978 String acceptedItemType = "String";
979 // The pid of the System idle process in Windows
982 StringType mockedProcessName = new StringType("MockedProcess.exe");
983 when(mockedSystemInfo.getProcessName(pid)).thenReturn(mockedProcessName);
985 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
986 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessName);
990 public void assertChannelProcessMemoryIsUpdatedWithPIDset() throws DeviceNotFoundException {
991 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_MEMORY;
992 String acceptedItemType = "Number";
993 // The pid of the System idle process in Windows
996 DecimalType mockedProcessMemory = new DecimalType(450);
997 when(mockedSystemInfo.getProcessMemoryUsage(pid)).thenReturn(mockedProcessMemory);
999 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1000 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessMemory);
1004 public void assertChannelProcessLoadIsUpdatedWithPIDset() throws DeviceNotFoundException {
1005 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_LOAD;
1006 String acceptedItemType = "Number";
1007 // The pid of the System idle process in Windows
1010 DecimalType mockedProcessLoad = new DecimalType(3);
1011 when(mockedSystemInfo.getProcessCpuUsage(pid)).thenReturn(mockedProcessLoad);
1013 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1014 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessLoad);
1018 public void testThingHandlesChannelPriorityChange() {
1019 String priorityKey = "priority";
1020 String pidKey = "pid";
1021 String initialPriority = DEFAULT_CHANNEL_TEST_PRIORITY; // Evaluates to High
1022 String newPriority = "Low";
1024 String acceptedItemType = "Number";
1025 initializeThingWithChannel(DEFAULT_TEST_CHANNEL_ID, acceptedItemType);
1027 Channel channel = systemInfoThing.getChannel(DEFAULT_TEST_CHANNEL_ID);
1028 if (channel == null) {
1029 throw new AssertionError("Channel '" + DEFAULT_TEST_CHANNEL_ID + "' is null");
1032 waitForAssert(() -> {
1033 assertThat("The initial priority of channel " + channel.getUID() + " is not as expected.",
1034 channel.getConfiguration().get(priorityKey), is(equalTo(initialPriority)));
1035 assertThat(systemInfoHandler.getHighPriorityChannels().contains(channel.getUID()), is(true));
1038 // Change the priority of a channel, keep the pid
1039 Configuration updatedConfig = new Configuration();
1040 updatedConfig.put(priorityKey, newPriority);
1041 updatedConfig.put(pidKey, channel.getConfiguration().get(pidKey));
1042 Channel updatedChannel = ChannelBuilder.create(channel.getUID(), channel.getAcceptedItemType())
1043 .withType(channel.getChannelTypeUID()).withKind(channel.getKind()).withConfiguration(updatedConfig)
1046 Thing updatedThing = ThingBuilder.create(systemInfoThing.getThingTypeUID(), systemInfoThing.getUID())
1047 .withConfiguration(systemInfoThing.getConfiguration()).withChannel(updatedChannel).build();
1049 systemInfoHandler.thingUpdated(updatedThing);
1051 waitForAssert(() -> {
1052 assertThat("The prority of the channel was not updated: ", channel.getConfiguration().get(priorityKey),
1053 is(equalTo(newPriority)));
1054 assertThat(systemInfoHandler.getLowPriorityChannels().contains(channel.getUID()), is(true));