2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.hueemulation.internal.rest;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.junit.Assert.*;
18 import java.io.IOException;
20 import javax.ws.rs.client.Entity;
21 import javax.ws.rs.core.Response;
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;
46 * Tests for {@link Sensors}.
48 * @author David Graeff - Initial contribution
51 public class SensorTests {
52 protected @NonNullByDefault({}) CommonSetup commonSetup;
53 protected @NonNullByDefault({}) ItemRegistry itemRegistry;
54 protected @NonNullByDefault({}) ConfigStore cs;
56 Sensors subject = new Sensors();
58 private void addItemToReg(GenericItem item, State state, String label) {
61 itemRegistry.add(item);
65 public void setUp() throws IOException {
66 commonSetup = new CommonSetup(false);
67 itemRegistry = new DummyItemRegistry();
69 this.cs = commonSetup.cs;
72 subject.userManagement = commonSetup.userManagement;
73 subject.itemRegistry = itemRegistry;
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), "");
84 commonSetup.start(new ResourceConfig().registerInstances(subject));
88 public void tearDown() {
89 commonSetup.dispose();
93 public void renameSensor() {
94 assertThat(cs.ds.sensors.get("switch1").name, is("name1"));
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"));
107 public void allAndSingleSensor() {
108 Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/sensors").request().get();
109 assertEquals(200, response.getStatus());
111 String body = response.readEntity(String.class);
113 assertThat(body, containsString("switch1"));
114 assertThat(body, containsString("color1"));
115 assertThat(body, containsString("white1"));
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"));