Skip to content

Commit

Permalink
Add Tests
Browse files Browse the repository at this point in the history
Functional testst of Devices and House
  • Loading branch information
mastastny committed Apr 3, 2023
1 parent 951f4aa commit d0cf9ca
Show file tree
Hide file tree
Showing 13 changed files with 1,001 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured-common</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.patriotframework.virtualsmarthomeplus;

import io.patriotframework.virtualsmarthomeplus.house.devices.Device;
import io.patriotframework.virtualsmarthomeplus.house.devices.finalDevices.Fireplace;
import org.apache.commons.lang3.NotImplementedException;

public class MockDevice extends Device {

public MockDevice(String label) {
super(label);
}

public Device createWithSameState(String newLabel) {
return new MockDevice("newLabel");
}

public boolean hasSameAttributes(Device device) throws IllegalArgumentException {
throw new NotImplementedException("Not implemented yet.");
}
public Fireplace createWithSameAttributes(String newLabel) {
throw new NotImplementedException("Not implemented yet.");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
package io.patriotframework.virtualsmarthomeplus.controllers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.patriotframework.virtualsmarthomeplus.MockDevice;
import io.patriotframework.virtualsmarthomeplus.house.House;
import io.patriotframework.virtualsmarthomeplus.house.devices.Device;
import io.patriotframework.virtualsmarthomeplus.house.devices.finalDevices.Fireplace;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.*;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
public abstract class DeviceControllerFunctionalTestBase {
protected Device minPostDevice;
protected Device fullDefaultDevice;
protected Device fullUpdatedDevice;
protected String pathToDevices;
protected String label;
protected Class<?extends Device[]> aClass = Fireplace[].class;

protected ObjectNode minPostDeviceJson;
protected ObjectNode fullDefaultDeviceJson;
protected ObjectNode fullUpdatedDeviceJson;
ObjectMapper jsonMapper = new com.fasterxml.jackson.databind.ObjectMapper();

@Autowired House house;

public DeviceControllerFunctionalTestBase() throws JSONException, JsonProcessingException {
pathToDevices = getPathToDevices();

fullDefaultDevice = getDefaultDevice();
fullUpdatedDevice = getFullUpdatedDevice();

minPostDeviceJson = (ObjectNode) jsonMapper.readTree(getMinPostDeviceJson());
fullDefaultDeviceJson = jsonMapper.valueToTree(fullDefaultDevice);
fullUpdatedDeviceJson = jsonMapper.valueToTree(fullUpdatedDevice);

label = fullDefaultDevice.getLabel();
}

/**
* Returns path to tested endpoint e.g. "/api/v0.1/house/device"
*/
protected abstract String getPathToDevices();

/**
* Returns JSON string which represents device with only mandatory attributes
*
* @return JSON string
* @throws JSONException if JSONException during creating JSON occurs
*/
protected abstract String getMinPostDeviceJson() throws JSONException;

/**
* Returns device with default setting and with the same identity as device from {@link #getFullUpdatedDevice()}
*
* @return device of the class which is under test with default setting
*/
protected abstract Device getDefaultDevice();

/**
* Returns device with the same identity as device from {@link #getDefaultDevice()}
*
* @return device of the class which is under test with some updated attribute
*/
protected abstract Device getFullUpdatedDevice();

@Test
public void getReturnsObject() {
house.addDevice(fullDefaultDevice);

when().get(pathToDevices + "/" + label)
.then().statusCode(200).body(equalTo(fullDefaultDeviceJson));

assertTrue(fullDefaultDevice.hasSameAttributes(house.getDevice(label)));
}

@Test
public void getNonExistentDeviceReturns404() {
when().get(pathToDevices + "/nonExistent")
.then().statusCode(404);
}


@Test
public void validPost() {
given().body(fullDefaultDevice.toString())
.when().post(pathToDevices)
.then().statusCode(200).body(equalTo(fullDefaultDevice));

assertTrue(fullDefaultDevice.hasSameAttributes(house.getDevice(label)));
}

@Test
public void postAlreadyPresentDeviceReturns409() {
house.addDevice(fullDefaultDevice);

given().body(fullUpdatedDevice.toString())
.when().post(pathToDevices)
.then().statusCode(409);

assertTrue(fullDefaultDevice.hasSameAttributes(house.getDevice(label)));
}

@Test
public void postWithMissingMandatoryAttributeReturns400() {
fullDefaultDeviceJson.remove("label");
given().body(fullDefaultDevice.toString())
.when().post(pathToDevices)
.then().statusCode(400);

assertThrows(NoSuchElementException.class, () -> {
house.getDevice(label);
});
}

@Test
public void postWithoutOptionalAttributesReturnsDefaultDevice() {
given().body(minPostDevice.toString())
.when().post(pathToDevices)
.then().statusCode(200).body(equalTo(fullDefaultDevice));

assertTrue(fullDefaultDevice.hasSameAttributes(house.getDevice(label)));
}

@Test
public void postWithSuperfluousAttributeReturns400() {
fullDefaultDeviceJson.put("superfluous", true);
given().body(fullDefaultDevice.toString())
.when().post(pathToDevices)
.then().statusCode(400);

assertThrows(NoSuchElementException.class, () -> {
house.getDevice(label);
});
}

@Test
public void validDelete() {
house.addDevice(fullDefaultDevice);

given()
.when().delete(pathToDevices + "/" + label)
.then().statusCode(200).body(equalTo(fullDefaultDevice.toString()));

assertThrows(NoSuchElementException.class, () -> {
house.getDevice(label);
});
}

@Test
public void deleteDeviceWhichIsNotPresentReturns404() {
given()
.when().delete(pathToDevices + "/" + label)
.then().statusCode(404);
}

@Test
public void updateDefaultDevice() {
fullUpdatedDeviceJson.get("label");
house.addDevice(fullDefaultDevice);

given().body(fullUpdatedDevice.toString())
.when().put(pathToDevices + "/" + label)
.then().statusCode(200).body(equalTo(fullUpdatedDevice));

assertTrue(fullUpdatedDevice.hasSameAttributes(house.getDevice(label)));
}

@Test
public void putNonExistentDeviceReturns404() {

given().body(fullDefaultDevice.toString())
.when().put(pathToDevices + "/nonExistent")
.then().statusCode(404);
}

@Test
public void putWithMissingMandatoryAttributeReturns400() {
fullDefaultDeviceJson.remove("label");
house.addDevice(fullDefaultDevice);

given().body(fullDefaultDevice.toString())
.when().put(pathToDevices + "/" + label)
.then().statusCode(400);

assertTrue(fullDefaultDevice.hasSameAttributes(house.getDevice(label)));
}

@Test
public void putWithSuperfluousAttributeReturns400() {
house.addDevice(fullDefaultDevice);
fullDefaultDeviceJson.put("Superfluous", true);

given().body(fullDefaultDevice.toString())
.when().put(pathToDevices + "/" + label)
.then().statusCode(400);

assertTrue(fullDefaultDevice.hasSameAttributes(house.getDevice(label)));
}

@Test
public void putAttributeWithWrongValueReturns400() {
house.addDevice(fullDefaultDevice);
fullDefaultDeviceJson.put("label", "newLabel");

given().body(fullDefaultDevice.toString())
.when().put(pathToDevices + "/" + label)
.then().statusCode(400);

assertTrue(fullDefaultDevice.hasSameAttributes(house.getDevice(label)));
}

@Test
public void getAllDevicesOfGivenType() {
Device secondFullDefaultDevice = fullDefaultDevice.createWithSameAttributes("label2");
house.addDevice(fullDefaultDevice);
house.addDevice(secondFullDefaultDevice);
house.addDevice(new MockDevice("label3"));
TreeSet<Device> tsGiven = new TreeSet<>();
TreeSet<Device> tsExpected = new TreeSet<>();
tsExpected.add(fullDefaultDevice);
tsExpected.add(secondFullDefaultDevice);

Device[] responseBody = given()
.when().get(pathToDevices)
.then().statusCode(200).extract().as(aClass);

Collections.addAll(tsGiven, responseBody);
assertEquals(tsExpected, tsGiven);
}

@Test
public void getEmptyListOfDevicesOfGivenType() throws JsonProcessingException {
given()
.when().get(pathToDevices)
.then().statusCode(200).body(equalTo(jsonMapper.writeValueAsString(jsonMapper.createArrayNode())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.patriotframework.virtualsmarthomeplus.controllers;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import static io.restassured.RestAssured.*;
import static io.restassured.http.ContentType.JSON;

@SpringBootTest
public abstract class DeviceControllerGeneralTestBase {

protected String pathToDevices;

public DeviceControllerGeneralTestBase() {
pathToDevices = getPathToDevices();
}

/**
* Returns path to tested endpoint e.g. "/api/v0.1/house/device"
*/
protected abstract String getPathToDevices();

@Test void noContentType() {
given().noContentType()
.get(pathToDevices).then()
.contentType(JSON);
}

@Test
public void acceptHeaderJson() {
given().accept("application/json")
.get().then()
.statusCode(200)
.contentType(JSON);
}

@Test
public void basicGetHouseWithQueryParams() {
given().queryParam("param", "param")
.get(pathToDevices).then()
.statusCode(200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.patriotframework.virtualsmarthomeplus.controllers.door;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.patriotframework.virtualsmarthomeplus.controllers.fireplace.FireplaceControllerFunctionalTest;
import org.json.JSONException;
import org.json.JSONObject;

public class DoorControllerFunctionalTest {
FireplaceControllerFunctionalTest a = new FireplaceControllerFunctionalTest();
String doorURN = "/api/v0.1/house/device/door";

JSONObject minPostFireplace = new JSONObject()
.put("label", "label1");

JSONObject fullDefaultFireplace = new JSONObject()
.put("label", "label1")
.put("enabled", false);

JSONObject fullUpdatedFireplace = new JSONObject()
.put("label", "label1")
.put("enabled", true);

public DoorControllerFunctionalTest() throws JSONException, JsonProcessingException {
}
}
Loading

0 comments on commit d0cf9ca

Please sign in to comment.