]> git.basschouten.com Git - openhab-addons.git/blob
654e37820bdfefaa2bf43899af2f8cc764feb766
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mielecloud.internal.config.servlet;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.mielecloud.internal.util.ReflectionUtil.setPrivate;
19
20 import java.util.Objects;
21 import java.util.Optional;
22 import java.util.concurrent.CompletableFuture;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeoutException;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
29 import org.openhab.binding.mielecloud.internal.auth.OAuthTokenRefresher;
30 import org.openhab.binding.mielecloud.internal.config.MieleCloudConfigService;
31 import org.openhab.binding.mielecloud.internal.util.AbstractConfigFlowTest;
32 import org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants;
33 import org.openhab.binding.mielecloud.internal.util.Website;
34 import org.openhab.core.config.discovery.inbox.Inbox;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingRegistry;
37 import org.openhab.core.thing.binding.ThingHandler;
38
39 /**
40  * @author Björn Lange - Initial Contribution
41  */
42 @NonNullByDefault
43 public class CreateBridgeServletTest extends AbstractConfigFlowTest {
44     private void whenBridgeCreationFailsThenAWarningIsShownOnTheSuccessPage(
45             CompletableFuture<Boolean> addInboxEntryResult) throws Exception {
46         // given:
47         MieleCloudConfigService configService = getService(MieleCloudConfigService.class);
48         assertNotNull(configService);
49
50         CreateBridgeServlet createBridgeServlet = configService.getCreateBridgeServlet();
51         assertNotNull(createBridgeServlet);
52
53         ThingRegistry thingRegistry = mock(ThingRegistry.class);
54         when(thingRegistry.get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID)).thenReturn(null);
55         setPrivate(Objects.requireNonNull(createBridgeServlet), "thingRegistry", thingRegistry);
56
57         Inbox inbox = mock(Inbox.class);
58         when(inbox.add(any())).thenReturn(addInboxEntryResult);
59         setPrivate(Objects.requireNonNull(createBridgeServlet), "inbox", inbox);
60
61         // when:
62         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
63                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
64                 + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
65                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
66
67         // then:
68         assertTrue(website.contains("Pairing successful!"));
69         assertTrue(website.contains(
70                 "Could not auto configure the bridge. Failed to approve the bridge from the inbox. Please try the configuration flow again."));
71     }
72
73     @Test
74     public void whenBridgeCreationFailsBecauseInboxEntryCannotBeAddedThenAWarningIsShownOnTheSuccessPage()
75             throws Exception {
76         whenBridgeCreationFailsThenAWarningIsShownOnTheSuccessPage(CompletableFuture.completedFuture(false));
77     }
78
79     @Test
80     public void whenBridgeCreationFailsBecauseInboxEntryAddResultIsNullThenAWarningIsShownOnTheSuccessPage()
81             throws Exception {
82         whenBridgeCreationFailsThenAWarningIsShownOnTheSuccessPage(CompletableFuture.completedFuture(null));
83     }
84
85     @SuppressWarnings("unchecked")
86     private CompletableFuture<Boolean> mockBooleanResultCompletableFuture() {
87         return mock(CompletableFuture.class);
88     }
89
90     @Test
91     public void whenBridgeCreationFailBecauseInboxEntryCreationIsInterruptedThenAWarningIsShownOnTheSuccessPage()
92             throws Exception {
93         CompletableFuture<Boolean> future = mockBooleanResultCompletableFuture();
94         when(future.get(anyLong(), any())).thenThrow(new InterruptedException());
95
96         whenBridgeCreationFailsThenAWarningIsShownOnTheSuccessPage(future);
97     }
98
99     @Test
100     public void whenBridgeCreationFailBecauseInboxEntryCreationFailsThenAWarningIsShownOnTheSuccessPage()
101             throws Exception {
102         CompletableFuture<Boolean> future = mockBooleanResultCompletableFuture();
103         when(future.get(anyLong(), any())).thenThrow(new ExecutionException(new NullPointerException()));
104
105         whenBridgeCreationFailsThenAWarningIsShownOnTheSuccessPage(future);
106     }
107
108     @Test
109     public void whenBridgeCreationFailBecauseInboxEntryCreationTimesOutThenAWarningIsShownOnTheSuccessPage()
110             throws Exception {
111         CompletableFuture<Boolean> future = mockBooleanResultCompletableFuture();
112         when(future.get(anyLong(), any())).thenThrow(new TimeoutException());
113
114         whenBridgeCreationFailsThenAWarningIsShownOnTheSuccessPage(future);
115     }
116
117     @Test
118     public void whenBridgeCreationFailBecauseInboxApprovalFailsThenAWarningIsShownOnTheSuccessPage() throws Exception {
119         // given:
120         MieleCloudConfigService configService = getService(MieleCloudConfigService.class);
121         assertNotNull(configService);
122
123         CreateBridgeServlet createBridgeServlet = configService.getCreateBridgeServlet();
124         assertNotNull(createBridgeServlet);
125
126         ThingRegistry thingRegistry = mock(ThingRegistry.class);
127         when(thingRegistry.get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID)).thenReturn(null);
128         setPrivate(Objects.requireNonNull(createBridgeServlet), "thingRegistry", thingRegistry);
129
130         Inbox inbox = mock(Inbox.class);
131         when(inbox.add(any())).thenReturn(CompletableFuture.completedFuture(true));
132         when(inbox.approve(any(), anyString(), anyString())).thenReturn(null);
133         setPrivate(Objects.requireNonNull(createBridgeServlet), "inbox", inbox);
134
135         // when:
136         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
137                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
138                 + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
139                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
140
141         // then:
142         assertTrue(website.contains("Pairing successful!"));
143         assertTrue(website.contains(
144                 "Could not auto configure the bridge. Failed to approve the bridge from the inbox. Please try the configuration flow again."));
145     }
146
147     @Test
148     public void whenBridgeReconfigurationFailsDueToMissingBridgeHandlerThenAWarningIsShownOnTheSuccessPage()
149             throws Exception {
150         // given:
151         MieleCloudConfigService configService = getService(MieleCloudConfigService.class);
152         assertNotNull(configService);
153
154         CreateBridgeServlet createBridgeServlet = configService.getCreateBridgeServlet();
155         assertNotNull(createBridgeServlet);
156
157         Inbox inbox = mock(Inbox.class);
158         setPrivate(Objects.requireNonNull(createBridgeServlet), "inbox", inbox);
159
160         Thing bridge = mock(Thing.class);
161         when(bridge.getHandler()).thenReturn(null);
162
163         ThingRegistry thingRegistry = mock(ThingRegistry.class);
164         when(thingRegistry.get(any())).thenReturn(bridge);
165         setPrivate(Objects.requireNonNull(createBridgeServlet), "thingRegistry", thingRegistry);
166
167         // when:
168         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
169                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
170                 + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
171                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
172
173         // then:
174         assertTrue(website.contains("Pairing successful!"));
175         assertTrue(website.contains(
176                 "Could not auto reconfigure the bridge. Bridge thing or thing handler is not available. Please try the configuration flow again."));
177     }
178
179     @Test
180     public void whenBridgeIsReconfiguredThenTheConfigurationParametersAreUpdatedAndTheOverviewPageIsDisplayed()
181             throws Exception {
182         // given:
183         setUpBridge();
184
185         MieleCloudConfigService configService = getService(MieleCloudConfigService.class);
186         assertNotNull(configService);
187
188         CreateBridgeServlet createBridgeServlet = configService.getCreateBridgeServlet();
189         assertNotNull(createBridgeServlet);
190
191         OAuthTokenRefresher tokenRefresher = mock(OAuthTokenRefresher.class);
192         when(tokenRefresher.getAccessTokenFromStorage(anyString()))
193                 .thenReturn(Optional.of(MieleCloudBindingIntegrationTestConstants.ALTERNATIVE_ACCESS_TOKEN));
194
195         Thing bridge = getThingRegistry().get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
196         assertNotNull(bridge);
197         ThingHandler bridgeHandler = bridge.getHandler();
198         assertNotNull(bridgeHandler);
199         setPrivate(Objects.requireNonNull(bridgeHandler), "tokenRefresher", tokenRefresher);
200
201         // when:
202         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
203                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
204                 + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
205                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
206
207         // then:
208         assertTrue(website.contains("<li class=\"active\">Overview</li>"));
209
210         assertEquals(MieleCloudBindingIntegrationTestConstants.ALTERNATIVE_ACCESS_TOKEN,
211                 bridge.getProperties().get(MieleCloudBindingConstants.PROPERTY_ACCESS_TOKEN));
212     }
213
214     @Test
215     public void whenNoBridgeUidIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
216             throws Exception {
217         // when:
218         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
219                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
220
221         // then:
222         assertTrue(website.contains("Pairing failed!"));
223         assertTrue(website.contains("Missing bridge UID."));
224     }
225
226     @Test
227     public void whenAnEmptyBridgeUidIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
228             throws Exception {
229         // when:
230         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
231                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "=&" + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "="
232                 + MieleCloudBindingIntegrationTestConstants.EMAIL);
233
234         // then:
235         assertTrue(website.contains("Pairing failed!"));
236         assertTrue(website.contains("Missing bridge UID."));
237     }
238
239     @Test
240     public void whenAMalformedBridgeUidIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
241             throws Exception {
242         // when:
243         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
244                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "=gen!e!sis&"
245                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
246
247         // then:
248         assertTrue(website.contains("Pairing failed!"));
249         assertTrue(website.contains("Malformed bridge UID."));
250     }
251
252     @Test
253     public void whenNoEmailIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
254             throws Exception {
255         // when:
256         Website website = getCrawler()
257                 .doGetRelative("/mielecloud/createBridgeThing?" + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
258                         + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString());
259
260         // then:
261         assertTrue(website.contains("Pairing failed!"));
262         assertTrue(website.contains("Missing e-mail address."));
263     }
264
265     @Test
266     public void whenAnEmptyEmailIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
267             throws Exception {
268         // when:
269         Website website = getCrawler()
270                 .doGetRelative("/mielecloud/createBridgeThing?" + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
271                         + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
272                         + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=");
273
274         // then:
275         assertTrue(website.contains("Pairing failed!"));
276         assertTrue(website.contains("Missing e-mail address."));
277     }
278 }