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 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;
44 * Tests for {@link Sensors}.
46 * @author David Graeff - Initial contribution
49 public class SensorTests {
50 protected @NonNullByDefault({}) CommonSetup commonSetup;
51 protected @NonNullByDefault({}) ItemRegistry itemRegistry;
52 protected @NonNullByDefault({}) ConfigStore cs;
54 private @NonNullByDefault({}) HttpClient httpClient;
56 Sensors subject = new Sensors();
58 private void addItemToReg(GenericItem item, State state, String label) {
61 itemRegistry.add(item);
65 public void setUp() throws Exception {
66 commonSetup = new CommonSetup(false);
67 itemRegistry = new DummyItemRegistry();
69 this.cs = commonSetup.cs;
72 subject.userManagement = commonSetup.userManagement;
73 subject.itemRegistry = itemRegistry;
76 httpClient = new HttpClient();
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), "");
87 commonSetup.start(new ResourceConfig().registerInstances(subject));
91 public void tearDown() throws Exception {
92 commonSetup.dispose();
96 public void renameSensor() throws Exception {
97 assertThat(cs.ds.sensors.get("switch1").name, is("name1"));
99 String body = "{'name':'name2'}";
100 ContentResponse response = commonSetup.sendPut("/testuser/sensors/switch1", body);
101 assertEquals(200, response.getStatus());
103 body = response.getContentAsString();
105 assertThat(body, containsString("success"));
106 assertThat(body, containsString("name"));
107 assertThat(cs.ds.sensors.get("switch1").name, is("name2"));
111 public void allAndSingleSensor() throws Exception {
112 ContentResponse response = commonSetup.sendGet("/testuser/sensors");
113 assertEquals(200, response.getStatus());
115 String body = response.getContentAsString();
117 assertThat(body, containsString("switch1"));
118 assertThat(body, containsString("color1"));
119 assertThat(body, containsString("white1"));
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"));