From dda711f33e8aae852ba7e270b44903a4b0e858c5 Mon Sep 17 00:00:00 2001 From: Ivaylo Ivanov Date: Tue, 17 Mar 2020 19:37:11 +0100 Subject: [PATCH] US01, TS13: Add tests to the horse backend --- backend/pom.xml | 3 ++ .../individual/endpoint/dto/HorseDto.java | 10 ++-- .../assignment/individual/entity/Horse.java | 6 +-- .../persistence/impl/HorseJdbcDao.java | 3 +- .../integration/HorseEndpointTest.java | 47 +++++++++++++++++++ .../unit/persistence/HorseDaoJdbcTest.java | 40 ++++++++++++++++ .../unit/persistence/HorseDaoTestBase.java | 36 ++++++++++++++ .../unit/service/HorseServiceTest.java | 43 +++++++++++++++++ 8 files changed, 178 insertions(+), 10 deletions(-) create mode 100644 backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/integration/HorseEndpointTest.java create mode 100644 backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoJdbcTest.java create mode 100644 backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoTestBase.java create mode 100644 backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/service/HorseServiceTest.java diff --git a/backend/pom.xml b/backend/pom.xml index 6386132..b4e4733 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -77,6 +77,9 @@ maven-surefire-plugin ${maven.surefire-plugin.version} + + false + diff --git a/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/endpoint/dto/HorseDto.java b/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/endpoint/dto/HorseDto.java index 3e2f5de..f5be27f 100644 --- a/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/endpoint/dto/HorseDto.java +++ b/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/endpoint/dto/HorseDto.java @@ -11,7 +11,7 @@ public class HorseDto extends BaseDto { private String description; private short score; private Date birthday; - private long owner; + private Long owner; public HorseDto() {} @@ -91,10 +91,10 @@ public class HorseDto extends BaseDto { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); return super.fieldsString() + ", name='" + name + '\'' + - "description='" + description + '\'' + - "score='" + score + '\'' + - "birthday='" + df.format(birthday) + '\'' + - "owner_id='" + owner + '\''; + ", description='" + description + '\'' + + ", score='" + score + '\'' + + ", birthday='" + df.format(birthday) + '\'' + + ", owner_id='" + owner + '\''; } @Override diff --git a/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/entity/Horse.java b/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/entity/Horse.java index 08f38c9..ab0f85a 100644 --- a/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/entity/Horse.java +++ b/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/entity/Horse.java @@ -101,9 +101,9 @@ public class Horse extends BaseEntity { return super.fieldsString() + ", name='" + name + '\'' + "description='" + description + '\'' + - "score='" + score + '\'' + - "birthday='" + df.format(birthday) + '\'' + - "owner='" + owner + '\''; + ", score='" + score + '\'' + + ", birthday='" + df.format(birthday) + '\'' + + ", owner='" + owner + '\''; } @Override diff --git a/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/persistence/impl/HorseJdbcDao.java b/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/persistence/impl/HorseJdbcDao.java index 1d20162..a713182 100644 --- a/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/persistence/impl/HorseJdbcDao.java +++ b/backend/src/main/java/at/ac/tuwien/sepm/assignment/individual/persistence/impl/HorseJdbcDao.java @@ -66,7 +66,7 @@ public class HorseJdbcDao implements HorseDao { ps.setInt(3, horse.getScore()); ps.setDate(4, horse.getBirthday()); - if(horse.getOwner() == 0) + if(horse.getOwner() == null || horse.getOwner() == 0) ps.setNull(5, Types.NULL); else ps.setObject(5, horse.getOwner()); @@ -98,7 +98,6 @@ public class HorseJdbcDao implements HorseDao { horse.setScore(resultSet.getShort("score")); horse.setBirthday(resultSet.getDate("birthday")); horse.setOwner(resultSet.getLong("owner_id")); - return horse; } } diff --git a/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/integration/HorseEndpointTest.java b/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/integration/HorseEndpointTest.java new file mode 100644 index 0000000..89f683b --- /dev/null +++ b/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/integration/HorseEndpointTest.java @@ -0,0 +1,47 @@ +package at.ac.tuwien.sepm.assignment.individual.integration; + +import static org.junit.jupiter.api.Assertions.*; + +import at.ac.tuwien.sepm.assignment.individual.endpoint.dto.HorseDto; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.web.client.RestTemplate; + +import java.sql.Date; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("test") +public class HorseEndpointTest { + private static final RestTemplate REST_TEMPLATE = new RestTemplate(); + private static final String BASE_URL = "http://localhost:"; + private static final String HORSE_URL = "/horses"; + + @LocalServerPort + private int port; + + @Test + @DisplayName("Adding a new horse with the correct parameters will return HTTP 201 and the new HorseDto") + public void addingNewHorse_correctParameters_shouldReturnStatus201AndHorse() { + String birthday = "2020-01-01"; + HorseDto newHorse = new HorseDto("Zephyr", "Nice horse", (short) 4, Date.valueOf(birthday), null); + + HttpEntity request = new HttpEntity<>(newHorse); + ResponseEntity response = REST_TEMPLATE + .exchange(BASE_URL + port + HORSE_URL, HttpMethod.POST, request, HorseDto.class); + + // Compare everything except ids and timestamps + assertEquals(response.getStatusCode(), HttpStatus.CREATED); + assertEquals(newHorse.getName(), response.getBody().getName()); + assertEquals(newHorse.getDescription(), response.getBody().getDescription()); + assertEquals(newHorse.getScore(), response.getBody().getScore()); + assertEquals(newHorse.getBirthday().toString(), response.getBody().getBirthday().toString()); + assertEquals(newHorse.getOwner(), response.getBody().getOwner()); + } +} diff --git a/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoJdbcTest.java b/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoJdbcTest.java new file mode 100644 index 0000000..4dc8ced --- /dev/null +++ b/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoJdbcTest.java @@ -0,0 +1,40 @@ +package at.ac.tuwien.sepm.assignment.individual.unit.persistence; + +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.DefaultTransactionDefinition; + +@ExtendWith(SpringExtension.class) +@SpringBootTest +@ActiveProfiles("test") +public class HorseDaoJdbcTest extends HorseDaoTestBase { + + @Autowired + PlatformTransactionManager txm; + + TransactionStatus txstatus; + + @BeforeEach + public void setupDBTransaction() { + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); + txstatus = txm.getTransaction(def); + assumeTrue(txstatus.isNewTransaction()); + txstatus.setRollbackOnly(); + } + + @AfterEach + public void tearDownDBData() { + txm.rollback(txstatus); + } +} diff --git a/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoTestBase.java b/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoTestBase.java new file mode 100644 index 0000000..571edf4 --- /dev/null +++ b/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/persistence/HorseDaoTestBase.java @@ -0,0 +1,36 @@ +package at.ac.tuwien.sepm.assignment.individual.unit.persistence; + +import static org.junit.jupiter.api.Assertions.*; + +import at.ac.tuwien.sepm.assignment.individual.entity.Horse; +import at.ac.tuwien.sepm.assignment.individual.persistence.HorseDao; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; + +import java.sql.Date; + +public abstract class HorseDaoTestBase { + + @Autowired + HorseDao horseDao; + + @Test + @DisplayName("Adding a new horse with the correct parameters should return the horse") + public void addingNewHorse_correctParameters_shouldReturnHorse() { + String birthday = "2020-01-01"; + Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf(birthday), null); + Horse savedHorse = horseDao.addHorse(newHorse); + assertEquals(newHorse, savedHorse); + } + + @Test + @DisplayName("Adding a new horse with the incorrect parameters should throw DataAccessException") + public void addingNewHorse_incorrectParameters_shouldThrowDataAccess() { + String birthday = "2020-01-01"; + Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 80, Date.valueOf(birthday), null); + assertThrows(DataAccessException.class, () -> horseDao.addHorse(newHorse)); + } + +} diff --git a/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/service/HorseServiceTest.java b/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/service/HorseServiceTest.java new file mode 100644 index 0000000..0dcae82 --- /dev/null +++ b/backend/src/test/java/at/ac/tuwien/sepm/assignment/individual/unit/service/HorseServiceTest.java @@ -0,0 +1,43 @@ +package at.ac.tuwien.sepm.assignment.individual.unit.service; + +import static org.junit.jupiter.api.Assertions.*; + +import at.ac.tuwien.sepm.assignment.individual.entity.Horse; +import at.ac.tuwien.sepm.assignment.individual.service.HorseService; +import at.ac.tuwien.sepm.assignment.individual.util.ValidationException; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.sql.Date; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@ExtendWith(SpringExtension.class) +@SpringBootTest +@ActiveProfiles("test") +public class HorseServiceTest { + @Autowired + HorseService horseService; + + @Test + @DisplayName("Adding a new horse with the correct parameters will return the new horse") + public void addingNewHorse_correctParameters_shouldReturnHorse() { + String birthday = "2020-01-01"; + Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 4, Date.valueOf(birthday), null); + Horse savedHorse = horseService.addHorse(newHorse); + assertEquals(newHorse, savedHorse); + } + + @Test + @DisplayName("Adding a new horse with the incorrect parameters will throw a ValidationException") + public void addingNewHorse_incorrectParameters_shouldThrowValidation() { + String birthday = "2020-01-01"; + Horse newHorse = new Horse("Zephyr", "Nice horse", (short) 80, Date.valueOf(birthday), null); + assertThrows(ValidationException.class, () -> horseService.addHorse(newHorse)); + } +}