2 * Copyright (c) 2010-2023 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.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import java.io.IOException;
21 import javax.ws.rs.client.Entity;
22 import javax.ws.rs.core.Response;
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;
47 * Tests for {@link Sensors}.
49 * @author David Graeff - Initial contribution
52 public class SensorTests {
53 protected @NonNullByDefault({}) CommonSetup commonSetup;
54 protected @NonNullByDefault({}) ItemRegistry itemRegistry;
55 protected @NonNullByDefault({}) ConfigStore cs;
57 Sensors subject = new Sensors();
59 private void addItemToReg(GenericItem item, State state, String label) {
62 itemRegistry.add(item);
66 public void setUp() throws IOException {
67 commonSetup = new CommonSetup(false);
68 itemRegistry = new DummyItemRegistry();
70 this.cs = commonSetup.cs;
73 subject.userManagement = commonSetup.userManagement;
74 subject.itemRegistry = itemRegistry;
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), "");
85 commonSetup.start(new ResourceConfig().registerInstances(subject));
89 public void tearDown() throws Exception {
90 commonSetup.dispose();
94 public void renameSensor() {
95 assertThat(cs.ds.sensors.get("switch1").name, is("name1"));
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"));
108 public void allAndSingleSensor() {
109 Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/sensors").request().get();
110 assertEquals(200, response.getStatus());
112 String body = response.readEntity(String.class);
114 assertThat(body, containsString("switch1"));
115 assertThat(body, containsString("color1"));
116 assertThat(body, containsString("white1"));
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"));