]> git.basschouten.com Git - openhab-addons.git/blob
bda2051ff79dec987898914e197c0498bf4aa8fd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.test;
14
15 import static org.openhab.binding.nest.internal.NestBindingConstants.BINDING_ID;
16
17 import java.util.Collections;
18 import java.util.Properties;
19 import java.util.Set;
20
21 import javax.ws.rs.client.ClientBuilder;
22
23 import org.openhab.binding.nest.internal.exceptions.InvalidAccessTokenException;
24 import org.openhab.binding.nest.internal.handler.NestBridgeHandler;
25 import org.openhab.binding.nest.internal.handler.NestRedirectUrlSupplier;
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 NestTestBridgeHandler} is a {@link NestBridgeHandler} modified for testing. Using the
32  * {@link NestTestRedirectUrlSupplier} it will always connect to same provided {@link #redirectUrl}.
33  *
34  * @author Wouter Born - Increase test coverage
35  */
36 public class NestTestBridgeHandler extends NestBridgeHandler {
37
38     class NestTestRedirectUrlSupplier extends NestRedirectUrlSupplier {
39
40         NestTestRedirectUrlSupplier(Properties httpHeaders) {
41             super(httpHeaders);
42             this.cachedUrl = redirectUrl;
43         }
44
45         @Override
46         public void resetCache() {
47             // Skip resetting the URL so the test server keeps being used
48         }
49     }
50
51     public static final ThingTypeUID THING_TYPE_TEST_BRIDGE = new ThingTypeUID(BINDING_ID, "test_account");
52     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_TEST_BRIDGE);
53
54     private String redirectUrl;
55
56     public NestTestBridgeHandler(Bridge bridge, ClientBuilder clientBuilder, SseEventSourceFactory eventSourceFactory,
57             String redirectUrl) {
58         super(bridge, clientBuilder, eventSourceFactory);
59         this.redirectUrl = redirectUrl;
60     }
61
62     @Override
63     protected NestRedirectUrlSupplier createRedirectUrlSupplier() throws InvalidAccessTokenException {
64         return new NestTestRedirectUrlSupplier(getHttpHeaders());
65     }
66 }