US01, TS13: Add tests to the horse backend
This commit is contained in:
parent
707487c236
commit
dda711f33e
@ -77,6 +77,9 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>${maven.surefire-plugin.version}</version>
|
<version>${maven.surefire-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<trimStackTrace>false</trimStackTrace>
|
||||||
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
@ -11,7 +11,7 @@ public class HorseDto extends BaseDto {
|
|||||||
private String description;
|
private String description;
|
||||||
private short score;
|
private short score;
|
||||||
private Date birthday;
|
private Date birthday;
|
||||||
private long owner;
|
private Long owner;
|
||||||
|
|
||||||
public HorseDto() {}
|
public HorseDto() {}
|
||||||
|
|
||||||
@ -91,10 +91,10 @@ public class HorseDto extends BaseDto {
|
|||||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
|
||||||
return super.fieldsString() + ", name='" + name + '\'' +
|
return super.fieldsString() + ", name='" + name + '\'' +
|
||||||
"description='" + description + '\'' +
|
", description='" + description + '\'' +
|
||||||
"score='" + score + '\'' +
|
", score='" + score + '\'' +
|
||||||
"birthday='" + df.format(birthday) + '\'' +
|
", birthday='" + df.format(birthday) + '\'' +
|
||||||
"owner_id='" + owner + '\'';
|
", owner_id='" + owner + '\'';
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -101,9 +101,9 @@ public class Horse extends BaseEntity {
|
|||||||
|
|
||||||
return super.fieldsString() + ", name='" + name + '\'' +
|
return super.fieldsString() + ", name='" + name + '\'' +
|
||||||
"description='" + description + '\'' +
|
"description='" + description + '\'' +
|
||||||
"score='" + score + '\'' +
|
", score='" + score + '\'' +
|
||||||
"birthday='" + df.format(birthday) + '\'' +
|
", birthday='" + df.format(birthday) + '\'' +
|
||||||
"owner='" + owner + '\'';
|
", owner='" + owner + '\'';
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -66,7 +66,7 @@ public class HorseJdbcDao implements HorseDao {
|
|||||||
ps.setInt(3, horse.getScore());
|
ps.setInt(3, horse.getScore());
|
||||||
ps.setDate(4, horse.getBirthday());
|
ps.setDate(4, horse.getBirthday());
|
||||||
|
|
||||||
if(horse.getOwner() == 0)
|
if(horse.getOwner() == null || horse.getOwner() == 0)
|
||||||
ps.setNull(5, Types.NULL);
|
ps.setNull(5, Types.NULL);
|
||||||
else
|
else
|
||||||
ps.setObject(5, horse.getOwner());
|
ps.setObject(5, horse.getOwner());
|
||||||
@ -98,7 +98,6 @@ public class HorseJdbcDao implements HorseDao {
|
|||||||
horse.setScore(resultSet.getShort("score"));
|
horse.setScore(resultSet.getShort("score"));
|
||||||
horse.setBirthday(resultSet.getDate("birthday"));
|
horse.setBirthday(resultSet.getDate("birthday"));
|
||||||
horse.setOwner(resultSet.getLong("owner_id"));
|
horse.setOwner(resultSet.getLong("owner_id"));
|
||||||
|
|
||||||
return horse;
|
return horse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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<HorseDto> request = new HttpEntity<>(newHorse);
|
||||||
|
ResponseEntity<HorseDto> 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());
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user