From 94b814c73c8d6bd97db5fcb97c64890e48c2eb37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anesa=20Fazlagi=C4=87?= <xfazlag@fi.muni.cz> Date: Sun, 26 Mar 2023 22:13:18 +0200 Subject: [PATCH] added tests for statistics microservice --- statistics/pom.xml | 5 + .../statistics/UnitTestUserStatisticsJPA.java | 102 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 statistics/src/test/java/cz/muni/fi/pa165/statistics/UnitTestUserStatisticsJPA.java diff --git a/statistics/pom.xml b/statistics/pom.xml index 8f7dddb..22106e9 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 0000000..461c0fd --- /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(); + } +} -- GitLab