]> git.basschouten.com Git - openhab-addons.git/blob
ed64abbbd8185a104040c59de5d4f2168e124a9f
[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.io.hueemulation.internal.rest;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertEquals;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.eclipse.jetty.client.api.ContentResponse;
22 import org.glassfish.jersey.server.ResourceConfig;
23 import org.junit.jupiter.api.AfterEach;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.core.items.GenericItem;
27 import org.openhab.core.items.ItemRegistry;
28 import org.openhab.core.library.items.ColorItem;
29 import org.openhab.core.library.items.ContactItem;
30 import org.openhab.core.library.items.DimmerItem;
31 import org.openhab.core.library.items.NumberItem;
32 import org.openhab.core.library.items.RollershutterItem;
33 import org.openhab.core.library.items.SwitchItem;
34 import org.openhab.core.library.types.DecimalType;
35 import org.openhab.core.library.types.HSBType;
36 import org.openhab.core.library.types.OnOffType;
37 import org.openhab.core.library.types.OpenClosedType;
38 import org.openhab.core.library.types.PercentType;
39 import org.openhab.core.types.State;
40 import org.openhab.io.hueemulation.internal.ConfigStore;
41 import org.openhab.io.hueemulation.internal.rest.mocks.DummyItemRegistry;
42
43 /**
44  * Tests for {@link Sensors}.
45  *
46  * @author David Graeff - Initial contribution
47  */
48 @NonNullByDefault
49 public class SensorTests {
50     protected @NonNullByDefault({}) CommonSetup commonSetup;
51     protected @NonNullByDefault({}) ItemRegistry itemRegistry;
52     protected @NonNullByDefault({}) ConfigStore cs;
53
54     private @NonNullByDefault({}) HttpClient httpClient;
55
56     Sensors subject = new Sensors();
57
58     private void addItemToReg(GenericItem item, State state, String label) {
59         item.setState(state);
60         item.setLabel(label);
61         itemRegistry.add(item);
62     }
63
64     @BeforeEach
65     public void setUp() throws Exception {
66         commonSetup = new CommonSetup(false);
67         itemRegistry = new DummyItemRegistry();
68
69         this.cs = commonSetup.cs;
70
71         subject.cs = cs;
72         subject.userManagement = commonSetup.userManagement;
73         subject.itemRegistry = itemRegistry;
74         subject.activate();
75
76         httpClient = new HttpClient();
77         httpClient.start();
78
79         // Add simulated sensor items
80         addItemToReg(new SwitchItem("switch1"), OnOffType.ON, "name1");
81         addItemToReg(new ContactItem("contact1"), OpenClosedType.OPEN, "");
82         addItemToReg(new ColorItem("color1"), HSBType.BLUE, "");
83         addItemToReg(new DimmerItem("white1"), new PercentType(12), "");
84         addItemToReg(new RollershutterItem("roller1"), new PercentType(12), "");
85         addItemToReg(new NumberItem("number1"), new DecimalType(12), "");
86
87         commonSetup.start(new ResourceConfig().registerInstances(subject));
88     }
89
90     @AfterEach
91     public void tearDown() throws Exception {
92         commonSetup.dispose();
93     }
94
95     @Test
96     public void renameSensor() throws Exception {
97         assertThat(cs.ds.sensors.get("switch1").name, is("name1"));
98
99         String body = "{'name':'name2'}";
100         ContentResponse response = commonSetup.sendPut("/testuser/sensors/switch1", body);
101         assertEquals(200, response.getStatus());
102
103         body = response.getContentAsString();
104
105         assertThat(body, containsString("success"));
106         assertThat(body, containsString("name"));
107         assertThat(cs.ds.sensors.get("switch1").name, is("name2"));
108     }
109
110     @Test
111     public void allAndSingleSensor() throws Exception {
112         ContentResponse response = commonSetup.sendGet("/testuser/sensors");
113         assertEquals(200, response.getStatus());
114
115         String body = response.getContentAsString();
116
117         assertThat(body, containsString("switch1"));
118         assertThat(body, containsString("color1"));
119         assertThat(body, containsString("white1"));
120
121         // Single light access test
122         response = commonSetup.sendGet("/testuser/sensors/switch1");
123         assertEquals(200, response.getStatus());
124         body = response.getContentAsString();
125         assertThat(body, containsString("CLIPGenericFlag"));
126     }
127 }