]> git.basschouten.com Git - openhab-addons.git/blob
5010aed71df861623dda09e8d4193e0720d58ea8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
26 import org.openhab.binding.mielecloud.internal.auth.OAuthTokenRefresher;
27 import org.openhab.binding.mielecloud.internal.config.MieleCloudConfigService;
28 import org.openhab.binding.mielecloud.internal.util.AbstractConfigFlowTest;
29 import org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants;
30 import org.openhab.binding.mielecloud.internal.util.Website;
31 import org.openhab.core.config.discovery.inbox.Inbox;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingRegistry;
34 import org.openhab.core.thing.binding.ThingHandler;
35
36 /**
37  * @author Björn Lange - Initial Contribution
38  */
39 @NonNullByDefault
40 public class CreateBridgeServletTest extends AbstractConfigFlowTest {
41     @Test
42     public void whenBridgeCreationFailsThenAWarningIsShownOnTheSuccessPage() throws Exception {
43         // given:
44         MieleCloudConfigService configService = getService(MieleCloudConfigService.class);
45         assertNotNull(configService);
46
47         CreateBridgeServlet createBridgeServlet = configService.getCreateBridgeServlet();
48         assertNotNull(createBridgeServlet);
49
50         Inbox inbox = mock(Inbox.class);
51         when(inbox.add(any())).thenReturn(true);
52         when(inbox.approve(any(), anyString(), anyString())).thenReturn(null);
53         setPrivate(Objects.requireNonNull(createBridgeServlet), "inbox", inbox);
54
55         // when:
56         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
57                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
58                 + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
59                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
60
61         // then:
62         assertTrue(website.contains("Pairing successful!"));
63         assertTrue(website.contains(
64                 "Could not auto configure the bridge. Failed to approve the bridge from the inbox. Please try the configuration flow again."));
65     }
66
67     @Test
68     public void whenBridgeReconfigurationFailsDueToMissingBridgeThenAWarningIsShownOnTheSuccessPage() throws Exception {
69         // given:
70         MieleCloudConfigService configService = getService(MieleCloudConfigService.class);
71         assertNotNull(configService);
72
73         CreateBridgeServlet createBridgeServlet = configService.getCreateBridgeServlet();
74         assertNotNull(createBridgeServlet);
75
76         Inbox inbox = mock(Inbox.class);
77         when(inbox.add(any())).thenReturn(false);
78         setPrivate(Objects.requireNonNull(createBridgeServlet), "inbox", inbox);
79
80         ThingRegistry thingRegistry = mock(ThingRegistry.class);
81         when(thingRegistry.get(any())).thenReturn(null);
82         setPrivate(Objects.requireNonNull(createBridgeServlet), "thingRegistry", thingRegistry);
83
84         // when:
85         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
86                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
87                 + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
88                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
89
90         // then:
91         assertTrue(website.contains("Pairing successful!"));
92         assertTrue(website.contains(
93                 "Could not auto reconfigure the bridge. Bridge thing or thing handler is not available. Please try the configuration flow again."));
94     }
95
96     @Test
97     public void whenBridgeReconfigurationFailsDueToMissingBridgeHandlerThenAWarningIsShownOnTheSuccessPage()
98             throws Exception {
99         // given:
100         MieleCloudConfigService configService = getService(MieleCloudConfigService.class);
101         assertNotNull(configService);
102
103         CreateBridgeServlet createBridgeServlet = configService.getCreateBridgeServlet();
104         assertNotNull(createBridgeServlet);
105
106         Inbox inbox = mock(Inbox.class);
107         when(inbox.add(any())).thenReturn(false);
108         setPrivate(Objects.requireNonNull(createBridgeServlet), "inbox", inbox);
109
110         Thing bridge = mock(Thing.class);
111         when(bridge.getHandler()).thenReturn(null);
112
113         ThingRegistry thingRegistry = mock(ThingRegistry.class);
114         when(thingRegistry.get(any())).thenReturn(bridge);
115         setPrivate(Objects.requireNonNull(createBridgeServlet), "thingRegistry", thingRegistry);
116
117         // when:
118         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
119                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
120                 + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
121                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
122
123         // then:
124         assertTrue(website.contains("Pairing successful!"));
125         assertTrue(website.contains(
126                 "Could not auto reconfigure the bridge. Bridge thing or thing handler is not available. Please try the configuration flow again."));
127     }
128
129     @Test
130     public void whenBridgeIsReconfiguredThenTheConfigurationParametersAreUpdatedAndTheOverviewPageIsDisplayed()
131             throws Exception {
132         // given:
133         setUpBridge();
134
135         MieleCloudConfigService configService = getService(MieleCloudConfigService.class);
136         assertNotNull(configService);
137
138         CreateBridgeServlet createBridgeServlet = configService.getCreateBridgeServlet();
139         assertNotNull(createBridgeServlet);
140
141         OAuthTokenRefresher tokenRefresher = mock(OAuthTokenRefresher.class);
142         when(tokenRefresher.getAccessTokenFromStorage(anyString()))
143                 .thenReturn(Optional.of(MieleCloudBindingIntegrationTestConstants.ALTERNATIVE_ACCESS_TOKEN));
144
145         Thing bridge = getThingRegistry().get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
146         assertNotNull(bridge);
147         ThingHandler bridgeHandler = bridge.getHandler();
148         assertNotNull(bridgeHandler);
149         setPrivate(Objects.requireNonNull(bridgeHandler), "tokenRefresher", tokenRefresher);
150
151         // when:
152         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
153                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
154                 + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
155                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
156
157         // then:
158         assertTrue(website.contains("<li class=\"active\">Overview</li>"));
159
160         assertEquals(MieleCloudBindingIntegrationTestConstants.ALTERNATIVE_ACCESS_TOKEN,
161                 bridge.getProperties().get(MieleCloudBindingConstants.PROPERTY_ACCESS_TOKEN));
162     }
163
164     @Test
165     public void whenNoBridgeUidIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
166             throws Exception {
167         // when:
168         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
169                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
170
171         // then:
172         assertTrue(website.contains("Pairing failed!"));
173         assertTrue(website.contains("Missing bridge UID."));
174     }
175
176     @Test
177     public void whenAnEmptyBridgeUidIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
178             throws Exception {
179         // when:
180         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
181                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "=&" + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "="
182                 + MieleCloudBindingIntegrationTestConstants.EMAIL);
183
184         // then:
185         assertTrue(website.contains("Pairing failed!"));
186         assertTrue(website.contains("Missing bridge UID."));
187     }
188
189     @Test
190     public void whenAMalformedBridgeUidIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
191             throws Exception {
192         // when:
193         Website website = getCrawler().doGetRelative("/mielecloud/createBridgeThing?"
194                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "=gen!e!sis&"
195                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
196
197         // then:
198         assertTrue(website.contains("Pairing failed!"));
199         assertTrue(website.contains("Malformed bridge UID."));
200     }
201
202     @Test
203     public void whenNoEmailIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
204             throws Exception {
205         // when:
206         Website website = getCrawler()
207                 .doGetRelative("/mielecloud/createBridgeThing?" + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
208                         + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString());
209
210         // then:
211         assertTrue(website.contains("Pairing failed!"));
212         assertTrue(website.contains("Missing e-mail address."));
213     }
214
215     @Test
216     public void whenAnEmptyEmailIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
217             throws Exception {
218         // when:
219         Website website = getCrawler()
220                 .doGetRelative("/mielecloud/createBridgeThing?" + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
221                         + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
222                         + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=");
223
224         // then:
225         assertTrue(website.contains("Pairing failed!"));
226         assertTrue(website.contains("Missing e-mail address."));
227     }
228
229     @Test
230     public void whenAMalformedEmailIsPassedToBridgeCreationThenTheBrowserIsRedirectedToTheFailurePageAndAnErrorIsShown()
231             throws Exception {
232         // when:
233         Website website = getCrawler()
234                 .doGetRelative("/mielecloud/createBridgeThing?" + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
235                         + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
236                         + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=openhab.openhab.org");
237
238         // then:
239         assertTrue(website.contains("Pairing failed!"));
240         assertTrue(website.contains("Malformed e-mail address."));
241     }
242 }