import java.util.Collections;
import java.util.Dictionary;
import java.util.Hashtable;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.api.ContentResponse;
+import org.eclipse.jetty.client.util.StringContentProvider;
+import org.eclipse.jetty.http.HttpHeader;
+import org.eclipse.jetty.http.HttpMethod;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.logging.LoggingFeature;
public class CommonSetup {
public String basePath;
- public Client client;
+ public HttpClient client;
public ConfigStore cs;
public HttpServer server;
log2.setLevel(Level.OFF);
server = GrizzlyHttpServerFactory.createHttpServer(URI.create(basePath), rc);
- client = ClientBuilder.newClient();
+ client = new HttpClient();
+ try {
+ client.start();
+ } catch (Exception e) {
+ throw new IllegalStateException("Failed to start HttpClient", e);
+ }
}
public void dispose() throws Exception {
if (client != null) {
- client.close();
+ client.stop();
}
if (server != null) {
server.shutdownNow();
mocksCloseable.close();
}
+
+ public ContentResponse sendDelete(String path) throws InterruptedException, TimeoutException, ExecutionException {
+ return client.newRequest(basePath + path).method(HttpMethod.DELETE).send();
+ }
+
+ public ContentResponse sendGet() throws InterruptedException, TimeoutException, ExecutionException {
+ return client.newRequest(basePath).method(HttpMethod.GET).send();
+ }
+
+ public ContentResponse sendGet(String path) throws InterruptedException, TimeoutException, ExecutionException {
+ return client.newRequest(basePath + path).method(HttpMethod.GET).send();
+ }
+
+ public ContentResponse sendPost(String content) throws InterruptedException, TimeoutException, ExecutionException {
+ return client.newRequest(basePath).method(HttpMethod.POST).header(HttpHeader.CONTENT_TYPE, "application/json")
+ .content(new StringContentProvider(content)).send();
+ }
+
+ public ContentResponse sendPost(String path, String content)
+ throws InterruptedException, TimeoutException, ExecutionException {
+ return client.newRequest(basePath + path).method(HttpMethod.POST)
+ .header(HttpHeader.CONTENT_TYPE, "application/json").content(new StringContentProvider(content)).send();
+ }
+
+ public ContentResponse sendPut(String path, String content)
+ throws InterruptedException, TimeoutException, ExecutionException {
+ return client.newRequest(basePath + path).method(HttpMethod.PUT)
+ .header(HttpHeader.CONTENT_TYPE, "application/json").content(new StringContentProvider(content)).send();
+ }
}
import java.io.IOException;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Response;
-
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jetty.client.api.ContentResponse;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
}
@Test
- public void changeSwitchState() {
+ public void changeSwitchState() throws Exception {
assertThat(((HueStatePlug) cs.ds.lights.get("1").state).on, is(false));
String body = "{'on':true}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/1/state").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/lights/1/state", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("success"));
+ assertThat(response.getContentAsString(), containsString("success"));
assertThat(((HueStatePlug) cs.ds.lights.get("1").state).on, is(true));
verify(commonSetup.eventPublisher).post(argThat((Event t) -> {
assertThat(t.getPayload(), is("{\"type\":\"OnOff\",\"value\":\"ON\"}"));
}
@Test
- public void changeGroupItemSwitchState() {
+ public void changeGroupItemSwitchState() throws Exception {
assertThat(((HueStatePlug) cs.ds.groups.get("10").action).on, is(false));
String body = "{'on':true}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/groups/10/action").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/groups/10/action", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("success"));
+ assertThat(response.getContentAsString(), containsString("success"));
assertThat(((HueStatePlug) cs.ds.groups.get("10").action).on, is(true));
verify(commonSetup.eventPublisher).post(argThat((Event t) -> {
assertThat(t.getPayload(), is("{\"type\":\"OnOff\",\"value\":\"ON\"}"));
}
@Test
- public void changeOnValue() {
+ public void changeOnValue() throws Exception {
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(false));
String body = "{'on':true}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/2/state").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/lights/2/state", body);
assertEquals(200, response.getStatus());
- String entity = response.readEntity(String.class);
+ String entity = response.getContentAsString();
assertThat(entity, is("[{\"success\":{\"/lights/2/state/on\":true}}]"));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(true));
}
@Test
- public void changeOnAndBriValues() {
+ public void changeOnAndBriValues() throws Exception {
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(false));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).bri, is(1));
String body = "{'on':true,'bri':200}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/2/state").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/lights/2/state", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("success"));
+ assertThat(response.getContentAsString(), containsString("success"));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(true));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).bri, is(200));
}
@Test
- public void changeHueSatValues() {
+ public void changeHueSatValues() throws Exception {
HueLightEntry hueDevice = cs.ds.lights.get("2");
hueDevice.item.setState(OnOffType.ON);
hueDevice.state.as(HueStateColorBulb.class).on = true;
String body = "{'hue':1000,'sat':50}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/2/state").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/lights/2/state", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("success"));
+ assertThat(response.getContentAsString(), containsString("success"));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(true));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).hue, is(1000));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).sat, is(50));
* Amazon echos are setting ct only, if commanded to turn a light white.
*/
@Test
- public void changeCtValue() {
+ public void changeCtValue() throws Exception {
HueLightEntry hueDevice = cs.ds.lights.get("2");
hueDevice.item.setState(OnOffType.ON);
hueDevice.state.as(HueStateColorBulb.class).on = true;
String body = "{'ct':500}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/2/state").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/lights/2/state", body);
assertEquals(200, response.getStatus());
- body = response.readEntity(String.class);
+ body = response.getContentAsString();
assertThat(body, containsString("success"));
assertThat(body, containsString("ct"));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(true));
}
@Test
- public void switchOnWithXY() {
+ public void switchOnWithXY() throws Exception {
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(false));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).bri, is(1));
String body = "{'on':true,'bri':200,'xy':[0.5119,0.4147]}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/2/state").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/lights/2/state", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("success"));
- assertThat(response.readEntity(String.class), containsString("xy"));
+ assertThat(response.getContentAsString(), containsString("success"));
+ assertThat(response.getContentAsString(), containsString("xy"));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).on, is(true));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).bri, is(200));
assertThat(((HueStateColorBulb) cs.ds.lights.get("2").state).xy[0], is(0.5119));
}
@Test
- public void allLightsAndSingleLight() {
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights").request().get();
+ public void allLightsAndSingleLight() throws Exception {
+ ContentResponse response = commonSetup.sendGet("/testuser/lights");
assertEquals(200, response.getStatus());
- String body = response.readEntity(String.class);
+ String body = response.getContentAsString();
assertThat(body, containsString("switch"));
assertThat(body, containsString("color"));
assertThat(body, containsString("white"));
// Single light access test
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/lights/2").request().get();
+ response = commonSetup.sendGet("/testuser/lights/2");
assertEquals(200, response.getStatus());
- body = response.readEntity(String.class);
+ body = response.getContentAsString();
assertThat(body, containsString("color"));
}
import java.util.Map.Entry;
import java.util.Random;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Response;
-
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jetty.client.api.ContentResponse;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@SuppressWarnings("null")
@Test
- public void addGetRemoveRuleViaRest() {
+ public void addGetRemoveRuleViaRest() throws Exception {
// 1. Create
String body = "{\"name\":\"test name\",\"description\":\"\",\"owner\":\"\",\"conditions\":[{\"address\":\"/lights/switch1/state/on\",\"operator\":\"dx\"}],\"actions\":[{\"address\":\"/lights/switch1/state\",\"method\":\"PUT\",\"body\":\"{\\u0027on\\u0027:true}\"}]}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/rules").request()
- .post(Entity.json(body));
+ ContentResponse response = commonSetup.sendPost("/testuser/rules", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("success"));
+ assertThat(response.getContentAsString(), containsString("success"));
// 1.1 Check for entry
Entry<String, HueRuleEntry> idAndEntry = cs.ds.rules.entrySet().stream().findAny().get();
assertThat(rule.getActions().get(0).getTypeUID(), is("rules.HttpAction"));
// 2. Get
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/rules/" + idAndEntry.getKey()).request()
- .get();
+ response = commonSetup.sendGet("/testuser/rules/" + idAndEntry.getKey());
assertEquals(200, response.getStatus());
- HueSceneEntry fromJson = new Gson().fromJson(response.readEntity(String.class), HueSceneEntry.class);
+ HueSceneEntry fromJson = new Gson().fromJson(response.getContentAsString(), HueSceneEntry.class);
assertThat(fromJson.name, is(idAndEntry.getValue().name));
// 3. Remove
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/rules/" + idAndEntry.getKey()).request()
- .delete();
+ response = commonSetup.sendDelete("/testuser/rules/" + idAndEntry.getKey());
assertEquals(200, response.getStatus());
assertTrue(cs.ds.rules.isEmpty());
}
@Test
- public void updateRuleViaRest() {
+ public void updateRuleViaRest() throws Exception {
HueCommand command = new HueCommand("/api/testuser/lights/switch1/state", "PUT", "{'on':true}");
HueRuleEntry.Condition condition = new HueRuleEntry.Condition("/lights/switch1/state/on", Operator.dx, null);
// Modify (just the name)
String body = "{ 'name':'A new name'}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/rules/demo1").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/rules/demo1", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("name"));
+ assertThat(response.getContentAsString(), containsString("name"));
Entry<String, HueRuleEntry> idAndEntry = cs.ds.rules.entrySet().stream().findAny().get();
HueRuleEntry entry = idAndEntry.getValue();
// Modify (Change condition)
body = "{\"conditions\":[{\"address\":\"/lights/switch1/state/on\",\"operator\":\"ddx\"}]}";
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/rules/demo1").request()
- .put(Entity.json(body));
+ response = commonSetup.sendPut("/testuser/rules/demo1", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("conditions"));
+ assertThat(response.getContentAsString(), containsString("conditions"));
idAndEntry = cs.ds.rules.entrySet().stream().findAny().get();
entry = idAndEntry.getValue();
// Modify (Change action)
body = "{\"actions\":[{\"address\":\"/lights/switch2/state\",\"method\":\"PUT\",\"body\":\"{\\u0027on\\u0027:false}\"}]}";
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/rules/demo1").request()
- .put(Entity.json(body));
+ response = commonSetup.sendPut("/testuser/rules/demo1", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("actions"));
+ assertThat(response.getContentAsString(), containsString("actions"));
idAndEntry = cs.ds.rules.entrySet().stream().findAny().get();
entry = idAndEntry.getValue();
}
@Test
- public void getAll() {
+ public void getAll() throws Exception {
HueCommand command = new HueCommand("/api/testuser/lights/switch1/state", "PUT", "{'on':true}");
HueRuleEntry.Condition condition = new HueRuleEntry.Condition("/lights/switch1/state/on", Operator.dx, null);
ruleRegistry.add(rule);
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/rules").request().get();
+ ContentResponse response = commonSetup.sendGet("/testuser/rules");
Type type = new TypeToken<Map<String, HueRuleEntry>>() {
}.getType();
- String body = response.readEntity(String.class);
+ String body = response.getContentAsString();
Map<String, HueRuleEntry> fromJson = new Gson().fromJson(body, type);
HueRuleEntry entry = fromJson.get("demo1");
assertThat(entry.name, is("test name"));
import java.util.Map;
import java.util.Map.Entry;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Response;
-
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jetty.client.api.ContentResponse;
import org.glassfish.jersey.server.ResourceConfig;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.AfterEach;
@SuppressWarnings("null")
@Test
- public void addGetRemoveSceneViaRest() {
+ public void addGetRemoveSceneViaRest() throws Exception {
// 1. Create
String body = "{ 'name':'Cozy dinner', 'recycle':false, 'lights':['switch1','white1'], 'type':'LightScene'}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/scenes").request()
- .post(Entity.json(body));
+ ContentResponse response = commonSetup.sendPost("/testuser/scenes", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("success"));
+ assertThat(response.getContentAsString(), containsString("success"));
// 1.1 Check for scene entry
Entry<String, HueSceneEntry> entry = cs.ds.scenes.entrySet().stream().findAny().get();
assertThat(rule.getActions().get(1).getId(), is("white1"));
// 2. Get
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/scenes/" + entry.getKey()).request()
- .get();
+ response = commonSetup.sendGet("/testuser/scenes/" + entry.getKey());
assertEquals(200, response.getStatus());
- HueSceneEntry fromJson = new Gson().fromJson(response.readEntity(String.class), HueSceneEntry.class);
+ HueSceneEntry fromJson = new Gson().fromJson(response.getContentAsString(), HueSceneEntry.class);
assertThat(fromJson.name, is(entry.getValue().name));
// 3. Remove
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/scenes/" + entry.getKey()).request()
- .delete();
+ response = commonSetup.sendDelete("/testuser/scenes/" + entry.getKey());
assertEquals(200, response.getStatus());
assertTrue(cs.ds.scenes.isEmpty());
}
@SuppressWarnings("null")
@Test
- public void updateSceneViaRest() {
+ public void updateSceneViaRest() throws Exception {
Rule rule = RuleBuilder.create("demo1").withTags("scene").withName("Some name") //
.withActions(Scenes.actionFromState("switch1", (Command) OnOffType.ON)).build();
// 3. Modify (just the name)
String body = "{ 'name':'A new name'}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/scenes/demo1").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/scenes/demo1", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("name"));
+ assertThat(response.getContentAsString(), containsString("name"));
Entry<String, HueSceneEntry> sceneEntry = cs.ds.scenes.entrySet().stream().findAny().get();
assertThat(sceneEntry.getValue().name, is("A new name"));
// Without store lights
body = "{ 'lights':['white1']}";
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/scenes/demo1").request()
- .put(Entity.json(body));
+ response = commonSetup.sendPut("/testuser/scenes/demo1", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("lights"));
+ assertThat(response.getContentAsString(), containsString("lights"));
sceneEntry = cs.ds.scenes.entrySet().stream().findAny().get();
assertThat(sceneEntry.getValue().name, is("Some name")); // should not have changed
// With store lights
body = "{ 'lights':['white1'], 'storelightstate':true }";
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/scenes/demo1").request()
- .put(Entity.json(body));
+ response = commonSetup.sendPut("/testuser/scenes/demo1", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("lights"));
+ assertThat(response.getContentAsString(), containsString("lights"));
sceneEntry = cs.ds.scenes.entrySet().stream().findAny().get();
assertThat(sceneEntry.getValue().lights.get(0), is("white1"));
}
@Test
- public void getAll() {
+ public void getAll() throws Exception {
Rule rule = RuleBuilder.create("demo1").withTags("scene") //
.withActions(Scenes.actionFromState("switch1", (Command) OnOffType.ON)).build();
ruleRegistry.add(rule);
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/scenes").request().get();
+ ContentResponse response = commonSetup.sendGet("/testuser/scenes");
Type type = new TypeToken<Map<String, HueSceneEntry>>() {
}.getType();
- Map<String, HueSceneEntry> fromJson = new Gson().fromJson(response.readEntity(String.class), type);
+ Map<String, HueSceneEntry> fromJson = new Gson().fromJson(response.getContentAsString(), type);
assertTrue(fromJson.containsKey("demo1"));
}
}
import java.util.Map.Entry;
import java.util.Random;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Response;
-
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jetty.client.api.ContentResponse;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@SuppressWarnings("null")
@Test
- public void addGetRemoveScheduleViaRest() {
+ public void addGetRemoveScheduleViaRest() throws Exception {
// 1. Create
String body = "{ 'name':'Wake up', 'description':'My wake up alarm', 'localtime':'2015-06-30T14:24:40'," + //
"'command':{'address':'/api/testuser/lights/1/state','method':'PUT','body':'{\"on\":true}'} }";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/schedules").request()
- .post(Entity.json(body));
+ ContentResponse response = commonSetup.sendPost("/testuser/schedules", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("success"));
+ assertThat(response.getContentAsString(), containsString("success"));
// 1.1 Check for entry
Entry<String, HueScheduleEntry> entry = cs.ds.schedules.entrySet().stream().findAny().get();
assertThat(rule.getActions().get(0).getTypeUID(), is("rules.HttpAction"));
// 2. Get
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/schedules/" + entry.getKey()).request()
- .get();
+ response = commonSetup.sendGet("/testuser/schedules/" + entry.getKey());
assertEquals(200, response.getStatus());
- HueSceneEntry fromJson = new Gson().fromJson(response.readEntity(String.class), HueSceneEntry.class);
+ HueSceneEntry fromJson = new Gson().fromJson(response.getContentAsString(), HueSceneEntry.class);
assertThat(fromJson.name, is(entry.getValue().name));
// 3. Remove
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/schedules/" + entry.getKey()).request()
- .delete();
+ response = commonSetup.sendDelete("/testuser/schedules/" + entry.getKey());
assertEquals(200, response.getStatus());
assertTrue(cs.ds.schedules.isEmpty());
}
@SuppressWarnings("null")
@Test
- public void updateScheduleViaRest() {
+ public void updateScheduleViaRest() throws Exception {
HueCommand command = new HueCommand("/api/testuser/lights/1/state", "PUT", "{'on':true}");
String localtime = "2020-02-01T12:12:00";
// Modify (just the name)
String body = "{ 'name':'A new name'}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/schedules/demo1").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/schedules/demo1", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("name"));
+ assertThat(response.getContentAsString(), containsString("name"));
Entry<String, HueScheduleEntry> entry = cs.ds.schedules.entrySet().stream().findAny().get();
assertThat(entry.getValue().name, is("A new name"));
// Modify (Change time)
body = "{ 'localtime':'2015-06-30T14:24:40'}";
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/schedules/demo1").request()
- .put(Entity.json(body));
+ response = commonSetup.sendPut("/testuser/schedules/demo1", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("localtime"));
+ assertThat(response.getContentAsString(), containsString("localtime"));
entry = cs.ds.schedules.entrySet().stream().findAny().get();
assertThat(entry.getValue().name, is("test name")); // should not have changed
// Modify (Change command)
body = "{ 'command':{'address':'/api/testuser/lights/2/state','method':'PUT','body':'{\"on\":true}'} }";
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/schedules/demo1").request()
- .put(Entity.json(body));
+ response = commonSetup.sendPut("/testuser/schedules/demo1", body);
assertEquals(200, response.getStatus());
- assertThat(response.readEntity(String.class), containsString("command"));
+ assertThat(response.getContentAsString(), containsString("command"));
entry = cs.ds.schedules.entrySet().stream().findAny().get();
assertThat(entry.getValue().name, is("test name")); // should not have changed
}
@Test
- public void getAll() {
+ public void getAll() throws Exception {
HueCommand command = new HueCommand("/api/testuser/lights/1/state", "POST", "{'on':true}");
String localtime = "2020-02-01T12:12:00";
ruleRegistry.add(rule);
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/schedules").request().get();
+ ContentResponse response = commonSetup.sendGet("/testuser/schedules");
Type type = new TypeToken<Map<String, HueSceneEntry>>() {
}.getType();
- Map<String, HueSceneEntry> fromJson = new Gson().fromJson(response.readEntity(String.class), type);
+ Map<String, HueSceneEntry> fromJson = new Gson().fromJson(response.getContentAsString(), type);
assertTrue(fromJson.containsKey("demo1"));
}
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import java.io.IOException;
-
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Response;
-
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.api.ContentResponse;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
protected @NonNullByDefault({}) ItemRegistry itemRegistry;
protected @NonNullByDefault({}) ConfigStore cs;
+ private @NonNullByDefault({}) HttpClient httpClient;
+
Sensors subject = new Sensors();
private void addItemToReg(GenericItem item, State state, String label) {
}
@BeforeEach
- public void setUp() throws IOException {
+ public void setUp() throws Exception {
commonSetup = new CommonSetup(false);
itemRegistry = new DummyItemRegistry();
subject.itemRegistry = itemRegistry;
subject.activate();
+ httpClient = new HttpClient();
+ httpClient.start();
+
// Add simulated sensor items
addItemToReg(new SwitchItem("switch1"), OnOffType.ON, "name1");
addItemToReg(new ContactItem("contact1"), OpenClosedType.OPEN, "");
}
@Test
- public void renameSensor() {
+ public void renameSensor() throws Exception {
assertThat(cs.ds.sensors.get("switch1").name, is("name1"));
String body = "{'name':'name2'}";
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/sensors/switch1").request()
- .put(Entity.json(body));
+ ContentResponse response = commonSetup.sendPut("/testuser/sensors/switch1", body);
assertEquals(200, response.getStatus());
- body = response.readEntity(String.class);
+
+ body = response.getContentAsString();
+
assertThat(body, containsString("success"));
assertThat(body, containsString("name"));
assertThat(cs.ds.sensors.get("switch1").name, is("name2"));
}
@Test
- public void allAndSingleSensor() {
- Response response = commonSetup.client.target(commonSetup.basePath + "/testuser/sensors").request().get();
+ public void allAndSingleSensor() throws Exception {
+ ContentResponse response = commonSetup.sendGet("/testuser/sensors");
assertEquals(200, response.getStatus());
- String body = response.readEntity(String.class);
+ String body = response.getContentAsString();
assertThat(body, containsString("switch1"));
assertThat(body, containsString("color1"));
assertThat(body, containsString("white1"));
// Single light access test
- response = commonSetup.client.target(commonSetup.basePath + "/testuser/sensors/switch1").request().get();
+ response = commonSetup.sendGet("/testuser/sensors/switch1");
assertEquals(200, response.getStatus());
- body = response.readEntity(String.class);
+ body = response.getContentAsString();
assertThat(body, containsString("CLIPGenericFlag"));
}
}
import java.util.Collections;
import java.util.Dictionary;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Response;
-
+import org.eclipse.jetty.client.api.ContentResponse;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
}
@Test
- public void addUser() {
+ public void addUser() throws Exception {
// GET should fail
- assertEquals(405, commonSetup.client.target(commonSetup.basePath).request().get().getStatus());
+ assertEquals(405, commonSetup.sendGet().getStatus());
String body = "{'username':'testuser','devicetype':'app#device'}";
- Response response;
- HueResponse[] r;
-
// Post should create a user, except: if linkbutton not enabled
- response = commonSetup.client.target(commonSetup.basePath).request().post(Entity.json(body));
+ ContentResponse response = commonSetup.sendPost(body);
assertThat(response.getStatus(), is(200));
- r = commonSetup.cs.gson.fromJson(response.readEntity(String.class), HueResponse[].class);
+ HueResponse[] r = commonSetup.cs.gson.fromJson(response.getContentAsString(), HueResponse[].class);
assertNotNull(r[0].error);
// Post should create a user
commonSetup.cs.ds.config.linkbutton = true;
- response = commonSetup.client.target(commonSetup.basePath).request().post(Entity.json(body));
+ response = commonSetup.sendPost(body);
assertThat(response.getStatus(), is(200));
- JsonElement e = JsonParser.parseString(response.readEntity(String.class)).getAsJsonArray().get(0);
+ JsonElement e = JsonParser.parseString(response.getContentAsString()).getAsJsonArray().get(0);
e = e.getAsJsonObject().get("success");
HueSuccessResponseCreateUser rc = commonSetup.cs.gson.fromJson(e, HueSuccessResponseCreateUser.class);
assertNotNull(rc);
}
@Test
- public void unauthorizedAccessTest() {
+ public void unauthorizedAccessTest() throws Exception {
// Unauthorized config
- Response response;
- response = commonSetup.client.target(commonSetup.basePath + "/config").request().get();
+ ContentResponse response = commonSetup.sendGet("/config");
assertThat(response.getStatus(), is(200));
- HueUnauthorizedConfig config = new Gson().fromJson(response.readEntity(String.class),
- HueUnauthorizedConfig.class);
+ HueUnauthorizedConfig config = new Gson().fromJson(response.getContentAsString(), HueUnauthorizedConfig.class);
assertThat(config.bridgeid, is(commonSetup.cs.ds.config.bridgeid));
assertThat(config.name, is(commonSetup.cs.ds.config.name));
// Invalid user name
- response = commonSetup.client.target(commonSetup.basePath + "/invalid/config").request().get();
+ response = commonSetup.sendGet("/invalid/config");
assertThat(response.getStatus(), is(403));
- assertThat(response.readEntity(String.class), containsString("error"));
+ assertThat(response.getContentAsString(), containsString("error"));
}
}
import java.util.concurrent.TimeoutException;
import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.core.Response;
+import org.eclipse.jetty.client.api.ContentResponse;
import org.glassfish.grizzly.osgi.httpservice.HttpServiceImpl;
import org.glassfish.grizzly.osgi.httpservice.OSGiMainHandler;
import org.glassfish.grizzly.osgi.httpservice.util.Logger;
}
@Test
- public void descriptionWithoutAddress() {
- Response response = commonSetup.client.target(descriptionPath).request().get();
+ public void descriptionWithoutAddress() throws Exception {
+ ContentResponse response = commonSetup.client.newRequest(descriptionPath).send();
assertEquals(404, response.getStatus());
}
HueEmulationConfigWithRuntime r = subject.createConfiguration(null);
r = subject.performAddressTest(r);
subject.applyConfiguration(r);
- Response response = commonSetup.client.target(descriptionPath).request().get();
+ ContentResponse response = commonSetup.client.newRequest(descriptionPath).send();
assertEquals(200, response.getStatus());
- String body = response.readEntity(String.class);
+ String body = response.getContentAsString();
assertThat(body, is(subject.xmlDocWithAddress));
if (r == null) {
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
- jakarta.ws.rs-api;version='[2.1.6,2.1.7)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
- jakarta.ws.rs-api;version='[2.1.6,2.1.7)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
- jakarta.ws.rs-api;version='[2.1.6,2.1.7)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.glassfish.hk2.osgi-resource-locator;version='[1.0.3,1.0.4)',\
org.apache.aries.javax.jax.rs-api;version='[1.0.1,1.0.2)',\
jakarta.annotation-api;version='[1.3.5,1.3.6)',\
- jakarta.xml.soap-api;version='[1.4.2,1.4.3)',\
- jakarta.xml.ws-api;version='[2.3.3,2.3.4)',\
org.apache.aries.component-dsl.component-dsl;version='[1.2.2,1.2.3)',\
- org.apache.ws.xmlschema.core;version='[2.2.5,2.2.6)',\
stax2-api;version='[4.2.1,4.2.2)',\
jakarta.inject.jakarta.inject-api;version='[2.0.0,2.0.1)',\
org.glassfish.hk2.external.javax.inject;version='[2.4.0,2.4.1)',\
- org.apache.cxf.cxf-core;version='[3.4.5,3.4.6)',\
- org.apache.cxf.cxf-rt-frontend-jaxrs;version='[3.4.5,3.4.6)',\
- org.apache.cxf.cxf-rt-rs-client;version='[3.4.5,3.4.6)',\
- org.apache.cxf.cxf-rt-rs-sse;version='[3.4.5,3.4.6)',\
- org.apache.cxf.cxf-rt-security;version='[3.4.5,3.4.6)',\
- org.apache.cxf.cxf-rt-transports-http;version='[3.4.5,3.4.6)',\
si-units;version='[2.1.0,2.1.1)',\
si.uom.si-quantity;version='[2.1.0,2.1.1)',\
org.apache.aries.jax.rs.whiteboard;version='[2.0.0,2.0.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
uom-lib-common;version='[2.2.0,2.2.1)',\
- com.fasterxml.woodstox.woodstox-core;version='[6.5.1,6.5.2)'
+ com.fasterxml.woodstox.woodstox-core;version='[6.5.1,6.5.2)',\
+ org.apache.cxf.cxf-core;version='[3.6.1,3.6.2)',\
+ org.apache.cxf.cxf-rt-frontend-jaxrs;version='[3.6.1,3.6.2)',\
+ org.apache.cxf.cxf-rt-rs-client;version='[3.6.1,3.6.2)',\
+ org.apache.cxf.cxf-rt-rs-sse;version='[3.6.1,3.6.2)',\
+ org.apache.cxf.cxf-rt-security;version='[3.6.1,3.6.2)',\
+ org.apache.cxf.cxf-rt-transports-http;version='[3.6.1,3.6.2)',\
+ org.apache.ws.xmlschema.core;version='[2.3.0,2.3.1)'
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.core.thing;version='[4.1.0,4.1.1)',\
org.openhab.core.transform;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\
org.openhab.persistence.mapdb;version='[4.1.0,4.1.1)',\
org.openhab.persistence.mapdb.tests;version='[4.1.0,4.1.1)',\
org.openhab.base-fixes;version='[1.0.0,1.0.1)',\
- org.osgi.service.cm;version='[1.6.0,1.6.1)',\
javax.measure.unit-api;version='[2.2.0,2.2.1)',\
org.apiguardian.api;version='[1.1.2,1.1.3)',\
tech.units.indriya;version='[2.2.0,2.2.1)',\