From 9bb358b6c96c35551546752dbbc36c4a6da36cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kollonay=20Zolt=C3=A1n?= Date: Sat, 27 Jan 2024 09:21:04 +0100 Subject: [PATCH 01/10] Test problem --- backend/sportsmatch/build.gradle | 4 ++ .../controllers/SportController.java | 30 ++++++++++ .../java/com/sportsmatch/dtos/SportDTO.java | 15 +++++ .../com/sportsmatch/mappers/SportMapper.java | 27 +++++++++ .../java/com/sportsmatch/models/Sport.java | 5 +- .../sportsmatch/repos/SportRepository.java | 1 + .../com/sportsmatch/service/SportService.java | 11 ++++ .../sportsmatch/service/SportServiceImp.java | 32 ++++++++++ .../service/SportServiceImpTest.java | 60 +++++++++++++++++++ 9 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java create mode 100644 backend/sportsmatch/src/main/java/com/sportsmatch/dtos/SportDTO.java create mode 100644 backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java create mode 100644 backend/sportsmatch/src/main/java/com/sportsmatch/service/SportService.java create mode 100644 backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java create mode 100644 backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java diff --git a/backend/sportsmatch/build.gradle b/backend/sportsmatch/build.gradle index 9e997a5c..3cfb8630 100644 --- a/backend/sportsmatch/build.gradle +++ b/backend/sportsmatch/build.gradle @@ -24,6 +24,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' + testImplementation 'org.testng:testng:7.1.0' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' @@ -31,6 +32,9 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation group: 'org.modelmapper', name: 'modelmapper', version: '3.1.1' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0' + + testImplementation 'junit:junit:4.13.2' + testImplementation 'org.hamcrest:hamcrest-core:2.2' } tasks.named('bootBuildImage') { diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java b/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java new file mode 100644 index 00000000..f0dc27c5 --- /dev/null +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java @@ -0,0 +1,30 @@ +package com.sportsmatch.controllers; + +import com.sportsmatch.dtos.SportDTO; +import com.sportsmatch.service.SportService; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/v1/sports") +public class SportController { + + private final SportService sportService; + + /** + * This endpoint returns paginated list of SportDto. + * + * @param pageable contains tha page and size value + * @return paginated list of SportDTO + */ + @GetMapping("/all") + public List getSports(final Pageable pageable) { + return sportService.getAllSports(pageable); + } +} diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/SportDTO.java b/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/SportDTO.java new file mode 100644 index 00000000..7f34ea7b --- /dev/null +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/SportDTO.java @@ -0,0 +1,15 @@ +package com.sportsmatch.dtos; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class SportDTO { + + public SportDTO(String name) { + this.name = name; + } + + public String name; +} diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java new file mode 100644 index 00000000..2d63952f --- /dev/null +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java @@ -0,0 +1,27 @@ +package com.sportsmatch.mappers; + +import com.sportsmatch.dtos.SportDTO; +import com.sportsmatch.models.Sport; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.springframework.stereotype.Component; + +@Getter +@Setter +@RequiredArgsConstructor +@Component +public class SportMapper { + + /** + * Converts a Sport entity to a SportDTO. + * + * @param entity The Sport entity to be converted. + * @return SportDTO containing information from the Sport entity. + */ + public static SportDTO toDTO (Sport entity) { + return SportDTO.builder() + .name(entity.getName()) + .build(); + } +} diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/models/Sport.java b/backend/sportsmatch/src/main/java/com/sportsmatch/models/Sport.java index 09e27563..67b98f9c 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/models/Sport.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/models/Sport.java @@ -1,10 +1,7 @@ package com.sportsmatch.models; import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; import java.util.HashSet; import java.util.Set; diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/repos/SportRepository.java b/backend/sportsmatch/src/main/java/com/sportsmatch/repos/SportRepository.java index 15746961..f6090da9 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/repos/SportRepository.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/repos/SportRepository.java @@ -6,4 +6,5 @@ @Repository public interface SportRepository extends JpaRepository { + } diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportService.java b/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportService.java new file mode 100644 index 00000000..3e034b43 --- /dev/null +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportService.java @@ -0,0 +1,11 @@ +package com.sportsmatch.service; + +import com.sportsmatch.dtos.SportDTO; +import org.springframework.data.domain.Pageable; + +import java.util.List; + +public interface SportService { + List getAllSports(final Pageable pageable); + +} diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java b/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java new file mode 100644 index 00000000..9bb3e4b7 --- /dev/null +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java @@ -0,0 +1,32 @@ +package com.sportsmatch.service; + +import com.sportsmatch.dtos.SportDTO; +import com.sportsmatch.mappers.SportMapper; +import com.sportsmatch.repos.SportRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class SportServiceImp implements SportService { + + private final SportRepository sportRepository; + private final SportMapper sportMapper; + + + /** + * This method returns a paginated list of SportsDTO. + * + * @param pageable contains the page and size values for pagination. + * @return paginated list of SportDTO. + */ + public List getAllSports(final Pageable pageable) { + return sportRepository.findAll(pageable).stream() + .map(SportMapper::toDTO) + .collect(Collectors.toList()); + } +} diff --git a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java new file mode 100644 index 00000000..78f1fb4e --- /dev/null +++ b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java @@ -0,0 +1,60 @@ +package com.sportsmatch.service; + +import com.sportsmatch.dtos.SportDTO; +import com.sportsmatch.mappers.SportMapper; +import com.sportsmatch.models.Sport; +import com.sportsmatch.repos.SportRepository; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; + + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@SpringBootTest +class SportServiceImpTest { + + @Mock + private SportRepository sportRepository; + + @InjectMocks + private SportServiceImp sportService; + + @Test + void getAllSports() { + // Arrange + Pageable pageable = Mockito.mock(Pageable.class); + + Sport sport1 = new Sport("Football"); + Sport sport2 = new Sport("Basketball"); + + List sports = Arrays.asList(sport1, sport2); + Page sportsPage = new PageImpl<>(sports, pageable, sports.size()); + + SportDTO sportDTO1 = new SportDTO("Football"); + SportDTO sportDTO2 = new SportDTO("Basketball"); + + List expectedSportDTOs = Arrays.asList(sportDTO1, sportDTO2); + + // Mocking repository + Mockito.when(sportRepository.findAll(Mockito.any(Pageable.class))).thenReturn(sportsPage); + + // Mocking mapper + Mockito.when(SportMapper.toDTO(sport1)).thenReturn(sportDTO1); + Mockito.when(SportMapper.toDTO(sport2)).thenReturn(sportDTO2); + + // Act + List result = sportService.getAllSports(pageable); + + // Assert + assertEquals(expectedSportDTOs, result); + } +} \ No newline at end of file From d4c0d40945cc766c15f6941b7790ae563e5dc342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Frnka?= Date: Sat, 27 Jan 2024 10:04:04 +0100 Subject: [PATCH 02/10] SMA-21: Simplified tests --- backend/sportsmatch/build.gradle | 4 ---- .../java/com/sportsmatch/service/SportServiceImpTest.java | 5 +---- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/backend/sportsmatch/build.gradle b/backend/sportsmatch/build.gradle index 3cfb8630..9e997a5c 100644 --- a/backend/sportsmatch/build.gradle +++ b/backend/sportsmatch/build.gradle @@ -24,7 +24,6 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' - testImplementation 'org.testng:testng:7.1.0' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' @@ -32,9 +31,6 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation group: 'org.modelmapper', name: 'modelmapper', version: '3.1.1' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0' - - testImplementation 'junit:junit:4.13.2' - testImplementation 'org.hamcrest:hamcrest-core:2.2' } tasks.named('bootBuildImage') { diff --git a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java index 78f1fb4e..72d7906e 100644 --- a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java +++ b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java @@ -18,6 +18,7 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.eq; @SpringBootTest class SportServiceImpTest { @@ -47,10 +48,6 @@ void getAllSports() { // Mocking repository Mockito.when(sportRepository.findAll(Mockito.any(Pageable.class))).thenReturn(sportsPage); - // Mocking mapper - Mockito.when(SportMapper.toDTO(sport1)).thenReturn(sportDTO1); - Mockito.when(SportMapper.toDTO(sport2)).thenReturn(sportDTO2); - // Act List result = sportService.getAllSports(pageable); From 19caf177334ce2f515afb1927d696f3485210ee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kollonay=20Zolt=C3=A1n?= Date: Sat, 27 Jan 2024 10:24:46 +0100 Subject: [PATCH 03/10] feat: -modify method name --- .../test/java/com/sportsmatch/service/SportServiceImpTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java index 72d7906e..89a57241 100644 --- a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java +++ b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java @@ -30,7 +30,7 @@ class SportServiceImpTest { private SportServiceImp sportService; @Test - void getAllSports() { + void getAllSportsShouldReturnAllSportsWhenRequired() { // Arrange Pageable pageable = Mockito.mock(Pageable.class); From 3ddc201654938fa4d08d0f9a03a3885ea7d79cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kollonay=20Zolt=C3=A1n?= Date: Tue, 30 Jan 2024 18:43:29 +0100 Subject: [PATCH 04/10] Feat: conflicts solved --- .../src/main/java/com/sportsmatch/service/SportServiceImp.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java b/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java index 9bb3e4b7..52899f0b 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java @@ -2,7 +2,7 @@ import com.sportsmatch.dtos.SportDTO; import com.sportsmatch.mappers.SportMapper; -import com.sportsmatch.repos.SportRepository; +import com.sportsmatch.repositories.SportRepository; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; From 9ce87597a2b37f9a75d7fb3a0002fa8d60f908b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kollonay=20Zolt=C3=A1n?= Date: Thu, 1 Feb 2024 18:21:36 +0100 Subject: [PATCH 05/10] Feat: conflicts solved --- .../java/com/sportsmatch/repositories/SportRepository.java | 5 +++-- .../java/com/sportsmatch/service/SportServiceImpTest.java | 5 +---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/SportRepository.java b/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/SportRepository.java index 20409e84..c098a31e 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/SportRepository.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/SportRepository.java @@ -1,12 +1,13 @@ package com.sportsmatch.repositories; import com.sportsmatch.models.Sport; -import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface SportRepository extends JpaRepository { - Optional findSportByName(String name); + Optional findSportByName(String name); } diff --git a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java index 89a57241..6f3302af 100644 --- a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java +++ b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java @@ -1,9 +1,8 @@ package com.sportsmatch.service; import com.sportsmatch.dtos.SportDTO; -import com.sportsmatch.mappers.SportMapper; import com.sportsmatch.models.Sport; -import com.sportsmatch.repos.SportRepository; +import com.sportsmatch.repositories.SportRepository; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -13,12 +12,10 @@ import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; - import java.util.Arrays; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.eq; @SpringBootTest class SportServiceImpTest { From c569fb3320df8de88fcec453cb41d1da77664288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kollonay=20Zolt=C3=A1n?= Date: Thu, 1 Feb 2024 18:34:45 +0100 Subject: [PATCH 06/10] Feat: conflicts solved --- .../main/java/com/sportsmatch/controllers/SportController.java | 2 +- .../src/main/java/com/sportsmatch/mappers/SportMapper.java | 2 +- .../com/sportsmatch/{service => services}/SportService.java | 2 +- .../com/sportsmatch/{service => services}/SportServiceImp.java | 3 ++- .../test/java/com/sportsmatch/service/SportServiceImpTest.java | 1 + 5 files changed, 6 insertions(+), 4 deletions(-) rename backend/sportsmatch/src/main/java/com/sportsmatch/{service => services}/SportService.java (85%) rename backend/sportsmatch/src/main/java/com/sportsmatch/{service => services}/SportServiceImp.java (92%) diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java b/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java index f0dc27c5..a6eaec0c 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java @@ -1,7 +1,7 @@ package com.sportsmatch.controllers; import com.sportsmatch.dtos.SportDTO; -import com.sportsmatch.service.SportService; +import com.sportsmatch.services.SportService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.GetMapping; diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java index 2d63952f..f5a8761c 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java @@ -19,7 +19,7 @@ public class SportMapper { * @param entity The Sport entity to be converted. * @return SportDTO containing information from the Sport entity. */ - public static SportDTO toDTO (Sport entity) { + public static SportDTO toDTO(Sport entity) { return SportDTO.builder() .name(entity.getName()) .build(); diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportService.java b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportService.java similarity index 85% rename from backend/sportsmatch/src/main/java/com/sportsmatch/service/SportService.java rename to backend/sportsmatch/src/main/java/com/sportsmatch/services/SportService.java index 3e034b43..1b849b40 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportService.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportService.java @@ -1,4 +1,4 @@ -package com.sportsmatch.service; +package com.sportsmatch.services; import com.sportsmatch.dtos.SportDTO; import org.springframework.data.domain.Pageable; diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java similarity index 92% rename from backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java rename to backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java index 52899f0b..3660dd6b 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java @@ -1,8 +1,9 @@ -package com.sportsmatch.service; +package com.sportsmatch.services; import com.sportsmatch.dtos.SportDTO; import com.sportsmatch.mappers.SportMapper; import com.sportsmatch.repositories.SportRepository; +import com.sportsmatch.services.SportService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; diff --git a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java index 6f3302af..5900ce05 100644 --- a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java +++ b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java @@ -3,6 +3,7 @@ import com.sportsmatch.dtos.SportDTO; import com.sportsmatch.models.Sport; import com.sportsmatch.repositories.SportRepository; +import com.sportsmatch.services.SportServiceImp; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; From 278e5c94533e0ba54b134cc63caffaf6dafe7a2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kollonay=20Zolt=C3=A1n?= Date: Thu, 1 Feb 2024 18:44:04 +0100 Subject: [PATCH 07/10] Feat: conflicts solved --- .../src/main/java/com/sportsmatch/mappers/SportMapper.java | 2 +- .../main/java/com/sportsmatch/services/SportServiceImp.java | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java index f5a8761c..e7ebb8fa 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java @@ -19,7 +19,7 @@ public class SportMapper { * @param entity The Sport entity to be converted. * @return SportDTO containing information from the Sport entity. */ - public static SportDTO toDTO(Sport entity) { + public SportDTO toDTO(Sport entity) { return SportDTO.builder() .name(entity.getName()) .build(); diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java index 3660dd6b..52361bfd 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java @@ -3,7 +3,6 @@ import com.sportsmatch.dtos.SportDTO; import com.sportsmatch.mappers.SportMapper; import com.sportsmatch.repositories.SportRepository; -import com.sportsmatch.services.SportService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -27,7 +26,7 @@ public class SportServiceImp implements SportService { */ public List getAllSports(final Pageable pageable) { return sportRepository.findAll(pageable).stream() - .map(SportMapper::toDTO) + .map(sportMapper::toDTO) .collect(Collectors.toList()); } } From e0ad4a42b803b56a7928f32c02f1ebde769d5d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kollonay=20Zolt=C3=A1n?= Date: Thu, 1 Feb 2024 20:51:58 +0100 Subject: [PATCH 08/10] Feat: conflicts solved --- .../controllers/SportController.java | 22 +++++----- .../com/sportsmatch/mappers/SportMapper.java | 22 +++++----- .../repositories/SportRepository.java | 2 +- .../sportsmatch/services/SportService.java | 2 +- .../sportsmatch/services/SportServiceImp.java | 27 ++++++------ .../service/SportServiceImpTest.java | 44 +++++++++---------- 6 files changed, 60 insertions(+), 59 deletions(-) diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java b/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java index a6eaec0c..58eea7e8 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java @@ -15,16 +15,16 @@ @RequestMapping("/api/v1/sports") public class SportController { - private final SportService sportService; + private final SportService sportService; - /** - * This endpoint returns paginated list of SportDto. - * - * @param pageable contains tha page and size value - * @return paginated list of SportDTO - */ - @GetMapping("/all") - public List getSports(final Pageable pageable) { - return sportService.getAllSports(pageable); - } + /** + * This endpoint returns paginated list of SportDto. + * + * @param pageable contains tha page and size value + * @return paginated list of SportDTO + */ + @GetMapping("/all") + public List getSports(final Pageable pageable) { + return sportService.getAllSports(pageable); + } } diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java index e7ebb8fa..c9e3cf46 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java @@ -13,15 +13,15 @@ @Component public class SportMapper { - /** - * Converts a Sport entity to a SportDTO. - * - * @param entity The Sport entity to be converted. - * @return SportDTO containing information from the Sport entity. - */ - public SportDTO toDTO(Sport entity) { - return SportDTO.builder() - .name(entity.getName()) - .build(); - } + /** + * Converts a Sport entity to a SportDTO. + * + * @param entity The Sport entity to be converted. + * @return SportDTO containing information from the Sport entity. + */ + public SportDTO toDTO(Sport entity) { + return SportDTO.builder() + .name(entity.getName()) + .build(); + } } diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/SportRepository.java b/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/SportRepository.java index c098a31e..4dcfae48 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/SportRepository.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/repositories/SportRepository.java @@ -9,5 +9,5 @@ @Repository public interface SportRepository extends JpaRepository { - Optional findSportByName(String name); + Optional findSportByName(String name); } diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportService.java b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportService.java index 1b849b40..009305cf 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportService.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportService.java @@ -6,6 +6,6 @@ import java.util.List; public interface SportService { - List getAllSports(final Pageable pageable); + List getAllSports(final Pageable pageable); } diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java index 52361bfd..60f76a89 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java @@ -14,19 +14,20 @@ @RequiredArgsConstructor public class SportServiceImp implements SportService { - private final SportRepository sportRepository; - private final SportMapper sportMapper; + private final SportRepository sportRepository; + private final SportMapper sportMapper; - /** - * This method returns a paginated list of SportsDTO. - * - * @param pageable contains the page and size values for pagination. - * @return paginated list of SportDTO. - */ - public List getAllSports(final Pageable pageable) { - return sportRepository.findAll(pageable).stream() - .map(sportMapper::toDTO) - .collect(Collectors.toList()); - } + /** + * This method returns a paginated list of SportsDTO. + * + * @param pageable contains the page and size values for pagination. + * @return paginated list of SportDTO. + */ + public List getAllSports(final Pageable pageable) { + return sportRepository.findAll(pageable) + .stream() + .map(sportMapper::toDTO) + .collect(Collectors.toList()); + } } diff --git a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java index 5900ce05..f29bfd9d 100644 --- a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java +++ b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java @@ -21,35 +21,35 @@ @SpringBootTest class SportServiceImpTest { - @Mock - private SportRepository sportRepository; + @Mock + private SportRepository sportRepository; - @InjectMocks - private SportServiceImp sportService; + @InjectMocks + private SportServiceImp sportService; - @Test - void getAllSportsShouldReturnAllSportsWhenRequired() { - // Arrange - Pageable pageable = Mockito.mock(Pageable.class); + @Test + void getAllSportsShouldReturnAllSportsWhenRequired() { + // Arrange + Pageable pageable = Mockito.mock(Pageable.class); - Sport sport1 = new Sport("Football"); - Sport sport2 = new Sport("Basketball"); + Sport sport1 = new Sport("Football"); + Sport sport2 = new Sport("Basketball"); - List sports = Arrays.asList(sport1, sport2); - Page sportsPage = new PageImpl<>(sports, pageable, sports.size()); + List sports = Arrays.asList(sport1, sport2); + Page sportsPage = new PageImpl<>(sports, pageable, sports.size()); - SportDTO sportDTO1 = new SportDTO("Football"); - SportDTO sportDTO2 = new SportDTO("Basketball"); + SportDTO sportDTO1 = new SportDTO("Football"); + SportDTO sportDTO2 = new SportDTO("Basketball"); - List expectedSportDTOs = Arrays.asList(sportDTO1, sportDTO2); + List expectedSportDTOs = Arrays.asList(sportDTO1, sportDTO2); - // Mocking repository - Mockito.when(sportRepository.findAll(Mockito.any(Pageable.class))).thenReturn(sportsPage); + // Mocking repository + Mockito.when(sportRepository.findAll(Mockito.any(Pageable.class))).thenReturn(sportsPage); - // Act - List result = sportService.getAllSports(pageable); + // Act + List result = sportService.getAllSports(pageable); - // Assert - assertEquals(expectedSportDTOs, result); - } + // Assert + assertEquals(expectedSportDTOs, result); + } } \ No newline at end of file From e49d34e220053472ddf86a95bcb4deb091431181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kollonay=20Zolt=C3=A1n?= Date: Thu, 1 Feb 2024 20:58:23 +0100 Subject: [PATCH 09/10] Feat: formatting --- .../src/main/java/com/sportsmatch/dtos/SportDTO.java | 8 ++++---- .../java/com/sportsmatch/services/SportServiceImp.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/SportDTO.java b/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/SportDTO.java index 7f34ea7b..2b61305f 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/SportDTO.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/SportDTO.java @@ -7,9 +7,9 @@ @Builder public class SportDTO { - public SportDTO(String name) { - this.name = name; - } + public String name; - public String name; + public SportDTO(String name) { + this.name = name; + } } diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java index 60f76a89..76f46a48 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java @@ -19,7 +19,7 @@ public class SportServiceImp implements SportService { /** - * This method returns a paginated list of SportsDTO. + * {@summary

This method returns a paginated list of SportsDTO.

} * * @param pageable contains the page and size values for pagination. * @return paginated list of SportDTO. From 6067e324da9d266246585eef40c66b0016527c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kollonay=20Zolt=C3=A1n?= Date: Thu, 1 Feb 2024 21:27:14 +0100 Subject: [PATCH 10/10] Feat: modified SportMapper to static method --- .../java/com/sportsmatch/mappers/SportMapper.java | 11 ++--------- .../src/main/java/com/sportsmatch/models/Sport.java | 1 + .../com/sportsmatch/services/SportServiceImp.java | 3 +-- .../com/sportsmatch/service/SportServiceImpTest.java | 11 ++++++++--- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java index c9e3cf46..ca1850f7 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java @@ -2,15 +2,8 @@ import com.sportsmatch.dtos.SportDTO; import com.sportsmatch.models.Sport; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import lombok.Setter; -import org.springframework.stereotype.Component; -@Getter -@Setter -@RequiredArgsConstructor -@Component + public class SportMapper { /** @@ -19,7 +12,7 @@ public class SportMapper { * @param entity The Sport entity to be converted. * @return SportDTO containing information from the Sport entity. */ - public SportDTO toDTO(Sport entity) { + public static SportDTO toDTO(Sport entity) { return SportDTO.builder() .name(entity.getName()) .build(); diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/models/Sport.java b/backend/sportsmatch/src/main/java/com/sportsmatch/models/Sport.java index 6570ec58..ed6d5e67 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/models/Sport.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/models/Sport.java @@ -12,6 +12,7 @@ @Setter @NoArgsConstructor @AllArgsConstructor +@ToString public class Sport { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java index 76f46a48..477b36a2 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/services/SportServiceImp.java @@ -15,7 +15,6 @@ public class SportServiceImp implements SportService { private final SportRepository sportRepository; - private final SportMapper sportMapper; /** @@ -27,7 +26,7 @@ public class SportServiceImp implements SportService { public List getAllSports(final Pageable pageable) { return sportRepository.findAll(pageable) .stream() - .map(sportMapper::toDTO) + .map(SportMapper::toDTO) .collect(Collectors.toList()); } } diff --git a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java index f29bfd9d..3077205b 100644 --- a/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java +++ b/backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java @@ -5,10 +5,11 @@ import com.sportsmatch.repositories.SportRepository; import com.sportsmatch.services.SportServiceImp; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; -import org.springframework.boot.test.context.SpringBootTest; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; @@ -17,13 +18,16 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; -@SpringBootTest +@ExtendWith(MockitoExtension.class) class SportServiceImpTest { @Mock private SportRepository sportRepository; + @InjectMocks private SportServiceImp sportService; @@ -44,7 +48,8 @@ void getAllSportsShouldReturnAllSportsWhenRequired() { List expectedSportDTOs = Arrays.asList(sportDTO1, sportDTO2); // Mocking repository - Mockito.when(sportRepository.findAll(Mockito.any(Pageable.class))).thenReturn(sportsPage); + when(sportRepository.findAll(any(Pageable.class))).thenReturn(sportsPage); + // Act List result = sportService.getAllSports(pageable);