Skip to content

Commit 1dc6319

Browse files
authored
[sensibo] Fix initialization for half-included devices (#20169)
* Fix #18018 Signed-off-by: Arne Seime <arne.seime@gmail.com>
1 parent bb961d4 commit 1dc6319

5 files changed

Lines changed: 1376 additions & 36 deletions

File tree

bundles/org.openhab.binding.sensibo/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@
2121
<version>2.27.2</version>
2222
<scope>test</scope>
2323
</dependency>
24+
<dependency>
25+
<groupId>ch.qos.logback</groupId>
26+
<artifactId>logback-classic</artifactId>
27+
<version>1.2.13</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.awaitility</groupId>
32+
<artifactId>awaitility</artifactId>
33+
<version>4.2.0</version>
34+
<scope>test</scope>
35+
</dependency>
2436
</dependencies>
2537

2638
</project>

bundles/org.openhab.binding.sensibo/src/main/java/org/openhab/binding/sensibo/internal/dto/poddetails/PodDetailsDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class PodDetailsDTO {
4141
private ModeCapabilityWrapperDTO remoteCapabilities;
4242

4343
public Map<String, ModeCapabilityDTO> getRemoteCapabilities() {
44-
return remoteCapabilities.modes;
44+
return remoteCapabilities != null ? remoteCapabilities.modes : null;
4545
}
4646

4747
public boolean isAlive() {

bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/handler/SensiboAccountHandlerTest.java

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
package org.openhab.binding.sensibo.internal.handler;
1414

1515
import 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;
1817
import static org.mockito.Mockito.when;
1918

2019
import java.io.IOException;
2120
import java.nio.charset.StandardCharsets;
22-
import java.util.List;
21+
import java.util.Map;
2322

2423
import org.eclipse.jetty.client.HttpClient;
2524
import org.junit.jupiter.api.AfterEach;
@@ -28,8 +27,6 @@
2827
import org.junit.jupiter.api.extension.ExtendWith;
2928
import org.mockito.Mock;
3029
import org.mockito.junit.jupiter.MockitoExtension;
31-
import org.openhab.binding.sensibo.internal.config.SensiboAccountConfiguration;
32-
import org.openhab.binding.sensibo.internal.model.SensiboSky;
3330
import org.openhab.core.config.core.Configuration;
3431
import org.openhab.core.thing.Bridge;
3532
import 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

Comments
 (0)