2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mielecloud.internal.config;
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;
20 import java.util.Objects;
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;
41 * @author Björn Lange - Initial Contribution
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);
53 setPrivate(getResultServlet(), "authorizationHandler", authorizationHandler);
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();
66 }).when(webservice).addConnectionStatusListener(any());
68 MieleWebserviceFactory webserviceFactory = mock(MieleWebserviceFactory.class);
69 when(webserviceFactory.create(any())).thenReturn(webservice);
71 MieleHandlerFactory handlerFactory = getService(ThingHandlerFactory.class, MieleHandlerFactory.class);
72 assertNotNull(handlerFactory);
73 setPrivate(Objects.requireNonNull(handlerFactory), "webserviceFactory", webserviceFactory);
76 private Website configureBridgeWithConfigFlow() throws Exception {
77 Website accountOverviewSite = getCrawler().doGetRelative("/mielecloud");
78 String pairAccountUrl = accountOverviewSite.getTargetOfLink("Pair Account");
80 Website pairAccountSite = getCrawler().doGetRelative(pairAccountUrl);
81 String forwardToLoginUrl = pairAccountSite.getFormAction();
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");
95 Website resultSite = getCrawler().doGetRelative(redirectionUrl + "?code="
96 + MieleCloudBindingIntegrationTestConstants.AUTHORIZATION_CODE + "&state=" + state);
97 String createBridgeUrl = resultSite.getFormAction();
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;
108 public void configFlowHappyPathCreatesABridge() throws Exception {
110 setUpAuthorizationHandler();
114 Website finalOverview = configureBridgeWithConfigFlow();
117 assertTrue(finalOverview.contains("<span class=\"status online\">ONLINE</span>"));
119 Thing bridge = getThingRegistry().get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
120 assertNotNull(bridge);
121 assertEquals(ThingStatus.ONLINE, bridge.getStatus());
126 public void configFlowWaitTimeoutExpiresWhenBridgeDoesNotComeOnline() throws Exception {
128 setUpAuthorizationHandler();
129 getCreateBridgeServlet().setOnlineWaitTimeoutInMilliseconds(0);
132 configureBridgeWithConfigFlow();
135 Thing bridge = getThingRegistry().get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
136 assertNotNull(bridge);