]> git.basschouten.com Git - openhab-addons.git/blob
a3fecd66db7da6c46da6d83451614a0b1b691635
[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.nest.internal.wwn.test;
14
15 import static org.openhab.binding.nest.internal.wwn.WWNBindingConstants.BINDING_ID;
16
17 import java.util.Properties;
18 import java.util.Set;
19
20 import javax.ws.rs.client.ClientBuilder;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.nest.internal.wwn.exceptions.InvalidWWNAccessTokenException;
24 import org.openhab.binding.nest.internal.wwn.handler.WWNAccountHandler;
25 import org.openhab.binding.nest.internal.wwn.handler.WWNRedirectUrlSupplier;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
29
30 /**
31  * The {@link WWNTestAccountHandler} is a {@link WWNAccountHandler} modified for testing. Using the
32  * {@link NestTestRedirectUrlSupplier} it will always connect to same provided {@link #redirectUrl}.
33  *
34  * @author Wouter Born - Initial contribution
35  */
36 @NonNullByDefault
37 public class WWNTestAccountHandler extends WWNAccountHandler {
38
39     class NestTestRedirectUrlSupplier extends WWNRedirectUrlSupplier {
40
41         NestTestRedirectUrlSupplier(Properties httpHeaders) {
42             super(httpHeaders);
43             this.cachedUrl = redirectUrl;
44         }
45
46         @Override
47         public void resetCache() {
48             // Skip resetting the URL so the test server keeps being used
49         }
50     }
51
52     public static final ThingTypeUID THING_TYPE_TEST_BRIDGE = new ThingTypeUID(BINDING_ID, "wwn_test_account");
53     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_TEST_BRIDGE);
54
55     private String redirectUrl;
56
57     public WWNTestAccountHandler(Bridge bridge, ClientBuilder clientBuilder, SseEventSourceFactory eventSourceFactory,
58             String redirectUrl) {
59         super(bridge, clientBuilder, eventSourceFactory);
60         this.redirectUrl = redirectUrl;
61     }
62
63     @Override
64     protected WWNRedirectUrlSupplier createRedirectUrlSupplier() throws InvalidWWNAccessTokenException {
65         return new NestTestRedirectUrlSupplier(getHttpHeaders());
66     }
67 }