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