]> git.basschouten.com Git - openhab-addons.git/blob
ba4a8f53c3bd82c64c63d714e12888f604165260
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.mercedesme;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.time.Instant;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.mercedesme.internal.Constants;
24 import org.openhab.binding.mercedesme.internal.handler.AccountHandlerMock;
25 import org.openhab.binding.mercedesme.internal.handler.ThingCallbackListener;
26 import org.openhab.binding.mercedesme.internal.utils.Utils;
27 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.internal.BridgeImpl;
33
34 /**
35  * {@link StatusTests} sequencess for testing ThingStatus
36  *
37  * @author Bernd Weymann - Initial contribution
38  */
39 @NonNullByDefault
40 class StatusTests {
41
42     public static void tearDown(AccountHandlerMock ahm) {
43         // ahm.setCallback(null);
44         ahm.dispose();
45         try {
46             Thread.sleep(250);
47         } catch (InterruptedException e) {
48             fail();
49         }
50     }
51
52     @Test
53     void testInvalidConfig() {
54         BridgeImpl bi = new BridgeImpl(new ThingTypeUID("test", "account"), "MB");
55         Map<String, Object> config = new HashMap<>();
56         config.put("callbackIP", "999.999.999.999");
57         config.put("callbackPort", "99999");
58         bi.setConfiguration(new Configuration(config));
59         AccountHandlerMock ahm = new AccountHandlerMock(bi, null);
60         ThingCallbackListener tcl = new ThingCallbackListener();
61         ahm.setCallback(tcl);
62         ahm.initialize();
63         assertEquals(ThingStatus.OFFLINE, tcl.getThingStatus().getStatus(), "EMail offline");
64         assertEquals(ThingStatusDetail.CONFIGURATION_ERROR, tcl.getThingStatus().getStatusDetail(), "EMail config");
65         assertEquals("@text/mercedesme.account.status.email-missing", tcl.getThingStatus().getDescription(),
66                 "EMail text");
67         config.put("email", "a@b.c");
68         bi.setConfiguration(new Configuration(config));
69         ahm.initialize();
70         assertEquals(ThingStatus.OFFLINE, tcl.getThingStatus().getStatus(), "Region offline");
71         assertEquals(ThingStatusDetail.CONFIGURATION_ERROR, tcl.getThingStatus().getStatusDetail(), "Region config");
72         assertEquals("@text/mercedesme.account.status.region-missing", tcl.getThingStatus().getDescription(),
73                 "Region text");
74         config.put("region", "row");
75         bi.setConfiguration(new Configuration(config));
76         ahm.initialize();
77         assertEquals(ThingStatus.OFFLINE, tcl.getThingStatus().getStatus(), "Auth offline");
78         assertEquals(ThingStatusDetail.NONE, tcl.getThingStatus().getStatusDetail(), "Auth detail");
79         config.put("refreshInterval", 0);
80         bi.setConfiguration(new Configuration(config));
81         ahm.initialize();
82         assertEquals(ThingStatus.OFFLINE, tcl.getThingStatus().getStatus(), "Refresh offline");
83         assertEquals(ThingStatusDetail.CONFIGURATION_ERROR, tcl.getThingStatus().getStatusDetail(), "Refresh config");
84         assertEquals("@text/mercedesme.account.status.refresh-invalid", tcl.getThingStatus().getDescription(),
85                 "Refresh text");
86         tearDown(ahm);
87     }
88
89     @Test
90     void testNoTokenStored() {
91         BridgeImpl bi = new BridgeImpl(new ThingTypeUID("test", "account"), "MB");
92         Map<String, Object> config = new HashMap<>();
93         config.put("refreshInterval", Integer.MAX_VALUE);
94         config.put("region", "row");
95         config.put("email", "a@b.c");
96         config.put("callbackIP", "999.999.999.999");
97         config.put("callbackPort", "99999");
98         bi.setConfiguration(new Configuration(config));
99         AccountHandlerMock ahm = new AccountHandlerMock(bi, null);
100         ThingCallbackListener tcl = new ThingCallbackListener();
101         ahm.setCallback(tcl);
102         ahm.initialize();
103         assertEquals(ThingStatus.OFFLINE, tcl.getThingStatus().getStatus(), "Auth Offline");
104         assertEquals(ThingStatusDetail.COMMUNICATION_ERROR, tcl.getThingStatus().getStatusDetail(), "Auth details");
105         String statusDescription = tcl.getThingStatus().getDescription();
106         assertNotNull(statusDescription);
107         assertTrue(statusDescription.contains("@text/mercedesme.account.status.authorization-needed"), "Auth text");
108         AccessTokenResponse token = new AccessTokenResponse();
109         token.setExpiresIn(3000);
110         token.setAccessToken(Constants.JUNIT_TOKEN);
111         token.setRefreshToken(Constants.JUNIT_REFRESH_TOKEN);
112         ahm.onAccessTokenResponse(token);
113         ahm.connect();
114         assertEquals(ThingStatus.ONLINE, tcl.getThingStatus().getStatus(), "Auth Online");
115         tearDown(ahm);
116     }
117
118     @Test
119     void testTokenStored() {
120         BridgeImpl bi = new BridgeImpl(new ThingTypeUID("test", "account"), "MB");
121         Map<String, Object> config = new HashMap<>();
122         config.put("refreshInterval", Integer.MAX_VALUE);
123         config.put("region", "row");
124         config.put("email", "a@b.c");
125         config.put("callbackIP", "999.999.999.999");
126         config.put("callbackPort", "99999");
127         bi.setConfiguration(new Configuration(config));
128         AccessTokenResponse token = new AccessTokenResponse();
129         token.setExpiresIn(3000);
130         token.setAccessToken(Constants.JUNIT_TOKEN);
131         token.setRefreshToken(Constants.JUNIT_REFRESH_TOKEN);
132         token.setCreatedOn(Instant.now());
133         token.setTokenType("Bearer");
134         token.setScope(Constants.SCOPE);
135         AccountHandlerMock ahm = new AccountHandlerMock(bi, Utils.toString(token));
136         ThingCallbackListener tcl = new ThingCallbackListener();
137         ahm.setCallback(tcl);
138         ahm.initialize();
139         assertEquals(ThingStatus.UNKNOWN, tcl.getThingStatus().getStatus(), "Socket Unknown "
140                 + tcl.getThingStatus().getStatusDetail() + " " + tcl.getThingStatus().getDescription());
141         ahm.connect();
142         assertEquals(ThingStatus.ONLINE, tcl.getThingStatus().getStatus(), "Spcket Online");
143         tearDown(ahm);
144     }
145 }