From ce51616385f76f05cf821324f17745defd990443 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pelayo=20Rojas=20=C3=8D=C3=B1igo?=
<31128562+Pelayori@users.noreply.github.com>
Date: Tue, 2 Apr 2024 00:02:10 +0200
Subject: [PATCH] Unit tests (#163)
* Test coverage fix
* Test coverage fix
* Test coverage fix
* Test coverage fix
* Test coverage fix
* Test coverage fix
* Test coverage fix
* Test coverage fix
* Test coverage fix
* Fix multiple cucumber scenarios. Persistent webdriver session
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Test run integration tests
* Add caching back
* Add caching back
* Add caching back
* Add caching back
* Add caching back
* Add caching back
* Add caching back
* Add caching back
* Add caching back
* Add caching back
* Add caching back
* Fix some tests
* Fix some tests
* Fix some tests
---
.github/workflows/unit-tests-push.yml | 2 +-
pom.xml | 54 +++++++++++++++----
src/main/java/com/uniovi/entities/Role.java | 2 +-
.../services/InsertSampleDataService.java | 17 +++---
src/test/java/com/uniovi/Wiq_UnitTests.java | 4 +-
5 files changed, 55 insertions(+), 24 deletions(-)
diff --git a/.github/workflows/unit-tests-push.yml b/.github/workflows/unit-tests-push.yml
index f64fe892..a05c42bd 100644
--- a/.github/workflows/unit-tests-push.yml
+++ b/.github/workflows/unit-tests-push.yml
@@ -58,7 +58,7 @@ jobs:
run: ss -tuln
- name: Run all tests with sonar analysis
run: |
- ./mvnw -B org.jacoco:jacoco-maven-plugin:prepare-agent clean test sonar:sonar -Dsonar.projectKey=Arquisoft_wiq_es04b -Dsonar.organization=arquisoft -Dsonar.branch.name=${{ github.ref }} -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=${{ secrets.SONAR_TOKEN }} -Dspring.profiles.active=test -Dspring.datasource.url=jdbc:mysql://localhost:3306/test_database -Dspring.datasource.username=root -Dspring.datasource.password=root -Dspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -Dtest="com.uniovi.Wiq_UnitTests,com.uniovi.CucumberRunnerTests"
+ ./mvnw -B org.jacoco:jacoco-maven-plugin:prepare-agent clean test sonar:sonar -DEXCLUDE_JUNIT=true -Dsonar.projectKey=Arquisoft_wiq_es04b -Dsonar.organization=arquisoft -Dsonar.branch.name=${{ github.ref }} -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=${{ secrets.SONAR_TOKEN }} -Dspring.profiles.active=test -Dspring.datasource.url=jdbc:mysql://localhost:3306/test_database -Dspring.datasource.username=root -Dspring.datasource.password=root -Dspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -Dtest="com.uniovi.Wiq_UnitTests,com.uniovi.CucumberRunnerTests"
env:
SPRING_PROFILES_ACTIVE: test
headless: true
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 65f9bebe..54cdb311 100644
--- a/pom.xml
+++ b/pom.xml
@@ -32,17 +32,6 @@
org.springframework.boot
spring-boot-starter-web
-
- org.springframework.boot
- spring-boot-starter-test
- test
-
-
- org.junit.jupiter
- junit-jupiter-engine
-
-
-
org.projectlombok
lombok
@@ -189,5 +178,48 @@
+
+
+
+ exclude-junit
+
+
+ env.EXCLUDE_JUNIT
+ true
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-engine
+
+
+
+
+
+
+
+ include-junit
+
+ true
+
+ !env.EXCLUDE_JUNIT
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+
diff --git a/src/main/java/com/uniovi/entities/Role.java b/src/main/java/com/uniovi/entities/Role.java
index deaa4b6a..c767c425 100644
--- a/src/main/java/com/uniovi/entities/Role.java
+++ b/src/main/java/com/uniovi/entities/Role.java
@@ -16,7 +16,7 @@ public class Role {
@Id
private String name;
- @ManyToMany(mappedBy = "roles")
+ @ManyToMany(mappedBy = "roles", fetch = FetchType.EAGER)
private Set players = new HashSet<>();
public Role(String name) {
diff --git a/src/main/java/com/uniovi/services/InsertSampleDataService.java b/src/main/java/com/uniovi/services/InsertSampleDataService.java
index fce3766e..f05dc00c 100644
--- a/src/main/java/com/uniovi/services/InsertSampleDataService.java
+++ b/src/main/java/com/uniovi/services/InsertSampleDataService.java
@@ -50,6 +50,15 @@ public InsertSampleDataService(PlayerService playerService, QuestionService ques
@Transactional
@EventListener(ApplicationReadyEvent.class) // Uncomment this line to insert sample data on startup
public void insertSampleQuestions() {
+ if (!playerService.getUserByEmail("test@test.com").isPresent()) {
+ PlayerDto player = new PlayerDto();
+ player.setEmail("test@test.com");
+ player.setUsername("test");
+ player.setPassword("test");
+ player.setRoles(new String[]{"ROLE_USER"});
+ playerService.generateApiKey(playerService.addNewPlayer(player));
+ }
+
if (Arrays.stream(environment.getActiveProfiles()).anyMatch(env -> (env.equalsIgnoreCase("test")))) {
log.info("Test profile active, skipping sample data insertion");
return;
@@ -60,14 +69,6 @@ public void insertSampleQuestions() {
@Transactional
public void generateSampleData() {
- if (!playerService.getUserByEmail("test@test.com").isPresent()) {
- PlayerDto player = new PlayerDto();
- player.setEmail("test@test.com");
- player.setUsername("test");
- player.setPassword("test");
- player.setRoles(new String[]{"ROLE_USER"});
- playerService.generateApiKey(playerService.addNewPlayer(player));
- }
questionRepository.deleteAll();
diff --git a/src/test/java/com/uniovi/Wiq_UnitTests.java b/src/test/java/com/uniovi/Wiq_UnitTests.java
index f1003da9..f96806f0 100644
--- a/src/test/java/com/uniovi/Wiq_UnitTests.java
+++ b/src/test/java/com/uniovi/Wiq_UnitTests.java
@@ -9,8 +9,6 @@
import org.springframework.test.context.ActiveProfiles;
import java.util.List;
-import java.util.Optional;
-
@SpringBootTest
@Tag("unit")
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@@ -24,6 +22,6 @@ public class Wiq_UnitTests {
@Order(1)
public void testPlayerService() {
List players = playerService.getUsersByRole("ROLE_USER");
- Assertions.assertEquals(0, players.size());
+ Assertions.assertEquals(1, players.size());
}
}