1313package org .openhab .binding .sensibo .internal .handler ;
1414
1515import static com .github .tomakehurst .wiremock .client .WireMock .*;
16- import static org .junit .jupiter .api .Assertions .assertEquals ;
17- import static org .mockito .ArgumentMatchers .eq ;
16+ import static org .awaitility .Awaitility .await ;
1817import static org .mockito .Mockito .when ;
1918
2019import java .io .IOException ;
2120import java .nio .charset .StandardCharsets ;
22- import java .util .List ;
21+ import java .util .Map ;
2322
2423import org .eclipse .jetty .client .HttpClient ;
2524import org .junit .jupiter .api .AfterEach ;
2827import org .junit .jupiter .api .extension .ExtendWith ;
2928import org .mockito .Mock ;
3029import org .mockito .junit .jupiter .MockitoExtension ;
31- import org .openhab .binding .sensibo .internal .config .SensiboAccountConfiguration ;
32- import org .openhab .binding .sensibo .internal .model .SensiboSky ;
3330import org .openhab .core .config .core .Configuration ;
3431import org .openhab .core .thing .Bridge ;
3532import org .openhab .core .thing .ThingUID ;
@@ -48,7 +45,7 @@ public class SensiboAccountHandlerTest {
4845 private HttpClient httpClient ;
4946
5047 private @ Mock Configuration configuration ;
51- private @ Mock Bridge sensiboAccountMock ;
48+ private @ Mock Bridge bridgeMock ;
5249
5350 @ BeforeEach
5451 public void setUp () throws Exception {
@@ -70,21 +67,27 @@ public void shutdown() throws Exception {
7067 }
7168
7269 @ Test
73- public void testInitialize1 () throws InterruptedException , IOException {
74- testInitialize ("/get_pods_response.json" , "/get_pod_details_response.json" );
70+ public void testInitialize1 () throws IOException {
71+ testInitialize ("/get_pods_response.json" , 1 );
7572 }
7673
7774 @ Test
78- public void testInitializeMarco () throws InterruptedException , IOException {
79- testInitialize ("/get_pods_response.json" , "/get_pod_details_response_marco.json" );
75+ public void testInitializeMarco () throws IOException {
76+ testInitialize ("/get_pods_response.json" , 1 );
8077 }
8178
82- private void testInitialize (String podsResponse , String podDetailsResponse )
83- throws InterruptedException , IOException {
84- // Setup account
85- final SensiboAccountConfiguration accountConfig = new SensiboAccountConfiguration ();
86- accountConfig .apiKey = "APIKEY" ;
87- when (configuration .as (eq (SensiboAccountConfiguration .class ))).thenReturn (accountConfig );
79+ /**
80+ * See github issue #18018 - test that initialization can handle a response where some pods are not fully setup and
81+ * thus missing some fields
82+ *
83+ * @throws IOException if the mocked response file cannot be read
84+ */
85+ @ Test
86+ public void testInitialize_Issue18018_Partial_Setup () throws IOException {
87+ testInitialize ("/get_pods_response_partial_setup.json" , 4 );
88+ }
89+
90+ private void testInitialize (String podsResponse , int numExpectedPods ) throws IOException {
8891
8992 // Setup initial response
9093 final String getPodsResponse = new String (getClass ().getResourceAsStream (podsResponse ).readAllBytes (),
@@ -93,26 +96,15 @@ private void testInitialize(String podsResponse, String podDetailsResponse)
9396 .withHeader ("Accept-Encoding" , equalTo ("gzip" ))
9497 .willReturn (aResponse ().withStatus (200 ).withBody (getPodsResponse )));
9598
96- when (sensiboAccountMock .getConfiguration ()).thenReturn (configuration );
97- when (sensiboAccountMock .getUID ()).thenReturn (new ThingUID ("sensibo:account:thinguid" ));
99+ // Setup account
100+ when (configuration .getProperties ()).thenReturn (Map .of ("apiKey" , "APIKEY" ));
101+ when (bridgeMock .getConfiguration ()).thenReturn (configuration );
102+ when (bridgeMock .getUID ()).thenReturn (new ThingUID ("sensibo:account:thinguid" ));
103+
104+ final SensiboAccountHandler handler = new SensiboAccountHandler (bridgeMock , httpClient );
105+ handler .initialize ();
98106
99- final SensiboAccountHandler subject = new SensiboAccountHandler (sensiboAccountMock , httpClient );
100107 // Async, poll for status
101- subject .initialize ();
102-
103- // Verify num things found == 1
104- int numPods = 0 ;
105- for (int i = 0 ; i < 20 ; i ++) {
106- final List <SensiboSky > things = subject .getModel ().getPods ();
107- numPods = things .size ();
108- if (numPods == 1 ) {
109- break ;
110- } else {
111- // Wait some more
112- Thread .sleep (200 );
113- }
114- }
115-
116- assertEquals (1 , numPods );
108+ await ().until (() -> handler .getModel ().getPods ().size () == numExpectedPods );
117109 }
118110}
0 commit comments