diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..813a923 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/vcs.xml +.idea +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store diff --git a/README.md b/README.md index b80ed7c..63ea033 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,40 @@ -# smart-home-tests-plus -integration tests for virtual smart home plus +# Virtual Smart Home Project + +This project requires Docker to be running on your machine. Follow the steps below to get the Virtual Smart Home Docker images up and running. + +## Prerequisites + +- Docker installed and running on your machine. +- Git installed on your machine. + +## Steps + +1. Clone the virtual-smart-home-plus repositories from GitHub: + ``` + git clone + ``` +2. Clone the virtual-smart-home-gateway-plus repository from GitHub: + ``` + git clone + ``` +3. Navigate to the project directory of both repositories: + ``` + cd + ``` +4. Run Maven to clean and build the both projects: + ``` + mvn clean install + ``` +5. Create the Docker image for both projects by running the following command in each project directory: + ``` + docker build -t . + ``` +6. Run the `init.py` script to initialize the project: + ``` + python init.py + ``` + +7. Run the tests to ensure everything is working correctly: + ``` + mvn test + ``` \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..02d49b4 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3.7' +services: + gateway: + image: virtual-smart-home-plus-api:latest + ports: + - "8080:8080" + expose: + - "8080" + smart-home: + image: virtual-smart-home-plus:latest + ports: + - "8081:8081" + expose: + - "8081" diff --git a/init.py b/init.py new file mode 100755 index 0000000..cefd68a --- /dev/null +++ b/init.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +import subprocess +import requests +import time + +with open("/tmp/output.log","a") as out: + subprocess.Popen("docker compose up", shell=True, stdout=out, stderr=out) + +print("Waiting for virtual-smart-home to start") +print("Waiting for virtual-smart-home-gateway to start") +time.sleep(9) +print("Servers started") + +house_url = "http://localhost:8081" +gateway_url = "http://localhost:8080" +doorObject = {"label": "door1", "deviceType": "door", "status": "open"} +thermometerObject = {"label": "thermometer1", "deviceType": "thermometer", "temperature": 15.3} +rgbObject = { + "label": "rgb1", "deviceType": "rgb", "switchedOn": "true", "red": 255, "green": 0, "blue": 0} +fireplaceObject = {"label": "fireplace1", "deviceType": "fireplace", "status": "extinguished"} +houseObject = {"name": "house1", "address": "http://smart-home:8081"} + +requests.post(gateway_url + "/api/v0.1/gateway/house", json=houseObject) +requests.post(house_url + "/api/v0.1/house/device/door/", json=doorObject) +requests.post(house_url + "/api/v0.1/house/device/thermometer/", json=thermometerObject) +requests.post(house_url + "/api/v0.1/house/device/rgb/", json=rgbObject) +requests.post(house_url + "/api/v0.1/house/device/fireplace/", json=fireplaceObject) +print("Devices created") diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b2e3f5e --- /dev/null +++ b/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + org.example + Virtaul-Smart-Home-Tests + 1.0-SNAPSHOT + + + + org.junit.jupiter + junit-jupiter-api + 5.8.1 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.8.1 + test + + + io.rest-assured + rest-assured + 5.4.0 + test + + + com.fasterxml.jackson.core + jackson-databind + 2.13.0 + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + + + + + 17 + 17 + + + diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java new file mode 100644 index 0000000..9a3edc1 --- /dev/null +++ b/src/main/java/org/example/Main.java @@ -0,0 +1,7 @@ +package org.example; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} diff --git a/src/test/java/DeviceTest.java b/src/test/java/DeviceTest.java new file mode 100644 index 0000000..150b010 --- /dev/null +++ b/src/test/java/DeviceTest.java @@ -0,0 +1,78 @@ +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static io.restassured.RestAssured.get; +import static io.restassured.RestAssured.with; +import static org.hamcrest.Matchers.*; + +public class DeviceTest { + + @Test + public void device_returns_200_with_correct_name_and_address() { + get("http://localhost:8080/api/v0.1/gateway/house1/device") + .then() + .statusCode(200).assertThat() + .log().all() + .body("size()",is(4)) + .body("deviceType", hasItems("RGBLight", "Door", "Fireplace", "Thermometer")); + } + + @Test + public void device_returns_404_with_incorrect_name() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer2") + .then() + .statusCode(404) + .assertThat() + .body("status", equalTo("NOT_FOUND")); + } + + @Test + public void device_returns_200_for_rgb() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/rgb1") + .then() + .statusCode(200).assertThat() + .body("label", equalTo("rgb1")) + .body("deviceType", equalTo("RGBLight")) + .body("enabled", equalTo(false)) + .body("red", instanceOf(Integer.class)) + .body("green", instanceOf(Integer.class)) + .body("blue", instanceOf(Integer.class)); + } + + @Test + public void device_returns_200_for_door() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/door1") + .then() + .statusCode(200).assertThat() + .body("label", equalTo("door1")) + .body("deviceType", equalTo("Door")) + .body("enabled", instanceOf(Boolean.class)) + .body("status", either(equalTo("Opened")).or(equalTo("Closed"))); + } + + @Test + public void device_returns_200_for_fireplace() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace1") + .then() + .statusCode(200).assertThat() + .body("label", equalTo("fireplace1")) + .body("deviceType", equalTo("Fireplace")) + .body("enabled", equalTo(false)) + .body("status", either(equalTo("Extinguished")).or(equalTo("On_fire"))); + + } + + @Test + public void device_returns_200_for_thermometer() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer1") + .then() + .statusCode(200).assertThat() + .body("label", equalTo("thermometer1")) + .body("deviceType", equalTo("Thermometer")) + .body("enabled", instanceOf(Boolean.class)) + .body("unit", equalTo("C")) + .body("temperature", instanceOf(Float.class)); + } + +} diff --git a/src/test/java/DoorTest.java b/src/test/java/DoorTest.java new file mode 100644 index 0000000..e33b511 --- /dev/null +++ b/src/test/java/DoorTest.java @@ -0,0 +1,286 @@ +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static io.restassured.RestAssured.get; +import static io.restassured.RestAssured.with; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; + +public class DoorTest { + + @Test + public void door_get_returns_200_with_correct_label_and_type() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("door1")) + .body("status", equalTo("Opened")); + } + + @Test + public void door_get_returns_404_with_incorrect_label() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/door/door2") + .then() + .log().all() + .log().body() + .statusCode(404) + .assertThat() + .body("status", equalTo("NOT_FOUND")); + } + + @Test + public void door_get_returns_404_with_incorrect_devType() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/Doorrrr/door1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void door_get_returns_404_with_empty_label() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/door/") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void update_door_returns_200_with_correct_label_and_devType() { + Map door = Map.of("label","door1","deviceType","Door" ,"enabled",true, "status", "Closed"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(200) + .assertThat() + .body("label", equalTo("door1")) + .body("enabled", equalTo(true)) + .body("status", equalTo("Closed")); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(200) + .assertThat() + .body("label", equalTo("door1")) + .body("enabled", equalTo(true)) + .body("status", equalTo("Closed")); + + door= Map.of("label", "door1", "enabled", false, "status", "Opened", "deviceType", "Door"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(200) + .assertThat() + .body("label", equalTo("door1")) + .body("enabled", equalTo(false)) + .body("status", equalTo("Opened")); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(200) + .assertThat() + .body("label", equalTo("door1")) + .body("enabled", equalTo(false)) + .body("status", equalTo("Opened")); + } + + @Test + public void update_door_returns_200_with_minimal_body() { + Map door = Map.of("label", "door1", "deviceType", "Door"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(200) + .assertThat() + .body("label", equalTo("door1")) + .body("enabled", instanceOf(Boolean.class)) + .body("status", equalTo("Opened")); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(200) + .assertThat() + .body("label", equalTo("door1")) + .body("enabled", instanceOf(Boolean.class)) + .body("status", equalTo("Opened")); + + } + + @Test + public void door_update_returns_400_with_incorrect_label() { + Map door = Map.of("label","door2","deviceType","Door" ,"enabled",true, "status", "opened"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door2") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void door_update_returns_400_with_incorrect_body_label_missing() { + Map door = Map.of("deviceType","Door" ,"enabled",true, "status", "opened"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void door_update_returns_400_with_incorrect_body_label_type() { + Map door = Map.of("label",1,"deviceType","Door" ,"enabled",true, "status", "opened"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void door_update_returns_400_with_incorrect_body_deviceType_wrong_type() { + Map door = Map.of("label","door1","deviceType",1 ,"enabled",true, "status", "opened"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void door_update_returns_400_with_incorrect_body_status_wrong_type() { + Map door = Map.of("label","door1","deviceType","Door" ,"enabled",true, "status", 1); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void door_update_returns_400_with_incorrect_body_enabled_wrong_type() { + Map door = Map.of("label","door1","deviceType","Door" ,"enabled",1, "status", "opened"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void door_update_returns_400_with_wrong_enable_not_boolean() { + Map door = Map.of("label","door1","deviceType","Door" ,"enabled","true", "status", "opened"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void door_update_returns_400_with_wrong_status() { + Map door = Map.of("label","door1","deviceType","Door" ,"enabled",true, "status", "open"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void door_update_returns_400_with_wrong_enable_type() { + Map door = Map.of("label","door1","deviceType","Door" ,"enabled",1, "status", "opened"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + + + @Test + public void door_update_returns_400_with_incorrect_devType() { + Map door = Map.of("label","door1","deviceType","Door" ,"enabled",true, "status", "opened"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/doorr/door1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void door_update_returns_404_with_empty_label() { + Map door = Map.of("deviceType","Door" ,"enabled",true, "status", "opened"); + + with().body(door) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/door/") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void door_post_returns_404() { + Map door = Map.of("label","door1","deviceType","Door" ,"enabled",true, "status", "opened"); + + with().body(door) + .when().post("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void door_delete_returns_404() { + with() + .when().delete("http://localhost:8080/api/v0.1/gateway/house1/device/door/door1") + .then() + .log().all() + .statusCode(404); + } + + + + +} diff --git a/src/test/java/FireplaceTest.java b/src/test/java/FireplaceTest.java new file mode 100644 index 0000000..addd9dd --- /dev/null +++ b/src/test/java/FireplaceTest.java @@ -0,0 +1,263 @@ +import org.junit.jupiter.api.Test; + +import java.net.PortUnreachableException; +import java.util.Map; + +import static io.restassured.RestAssured.get; +import static io.restassured.RestAssured.with; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; + +public class FireplaceTest { + + @Test + public void fireplace_get_returns_200_with_correct_label_and_type() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("fireplace1")) + .body("enabled", equalTo(false)) + .body("status", equalTo("On_fire")); + } + + @Test + public void fireplace_get_returns_404_with_incorrect_label() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace2") + .then() + .log().all() + .log().body() + .statusCode(404) + .assertThat() + .body("status", equalTo("NOT_FOUND")); + } + + @Test + public void fireplace_get_returns_404_with_incorrect_devType() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/Fireplacerrr/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void fireplace_get_returns_404_with_empty_label() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void update_fireplace_returns_200_with_correct_label_and_devType() { + Map fireplace = Map.of("label","fireplace1","deviceType","Fireplace" ,"enabled",true, "status", "Extinguished"); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .statusCode(200).assertThat() + .body("label", equalTo("fireplace1")) + .body("enabled", equalTo(true)) + .body("status", equalTo("Extinguished")); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("fireplace1")) + .body("enabled", equalTo(true)) + .body("status", equalTo("Extinguished")); + + fireplace= Map.of("label", "fireplace1","deviceType","Fireplace", "enabled", false, "status", "On_fire"); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("fireplace1")) + .body("enabled", equalTo(false)) + .body("status", equalTo("On_fire")); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("fireplace1")) + .body("enabled", equalTo(false)) + .body("status", equalTo("On_fire")); + } + + @Test + public void update_fireplace_returns_200_with_minimal_body() { + Map fireplace = Map.of("label","fireplace1","deviceType","Fireplace"); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .statusCode(200).assertThat() + .body("label", equalTo("fireplace1")) + .body("enabled", equalTo(false)) + .body("status", equalTo("On_fire")); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("fireplace1")) + .body("enabled", equalTo(false)) + .body("status", equalTo("On_fire")); + } + + @Test + public void update_fireplace_returns_404_with_incorrect_label() { + Map fireplace = Map.of("label","fireplace2","deviceType","Fireplace" ,"enabled",true); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace2") + .then() + .log().all() + .log().body() + .statusCode(404) + .assertThat() + .body("status", equalTo("NOT_FOUND")); + } + + @Test + public void update_fireplace_returns_404_with_incorrect_devType() { + Map fireplace = Map.of("label","fireplace1","deviceType","Fireplace" ,"enabled",true); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplaceeee/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(404) + .assertThat() + .body("error", equalTo("Not Found")); + } + + @Test + public void update_fireplace_returns_404_with_empty_label() { + Map fireplace = Map.of("label","","deviceType","Fireplace" ,"enabled",true); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void update_fireplace_returns_400_with_incorrect_status_type() { + Map fireplace = Map.of("label","fireplace1","deviceType","Fireplace" ,"enabled",true, "status", 1); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(400) + .assertThat(); + } + + @Test + public void update_fireplace_returns_400_with_incorrect_status() { + Map fireplace = Map.of("label","fireplace1","deviceType","Fireplace" ,"enabled",true, "status", "on_fireee"); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(400) + .assertThat(); + } + + @Test + public void update_fireplace_returns_400_with_incorrect_enabled_type() { + Map fireplace = Map.of("label","fireplace1","deviceType","Fireplace" ,"enabled", 1, "status", "on_fire"); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(400) + .assertThat(); + } + + @Test + public void update_fireplace_returns_400_with_incorrect_label_type() { + Map fireplace = Map.of("label",1,"deviceType","Fireplace" ,"enabled",true, "status", "on_fire"); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(400) + .assertThat(); + } + + @Test + public void update_fireplace_returns_400_with_incorrect_devType_type() { + Map fireplace = Map.of("label","fireplace1","deviceType",1 ,"enabled",true, "status", "on_fire"); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(400) + .assertThat(); + } + + @Test + public void update_fireplace_returns_400_with_empty_body() { + Map fireplace = Map.of(); + + with().body(fireplace) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(400) + .assertThat(); + } + + + @Test + public void fireplace_post_returns_404() { + Map fireplace = Map.of("label","fireplace1","deviceType","Fireplace" ,"enabled",true); + + with().body(fireplace) + .when().post("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void fireplace_delete_returns_404() { + with() + .when().delete("http://localhost:8080/api/v0.1/gateway/house1/device/fireplace/fireplace1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + +} diff --git a/src/test/java/HouseTest.java b/src/test/java/HouseTest.java new file mode 100644 index 0000000..87cdb75 --- /dev/null +++ b/src/test/java/HouseTest.java @@ -0,0 +1,148 @@ +import org.junit.jupiter.api.Test; + + +import java.util.HashMap; +import java.util.Map; + +import static io.restassured.RestAssured.*; +import static org.hamcrest.Matchers.*; + +public class HouseTest { + + @Test + public void house_returns_200_with_correct_name_and_address() { + get("http://localhost:8080/api/v0.1/gateway/house") + .then() + .statusCode(200).assertThat() + .body("name", hasItem("house1")) + .body("address", hasItem("http://smart-home:8081")); + } + + + @Test + public void house_returns_404_with_incorrect_name() { + get("http://localhost:8080/api/v0.1/gateway/house2") + .then() + .statusCode(404) + .assertThat() + .body("error", equalTo("House not Found")); + } + + @Test + public void house_returns_404_with_empty_name() { + get("http://localhost:8080/api/v0.1/gateway/") + .then() + .statusCode(404); + } + + @Test + public void create_house_returns_201_with_correct_name_and_address() { + Map house = new HashMap<>(); + house.put("name", "house2"); + house.put("address", "http://smart-home:8081"); + + with().body(house) + .when().post("http://localhost:8080/api/v0.1/gateway/house") + .then() + .statusCode(200) + .assertThat() + .body("name", equalTo("house2")) + .body("address", equalTo("http://smart-home:8081")); + + delete("http://localhost:8080/api/v0.1/gateway/house2") + .then() + .statusCode(204); + } + + @Test + public void create_house_returns_409_with_duplicate_name() { + Map house = new HashMap<>(); + house.put("name", "house2"); + house.put("address", "http://smart-home:8081"); + + + with().body(house).when().post("http://localhost:8080/api/v0.1/gateway/house") + .then().statusCode(200); + + with().body(house) + .when().post("http://localhost:8080/api/v0.1/gateway/house") + .then() + .statusCode(409) + .assertThat() + .body("error", equalTo("House already exists")); + + delete("http://localhost:8080/api/v0.1/gateway/house2") + .then() + .statusCode(204); + } + + @Test + public void create_house_returns_400_with_empty_name() { + Map house = new HashMap<>(); + house.put("name", ""); + house.put("address", "http://smart-home:8081"); + + with().body(house) + .when().post("http://localhost:8080/api/v0.1/gateway/house") + .then() + .statusCode(400) + .assertThat() + .body("error", equalTo("Bad Request")); + } + + @Test + public void create_house_returns_400_with_empty_address() { + Map house = new HashMap<>(); + house.put("name", "house2"); + house.put("address", ""); + + with().body(house) + .when().post("http://localhost:8080/api/v0.1/gateway/house") + .then() + .statusCode(400) + .assertThat() + .body("error", equalTo("Bad Request")); + } + + @Test + public void create_house_returns_400_with_wrong_address() { + Map house = new HashMap<>(); + house.put("name", "house2"); + house.put("address", "aaa"); + + with().body(house) + .when().post("http://localhost:8080/api/v0.1/gateway/house") + .then() + .statusCode(400) + .assertThat() + .body("error", equalTo("Bad Request")); + } + + @Test + public void update_house_returns_200_with_correct_name_and_address() { + Map house = new HashMap<>(); + house.put("name", "house2"); + house.put("address", "http://smart-home:8081"); + + with().body(house) + .when().put("http://localhost:8080/api/v0.1/gateway/house1") + .then() + .statusCode(200) + .assertThat() + .body("name", equalTo("house2")) + .body("address", equalTo("http://smart-home:8081")); + + house.put("name", "house1"); + house.put("address", "http://smart-home:8081"); + + with().body(house) + .when().put("http://localhost:8080/api/v0.1/gateway/house2") + .then() + .statusCode(200) + .assertThat() + .body("name", equalTo("house1")) + .body("address", equalTo("http://smart-home:8081")); + } + + +} diff --git a/src/test/java/RGBTest.java b/src/test/java/RGBTest.java new file mode 100644 index 0000000..ca342c1 --- /dev/null +++ b/src/test/java/RGBTest.java @@ -0,0 +1,360 @@ +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static io.restassured.RestAssured.get; +import static io.restassured.RestAssured.with; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; + +public class RGBTest { + + @Test + public void rgb_get_returns_200_with_correct_label_and_type() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("rgb1")) + .body("enabled", equalTo(false)) + .body("red", instanceOf(Integer.class)) + .body("green", instanceOf(Integer.class)) + .body("blue", instanceOf(Integer.class)); + } + + @Test + public void rgb_get_returns_404_with_incorrect_label() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb2") + .then() + .log().all() + .log().body() + .statusCode(404) + .assertThat() + .body("status", equalTo("NOT_FOUND")); + } + + @Test + public void rgb_get_returns_404_with_incorrect_devType() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/RGBrrr/rgb1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void rgb_get_returns_404_with_empty_label() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void update_rgb_returns_200_with_correct_label_and_devType() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "red", 5); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(200) + .assertThat() + .body("label", equalTo("rgb1")) + .body("enabled", equalTo(true)); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(200) + .assertThat() + .body("label", equalTo("rgb1")) + .body("enabled", equalTo(true)) + .body("red", equalTo(5)); + + rgb = Map.of("label", "rgb1","deviceType","RGBLight", "enabled", false, "blue", 99); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(200) + .assertThat() + .body("label", equalTo("rgb1")) + .body("enabled", equalTo(false)) + .body("blue", equalTo(99)); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(200) + .assertThat() + .body("label", equalTo("rgb1")) + .body("enabled", equalTo(false)) + .body("blue", equalTo(99)); + } + + @Test + public void update_rgb_returns_404_with_incorrect_label() { + Map rgb = Map.of("label", "rgb2", "deviceType", "RGBLight", "enabled", true ); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb2") + .then() + .log().all() + .log().body() + .statusCode(404) + .assertThat() + .body("status", equalTo("NOT_FOUND")); + } + + @Test + public void update_rgb_returns_400_with_incorrect_body() { + Map rgb = Map.of("deviceType", "RGBLight", "enabled", true); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_404_with_incorrect_devType() { + Map rgb = Map.of("label", "rgb1", "deviceType", "rgb", "enabled", true ); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/RGBrrr/rgb1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void update_rgb_returns_404_with_empty_label() { + Map rgb = Map.of("label", "", "deviceType", "RGBLight", "enabled", true ); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void update_rgb_returns_400_with_red_out_of_range() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "red", 256); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + + rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "red", -1); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_wrong_red_type() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "red", "red"); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_green_out_of_range() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "green", 256); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + + rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "green", -1); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_wrong_green_type() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "green", "green"); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_blue_out_of_range() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "blue", 256); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + + rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "blue", -1); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_wrong_blue_type() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "blue", "blue"); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_enabled_not_boolean() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", "true", "red", 5); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_switchedOn_not_boolean() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "red", 5, "switchedOn", "true"); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_devType_not_string() { + Map rgb = Map.of("label", "rgb1", "deviceType", 1, "enabled", true, "red", 5); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_label_not_string() { + Map rgb = Map.of("label", 1, "deviceType", "RGBLight", "enabled", true, "red", 5); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_red_not_number() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "red", "red"); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_green_not_number() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "green", "green"); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void update_rgb_returns_400_with_blue_not_number() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true, "blue", "blue"); + + with().body(rgb) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + + @Test + public void rgb_post_returns_404() { + Map rgb = Map.of("label", "rgb1", "deviceType", "RGBLight", "enabled", true); + + with().body(rgb) + .when().post("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void rgb_delete_returns_404() { + with() + .when().delete("http://localhost:8080/api/v0.1/gateway/house1/device/rgb/rgb1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + +} diff --git a/src/test/java/ThermometerTest.java b/src/test/java/ThermometerTest.java new file mode 100644 index 0000000..92da12c --- /dev/null +++ b/src/test/java/ThermometerTest.java @@ -0,0 +1,284 @@ +import groovy.xml.StreamingDOMBuilder; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static io.restassured.RestAssured.get; +import static io.restassured.RestAssured.with; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; + +public class ThermometerTest { + + @Test + public void thermometer_get_returns_200_with_correct_label_and_type() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("thermometer1")) + .body("unit", equalTo("C")) + .body("enabled", equalTo(true)) + .body("temperature", instanceOf(Float.class)); + } + + @Test + public void thermometer_get_returns_404_with_incorrect_label() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer2") + .then() + .log().all() + .log().body() + .statusCode(404) + .assertThat() + .body("status", equalTo("NOT_FOUND")); + } + + @Test + public void thermometer_get_returns_404_with_incorrect_devType() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/Thermometerrrr/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + + @Test + public void thermometer_get_returns_404_with_empty_label() { + get("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void update_thermometer_returns_200_with_correct_label_and_devType() { + Map thermometer = Map.of("label","thermometer1","deviceType","Thermometer" ,"unit", "F", "enabled",true); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("thermometer1")) + .body("unit", equalTo("F")) + .body("enabled", equalTo(true)) + .body("temperature", instanceOf(Float.class)); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("thermometer1")) + .body("unit", equalTo("F")) + .body("enabled", equalTo(true)) + .body("temperature", instanceOf(Float.class)); + + thermometer= Map.of("label", "thermometer1", "deviceType", "Thermometer","unit", "C", "enabled", false); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("thermometer1")) + .body("unit", equalTo("C")) + .body("enabled", equalTo(false)) + .body("temperature", instanceOf(Float.class)); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("thermometer1")) + .body("unit", equalTo("C")) + .body("enabled", equalTo(true)) + .body("temperature", instanceOf(Float.class)); + } + + @Test + public void update_thermometer_returns_200_with_minimal_body() { + Map thermometer = Map.of("label","thermometer1","deviceType","Thermometer"); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("thermometer1")) + .body("unit", equalTo("C")) + .body("enabled", equalTo(true)) + .body("temperature", instanceOf(Float.class)); + + get("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(200).assertThat() + .body("label", equalTo("thermometer1")) + .body("unit", equalTo("C")) + .body("enabled", equalTo(true)) + .body("temperature", instanceOf(Float.class)); + } + + + @Test + public void thermometer_update_returns_404_with_incorrect_label() { + Map thermometer = Map.of("label","thermometer2","deviceType","Thermometer","unit", "F", "enabled",false); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer2") + .then() + .log().all() + .log().body() + .statusCode(404) + .assertThat() + .body("status", equalTo("NOT_FOUND")); + } + + @Test + public void thermometer_update_returns_400_with_incorrect_body() { + Map thermometer = Map.of("deviceType","Thermometer" ,"unit", "F", "enabled",false); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(400) + .assertThat(); + } + + @Test + public void thermometer_updates_returns_400_with_wrong_unit() { + Map thermometer = Map.of("label","thermometer1","deviceType","Thermometer" ,"unit", 1, "enabled",false); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(400) + .assertThat(); + } + + @Test + public void thermometer_update_returns_404_with_incorrect_devType() { + Map thermometer = Map.of("label","thermometer1","deviceType","Thermometer" ,"unit", "F", "enabled",false); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometerr/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void thermometer_update_returns_404_with_empty_label() { + Map thermometer = Map.of("unit", "F", "enabled",false); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + + @Test + public void thermometer_update_returns_400_with_wrong_unit_type() { + Map thermometer = Map.of("label","thermometer1","deviceType","Thermometer" ,"unit", 1, "enabled",false); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void thermometer_update_returns_400_with_wrong_enabled_type() { + Map thermometer = Map.of("label","thermometer1","deviceType","Thermometer" ,"unit", "F", "enabled","true"); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void thermometer_update_returns_400_with_wrong_temperature_type() { + Map thermometer = Map.of("label","thermometer1","deviceType","Thermometer" ,"unit", "F", "enabled",true, "temperature", "25.0"); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + + + @Test + public void thermometer_update_returns_400_with_wrong_label_type() { + Map thermometer = Map.of("label",1,"deviceType","Thermometer" ,"unit", "F", "enabled",true); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void thermometer_update_returns_400_with_wrong_devType_type() { + Map thermometer = Map.of("label","thermometer1","deviceType",1 ,"unit", "F", "enabled",true); + + with().body(thermometer) + .when().put("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(400); + } + + @Test + public void thermometer_post_returns_404() { + Map thermometer = Map.of("label","thermometer1","deviceType","Tshermometer" ,"unit", "F", "enabled",true); + + with().body(thermometer) + .when().post("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + @Test + public void thermometer_delete_returns_404() { + with() + .when().delete("http://localhost:8080/api/v0.1/gateway/house1/device/thermometer/thermometer1") + .then() + .log().all() + .log().body() + .statusCode(404); + } + + + + +}