88import java .io .IOException ;
99import java .util .Arrays ;
1010import java .util .Collections ;
11+ import java .util .List ;
1112import org .junit .jupiter .api .BeforeEach ;
1213import org .junit .jupiter .api .Test ;
1314import org .mockito .Mock ;
@@ -53,7 +54,7 @@ public void testGetDeviceNameFromIdWithUnknownDevice() throws IOException {
5354 device .setDeviceName ("Known Device" );
5455
5556 SwitchBotDeviceApi spyDeviceApi = spy (deviceApi );
56- doReturn (Arrays . asList (device )).when (spyDeviceApi ).getDevices ();
57+ doReturn (List . of (device )).when (spyDeviceApi ).getDevices ();
5758
5859 String result = spyDeviceApi .getDeviceNameFromId ("unknown-device" );
5960 assertEquals ("unknown-device" , result );
@@ -68,7 +69,7 @@ public void testGetDeviceNameFromIdCacheExpiry() throws IOException {
6869 SwitchBotDeviceApi spyDeviceApi = spy (deviceApi );
6970 spyDeviceApi .deviceIdToNamesCacheTime = System .currentTimeMillis () - (ITimeConstants .HOUR * 2 );
7071
71- doReturn (Arrays . asList (device )).when (spyDeviceApi ).getDevices ();
72+ doReturn (List . of (device )).when (spyDeviceApi ).getDevices ();
7273
7374 String result = spyDeviceApi .getDeviceNameFromId ("device1" );
7475 assertEquals ("Test Device" , result );
@@ -85,7 +86,7 @@ public void testGetDeviceNameFromIdWithValidCache() throws IOException {
8586 SwitchBotDeviceApi spyDeviceApi = spy (deviceApi );
8687 spyDeviceApi .deviceIdToNamesCacheTime = System .currentTimeMillis ();
8788
88- doReturn (Arrays . asList (device )).when (spyDeviceApi ).getDevices ();
89+ doReturn (List . of (device )).when (spyDeviceApi ).getDevices ();
8990 spyDeviceApi .getDeviceNameFromId ("device1" );
9091
9192 reset (spyDeviceApi );
@@ -112,9 +113,7 @@ public void testGetDeviceStatusWithNullDeviceId() throws IOException {
112113
113114 @ Test
114115 public void testGetDeviceStatusInputValidation () {
115- assertDoesNotThrow (() -> {
116- assertNotNull (deviceApi );
117- });
116+ assertDoesNotThrow (() -> assertNotNull (deviceApi ));
118117 }
119118
120119 @ Test
@@ -145,18 +144,14 @@ public void testConcurrentCacheRefresh() throws InterruptedException {
145144 device .setDeviceId ("concurrent-device" );
146145 device .setDeviceName ("Concurrent Test" );
147146 try {
148- doReturn (Arrays . asList (device )).when (spyDeviceApi ).getDevices ();
147+ doReturn (List . of (device )).when (spyDeviceApi ).getDevices ();
149148 } catch (IOException e ) {
150149 fail ("Setup failed: " + e .getMessage ());
151150 }
152151
153- Thread thread1 = new Thread (() -> {
154- spyDeviceApi .getDeviceNameFromId ("concurrent-device" );
155- });
152+ Thread thread1 = new Thread (() -> spyDeviceApi .getDeviceNameFromId ("concurrent-device" ));
156153
157- Thread thread2 = new Thread (() -> {
158- spyDeviceApi .getDeviceNameFromId ("concurrent-device" );
159- });
154+ Thread thread2 = new Thread (() -> spyDeviceApi .getDeviceNameFromId ("concurrent-device" ));
160155
161156 thread1 .start ();
162157 thread2 .start ();
@@ -192,4 +187,83 @@ public void testGetDeviceStatusReturnsNullForNullInput() throws IOException {
192187 public void testDeviceApiNotNull () {
193188 assertNotNull (deviceApi );
194189 }
190+
191+ @ Test
192+ public void testParseResponseWithIOException () {
193+ SwitchBotDeviceApi spyDeviceApi = spy (deviceApi );
194+ when (mockSwitchBotApi .getMoshi ()).thenReturn (new com .squareup .moshi .Moshi .Builder ().build ());
195+
196+ DeviceCommand command = new DeviceCommand ("turnOn" , "default" );
197+ assertDoesNotThrow (() -> {
198+ String json =
199+ mockSwitchBotApi .getMoshi ().adapter (DeviceCommand .class ).toJson (command );
200+ assertNotNull (json );
201+ });
202+ }
203+
204+ @ Test
205+ public void testSendDeviceControlCommandsWithValidInput () {
206+ DeviceCommand command = new DeviceCommand ("turnOn" , "default" );
207+ String deviceId = "valid-device-id" ;
208+
209+ when (mockSwitchBotApi .getMoshi ()).thenReturn (new com .squareup .moshi .Moshi .Builder ().build ());
210+
211+ assertNotNull (command .getCommand ());
212+ assertNotNull (command .getParameter ());
213+ assertNotNull (deviceId );
214+ assertEquals ("turnOn" , command .getCommand ());
215+ assertEquals ("default" , command .getParameter ());
216+ }
217+
218+ @ Test
219+ public void testDeviceStatusValidation () throws IOException {
220+ assertNull (deviceApi .getDeviceStatus (null ));
221+ }
222+
223+ @ Test
224+ public void testGetDeviceNameFromIdWithNullDeviceNameMap () throws IOException {
225+ SwitchBotDeviceApi spyDeviceApi = spy (deviceApi );
226+ spyDeviceApi .deviceIdToNamesCacheTime = -1 ;
227+
228+ doThrow (new IOException ("Network error" )).when (spyDeviceApi ).getDevices ();
229+
230+ String result = spyDeviceApi .getDeviceNameFromId ("test-device" );
231+ assertEquals ("test-device" , result );
232+ }
233+
234+ @ Test
235+ public void testCacheTimeValidation () {
236+ SwitchBotDeviceApi spyDeviceApi = spy (deviceApi );
237+
238+ spyDeviceApi .deviceIdToNamesCacheTime = System .currentTimeMillis () - (ITimeConstants .HOUR * 2 );
239+ assertTrue (spyDeviceApi .deviceIdToNamesCacheTime < System .currentTimeMillis () - ITimeConstants .HOUR );
240+
241+ spyDeviceApi .deviceIdToNamesCacheTime = System .currentTimeMillis ();
242+ assertTrue (spyDeviceApi .deviceIdToNamesCacheTime > System .currentTimeMillis () - ITimeConstants .HOUR );
243+ }
244+
245+ @ Test
246+ public void testDeviceApiConstructor () {
247+ SwitchBotDeviceApi newDeviceApi = new SwitchBotDeviceApi (mockSwitchBotApi );
248+ assertNotNull (newDeviceApi );
249+ assertEquals (-1 , newDeviceApi .deviceIdToNamesCacheTime );
250+ }
251+
252+ @ Test
253+ public void testGetDeviceStatusWithEmptyDeviceId () {
254+ assertDoesNotThrow (() -> assertNotNull (deviceApi ));
255+ }
256+
257+ @ Test
258+ public void testGetDeviceNameFromIdWithCacheRefreshFailure () throws IOException {
259+ SwitchBotDeviceApi spyDeviceApi = spy (deviceApi );
260+
261+ spyDeviceApi .deviceIdToNamesCacheTime = System .currentTimeMillis () - (ITimeConstants .HOUR * 2 );
262+
263+ doThrow (new IOException ("Network failure" )).when (spyDeviceApi ).getDevices ();
264+
265+ String result = spyDeviceApi .getDeviceNameFromId ("test-device" );
266+ assertEquals ("test-device" , result );
267+ assertEquals (-1 , spyDeviceApi .deviceIdToNamesCacheTime );
268+ }
195269}
0 commit comments