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