-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
SysteminfoOSGiTest.java
1168 lines (941 loc) · 52.1 KB
/
SysteminfoOSGiTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.systeminfo.test;
import static java.lang.Thread.sleep;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.*;
import java.math.BigDecimal;
import java.net.UnknownHostException;
import java.util.Hashtable;
import java.util.List;
import javax.measure.quantity.ElectricPotential;
import javax.measure.quantity.Frequency;
import javax.measure.quantity.Temperature;
import javax.measure.quantity.Time;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.binding.systeminfo.internal.SysteminfoBindingConstants;
import org.openhab.binding.systeminfo.internal.SysteminfoHandlerFactory;
import org.openhab.binding.systeminfo.internal.discovery.SysteminfoDiscoveryService;
import org.openhab.binding.systeminfo.internal.handler.SysteminfoHandler;
import org.openhab.binding.systeminfo.internal.model.DeviceNotFoundException;
import org.openhab.binding.systeminfo.internal.model.OSHISysteminfo;
import org.openhab.binding.systeminfo.internal.model.SysteminfoInterface;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.config.discovery.inbox.Inbox;
import org.openhab.core.config.discovery.inbox.InboxPredicates;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.library.dimension.DataAmount;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.Units;
import org.openhab.core.test.java.JavaOSGiTest;
import org.openhab.core.test.storage.VolatileStorageService;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.ManagedThingProvider;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingProvider;
import org.openhab.core.thing.ThingRegistry;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.thing.binding.builder.ThingBuilder;
import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.thing.link.ManagedItemChannelLinkProvider;
import org.openhab.core.thing.type.ChannelKind;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
/**
* OSGi tests for the {@link SysteminfoHandler}
*
* @author Svilen Valkanov - Initial contribution
* @author Lyubomir Papazov - Created a mock systeminfo object. This way, access to the user's OS will not be required,
* but mock data will be used instead, avoiding potential errors from the OS queries.
* @author Wouter Born - Migrate Groovy to Java tests
* @author Mark Herwege - Processor frequency channels
*/
@NonNullByDefault
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class SysteminfoOSGiTest extends JavaOSGiTest {
private static final String DEFAULT_TEST_THING_NAME = "work";
private static final String DEFAULT_TEST_ITEM_NAME = "test";
private static final String DEFAULT_CHANNEL_TEST_PRIORITY = "High";
private static final int DEFAULT_CHANNEL_PID = -1;
private static final String DEFAULT_TEST_CHANNEL_ID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
private static final int DEFAULT_DEVICE_INDEX = 0;
/**
* Refresh time in seconds for tasks with priority High.
* Default value for the parameter interval_high in the thing configuration
*/
private static final int DEFAULT_TEST_INTERVAL_HIGH = 1;
/**
* Refresh time in seconds for tasks with priority Medium.
*/
private static final int DEFAULT_TEST_INTERVAL_MEDIUM = 3;
private @Nullable Thing systeminfoThing;
private @Nullable GenericItem testItem;
private @Mock @NonNullByDefault({}) OSHISysteminfo mockedSystemInfo;
private @NonNullByDefault({}) SysteminfoHandlerFactory systeminfoHandlerFactory;
private @NonNullByDefault({}) ThingRegistry thingRegistry;
private @NonNullByDefault({}) ItemRegistry itemRegistry;
private @NonNullByDefault({}) ManagedThingProvider managedThingProvider;
private @NonNullByDefault({}) ManagedItemChannelLinkProvider itemChannelLinkProvider;
private @NonNullByDefault({}) UnitProvider unitProvider;
private @NonNullByDefault({}) VolatileStorageService volatileStorageService;
@BeforeEach
public void setUp() {
volatileStorageService = new VolatileStorageService();
registerService(volatileStorageService);
// Preparing the mock with OS properties, that are used in the initialize method of SysteminfoHandler
// Make this lenient because the assertInvalidThingConfigurationValuesAreHandled test does not require them
lenient().when(mockedSystemInfo.getCpuLogicalCores()).thenReturn(new DecimalType(1));
lenient().when(mockedSystemInfo.getCpuPhysicalCores()).thenReturn(new DecimalType(1));
lenient().when(mockedSystemInfo.getOsFamily()).thenReturn(new StringType("Mock OS"));
lenient().when(mockedSystemInfo.getOsManufacturer()).thenReturn(new StringType("Mock OS Manufacturer"));
lenient().when(mockedSystemInfo.getOsVersion()).thenReturn(new StringType("Mock Os Version"));
// Following mock method returns will make sure the thing does not get recreated with extra channels
lenient().when(mockedSystemInfo.getNetworkIFCount()).thenReturn(1);
lenient().when(mockedSystemInfo.getDisplayCount()).thenReturn(1);
lenient().when(mockedSystemInfo.getFileOSStoreCount()).thenReturn(1);
lenient().when(mockedSystemInfo.getPowerSourceCount()).thenReturn(1);
lenient().when(mockedSystemInfo.getDriveCount()).thenReturn(1);
lenient().when(mockedSystemInfo.getFanCount()).thenReturn(1);
registerService(mockedSystemInfo);
waitForAssert(() -> {
systeminfoHandlerFactory = getService(ThingHandlerFactory.class, SysteminfoHandlerFactory.class);
assertThat(systeminfoHandlerFactory, is(notNullValue()));
});
if (systeminfoHandlerFactory != null) {
// Unbind oshiSystemInfo service and bind the mock service to make the systeminfo binding tests independent
// of the external OSHI library
SysteminfoInterface oshiSystemInfo = getService(SysteminfoInterface.class);
if (oshiSystemInfo != null) {
systeminfoHandlerFactory.unbindSystemInfo(oshiSystemInfo);
}
systeminfoHandlerFactory.bindSystemInfo(mockedSystemInfo);
}
waitForAssert(() -> {
thingRegistry = getService(ThingRegistry.class);
assertThat(thingRegistry, is(notNullValue()));
});
waitForAssert(() -> {
itemRegistry = getService(ItemRegistry.class);
assertThat(itemRegistry, is(notNullValue()));
});
waitForAssert(() -> {
managedThingProvider = getService(ThingProvider.class, ManagedThingProvider.class);
assertThat(managedThingProvider, is(notNullValue()));
});
waitForAssert(() -> {
itemChannelLinkProvider = getService(ManagedItemChannelLinkProvider.class);
assertThat(itemChannelLinkProvider, is(notNullValue()));
});
waitForAssert(() -> {
unitProvider = getService(UnitProvider.class);
assertThat(unitProvider, is(notNullValue()));
});
}
@AfterEach
public void tearDown() {
Thing thing = systeminfoThing;
if (thing != null) {
// Remove the systeminfo thing. The handler will also be disposed automatically
Thing removedThing = thingRegistry.forceRemove(thing.getUID());
assertThat("The systeminfo thing cannot be deleted", removedThing, is(notNullValue()));
waitForAssert(() -> {
ThingHandler systemInfoHandler = thing.getHandler();
assertThat(systemInfoHandler, is(nullValue()));
});
managedThingProvider.remove(thing.getUID());
}
if (testItem != null) {
itemRegistry.remove(DEFAULT_TEST_ITEM_NAME);
}
unregisterService(mockedSystemInfo);
unregisterService(volatileStorageService);
}
private void initializeThingWithChannelAndPID(String channelID, String acceptedItemType, int pid) {
Configuration thingConfig = new Configuration();
thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
}
private void initializeThingWithChannelAndPriority(String channelID, String acceptedItemType, String priority) {
Configuration thingConfig = new Configuration();
thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
int pid = DEFAULT_CHANNEL_PID;
initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
}
private void initializeThingWithConfiguration(Configuration config) {
String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
String channelID = DEFAULT_TEST_CHANNEL_ID;
String acceptedItemType = "String";
int pid = DEFAULT_CHANNEL_PID;
initializeThing(config, channelID, acceptedItemType, priority, pid);
}
private void initializeThingWithChannel(String channelID, String acceptedItemType) {
Configuration thingConfig = new Configuration();
thingConfig.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME,
new BigDecimal(DEFAULT_TEST_INTERVAL_HIGH));
thingConfig.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
new BigDecimal(DEFAULT_TEST_INTERVAL_MEDIUM));
String priority = DEFAULT_CHANNEL_TEST_PRIORITY;
int pid = DEFAULT_CHANNEL_PID;
initializeThing(thingConfig, channelID, acceptedItemType, priority, pid);
}
private void initializeThing(Configuration thingConfiguration, String channelID, String acceptedItemType,
String priority, int pid) {
ThingTypeUID thingTypeUID = SysteminfoBindingConstants.THING_TYPE_COMPUTER;
ThingUID thingUID = new ThingUID(thingTypeUID, DEFAULT_TEST_THING_NAME);
ChannelUID channelUID = new ChannelUID(thingUID, channelID);
String channelTypeId = channelUID.getIdWithoutGroup();
if ("load1".equals(channelTypeId) || "load5".equals(channelTypeId) || "load15".equals(channelTypeId)) {
channelTypeId = "loadAverage";
}
ChannelTypeUID channelTypeUID = new ChannelTypeUID(SysteminfoBindingConstants.BINDING_ID, channelTypeId);
Configuration channelConfig = new Configuration();
channelConfig.put("priority", priority);
channelConfig.put("pid", new BigDecimal(pid));
Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID)
.withKind(ChannelKind.STATE).withConfiguration(channelConfig).build();
Thing thing = ThingBuilder.create(thingTypeUID, thingUID).withConfiguration(thingConfiguration)
.withChannel(channel).build();
systeminfoThing = thing;
managedThingProvider.add(thing);
waitForAssert(() -> {
SysteminfoHandler handler = (SysteminfoHandler) thing.getHandler();
assertThat(handler, is(notNullValue()));
});
waitForAssert(() -> {
assertThat("Thing is not initialized, before an Item is created", thing.getStatus(),
anyOf(equalTo(ThingStatus.OFFLINE), equalTo(ThingStatus.ONLINE)));
});
intializeItem(channelUID, DEFAULT_TEST_ITEM_NAME, acceptedItemType);
}
private void assertItemState(String acceptedItemType, String itemName, String priority, State expectedState) {
Thing thing = systeminfoThing;
if (thing == null) {
throw new AssertionError("Thing is null");
}
waitForAssert(() -> {
ThingStatusDetail thingStatusDetail = thing.getStatusInfo().getStatusDetail();
String description = thing.getStatusInfo().getDescription();
assertThat("Thing status detail is " + thingStatusDetail + " with description " + description,
thing.getStatus(), is(equalTo(ThingStatus.ONLINE)));
});
// The binding starts all refresh tasks in SysteminfoHandler.scheduleUpdates() after this delay !
try {
sleep(SysteminfoHandler.WAIT_TIME_CHANNEL_ITEM_LINK_INIT * 1000);
} catch (InterruptedException e) {
throw new AssertionError("Interrupted while sleeping");
}
GenericItem item;
try {
item = (GenericItem) itemRegistry.getItem(itemName);
} catch (ItemNotFoundException e) {
throw new AssertionError("Item not found in registry");
}
int waitTime;
if ("High".equals(priority)) {
waitTime = DEFAULT_TEST_INTERVAL_HIGH * 1000;
} else if ("Medium".equals(priority)) {
waitTime = DEFAULT_TEST_INTERVAL_MEDIUM * 1000;
} else {
waitTime = 100;
}
waitForAssert(() -> {
State itemState = item.getState();
assertThat(itemState, is(equalTo(expectedState)));
}, waitTime, DFL_SLEEP_TIME);
}
private void intializeItem(ChannelUID channelUID, String itemName, String acceptedItemType) {
GenericItem item = null;
if (acceptedItemType.startsWith("Number")) {
item = new NumberItem(acceptedItemType, itemName, unitProvider);
} else if ("String".equals(acceptedItemType)) {
item = new StringItem(itemName);
}
if (item == null) {
throw new AssertionError("Item is null");
}
itemRegistry.add(item);
testItem = item;
itemChannelLinkProvider.add(new ItemChannelLink(itemName, channelUID));
}
@Test
public void assertInvalidThingConfigurationValuesAreHandled() {
Configuration configuration = new Configuration();
// invalid value - must be positive
int refreshIntervalHigh = -5;
configuration.put(SysteminfoBindingConstants.HIGH_PRIORITY_REFRESH_TIME, new BigDecimal(refreshIntervalHigh));
int refreshIntervalMedium = 3;
configuration.put(SysteminfoBindingConstants.MEDIUM_PRIORITY_REFRESH_TIME,
new BigDecimal(refreshIntervalMedium));
initializeThingWithConfiguration(configuration);
testInvalidConfiguration();
}
private void testInvalidConfiguration() {
waitForAssert(() -> {
Thing thing = systeminfoThing;
if (thing != null) {
assertThat("Invalid configuration is used !", thing.getStatus(), is(equalTo(ThingStatus.OFFLINE)));
assertThat(thing.getStatusInfo().getStatusDetail(),
is(equalTo(ThingStatusDetail.HANDLER_INITIALIZING_ERROR)));
assertThat(thing.getStatusInfo().getDescription(), is(equalTo("@text/offline.cannot-initialize")));
}
});
}
@Test
public void assertMediumPriorityChannelIsUpdated() {
String channnelID = DEFAULT_TEST_CHANNEL_ID;
String acceptedItemType = "Number";
String priority = "Medium";
initializeThingWithChannelAndPriority(channnelID, acceptedItemType, priority);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, priority, UnDefType.UNDEF);
}
@Test
public void assertStateOfSecondDeviceIsUpdated() {
// This test assumes that at least 2 network interfaces are present on the test platform
int deviceIndex = 1;
String channnelID = "network" + deviceIndex + "#mac";
String acceptedItemType = "String";
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, UnDefType.UNDEF);
}
@Test
public void assertChannelCpuMaxFreq() {
String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_MAXFREQ;
String acceptedItemType = "Number:Frequency";
QuantityType<Frequency> mockedCpuMaxFreqValue = new QuantityType<>(2500, Units.HERTZ);
when(mockedSystemInfo.getCpuMaxFreq()).thenReturn(mockedCpuMaxFreqValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuMaxFreqValue);
}
@Test
public void assertChannelCpuFreq() {
String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_FREQ;
String acceptedItemType = "Number:Frequency";
QuantityType<Frequency> mockedCpuFreqValue = new QuantityType<>(2500, Units.HERTZ);
when(mockedSystemInfo.getCpuFreq(0)).thenReturn(mockedCpuFreqValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuFreqValue);
}
@Test
public void assertChannelCpuLoadIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD;
String acceptedItemType = "Number";
PercentType mockedCpuLoadValue = new PercentType(9);
when(mockedSystemInfo.getSystemCpuLoad()).thenReturn(mockedCpuLoadValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoadValue);
}
@Test
public void assertChannelCpuLoad1IsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_1;
String acceptedItemType = "Number";
DecimalType mockedCpuLoad1Value = new DecimalType(1.1);
when(mockedSystemInfo.getCpuLoad1()).thenReturn(mockedCpuLoad1Value);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad1Value);
}
@Test
public void assertChannelCpuLoad5IsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_5;
String acceptedItemType = "Number";
DecimalType mockedCpuLoad5Value = new DecimalType(5.5);
when(mockedSystemInfo.getCpuLoad5()).thenReturn(mockedCpuLoad5Value);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad5Value);
}
@Test
public void assertChannelCpuLoad15IsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_LOAD_15;
String acceptedItemType = "Number";
DecimalType mockedCpuLoad15Value = new DecimalType(15.15);
when(mockedSystemInfo.getCpuLoad15()).thenReturn(mockedCpuLoad15Value);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuLoad15Value);
}
@Test
public void assertChannelCpuThreadsIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_THREADS;
String acceptedItemType = "Number";
DecimalType mockedCpuThreadsValue = new DecimalType(16);
when(mockedSystemInfo.getCpuThreads()).thenReturn(mockedCpuThreadsValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuThreadsValue);
}
@Test
public void assertChannelCpuUptimeIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_UPTIME;
String acceptedItemType = "Number:Time";
QuantityType<Time> mockedCpuUptimeValue = new QuantityType<>(100, Units.MINUTE);
when(mockedSystemInfo.getCpuUptime()).thenReturn(mockedCpuUptimeValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuUptimeValue);
}
@Test
public void assertChannelCpuDescriptionIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_DESCRIPTION;
String acceptedItemType = "String";
StringType mockedCpuDescriptionValue = new StringType("Mocked Cpu Descr");
when(mockedSystemInfo.getCpuDescription()).thenReturn(mockedCpuDescriptionValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedCpuDescriptionValue);
}
@Test
public void assertChannelCpuNameIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_CPU_NAME;
String acceptedItemType = "String";
StringType mockedCpuNameValue = new StringType("Mocked Cpu Name");
when(mockedSystemInfo.getCpuName()).thenReturn(mockedCpuNameValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedCpuNameValue);
}
@Test
public void assertChannelMemoryAvailableIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedMemoryAvailableValue = new QuantityType<>(1000, Units.MEBIBYTE);
when(mockedSystemInfo.getMemoryAvailable()).thenReturn(mockedMemoryAvailableValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedMemoryAvailableValue);
}
@Test
public void assertChannelMemoryUsedIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_USED;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedMemoryUsedValue = new QuantityType<>(24, Units.MEBIBYTE);
when(mockedSystemInfo.getMemoryUsed()).thenReturn(mockedMemoryUsedValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedMemoryUsedValue);
}
@Test
public void assertChannelMemoryTotalIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_TOTAL;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedMemoryTotalValue = new QuantityType<>(1024, Units.MEBIBYTE);
when(mockedSystemInfo.getMemoryTotal()).thenReturn(mockedMemoryTotalValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedMemoryTotalValue);
}
@Test
public void assertChannelMemoryAvailablePercentIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_MEMORY_AVAILABLE_PERCENT;
String acceptedItemType = "Number";
PercentType mockedMemoryAvailablePercentValue = new PercentType(97);
when(mockedSystemInfo.getMemoryAvailablePercent()).thenReturn(mockedMemoryAvailablePercentValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedMemoryAvailablePercentValue);
}
@Test
public void assertChannelSwapAvailableIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedSwapAvailableValue = new QuantityType<>(482, Units.MEBIBYTE);
when(mockedSystemInfo.getSwapAvailable()).thenReturn(mockedSwapAvailableValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedSwapAvailableValue);
}
@Test
public void assertChannelSwapUsedIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_USED;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedSwapUsedValue = new QuantityType<>(30, Units.MEBIBYTE);
when(mockedSystemInfo.getSwapUsed()).thenReturn(mockedSwapUsedValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapUsedValue);
}
@Test
public void assertChannelSwapTotalIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_TOTAL;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedSwapTotalValue = new QuantityType<>(512, Units.MEBIBYTE);
when(mockedSystemInfo.getSwapTotal()).thenReturn(mockedSwapTotalValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedSwapTotalValue);
}
@Test
public void assertChannelSwapAvailablePercentIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_SWAP_AVAILABLE_PERCENT;
String acceptedItemType = "Number";
PercentType mockedSwapAvailablePercentValue = new PercentType(94);
when(mockedSystemInfo.getSwapAvailablePercent()).thenReturn(mockedSwapAvailablePercentValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedSwapAvailablePercentValue);
}
@Test
public void assertChannelStorageNameIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_NAME;
String acceptedItemType = "String";
StringType mockedStorageName = new StringType("Mocked Storage Name");
when(mockedSystemInfo.getStorageName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageName);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageName);
}
@Test
public void assertChannelStorageTypeIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TYPE;
String acceptedItemType = "String";
StringType mockedStorageType = new StringType("Mocked Storage Type");
when(mockedSystemInfo.getStorageType(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageType);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedStorageType);
}
@Test
public void assertChannelStorageDescriptionIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_DESCRIPTION;
String acceptedItemType = "String";
StringType mockedStorageDescription = new StringType("Mocked Storage Description");
when(mockedSystemInfo.getStorageDescription(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageDescription);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedStorageDescription);
}
@Test
public void assertChannelStorageAvailableIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedStorageAvailableValue = new QuantityType<>(2000, Units.MEBIBYTE);
when(mockedSystemInfo.getStorageAvailable(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageAvailableValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedStorageAvailableValue);
}
@Test
public void assertChannelStorageUsedIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_USED;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedStorageUsedValue = new QuantityType<>(500, Units.MEBIBYTE);
when(mockedSystemInfo.getStorageUsed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageUsedValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedStorageUsedValue);
}
@Test
public void assertChannelStorageTotalIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_TOTAL;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedStorageTotalValue = new QuantityType<>(2500, Units.MEBIBYTE);
when(mockedSystemInfo.getStorageTotal(DEFAULT_DEVICE_INDEX)).thenReturn(mockedStorageTotalValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedStorageTotalValue);
}
@Test
public void assertChannelStorageAvailablePercentIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_STORAGE_AVAILABLE_PERCENT;
String acceptedItemType = "Number";
PercentType mockedStorageAvailablePercent = new PercentType(20);
when(mockedSystemInfo.getStorageAvailablePercent(DEFAULT_DEVICE_INDEX))
.thenReturn(mockedStorageAvailablePercent);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedStorageAvailablePercent);
}
@Test
public void assertChannelDriveNameIsUpdated() throws DeviceNotFoundException {
String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_NAME;
String acceptedItemType = "String";
StringType mockedDriveNameValue = new StringType("Mocked Drive Name");
when(mockedSystemInfo.getDriveName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveNameValue);
initializeThingWithChannel(channelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveNameValue);
}
@Test
public void assertChannelDriveModelIsUpdated() throws DeviceNotFoundException {
String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_MODEL;
String acceptedItemType = "String";
StringType mockedDriveModelValue = new StringType("Mocked Drive Model");
when(mockedSystemInfo.getDriveModel(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveModelValue);
initializeThingWithChannel(channelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDriveModelValue);
}
@Test
public void assertChannelDriveSerialIsUpdated() throws DeviceNotFoundException {
String channelID = SysteminfoBindingConstants.CHANNEL_DRIVE_SERIAL;
String acceptedItemType = "String";
StringType mockedDriveSerialNumber = new StringType("Mocked Drive Serial Number");
when(mockedSystemInfo.getDriveSerialNumber(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDriveSerialNumber);
initializeThingWithChannel(channelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedDriveSerialNumber);
}
// Re-enable this previously disabled test, as it is not relying on hardware anymore, but a mocked object
// There is a bug opened for this issue - https://github.com/dblock/oshi/issues/185
@Test
public void assertChannelSensorsCpuTempIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_CPU_TEMPERATURE;
String acceptedItemType = "Number:Temperature";
QuantityType<Temperature> mockedSensorsCpuTemperatureValue = new QuantityType<>(60, SIUnits.CELSIUS);
when(mockedSystemInfo.getSensorsCpuTemperature()).thenReturn(mockedSensorsCpuTemperatureValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedSensorsCpuTemperatureValue);
}
@Test
public void assertChannelSensorsCpuVoltageIsUpdated() {
String channnelID = SysteminfoBindingConstants.CHANNEL_SENOSRS_CPU_VOLTAGE;
String acceptedItemType = "Number:ElectricPotential";
QuantityType<ElectricPotential> mockedSensorsCpuVoltageValue = new QuantityType<>(1000, Units.VOLT);
when(mockedSystemInfo.getSensorsCpuVoltage()).thenReturn(mockedSensorsCpuVoltageValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedSensorsCpuVoltageValue);
}
@Test
public void assertChannelSensorsFanSpeedIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_SENSORS_FAN_SPEED;
String acceptedItemType = "Number";
DecimalType mockedSensorsCpuFanSpeedValue = new DecimalType(180);
when(mockedSystemInfo.getSensorsFanSpeed(DEFAULT_DEVICE_INDEX)).thenReturn(mockedSensorsCpuFanSpeedValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedSensorsCpuFanSpeedValue);
}
@Test
public void assertChannelBatteryNameIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_NAME;
String acceptedItemType = "String";
StringType mockedBatteryName = new StringType("Mocked Battery Name");
when(mockedSystemInfo.getBatteryName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryName);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedBatteryName);
}
@Test
public void assertChannelBatteryRemainingCapacityIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_CAPACITY;
String acceptedItemType = "Number";
PercentType mockedBatteryRemainingCapacity = new PercentType(20);
when(mockedSystemInfo.getBatteryRemainingCapacity(DEFAULT_DEVICE_INDEX))
.thenReturn(mockedBatteryRemainingCapacity);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedBatteryRemainingCapacity);
}
@Test
public void assertChannelBatteryRemainingTimeIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_BATTERY_REMAINING_TIME;
String acceptedItemType = "Number:Time";
QuantityType<Time> mockedBatteryRemainingTime = new QuantityType<>(3600, Units.MINUTE);
when(mockedSystemInfo.getBatteryRemainingTime(DEFAULT_DEVICE_INDEX)).thenReturn(mockedBatteryRemainingTime);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedBatteryRemainingTime);
}
@Test
public void assertChannelDisplayInformationIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_DISPLAY_INFORMATION;
String acceptedItemType = "String";
StringType mockedDisplayInfo = new StringType("Mocked Display Information");
when(mockedSystemInfo.getDisplayInformation(DEFAULT_DEVICE_INDEX)).thenReturn(mockedDisplayInfo);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedDisplayInfo);
}
@Test
public void assertChannelNetworkIpIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_IP;
String acceptedItemType = "String";
StringType mockedNetworkIp = new StringType("192.168.1.0");
when(mockedSystemInfo.getNetworkIp(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkIp);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkIp);
}
@Test
public void assertChannelNetworkMacIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_MAC;
String acceptedItemType = "String";
StringType mockedNetworkMacValue = new StringType("AB-10-11-12-13-14");
when(mockedSystemInfo.getNetworkMac(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkMacValue);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkMacValue);
}
@Test
public void assertChannelNetworkDataSentIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_SENT;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedNetworkDataSent = new QuantityType<>(1000, Units.MEBIBYTE);
when(mockedSystemInfo.getNetworkDataSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataSent);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkDataSent);
}
@Test
public void assertChannelNetworkDataReceivedIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_DATA_RECEIVED;
String acceptedItemType = "Number:DataAmount";
QuantityType<DataAmount> mockedNetworkDataReceiveed = new QuantityType<>(800, Units.MEBIBYTE);
when(mockedSystemInfo.getNetworkDataReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkDataReceiveed);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedNetworkDataReceiveed);
}
@Test
public void assertChannelNetworkPacketsSentIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_SENT;
String acceptedItemType = "Number";
DecimalType mockedNetworkPacketsSent = new DecimalType(50);
when(mockedSystemInfo.getNetworkPacketsSent(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsSent);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedNetworkPacketsSent);
}
@Test
public void assertChannelNetworkPacketsReceivedIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_PACKETS_RECEIVED;
String acceptedItemType = "Number";
DecimalType mockedNetworkPacketsReceived = new DecimalType(48);
when(mockedSystemInfo.getNetworkPacketsReceived(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkPacketsReceived);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedNetworkPacketsReceived);
}
@Test
public void assertChannelNetworkNetworkNameIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_NAME;
String acceptedItemType = "String";
StringType mockedNetworkName = new StringType("MockN-AQ34");
when(mockedSystemInfo.getNetworkName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkName);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY, mockedNetworkName);
}
@Test
public void assertChannelNetworkNetworkDisplayNameIsUpdated() throws DeviceNotFoundException {
String channnelID = SysteminfoBindingConstants.CHANNEL_NETWORK_ADAPTER_NAME;
String acceptedItemType = "String";
StringType mockedNetworkAdapterName = new StringType("Mocked Network Adapter Name");
when(mockedSystemInfo.getNetworkDisplayName(DEFAULT_DEVICE_INDEX)).thenReturn(mockedNetworkAdapterName);
initializeThingWithChannel(channnelID, acceptedItemType);
assertItemState(acceptedItemType, DEFAULT_TEST_ITEM_NAME, DEFAULT_CHANNEL_TEST_PRIORITY,
mockedNetworkAdapterName);
}
class SysteminfoDiscoveryServiceMock extends SysteminfoDiscoveryService {
String hostname;
SysteminfoDiscoveryServiceMock(String hostname) {
super();
this.hostname = hostname;
}
@Override
protected String getHostName() throws UnknownHostException {
if ("unresolved".equals(hostname)) {
throw new UnknownHostException();
}
return hostname;
}
@Override
public void startScan() {
super.startScan();
}
}
@Test
public void testDiscoveryWithInvalidHostname() {
String hostname = "Hilo.fritz.box";
String expectedHostname = "Hilo_fritz_box";
testDiscoveryService(expectedHostname, hostname);
}
@Test
public void testDiscoveryWithValidHostname() {
String hostname = "MyComputer";
String expectedHostname = "MyComputer";
testDiscoveryService(expectedHostname, hostname);
}
@Test
public void testDiscoveryWithUnresolvedHostname() {
String hostname = "unresolved";
String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
testDiscoveryService(expectedHostname, hostname);
}
@Test
public void testDiscoveryWithEmptyHostnameString() {
String hostname = "";
String expectedHostname = SysteminfoDiscoveryService.DEFAULT_THING_ID;
testDiscoveryService(expectedHostname, hostname);
}
private void testDiscoveryService(String expectedHostname, String hostname) {
SysteminfoDiscoveryService discoveryService = getService(DiscoveryService.class,
SysteminfoDiscoveryService.class);
waitForAssert(() -> {