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