]> git.basschouten.com Git - openhab-addons.git/blob
a156e01e00613389dd3006b98dfb5e6def43a85f
[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.servlet;
14
15 import static org.junit.jupiter.api.Assertions.assertTrue;
16 import static org.mockito.Mockito.*;
17
18 import java.util.stream.Stream;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
23 import org.openhab.binding.mielecloud.internal.util.AbstractConfigFlowTest;
24 import org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants;
25 import org.openhab.binding.mielecloud.internal.util.ReflectionUtil;
26 import org.openhab.binding.mielecloud.internal.util.Website;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ThingRegistry;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.ThingStatusInfo;
33 import org.openhab.core.thing.ThingUID;
34
35 /**
36  * @author Björn Lange - Initial Contribution
37  */
38 @NonNullByDefault
39 public class AccountOverviewServletTest extends AbstractConfigFlowTest {
40     @Test
41     public void whenAccountOverviewServletIsCalledOverNonSslConnectionThenAWarningIsShown() throws Exception {
42         // when:
43         Website accountOverviewSite = getCrawler().doGetRelative("/mielecloud");
44
45         // then:
46         assertTrue(accountOverviewSite
47                 .contains("Warning: We strongly advice to proceed only with SSL enabled for a secure data exchange."));
48         assertTrue(accountOverviewSite.contains(
49                 "See <a href=\"https://www.openhab.org/docs/installation/security.html\">Securing access to openHAB</a> for details."));
50     }
51
52     @Test
53     public void whenAccountOverviewServletIsCalledAndNoBridgeIsPresentThenThePageSaysThatThereIsNoBridgePaired()
54             throws Exception {
55         // when:
56         Website accountOverviewSite = getCrawler().doGetRelative("/mielecloud");
57
58         // then:
59         assertTrue(accountOverviewSite.contains("There is no account paired at the moment."));
60     }
61
62     @Test
63     public void whenAccountOverviewServletIsCalledAndBridgesArePresentThenThePageDisplaysInformationAboutThem()
64             throws Exception {
65         // given:
66         Configuration configuration1 = mock(Configuration.class);
67         when(configuration1.get(MieleCloudBindingConstants.CONFIG_PARAM_LOCALE)).thenReturn("de");
68         when(configuration1.get(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL)).thenReturn("openhab@openhab.org");
69
70         Bridge bridge1 = mock(Bridge.class);
71         when(bridge1.getThingTypeUID()).thenReturn(MieleCloudBindingConstants.THING_TYPE_BRIDGE);
72         when(bridge1.getUID()).thenReturn(MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID);
73         when(bridge1.getStatus()).thenReturn(ThingStatus.ONLINE);
74         when(bridge1.getStatusInfo()).thenReturn(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null));
75         when(bridge1.getConfiguration()).thenReturn(configuration1);
76
77         Configuration configuration2 = mock(Configuration.class);
78         when(configuration2.get(MieleCloudBindingConstants.CONFIG_PARAM_LOCALE)).thenReturn("en");
79         when(configuration2.get(MieleCloudBindingConstants.CONFIG_PARAM_EMAIL)).thenReturn("everyone@openhab.org");
80
81         Bridge bridge2 = mock(Bridge.class);
82         when(bridge2.getThingTypeUID()).thenReturn(MieleCloudBindingConstants.THING_TYPE_BRIDGE);
83         when(bridge2.getUID()).thenReturn(new ThingUID(MieleCloudBindingConstants.THING_TYPE_BRIDGE, "test"));
84         when(bridge2.getStatus()).thenReturn(ThingStatus.OFFLINE);
85         when(bridge2.getStatusInfo()).thenReturn(
86                 new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Error message"));
87         when(bridge2.getConfiguration()).thenReturn(configuration2);
88
89         ThingRegistry thingRegistry = mock(ThingRegistry.class);
90         when(thingRegistry.stream()).thenAnswer(invocation -> Stream.of(bridge1, bridge2));
91         ReflectionUtil.setPrivate(getAccountOverviewServlet(), "thingRegistry", thingRegistry);
92
93         // when:
94         Website accountOverviewSite = getCrawler().doGetRelative("/mielecloud");
95
96         // then:
97         assertTrue(accountOverviewSite.contains("The following bridges are paired"));
98         assertTrue(accountOverviewSite.contains("openhab@openhab.org"));
99         assertTrue(accountOverviewSite.contains("mielecloud:account:genesis"));
100         assertTrue(accountOverviewSite.contains("<span class=\"status online\">"));
101         assertTrue(accountOverviewSite.contains("everyone@openhab.org"));
102         assertTrue(accountOverviewSite.contains("mielecloud:account:test"));
103         assertTrue(accountOverviewSite.contains("<span class=\"status offline\">"));
104     }
105 }