-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Functional testst of Devices and House
- Loading branch information
Showing
13 changed files
with
1,001 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/test/java/io/patriotframework/virtualsmarthomeplus/MockDevice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
|
248 changes: 248 additions & 0 deletions
248
...patriotframework/virtualsmarthomeplus/controllers/DeviceControllerFunctionalTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...io/patriotframework/virtualsmarthomeplus/controllers/DeviceControllerGeneralTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../patriotframework/virtualsmarthomeplus/controllers/door/DoorControllerFunctionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} |
Oops, something went wrong.