]> git.basschouten.com Git - openhab-addons.git/blob
14c6ae684131ce85ce9e6a559e262eeac11fb9df
[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;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.mielecloud.internal.util.ReflectionUtil.setPrivate;
19
20 import java.util.Objects;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.mielecloud.internal.config.servlet.CreateBridgeServlet;
25 import org.openhab.binding.mielecloud.internal.config.servlet.ForwardToLoginServlet;
26 import org.openhab.binding.mielecloud.internal.handler.MieleBridgeHandler;
27 import org.openhab.binding.mielecloud.internal.handler.MieleHandlerFactory;
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.binding.mielecloud.internal.util.WebsiteCrawler;
32 import org.openhab.binding.mielecloud.internal.webservice.MieleWebservice;
33 import org.openhab.binding.mielecloud.internal.webservice.MieleWebserviceFactory;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38
39 /**
40  * @author Björn Lange - Initial Contribution
41  */
42 @NonNullByDefault
43 public class ConfigFlowTest extends AbstractConfigFlowTest {
44     private void setUpAuthorizationHandler() throws NoSuchFieldException, IllegalAccessException {
45         OAuthAuthorizationHandler authorizationHandler = mock(OAuthAuthorizationHandler.class);
46         when(authorizationHandler.getAccessToken(MieleCloudBindingIntegrationTestConstants.EMAIL))
47                 .thenReturn(MieleCloudBindingIntegrationTestConstants.ACCESS_TOKEN);
48         when(authorizationHandler.getBridgeUid())
49                 .thenReturn(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
50         when(authorizationHandler.getEmail()).thenReturn(MieleCloudBindingIntegrationTestConstants.EMAIL);
51
52         setPrivate(getResultServlet(), "authorizationHandler", authorizationHandler);
53     }
54
55     private void setUpWebservice() throws Exception {
56         MieleWebservice webservice = mock(MieleWebservice.class);
57         doAnswer(invocation -> {
58             Thing bridge = getThingRegistry().get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
59             assertNotNull(bridge);
60             ThingHandler handler = bridge.getHandler();
61             if (handler instanceof MieleBridgeHandler) {
62                 ((MieleBridgeHandler) handler).onConnectionAlive();
63             }
64             return null;
65         }).when(webservice).addConnectionStatusListener(any());
66
67         MieleWebserviceFactory webserviceFactory = mock(MieleWebserviceFactory.class);
68         when(webserviceFactory.create(any())).thenReturn(webservice);
69
70         MieleHandlerFactory handlerFactory = getService(ThingHandlerFactory.class, MieleHandlerFactory.class);
71         assertNotNull(handlerFactory);
72         setPrivate(Objects.requireNonNull(handlerFactory), "webserviceFactory", webserviceFactory);
73     }
74
75     private Website configureBridgeWithConfigFlow() throws Exception {
76         Website accountOverviewSite = getCrawler().doGetRelative("/mielecloud");
77         String pairAccountUrl = accountOverviewSite.getTargetOfLink("Pair Account");
78
79         Website pairAccountSite = getCrawler().doGetRelative(pairAccountUrl);
80         String forwardToLoginUrl = pairAccountSite.getFormAction();
81
82         Website mieleLoginSite = getCrawler().doGetRelative(forwardToLoginUrl + "?"
83                 + ForwardToLoginServlet.CLIENT_ID_PARAMETER_NAME + "="
84                 + MieleCloudBindingIntegrationTestConstants.CLIENT_ID + "&"
85                 + ForwardToLoginServlet.CLIENT_SECRET_PARAMETER_NAME + "="
86                 + MieleCloudBindingIntegrationTestConstants.CLIENT_SECRET + "&"
87                 + ForwardToLoginServlet.BRIDGE_ID_PARAMETER_NAME + "="
88                 + MieleCloudBindingIntegrationTestConstants.BRIDGE_ID + "&" + ForwardToLoginServlet.EMAIL_PARAMETER_NAME
89                 + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
90         String redirectionUrl = mieleLoginSite.getValueOfInput("redirect_uri")
91                 .replace("http://127.0.0.1:" + WebsiteCrawler.getServerPort(), "");
92         String state = mieleLoginSite.getValueOfInput("state");
93
94         Website resultSite = getCrawler().doGetRelative(redirectionUrl + "?code="
95                 + MieleCloudBindingIntegrationTestConstants.AUTHORIZATION_CODE + "&state=" + state);
96         String createBridgeUrl = resultSite.getFormAction();
97
98         Website finalOverview = getCrawler().doGetRelative(createBridgeUrl + "?"
99                 + CreateBridgeServlet.BRIDGE_UID_PARAMETER_NAME + "="
100                 + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.toString() + "&"
101                 + CreateBridgeServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
102         return finalOverview;
103     }
104
105     @Test
106     public void configFlowHappyPathCreatesABridge() throws Exception {
107         // given:
108         setUpAuthorizationHandler();
109         setUpWebservice();
110
111         // when:
112         Website finalOverview = configureBridgeWithConfigFlow();
113
114         // then:
115         assertTrue(finalOverview.contains("<span class=\"status online\">ONLINE</span>"));
116
117         Thing bridge = getThingRegistry().get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
118         assertNotNull(bridge);
119         assertEquals(ThingStatus.ONLINE, bridge.getStatus());
120     }
121
122     @Test
123     public void configFlowWaitTimeoutExpiresWhenBridgeDoesNotComeOnline() throws Exception {
124         // given:
125         setUpAuthorizationHandler();
126         getCreateBridgeServlet().setOnlineWaitTimeoutInMilliseconds(0);
127
128         // when:
129         configureBridgeWithConfigFlow();
130
131         // then:
132         Thing bridge = getThingRegistry().get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
133         assertNotNull(bridge);
134     }
135 }