]> git.basschouten.com Git - openhab-addons.git/blob
b9417d8dda87081695775da9690a4f268ef1d9d8
[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 import static org.openhab.binding.mielecloud.internal.util.ReflectionUtil.setPrivate;
18
19 import java.util.Optional;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
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.Website;
26 import org.openhab.binding.mielecloud.internal.webservice.language.LanguageProvider;
27
28 /**
29  * @author Björn Lange - Initial Contribution
30  */
31 @NonNullByDefault
32 public class SuccessServletTest extends AbstractConfigFlowTest {
33     @Test
34     public void whenTheSuccessPageIsShownThenAThingsFileTemplateIsPresent() throws Exception {
35         // when:
36         Website website = getCrawler().doGetRelative("/mielecloud/success?" + SuccessServlet.BRIDGE_UID_PARAMETER_NAME
37                 + "=" + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
38                 + SuccessServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
39
40         // then:
41         assertTrue(website.contains("Bridge mielecloud:account:genesis [ email=\"openhab@openhab.org\""));
42     }
43
44     @Test
45     public void whenTheSuccessPageIsShownThenTheLocaleIsSelectedAutomatically() throws Exception {
46         // given:
47         LanguageProvider languageProvider = mock(LanguageProvider.class);
48         when(languageProvider.getLanguage()).thenReturn(Optional.of("de"));
49         setPrivate(getSuccessServlet(), "languageProvider", languageProvider);
50
51         // when:
52         Website website = getCrawler().doGetRelative("/mielecloud/success?" + SuccessServlet.BRIDGE_UID_PARAMETER_NAME
53                 + "=" + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
54                 + SuccessServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
55
56         // then:
57         assertTrue(website.contains("<option value=\"de\" selected=\"selected\">Deutsch - de</option>"));
58         assertTrue(website.contains("locale=\"de\""));
59     }
60
61     @Test
62     public void whenTheSuccessPageIsShownAndNoLocaleIsProvidedThenEnglishIsSelectedAutomatically() throws Exception {
63         // given:
64         LanguageProvider languageProvider = mock(LanguageProvider.class);
65         when(languageProvider.getLanguage()).thenReturn(Optional.of("en"));
66         setPrivate(getSuccessServlet(), "languageProvider", languageProvider);
67
68         // when:
69         Website website = getCrawler().doGetRelative("/mielecloud/success?" + SuccessServlet.BRIDGE_UID_PARAMETER_NAME
70                 + "=" + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
71                 + SuccessServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
72
73         // then:
74         assertTrue(website.contains("<option value=\"en\" selected=\"selected\">English - en</option>"));
75         assertTrue(website.contains("locale=\"en\""));
76     }
77
78     @Test
79     public void whenTheSuccessPageIsRequestedAndNoBridgeUidIsPassedThenTheFailurePageIsShown() throws Exception {
80         // when:
81         Website website = getCrawler().doGetRelative("/mielecloud/success");
82
83         // then:
84         assertTrue(website.contains("Pairing failed!"));
85         assertTrue(website.contains("Missing bridge UID."));
86     }
87
88     @Test
89     public void whenTheSuccessPageIsRequestedAndAnEmptyBridgeUidIsPassedThenTheFailurePageIsShown() throws Exception {
90         // when:
91         Website website = getCrawler().doGetRelative("/mielecloud/success?" + SuccessServlet.BRIDGE_UID_PARAMETER_NAME
92                 + "=&" + SuccessServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
93
94         // then:
95         assertTrue(website.contains("Pairing failed!"));
96         assertTrue(website.contains("Missing bridge UID."));
97     }
98
99     @Test
100     public void whenTheSuccessPageIsRequestedAndAMalformedBridgeUidIsPassedThenTheFailurePageIsShown()
101             throws Exception {
102         // when:
103         Website website = getCrawler()
104                 .doGetRelative("/mielecloud/success?" + SuccessServlet.BRIDGE_UID_PARAMETER_NAME + "=!genesis&"
105                         + SuccessServlet.EMAIL_PARAMETER_NAME + "=" + MieleCloudBindingIntegrationTestConstants.EMAIL);
106
107         // then:
108         assertTrue(website.contains("Pairing failed!"));
109         assertTrue(website.contains("Malformed bridge UID."));
110     }
111
112     @Test
113     public void whenTheSuccessPageIsRequestedAndNoEmailIsPassedThenTheFailurePageIsShown() throws Exception {
114         // when:
115         Website website = getCrawler().doGetRelative("/mielecloud/success?" + SuccessServlet.BRIDGE_UID_PARAMETER_NAME
116                 + "=" + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString());
117
118         // then:
119         assertTrue(website.contains("Pairing failed!"));
120         assertTrue(website.contains("Missing e-mail address."));
121     }
122
123     @Test
124     public void whenTheSuccessPageIsRequestedAndAnEmptyEmailIsPassedThenTheFailurePageIsShown() throws Exception {
125         // when:
126         Website website = getCrawler().doGetRelative("/mielecloud/success?" + SuccessServlet.BRIDGE_UID_PARAMETER_NAME
127                 + "=" + MieleCloudBindingIntegrationTestConstants.BRIDGE_THING_UID.getAsString() + "&"
128                 + SuccessServlet.EMAIL_PARAMETER_NAME + "=");
129
130         // then:
131         assertTrue(website.contains("Pairing failed!"));
132         assertTrue(website.contains("Missing e-mail address."));
133     }
134 }