diff --git a/statistics/pom.xml b/statistics/pom.xml index 8f7dddb1ece679d3fb6be0168a250a3f4f1a2576..22106e989ce3a8cda9dc63ec3e1cefead28ce569 100644 --- a/statistics/pom.xml +++ b/statistics/pom.xml @@ -36,6 +36,11 @@ <version>1.5.3.Final</version> <scope>compile</scope> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> </dependencies> <modelVersion>4.0.0</modelVersion> diff --git a/statistics/src/test/java/cz/muni/fi/pa165/statistics/UnitTestUserStatisticsJPA.java b/statistics/src/test/java/cz/muni/fi/pa165/statistics/UnitTestUserStatisticsJPA.java new file mode 100644 index 0000000000000000000000000000000000000000..461c0fd0ff991b6e9b1f092d70f01d70b32705d7 --- /dev/null +++ b/statistics/src/test/java/cz/muni/fi/pa165/statistics/UnitTestUserStatisticsJPA.java @@ -0,0 +1,102 @@ +package cz.muni.fi.pa165.statistics; + +import cz.muni.fi.pa165.statistics.statistics.StatisticRepository; +import cz.muni.fi.pa165.statistics.statistics.UserStatistic; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.regex.Pattern; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@DataJpaTest +public class UnitTestUserStatisticsJPA { + @Autowired + private TestEntityManager entityManager; + @Autowired + private StatisticRepository statisticRepository; + + @Test + public void createTesting() throws Exception { + String userStatistics = this.entityManager.persistAndGetId(new UserStatistic()).toString(); + // Regular expression to match UUID format + Pattern uuidPattern = Pattern.compile( + "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", + Pattern.CASE_INSENSITIVE); + // Check if the string matches the UUID format + boolean isUUID = uuidPattern.matcher(userStatistics).matches(); + assertThat(isUUID).isTrue(); + } + + @Test + public void shouldFindNoneIfRepositoryIsEmpty() { + Iterable<UserStatistic> userStatistics = statisticRepository.findAll(); + assertThat(userStatistics).isEmpty(); + } + + @Test + public void shouldStore() { + assertThat(new UserStatistic()).hasFieldOrProperty("id"); + } + + @Test + public void shouldFindAll() { + UserStatistic m = new UserStatistic(); + entityManager.persist(m); + + UserStatistic m1 = new UserStatistic(); + entityManager.persist(m1); + + UserStatistic m2 = new UserStatistic(); + entityManager.persist(m2); + + assertThat(statisticRepository.findAll()).hasSize(3).contains(m, m1, m2); + } + + @Test + public void shouldUpdateById() { + UserStatistic m = new UserStatistic(); + entityManager.persist(m); + + UserStatistic m1 = new UserStatistic(); + entityManager.persist(m1); + + UserStatistic update = statisticRepository.findById(m.getId()).get(); + update.setName("New name"); + statisticRepository.save(update); + + UserStatistic check = statisticRepository.findById(m.getId()).get(); + + assertThat(check.getId()).isEqualTo(m.getId()); + assertThat(check.getName()).isEqualTo(m.getName()); + } + + @Test + public void shouldDeleteById() { + UserStatistic m = new UserStatistic(); + entityManager.persist(m); + + UserStatistic m1 = new UserStatistic(); + entityManager.persist(m1); + + statisticRepository.deleteById(m.getId()); + + assertThat(statisticRepository.findAll()).hasSize(1).contains(m1); + assertThat(statisticRepository.findAll()).hasSize(1).doesNotContain(m); + } + + @Test + public void shouldDeleteAll() { + entityManager.persist(new UserStatistic()); + entityManager.persist(new UserStatistic()); + + statisticRepository.deleteAll(); + + assertThat(statisticRepository.findAll()).isEmpty(); + } +}