2 * Copyright (c) 2010-2024 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.mercedesme;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.time.Instant;
18 import java.util.HashMap;
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;
35 * {@link StatusTests} sequencess for testing ThingStatus
37 * @author Bernd Weymann - Initial contribution
42 public static void tearDown(AccountHandlerMock ahm) {
43 // ahm.setCallback(null);
47 } catch (InterruptedException e) {
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();
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(),
67 config.put("email", "a@b.c");
68 bi.setConfiguration(new Configuration(config));
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(),
74 config.put("region", "row");
75 bi.setConfiguration(new Configuration(config));
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));
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(),
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);
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);
114 assertEquals(ThingStatus.ONLINE, tcl.getThingStatus().getStatus(), "Auth Online");
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);
139 assertEquals(ThingStatus.UNKNOWN, tcl.getThingStatus().getStatus(), "Socket Unknown "
140 + tcl.getThingStatus().getStatusDetail() + " " + tcl.getThingStatus().getDescription());
142 assertEquals(ThingStatus.ONLINE, tcl.getThingStatus().getStatus(), "Spcket Online");