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.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;
40 * @author Björn Lange - Initial Contribution
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);
52 setPrivate(getResultServlet(), "authorizationHandler", authorizationHandler);
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();
65 }).when(webservice).addConnectionStatusListener(any());
67 MieleWebserviceFactory webserviceFactory = mock(MieleWebserviceFactory.class);
68 when(webserviceFactory.create(any())).thenReturn(webservice);
70 MieleHandlerFactory handlerFactory = getService(ThingHandlerFactory.class, MieleHandlerFactory.class);
71 assertNotNull(handlerFactory);
72 setPrivate(Objects.requireNonNull(handlerFactory), "webserviceFactory", webserviceFactory);
75 private Website configureBridgeWithConfigFlow() throws Exception {
76 Website accountOverviewSite = getCrawler().doGetRelative("/mielecloud");
77 String pairAccountUrl = accountOverviewSite.getTargetOfLink("Pair Account");
79 Website pairAccountSite = getCrawler().doGetRelative(pairAccountUrl);
80 String forwardToLoginUrl = pairAccountSite.getFormAction();
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");
94 Website resultSite = getCrawler().doGetRelative(redirectionUrl + "?code="
95 + MieleCloudBindingIntegrationTestConstants.AUTHORIZATION_CODE + "&state=" + state);
96 String createBridgeUrl = resultSite.getFormAction();
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;
106 public void configFlowHappyPathCreatesABridge() throws Exception {
108 setUpAuthorizationHandler();
112 Website finalOverview = configureBridgeWithConfigFlow();
115 assertTrue(finalOverview.contains("<span class=\"status online\">ONLINE</span>"));
117 Thing bridge = getThingRegistry().get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
118 assertNotNull(bridge);
119 assertEquals(ThingStatus.ONLINE, bridge.getStatus());
123 public void configFlowWaitTimeoutExpiresWhenBridgeDoesNotComeOnline() throws Exception {
125 setUpAuthorizationHandler();
126 getCreateBridgeServlet().setOnlineWaitTimeoutInMilliseconds(0);
129 configureBridgeWithConfigFlow();
132 Thing bridge = getThingRegistry().get(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
133 assertNotNull(bridge);