Skip to content

Commit

Permalink
add RestAssuredTests (build will fail)
Browse files Browse the repository at this point in the history
  • Loading branch information
shelajev committed Oct 29, 2024
1 parent 495dcfd commit 73a26bd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
Expand Down
71 changes: 70 additions & 1 deletion src/test/java/com/atomicjar/todos/ApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,89 @@
package com.atomicjar.todos;

import com.atomicjar.todos.entity.Todo;
import com.atomicjar.todos.repository.TodoRepository;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.util.List;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.hasSize;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = {ContainersConfig.class})
class ApplicationTests {

@LocalServerPort
protected Integer localServerPort;
protected RequestSpecification requestSpecification;


@Autowired
private TodoRepository todoRepository;


@BeforeEach
void setUp() {
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
requestSpecification = new RequestSpecBuilder()
.setPort(localServerPort)
.addHeader(
HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE
)
.build();
}

@Test
void contextLoads() {
// Assertions.assertTrue(false);
List<Todo> customers = List.of(
new Todo(null, "Feed all horses", false, 1),
new Todo(null, "Free all horses", false, 2),
new Todo(null, "Feel all horses", false, 3)
);
todoRepository.saveAll(customers);

given(requestSpecification)
.when()
.get("/todos")
.then()
.statusCode(200)
.body(".", hasSize(4));

todoRepository.deleteAll();
}


@Test
void getByIDFindsId() {
Todo feedAllHorses = todoRepository.save(new Todo(null, "Feed all horses", false, 1));

given(requestSpecification)
.when()
.get("/todos/" + feedAllHorses.getId())
.then()
.statusCode(200)
.body(".", hasSize(4));

todoRepository.deleteAll();
}



}
8 changes: 1 addition & 7 deletions src/test/java/com/atomicjar/todos/TestApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ public DataLoader(TodoRepository todoRepository, JdbcTemplate jdbcTemplate) {

@PostConstruct
public void addTodo() {
String serverVersion = DockerClientFactory.instance().getInfo().getServerVersion();
String title = "Set up and run with Testcontainers desktop app and Testcontainers Cloud!";

if (serverVersion.contains("testcontainerscloud")) {
String string = jdbcTemplate.queryForObject("SELECT encode(sha256(?::bytea), 'hex')", String.class, serverVersion);
title = "I need your root, your RAM, and your CPU cycles";
}
String title = "I need your root, your RAM, and your CPU cycles";

Todo t = new Todo();
t.setTitle(title);
Expand Down

0 comments on commit 73a26bd

Please sign in to comment.