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