2 * Copyright (c) 2010-2020 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.junit.Assert.*;
19 import static org.mockito.Mockito.*;
21 import java.math.BigDecimal;
22 import java.net.UnknownHostException;
23 import java.util.Hashtable;
24 import java.util.List;
26 import org.openhab.core.config.core.Configuration;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.config.discovery.inbox.Inbox;
30 import org.openhab.core.config.discovery.inbox.InboxPredicates;
31 import org.openhab.core.items.GenericItem;
32 import org.openhab.core.items.ItemNotFoundException;
33 import org.openhab.core.items.ItemRegistry;
34 import org.openhab.core.library.items.NumberItem;
35 import org.openhab.core.library.items.StringItem;
36 import org.openhab.core.library.types.DecimalType;
37 import org.openhab.core.library.types.StringType;
38 import org.openhab.core.thing.Channel;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.ManagedThingProvider;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingProvider;
43 import org.openhab.core.thing.ThingRegistry;
44 import org.openhab.core.thing.ThingStatus;
45 import org.openhab.core.thing.ThingStatusDetail;
46 import org.openhab.core.thing.ThingTypeUID;
47 import org.openhab.core.thing.ThingUID;
48 import org.openhab.core.thing.binding.ThingHandler;
49 import org.openhab.core.thing.binding.ThingHandlerFactory;
50 import org.openhab.core.thing.binding.builder.ChannelBuilder;
51 import org.openhab.core.thing.binding.builder.ThingBuilder;
52 import org.openhab.core.thing.link.ItemChannelLink;
53 import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
54 import org.openhab.core.thing.type.ChannelKind;
55 import org.openhab.core.thing.type.ChannelTypeUID;
56 import org.openhab.core.types.State;
57 import org.openhab.core.types.UnDefType;
58 import org.openhab.core.test.java.JavaOSGiTest;
59 import org.openhab.core.test.storage.VolatileStorageService;
60 import org.junit.After;
61 import org.junit.Before;
62 import org.junit.Ignore;
63 import org.junit.Test;
64 import org.openhab.binding.systeminfo.internal.SysteminfoBindingConstants;
65 import org.openhab.binding.systeminfo.internal.SysteminfoHandlerFactory;
66 import org.openhab.binding.systeminfo.internal.discovery.SysteminfoDiscoveryService;
67 import org.openhab.binding.systeminfo.internal.handler.SysteminfoHandler;
68 import org.openhab.binding.systeminfo.internal.model.DeviceNotFoundException;
69 import org.openhab.binding.systeminfo.internal.model.SysteminfoInterface;
72 * OSGi tests for the {@link SysteminfoHandler}
74 * @author Svilen Valkanov - Initial contribution
75 * @author Lyubomir Papazov - Created a mock systeminfo object. This way, access to the user's OS will not be required,
76 * but mock data will be used instead, avoiding potential errors from the OS queries.
77 * @author Wouter Born - Migrate Groovy to Java tests
79 public class SysteminfoOSGiTest extends JavaOSGiTest {
80 private static final String DEFAULT_TEST_THING_NAME = "work";
81 private static final String DEFAULT_TEST_ITEM_NAME = "test";
82 private static final String DEFAULT_CHANNEL_TEST_PRIORITY = "High";
83 private static final int DEFAULT_CHANNEL_PID = -1;
84 private static final String DEFAULT_TEST_CHANNEL_ID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
85 private static final int DEFAULT_DEVICE_INDEX = 0;
88 * Refresh time in seconds for tasks with priority High.
89 * Default value for the parameter interval_high in the thing configuration
91 private static final int DEFAULT_TEST_INTERVAL_HIGH = 1;
94 * Refresh time in seconds for tasks with priority Medium.
96 private static final int DEFAULT_TEST_INTERVAL_MEDIUM = 3;
98 private Thing systemInfoThing;
99 private SysteminfoHandler systemInfoHandler;
100 private GenericItem testItem;
102 private SysteminfoInterface mockedSystemInfo;
103 private ManagedThingProvider managedThingProvider;
104 private ThingRegistry thingRegistry;
105 private ItemRegistry itemRegistry;
106 private SysteminfoHandlerFactory systeminfoHandlerFactory;
109 public void setUp() {
110 VolatileStorageService volatileStorageService = new VolatileStorageService();
111 registerService(volatileStorageService);
113 // Preparing the mock with OS properties, that are used in the initialize method of SysteminfoHandler
114 mockedSystemInfo = mock(SysteminfoInterface.class);
115 when(mockedSystemInfo.getCpuLogicalCores()).thenReturn(new DecimalType(2));
116 when(mockedSystemInfo.getCpuPhysicalCores()).thenReturn(new DecimalType(2));
117 when(mockedSystemInfo.getOsFamily()).thenReturn(new StringType("Mock OS"));
118 when(mockedSystemInfo.getOsManufacturer()).thenReturn(new StringType("Mock OS Manufacturer"));
119 when(mockedSystemInfo.getOsVersion()).thenReturn(new StringType("Mock Os Version"));
121 systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
122 SysteminfoInterface oshiSystemInfo = getService(SysteminfoInterface.class);
124 // Unbind oshiSystemInfo service and bind the mock service to make the systeminfobinding tests independent of
125 // the external OSHI library
126 if (oshiSystemInfo != null) {
127 systeminfoHandlerFactory.unbindSystemInfo(oshiSystemInfo);
129 systeminfoHandlerFactory.bindSystemInfo(mockedSystemInfo);
131 managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
132 assertThat(managedThingProvider, is(notNullValue()));
134 thingRegistry = getService(ThingRegistry.class);
135 assertThat(thingRegistry, is(notNullValue()));
137 itemRegistry = getService(ItemRegistry.class);
138 assertThat(itemRegistry, is(notNullValue()));
142 public void tearDown() {
143 if (systemInfoThing != null) {
144 // Remove the systeminfo thing. The handler will be also disposed automatically
145 Thing removedThing = thingRegistry.forceRemove(systemInfoThing.getUID());
146 assertThat("The systeminfo thing cannot be deleted", removedThing, is(notNullValue()));
148 waitForAssert(() -> {
149 ThingHandler systemInfoHandler = systemInfoThing.getHandler();
150 assertThat(systemInfoHandler, is(nullValue()));
153 if (testItem != null) {
154 itemRegistry.remove(DEFAULT_TEST_ITEM_NAME);
158 private void initializeThingWithChannelAndPID(String channelID, String acceptedItemType, int pid) {
159 Configuration thingConfig = new Configuration();
160 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
161 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
162 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
163 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
164 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
166 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
169 private void initializeThingWithChannelAndPriority(String channelID, String acceptedItemType, String priority) {
170 Configuration thingConfig = new Configuration();
171 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
172 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
173 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
174 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
175 int pid = DEFAULT_CHANNEL_PID;
177 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
180 private void initializeThingWithConfiguration(Configuration config) {
181 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
182 String channelID = DEFAULT_TEST_CHANNEL_ID;
183 String acceptedItemType = "String";
184 int pid = DEFAULT_CHANNEL_PID;
186 initializeThing(config, channelID, acceptedItemType, priority, pid);
189 private void initializeThingWithChannel(String channelID, String acceptedItemType) {
190 Configuration thingConfig = new Configuration();
191 thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
192 new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
193 thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
194 new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
196 String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
197 int pid = DEFAULT_CHANNEL_PID;
198 initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
201 private void initializeThing(Configuration thingConfiguration, String channelID, String acceptedItemType,
202 String priority, int pid) {
203 ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
204 ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
206 ChannelUID channelUID = new ChannelUID(thingUID, channelID);
207 ChannelTypeUID channelTypeUID = new ChannelTypeUID(SysteminfoBindingConstants.BINDING_ID,
208 channelUID.getIdWithoutGroup());
209 Configuration channelConfig = new Configuration();
210 channelConfig.put("priority", priority);
211 channelConfig.put("pid", new BigDecimal(pid));
212 Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
213 .withKind(ChannelKind.STATE).withConfiguration(channelConfig).build();
215 systemInfoThing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(thingConfiguration)
216 .withChannel(channel).build();
218 managedThingProvider.add(systemInfoThing);
220 waitForAssert(() -> {
221 systemInfoHandler = (SysteminfoHandler) systemInfoThing.getHandler();
222 assertThat(systemInfoHandler, is(notNullValue()));
225 waitForAssert(() -> {
226 assertThat("Thing is not initilized, before an Item is created", systemInfoThing.getStatus(),
227 anyOf(equalTo(ThingStatus.OFFLINE), equalTo(ThingStatus.ONLINE)));
230 intializeItem(channelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
233 private void assertItemState(String acceptedItemType, String itemName, String priority, State expectedState) {
234 waitForAssert(() -> {
235 ThingStatusDetail thingStatusDetail = systemInfoThing.getStatusInfo().getStatusDetail();
236 String description = systemInfoThing.getStatusInfo().getDescription();
237 assertThat("Thing status detail is " + thingStatusDetail + " with description " + description,
238 systemInfoThing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
240 // The binding starts all refresh tasks in SysteminfoHandler.scheduleUpdates() after this delay !
242 sleep(SysteminfoHandler.WAIT_TIME_CHANNEL_ITEM_LINK_INIT * 1000);
243 } catch (InterruptedException e) {
244 throw new AssertionError("Interrupted while sleeping");
249 item = (GenericItem) itemRegistry.getItem(itemName);
250 } catch (ItemNotFoundException e) {
251 throw new AssertionError("Item not found in registry");
255 if (priority.equals("High")) {
256 waitTime = DEFAULT_TEST_INTERVAL_HIGH * 1000;
257 } else if (priority.equals("Medium")) {
258 waitTime = DEFAULT_TEST_INTERVAL_MEDIUM * 1000;
263 waitForAssert(() -> {
264 State itemState = item.getState();
265 assertThat(itemState, is(equalTo(expectedState)));
266 }, waitTime, DFL_SLEEP_TIME);
269 private void intializeItem(ChannelUID channelUID, String itemName, String acceptedItemType) {
270 if (acceptedItemType.equals("Number")) {
271 testItem = new NumberItem(itemName);
272 } else if (acceptedItemType.equals("String")) {
273 testItem = new StringItem(itemName);
275 itemRegistry.add(testItem);
277 ManagedItemChannelLinkProvider itemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
278 assertThat(itemChannelLinkProvider, is(notNullValue()));
280 itemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
284 public void assertInvalidThingConfigurationValuesAreHandled() {
285 Configuration configuration = new Configuration();
287 // invalid value - must be positive
288 int refreshIntervalHigh = -5;
289 configuration.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME, new BigDecimal(refreshIntervalHigh));
291 int refreshIntervalMedium = 3;
292 configuration.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
293 new BigDecimal(refreshIntervalMedium));
294 initializeThingWithConfiguration(configuration);
296 testInvalidConfiguration();
299 private void testInvalidConfiguration() {
300 waitForAssert(() -> {
301 assertThat("Invalid configuratuin is used !", systemInfoThing.getStatus(),
302 is(equalTo(ThingStatus.OFFLINE)));
303 assertThat(systemInfoThing.getStatusInfo().getStatusDetail(),
304 is(equalTo(ThingStatusDetail.HANDLER_INITIALIZING_ERROR)));
305 assertThat(systemInfoThing.getStatusInfo().getDescription(), is(equalTo("Thing cannot be initialized!")));
310 public void assertThingStatusIsUninitializedWhenThereIsNoSysteminfoServiceProvided() {
311 // Unbind the mock service to verify the systeminfo thing will not be initialized when no systeminfo service is
313 systeminfoHandlerFactory.unbindSystemInfo(mockedSystemInfo);
315 ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
316 ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
318 systemInfoThing = ThingBuilder.create(thingTypeUID, thingUID).build();
319 managedThingProvider.add(systemInfoThing);
321 waitForAssert(() -> {
322 assertThat("The thing status is uninitialized when systeminfo service is missing",
323 systemInfoThing.getStatus(), equalTo(ThingStatus.UNINITIALIZED));
328 public void assertMediumPriorityChannelIsUpdated() {
329 String channnelID = DEFAULT_TEST_CHANNEL_ID;
330 String acceptedItemType = "Number";
331 String priority = "Medium";
333 initializeThingWithChannelAndPriority(channnelID, acceptedItemType, priority);
334 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, priority, UnDefType.UNDEF);
338 public void assertStateOfSecondDeviceIsUpdated() {
339 // This test assumes that at least 2 network interfaces are present on the test platform
341 String channnelID = "network" + deviceIndex + "#mac";
342 String acceptedItemType = "String";
344 initializeThingWithChannel(channnelID, acceptedItemType);
345 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, UnDefType.UNDEF);
349 public void assertChannelCpuLoad1IsUpdated() {
350 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_1;
351 String acceptedItemType = "Number";
353 DecimalType mockedCpuLoad1Value = new DecimalType(1.1);
354 when(mockedSystemInfo.getCpuLoad1()).thenReturn(mockedCpuLoad1Value);
356 initializeThingWithChannel(channnelID, acceptedItemType);
357 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad1Value);
361 public void assertChannelCpuLoad5IsUpdated() {
362 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_5;
363 String acceptedItemType = "Number";
365 DecimalType mockedCpuLoad5Value = new DecimalType(5.5);
366 when(mockedSystemInfo.getCpuLoad5()).thenReturn(mockedCpuLoad5Value);
368 initializeThingWithChannel(channnelID, acceptedItemType);
369 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad5Value);
373 public void assertChannelCpuLoad15IsUpdated() {
374 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_15;
375 String acceptedItemType = "Number";
377 DecimalType mockedCpuLoad15Value = new DecimalType(15.15);
378 when(mockedSystemInfo.getCpuLoad15()).thenReturn(mockedCpuLoad15Value);
380 initializeThingWithChannel(channnelID, acceptedItemType);
381 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad15Value);
385 public void assertChannelCpuThreadsIsUpdated() {
386 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_THREADS;
387 String acceptedItemType = "Number";
389 DecimalType mockedCpuThreadsValue = new DecimalType(16);
390 when(mockedSystemInfo.getCpuThreads()).thenReturn(mockedCpuThreadsValue);
392 initializeThingWithChannel(channnelID, acceptedItemType);
393 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuThreadsValue);
397 public void assertChannelCpuUptimeIsUpdated() {
398 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_UPTIME;
399 String acceptedItemType = "Number";
401 DecimalType mockedCpuUptimeValue = new DecimalType(100);
402 when(mockedSystemInfo.getCpuUptime()).thenReturn(mockedCpuUptimeValue);
404 initializeThingWithChannel(channnelID, acceptedItemType);
405 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuUptimeValue);
409 public void assertChannelCpuDescriptionIsUpdated() {
410 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_DESCRIPTION;
411 String acceptedItemType = "String";
413 StringType mockedCpuDescriptionValue = new StringType("Mocked Cpu Descr");
414 when(mockedSystemInfo.getCpuDescription()).thenReturn(mockedCpuDescriptionValue);
416 initializeThingWithChannel(channnelID, acceptedItemType);
417 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
418 mockedCpuDescriptionValue);
422 public void assertChannelCpuNameIsUpdated() {
423 String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_NAME;
424 String acceptedItemType = "String";
426 StringType mockedCpuNameValue = new StringType("Mocked Cpu Name");
427 when(mockedSystemInfo.getCpuName()).thenReturn(mockedCpuNameValue);
429 initializeThingWithChannel(channnelID, acceptedItemType);
430 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuNameValue);
434 public void assertChannelMemoryAvailableIsUpdated() {
435 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE;
436 String acceptedItemType = "Number";
438 DecimalType mockedMemoryAvailableValue = new DecimalType(1000);
439 when(mockedSystemInfo.getMemoryAvailable()).thenReturn(mockedMemoryAvailableValue);
441 initializeThingWithChannel(channnelID, acceptedItemType);
442 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
443 mockedMemoryAvailableValue);
447 public void assertChannelMemoryUsedIsUpdated() {
448 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_USED;
449 String acceptedItemType = "Number";
451 DecimalType mockedMemoryUsedValue = new DecimalType(24);
452 when(mockedSystemInfo.getMemoryUsed()).thenReturn(mockedMemoryUsedValue);
454 initializeThingWithChannel(channnelID, acceptedItemType);
455 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedMemoryUsedValue);
459 public void assertChannelMemoryTotalIsUpdated() {
460 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_TOTAL;
461 String acceptedItemType = "Number";
463 DecimalType mockedMemoryTotalValue = new DecimalType(1024);
464 when(mockedSystemInfo.getMemoryTotal()).thenReturn(mockedMemoryTotalValue);
466 initializeThingWithChannel(channnelID, acceptedItemType);
467 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
468 mockedMemoryTotalValue);
472 public void assertChannelMemoryAvailablePercentIsUpdated() {
473 String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE_PERCENT;
474 String acceptedItemType = "Number";
476 DecimalType mockedMemoryAvailablePercentValue = new DecimalType(97);
477 when(mockedSystemInfo.getMemoryAvailablePercent()).thenReturn(mockedMemoryAvailablePercentValue);
479 initializeThingWithChannel(channnelID, acceptedItemType);
480 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
481 mockedMemoryAvailablePercentValue);
485 public void assertChannelSwapAvailableIsUpdated() {
486 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE;
487 String acceptedItemType = "Number";
489 DecimalType mockedSwapAvailableValue = new DecimalType(482);
490 when(mockedSystemInfo.getSwapAvailable()).thenReturn(mockedSwapAvailableValue);
492 initializeThingWithChannel(channnelID, acceptedItemType);
493 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
494 mockedSwapAvailableValue);
498 public void assertChannelSwapUsedIsUpdated() {
499 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_USED;
500 String acceptedItemType = "Number";
502 DecimalType mockedSwapUsedValue = new DecimalType(30);
503 when(mockedSystemInfo.getSwapUsed()).thenReturn(mockedSwapUsedValue);
505 initializeThingWithChannel(channnelID, acceptedItemType);
506 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapUsedValue);
510 public void assertChannelSwapTotalIsUpdated() {
511 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_TOTAL;
512 String acceptedItemType = "Number";
514 DecimalType mockedSwapTotalValue = new DecimalType(512);
515 when(mockedSystemInfo.getSwapTotal()).thenReturn(mockedSwapTotalValue);
517 initializeThingWithChannel(channnelID, acceptedItemType);
518 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapTotalValue);
522 public void assertChannelSwapAvailablePercentIsUpdated() {
523 String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE_PERCENT;
524 String acceptedItemType = "Number";
526 DecimalType mockedSwapAvailablePercentValue = new DecimalType(94);
527 when(mockedSystemInfo.getSwapAvailablePercent()).thenReturn(mockedSwapAvailablePercentValue);
529 initializeThingWithChannel(channnelID, acceptedItemType);
530 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
531 mockedSwapAvailablePercentValue);
535 public void assertChannelStorageNameIsUpdated() throws DeviceNotFoundException {
536 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_NAME;
537 String acceptedItemType = "String";
539 StringType mockedStorageName = new StringType("Mocked Storage Name");
540 when(mockedSystemInfo.getStorageName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageName);
542 initializeThingWithChannel(channnelID, acceptedItemType);
543 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageName);
547 public void assertChannelStorageTypeIsUpdated() throws DeviceNotFoundException {
548 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TYPE;
549 String acceptedItemType = "String";
551 StringType mockedStorageType = new StringType("Mocked Storage Type");
552 when(mockedSystemInfo.getStorageType(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageType);
554 initializeThingWithChannel(channnelID, acceptedItemType);
555 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageType);
559 public void assertChannelStorageDescriptionIsUpdated() throws DeviceNotFoundException {
560 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_DESCRIPTION;
561 String acceptedItemType = "String";
563 StringType mockedStorageDescription = new StringType("Mocked Storage Description");
564 when(mockedSystemInfo.getStorageDescription(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageDescription);
566 initializeThingWithChannel(channnelID, acceptedItemType);
567 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
568 mockedStorageDescription);
572 public void assertChannelStorageAvailableIsUpdated() throws DeviceNotFoundException {
573 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE;
574 String acceptedItemType = "Number";
576 DecimalType mockedStorageAvailableValue = new DecimalType(2000);
577 when(mockedSystemInfo.getStorageAvailable(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageAvailableValue);
579 initializeThingWithChannel(channnelID, acceptedItemType);
580 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
581 mockedStorageAvailableValue);
585 public void assertChannelStorageUsedIsUpdated() throws DeviceNotFoundException {
586 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_USED;
587 String acceptedItemType = "Number";
589 DecimalType mockedStorageUsedValue = new DecimalType(500);
590 when(mockedSystemInfo.getStorageUsed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageUsedValue);
592 initializeThingWithChannel(channnelID, acceptedItemType);
593 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
594 mockedStorageUsedValue);
598 public void assertChannelStorageTotalIsUpdated() throws DeviceNotFoundException {
599 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TOTAL;
600 String acceptedItemType = "Number";
602 DecimalType mockedStorageTotalValue = new DecimalType(2500);
603 when(mockedSystemInfo.getStorageTotal(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageTotalValue);
605 initializeThingWithChannel(channnelID, acceptedItemType);
606 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
607 mockedStorageTotalValue);
611 public void assertChannelStorageAvailablePercentIsUpdated() throws DeviceNotFoundException {
612 String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE_PERCENT;
613 String acceptedItemType = "Number";
615 DecimalType mockedStorageAvailablePercent = new DecimalType(20);
616 when(mockedSystemInfo.getStorageAvailablePercent(DEFAULT_DEVICE_INDEX))
617 .thenReturn(mockedStorageAvailablePercent);
619 initializeThingWithChannel(channnelID, acceptedItemType);
620 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
621 mockedStorageAvailablePercent);
625 public void assertChannelDriveNameIsUpdated() throws DeviceNotFoundException {
626 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_NAME;
627 String acceptedItemType = "String";
629 StringType mockedDriveNameValue = new StringType("Mocked Drive Name");
630 when(mockedSystemInfo.getDriveName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveNameValue);
632 initializeThingWithChannel(channelID, acceptedItemType);
633 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveNameValue);
637 public void assertChannelDriveModelIsUpdated() throws DeviceNotFoundException {
638 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_MODEL;
639 String acceptedItemType = "String";
641 StringType mockedDriveModelValue = new StringType("Mocked Drive Model");
642 when(mockedSystemInfo.getDriveModel(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveModelValue);
644 initializeThingWithChannel(channelID, acceptedItemType);
645 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveModelValue);
649 public void assertChannelDriveSerialIsUpdated() throws DeviceNotFoundException {
650 String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_SERIAL;
651 String acceptedItemType = "String";
653 StringType mockedDriveSerialNumber = new StringType("Mocked Drive Serial Number");
654 when(mockedSystemInfo.getDriveSerialNumber(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveSerialNumber);
656 initializeThingWithChannel(channelID, acceptedItemType);
657 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
658 mockedDriveSerialNumber);
662 // There is a bug opened for this issue - https://github.com/dblock/oshi/issues/185
664 public void assertChannelSensorsCpuTempIsUpdated() {
665 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_CPU_TEMPERATURE;
666 String acceptedItemType = "Number";
668 DecimalType mockedSensorsCpuTemperatureValue = new DecimalType(60);
669 when(mockedSystemInfo.getSensorsCpuTemperature()).thenReturn(mockedSensorsCpuTemperatureValue);
671 initializeThingWithChannel(channnelID, acceptedItemType);
672 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
673 mockedSensorsCpuTemperatureValue);
677 public void assertChannelSensorsCpuVoltageIsUpdated() {
678 String channnelID = SysteminfoBindingConstants.CHANNEL_SENOSRS_CPU_VOLTAGE;
679 String acceptedItemType = "Number";
681 DecimalType mockedSensorsCpuVoltageValue = new DecimalType(1000);
682 when(mockedSystemInfo.getSensorsCpuVoltage()).thenReturn(mockedSensorsCpuVoltageValue);
684 initializeThingWithChannel(channnelID, acceptedItemType);
685 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
686 mockedSensorsCpuVoltageValue);
690 public void assertChannelSensorsFanSpeedIsUpdated() throws DeviceNotFoundException {
691 String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_FAN_SPEED;
692 String acceptedItemType = "Number";
694 DecimalType mockedSensorsCpuFanSpeedValue = new DecimalType(180);
695 when(mockedSystemInfo.getSensorsFanSpeed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedSensorsCpuFanSpeedValue);
697 initializeThingWithChannel(channnelID, acceptedItemType);
698 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
699 mockedSensorsCpuFanSpeedValue);
703 public void assertChannelBatteryNameIsUpdated() throws DeviceNotFoundException {
704 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_NAME;
705 String acceptedItemType = "String";
707 StringType mockedBatteryName = new StringType("Mocked Battery Name");
708 when(mockedSystemInfo.getBatteryName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryName);
710 initializeThingWithChannel(channnelID, acceptedItemType);
711 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedBatteryName);
715 public void assertChannelBatteryRemainingCapacityIsUpdated() throws DeviceNotFoundException {
716 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_CAPACITY;
717 String acceptedItemType = "Number";
719 DecimalType mockedBatteryRemainingCapacity = new DecimalType(200);
720 when(mockedSystemInfo.getBatteryRemainingCapacity(DEFAULT_DEVICE_INDEX))
721 .thenReturn(mockedBatteryRemainingCapacity);
723 initializeThingWithChannel(channnelID, acceptedItemType);
724 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
725 mockedBatteryRemainingCapacity);
729 public void assertChannelBatteryRemainingTimeIsUpdated() throws DeviceNotFoundException {
730 String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_TIME;
731 String acceptedItemType = "Number";
733 DecimalType mockedBatteryRemainingTime = new DecimalType(3600);
734 when(mockedSystemInfo.getBatteryRemainingTime(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryRemainingTime);
736 initializeThingWithChannel(channnelID, acceptedItemType);
737 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
738 mockedBatteryRemainingTime);
742 public void assertChannelDisplayInformationIsUpdated() throws DeviceNotFoundException {
743 String channnelID = SysteminfoBindingConstants.CHANNEL_DISPLAY_INFORMATION;
744 String acceptedItemType = "String";
746 StringType mockedDisplayInfo = new StringType("Mocked Display Information");
747 when(mockedSystemInfo.getDisplayInformation(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDisplayInfo);
749 initializeThingWithChannel(channnelID, acceptedItemType);
750 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDisplayInfo);
754 public void assertChannelNetworkIpIsUpdated() throws DeviceNotFoundException {
755 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_IP;
756 String acceptedItemType = "String";
758 StringType mockedNetworkIp = new StringType("192.168.1.0");
759 when(mockedSystemInfo.getNetworkIp(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkIp);
761 initializeThingWithChannel(channnelID, acceptedItemType);
762 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkIp);
766 public void assertChannelNetworkMacIsUpdated() throws DeviceNotFoundException {
767 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_MAC;
768 String acceptedItemType = "String";
770 StringType mockedNetworkMacValue = new StringType("AB-10-11-12-13-14");
771 when(mockedSystemInfo.getNetworkMac(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkMacValue);
773 initializeThingWithChannel(channnelID, acceptedItemType);
774 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkMacValue);
778 public void assertChannelNetworkDataSentIsUpdated() throws DeviceNotFoundException {
779 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_SENT;
780 String acceptedItemType = "Number";
782 DecimalType mockedNetworkDataSent = new DecimalType(1000);
783 when(mockedSystemInfo.getNetworkDataSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataSent);
785 initializeThingWithChannel(channnelID, acceptedItemType);
786 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkDataSent);
790 public void assertChannelNetworkDataReceivedIsUpdated() throws DeviceNotFoundException {
791 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_RECEIVED;
792 String acceptedItemType = "Number";
794 DecimalType mockedNetworkDataReceiveed = new DecimalType(800);
795 when(mockedSystemInfo.getNetworkDataReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataReceiveed);
797 initializeThingWithChannel(channnelID, acceptedItemType);
798 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
799 mockedNetworkDataReceiveed);
803 public void assertChannelNetworkPacketsSentIsUpdated() throws DeviceNotFoundException {
804 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_SENT;
805 String acceptedItemType = "Number";
807 DecimalType mockedNetworkPacketsSent = new DecimalType(50);
808 when(mockedSystemInfo.getNetworkPacketsSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsSent);
810 initializeThingWithChannel(channnelID, acceptedItemType);
811 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
812 mockedNetworkPacketsSent);
816 public void assertChannelNetworkPacketsReceivedIsUpdated() throws DeviceNotFoundException {
817 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_RECEIVED;
818 String acceptedItemType = "Number";
820 DecimalType mockedNetworkPacketsReceived = new DecimalType(48);
821 when(mockedSystemInfo.getNetworkPacketsReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsReceived);
823 initializeThingWithChannel(channnelID, acceptedItemType);
824 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
825 mockedNetworkPacketsReceived);
829 public void assertChannelNetworkNetworkNameIsUpdated() throws DeviceNotFoundException {
830 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_NAME;
831 String acceptedItemType = "String";
833 StringType mockedNetworkName = new StringType("MockN-AQ34");
834 when(mockedSystemInfo.getNetworkName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkName);
836 initializeThingWithChannel(channnelID, acceptedItemType);
837 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkName);
841 public void assertChannelNetworkNetworkDisplayNameIsUpdated() throws DeviceNotFoundException {
842 String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_ADAPTER_NAME;
843 String acceptedItemType = "String";
845 StringType mockedNetworkAdapterName = new StringType("Mocked Network Adapter Name");
846 when(mockedSystemInfo.getNetworkDisplayName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkAdapterName);
848 initializeThingWithChannel(channnelID, acceptedItemType);
849 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
850 mockedNetworkAdapterName);
853 class SysteminfoDiscoveryServiceMock extends SysteminfoDiscoveryService {
856 SysteminfoDiscoveryServiceMock(String hostname) {
858 this.hostname = hostname;
862 protected String getHostName() throws UnknownHostException {
863 if (hostname.equals("unresolved")) {
864 throw new UnknownHostException();
870 public void startScan() {
876 public void testDiscoveryWithInvalidHostname() {
877 String hostname = "Hilo.fritz.box";
878 String expectedHostname = "Hilo_fritz_box";
880 testDiscoveryService(expectedHostname, hostname);
884 public void testDiscoveryWithValidHostname() {
885 String hostname = "MyComputer";
886 String expectedHostname = "MyComputer";
888 testDiscoveryService(expectedHostname, hostname);
892 public void testDiscoveryWithUnresolvedHostname() {
893 String hostname = "unresolved";
894 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
896 testDiscoveryService(expectedHostname, hostname);
900 public void testDiscoveryWithEmptyHostnameString() {
901 String hostname = "";
902 String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
904 testDiscoveryService(expectedHostname, hostname);
907 private void testDiscoveryService(String expectedHostname, String hostname) {
908 SysteminfoDiscoveryService discoveryService = getService(DiscoveryService.class,
909 SysteminfoDiscoveryService.class);
910 waitForAssert(() -> {
911 assertThat(discoveryService, is(notNullValue()));
913 SysteminfoDiscoveryServiceMock discoveryServiceMock = new SysteminfoDiscoveryServiceMock(hostname);
914 if (discoveryService != null) {
915 unregisterService(DiscoveryService.class);
917 registerService(discoveryServiceMock, DiscoveryService.class.getName(), new Hashtable<>());
919 ThingTypeUID computerType = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
920 ThingUID computerUID = new ThingUID(computerType, expectedHostname);
922 discoveryServiceMock.startScan();
924 Inbox inbox = getService(Inbox.class);
925 assertThat(inbox, is(notNullValue()));
927 waitForAssert(() -> {
928 List<DiscoveryResult> results = inbox.stream().filter(InboxPredicates.forThingUID(computerUID))
930 assertFalse("No Thing with UID " + computerUID.getAsString() + " in inbox", results.isEmpty());
933 inbox.approve(computerUID, SysteminfoDiscoveryService.DEFAULT_THING_LABEL);
935 waitForAssert(() -> {
936 systemInfoThing = thingRegistry.get(computerUID);
937 assertThat(systemInfoThing, is(notNullValue()));
940 waitForAssert(() -> {
941 assertThat("Thing is not initialized.", systemInfoThing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
946 public void assertChannelProcessThreadsIsUpdatedWithPIDse() throws DeviceNotFoundException {
947 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_THREADS;
948 String acceptedItemType = "Number";
949 // The pid of the System idle process in Windows
952 DecimalType mockedProcessThreadsCount = new DecimalType(4);
953 when(mockedSystemInfo.getProcessThreads(pid)).thenReturn(mockedProcessThreadsCount);
955 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
956 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
957 mockedProcessThreadsCount);
961 public void assertChannelProcessPathIsUpdatedWithPIDset() throws DeviceNotFoundException {
962 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_PATH;
963 String acceptedItemType = "String";
964 // The pid of the System idle process in Windows
967 StringType mockedProcessPath = new StringType("C:\\Users\\MockedUser\\Process");
968 when(mockedSystemInfo.getProcessPath(pid)).thenReturn(mockedProcessPath);
970 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
971 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessPath);
975 public void assertChannelProcessNameIsUpdatedWithPIDset() throws DeviceNotFoundException {
976 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_NAME;
977 String acceptedItemType = "String";
978 // The pid of the System idle process in Windows
981 StringType mockedProcessName = new StringType("MockedProcess.exe");
982 when(mockedSystemInfo.getProcessName(pid)).thenReturn(mockedProcessName);
984 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
985 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessName);
989 public void assertChannelProcessMemoryIsUpdatedWithPIDset() throws DeviceNotFoundException {
990 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_MEMORY;
991 String acceptedItemType = "Number";
992 // The pid of the System idle process in Windows
995 DecimalType mockedProcessMemory = new DecimalType(450);
996 when(mockedSystemInfo.getProcessMemoryUsage(pid)).thenReturn(mockedProcessMemory);
998 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
999 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessMemory);
1003 public void assertChannelProcessLoadIsUpdatedWithPIDset() throws DeviceNotFoundException {
1004 String channnelID = SysteminfoBindingConstants.CHANNEL_PROCESS_LOAD;
1005 String acceptedItemType = "Number";
1006 // The pid of the System idle process in Windows
1009 DecimalType mockedProcessLoad = new DecimalType(3);
1010 when(mockedSystemInfo.getProcessCpuUsage(pid)).thenReturn(mockedProcessLoad);
1012 initializeThingWithChannelAndPID(channnelID, acceptedItemType, pid);
1013 assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedProcessLoad);
1017 public void testThingHandlesChannelPriorityChange() {
1018 String priorityKey = "priority";
1019 String pidKey = "pid";
1020 String initialPriority = DEFAULT_CHANNEL_TEST_PRIORITY; // Evaluates to High
1021 String newPriority = "Low";
1023 String acceptedItemType = "Number";
1024 initializeThingWithChannel(DEFAULT_TEST_CHANNEL_ID, acceptedItemType);
1026 Channel channel = systemInfoThing.getChannel(DEFAULT_TEST_CHANNEL_ID);
1027 if (channel == null) {
1028 throw new AssertionError("Channel '" + DEFAULT_TEST_CHANNEL_ID + "' is null");
1031 waitForAssert(() -> {
1032 assertThat("The initial priority of channel " + channel.getUID() + " is not as expected.",
1033 channel.getConfiguration().get(priorityKey), is(equalTo(initialPriority)));
1034 assertThat(systemInfoHandler.getHighPriorityChannels().contains(channel.getUID()), is(true));
1037 // Change the priority of a channel, keep the pid
1038 Configuration updatedConfig = new Configuration();
1039 updatedConfig.put(priorityKey, newPriority);
1040 updatedConfig.put(pidKey, channel.getConfiguration().get(pidKey));
1041 Channel updatedChannel = ChannelBuilder.create(channel.getUID(), channel.getAcceptedItemType())
1042 .withType(channel.getChannelTypeUID()).withKind(channel.getKind()).withConfiguration(updatedConfig)
1045 Thing updatedThing = ThingBuilder.create(systemInfoThing.getThingTypeUID(), systemInfoThing.getUID())
1046 .withConfiguration(systemInfoThing.getConfiguration()).withChannel(updatedChannel).build();
1048 systemInfoHandler.thingUpdated(updatedThing);
1050 waitForAssert(() -> {
1051 assertThat("The prority of the channel was not updated: ", channel.getConfiguration().get(priorityKey),
1052 is(equalTo(newPriority)));
1053 assertThat(systemInfoHandler.getLowPriorityChannels().contains(channel.getUID()), is(true));