Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic tests implemented #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <repository_url>
```
2. Clone the virtual-smart-home-gateway-plus repository from GitHub:
```
git clone <repository_url>
```
3. Navigate to the project directory of both repositories:
```
cd <project_directory>
```
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 <image_name> .
```
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
```
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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"
28 changes: 28 additions & 0 deletions init.py
Original file line number Diff line number Diff line change
@@ -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")
52 changes: 52 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>Virtaul-Smart-Home-Tests</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

</project>
7 changes: 7 additions & 0 deletions src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
78 changes: 78 additions & 0 deletions src/test/java/DeviceTest.java
Original file line number Diff line number Diff line change
@@ -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));
}

}
Loading