sqt/assignment-w13/comments-api/src/test/java/comments/CommentsTest.java
2023-05-23 14:07:42 +02:00

172 lines
5.0 KiB
Java

package comments;
import static org.junit.Assert.*;
import comments.main.Application;
import io.restassured.response.ValidatableResponse;
import org.junit.*;
import com.atlassian.oai.validator.restassured.OpenApiValidationFilter;
import static org.hamcrest.Matchers.*;
import io.restassured.RestAssured;
import io.restassured.http.Header;
import io.restassured.response.Response;
import net.minidev.json.JSONObject;
public class CommentsTest {
private String BASE_URI_COMMENTS = "http://localhost:8080/api/comments";
private Integer responseStatusCode;
private String responseBody;
@BeforeClass
public static void setup() {
Application.main(new String[] {});
}
@Before
public void setupTest() {
responseStatusCode = null;
responseBody = null;
}
@After
public void tearDown() {
// TODO: Add general oracle
}
@Test
public void getAllComments() {
ValidatableResponse response = RestAssured.given().log().all()
.when()
.get(BASE_URI_COMMENTS)
.then().log().all();
responseStatusCode = response.extract().statusCode();
responseBody = response.extract().body().asPrettyString();
// TODO: Add assertions
}
@Test
public void getOneComment() {
ValidatableResponse response = RestAssured.given().log().all()
.when()
.get(BASE_URI_COMMENTS+"/c1")
.then().log().all();
responseStatusCode = response.extract().statusCode();
responseBody = response.extract().body().asPrettyString();
// TODO: Add assertions
}
@Test
public void getOneNonExistingComment() {
ValidatableResponse response = RestAssured.given().log().all()
.when()
.get(BASE_URI_COMMENTS+"/fsfasdf")
.then().log().all();
responseStatusCode = response.extract().statusCode();
responseBody = response.extract().body().asPrettyString();
// TODO: Add assertions
}
@Test
public void putComment() {
JSONObject requestParam = new JSONObject();
requestParam.put("id", "c1");
requestParam.put("userName", "Prueba");
requestParam.put("text", "Hello my name is John Doe");
requestParam.put("date", "2015-01-16T20:44:53.950");
requestParam.put("type", "Complain");
Header acceptJson = new Header("content-type", "application/json");
ValidatableResponse response = RestAssured.given().log().all()
.header(acceptJson)
.body(requestParam.toJSONString())
.when()
.put(BASE_URI_COMMENTS)
.then().log().all();
responseStatusCode = response.extract().statusCode();
responseBody = response.extract().body().asPrettyString();
// TODO: Add assertions
}
@Test
public void postComment() {
JSONObject requestParam = new JSONObject();
requestParam.put("id", "c1");
requestParam.put("userName", "Prueba");
requestParam.put("text", "Hello my name is John Doe");
requestParam.put("date", "2015-01-16T20:44:53.950");
Header acceptJson = new Header("content-type", "application/json");
ValidatableResponse response = RestAssured.given().log().all()
.header(acceptJson)
.body(requestParam.toJSONString())
.when()
.post(BASE_URI_COMMENTS)
.then().log().all();
responseStatusCode = response.extract().statusCode();
responseBody = response.extract().body().asPrettyString();
// TODO: Add assertions
}
@Test
public void deleteOneComment() {
ValidatableResponse response = RestAssured.given().log().all()
.when()
.delete(BASE_URI_COMMENTS+"/c2")
.then().log().all();
responseStatusCode = response.extract().statusCode();
responseBody = response.extract().body().asPrettyString();
// TODO: Add assertions
}
@Test
public void postAndGetComment() {
JSONObject requestParam = new JSONObject();
requestParam.put("id", "c1");
requestParam.put("userName", "Prueba");
requestParam.put("text", "Hello my name is John Doe");
requestParam.put("date", "2015-01-16T20:44:53.950");
Header acceptJson = new Header("content-type", "application/json");
Response previousResponse = RestAssured.given().log().all()
.header(acceptJson)
.body(requestParam.toJSONString())
.when()
.post(BASE_URI_COMMENTS);
previousResponse.then().log().all();
ValidatableResponse response = RestAssured.given().log().all()
.when()
.get(BASE_URI_COMMENTS + "/" + previousResponse.getBody().path("id"))
.then().log().all();
responseStatusCode = response.extract().statusCode();
responseBody = response.extract().body().asPrettyString();
// TODO: Add assertions
}
}