From f48641d65eabe5eaee4af2873d5bc50c5cbd9fe0 Mon Sep 17 00:00:00 2001 From: BilledTrain380 Date: Fri, 31 Aug 2018 10:06:17 +0200 Subject: [PATCH] Fix participant group relation --- .../participation/ParticipantManager.kt | 3 +- .../participation/ParticipantManagerImpl.kt | 73 ++----------------- .../sporttagpsa/entity/ParticipantEntity.kt | 2 +- .../ParticipantManagerImplSpec.kt | 32 ++++---- 4 files changed, 28 insertions(+), 82 deletions(-) diff --git a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManager.kt b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManager.kt index a15bf381..2fc3d9ca 100644 --- a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManager.kt +++ b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManager.kt @@ -106,8 +106,6 @@ interface ParticipantManager { * * If the {@link Participant#town} does not exist, it will be created * - * The {@link Participant#group} relation will be created if it does not exist yet. - * * The properties {@link Participant#absent} and {@link Participant#sport} will be ignored. * To update those use {@link ParticipationManager#markAsAbsent}, {@link ParticipationManager#markAsPresent} * or {@link ParticipationManager#participate}. @@ -115,6 +113,7 @@ interface ParticipantManager { * @param participant the participant to save * * @return the created participant + * @throws NoSuchElementException if the group of the given {@code participant} does not exist */ fun saveParticipant(participant: Participant): Participant } diff --git a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManagerImpl.kt b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManagerImpl.kt index bdf37db1..e5a84a11 100644 --- a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManagerImpl.kt +++ b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManagerImpl.kt @@ -39,10 +39,12 @@ package ch.schulealtendorf.sporttagpsa.business.participation import ch.schulealtendorf.sporttagpsa.entity.* import ch.schulealtendorf.sporttagpsa.model.* import ch.schulealtendorf.sporttagpsa.repository.AbsentParticipantRepository +import ch.schulealtendorf.sporttagpsa.repository.GroupRepository import ch.schulealtendorf.sporttagpsa.repository.ParticipantRepository import ch.schulealtendorf.sporttagpsa.repository.TownRepository import org.springframework.stereotype.Component import java.util.* +import kotlin.NoSuchElementException /** * Default implementation of a {@link ParticipantManager} which uses the repository classes. @@ -54,76 +56,20 @@ import java.util.* class ParticipantManagerImpl( private val participantRepository: ParticipantRepository, private val absentRepository: AbsentParticipantRepository, - private val townRepository: TownRepository + private val townRepository: TownRepository, + private val groupRepository: GroupRepository ): ParticipantManager { - /** - * @return a list of all participants - */ override fun getParticipants() = participantRepository.findAll().map { it.toParticipant() } - /** - * Filters all participant by the given {@code group}. - * Participants which are not related to the group - * are not included in the returned list. - * - * @param group the group where a participants belongs to - * - * @return the filtered participant list - */ override fun getParticipants(group: Group) = participantRepository.findByGroupName(group.name).map { it.toParticipant() } - /** - * Filters all participants by the given {@code gender}. - * Participants which are not equal to the gender - * are not included in the returned list. - * - * @param gender the gender of the participants - * - * @return the filtered participant list - */ override fun getParticipants(gender: Gender) = participantRepository.findByGender(gender.toString()).map { it.toParticipant() } - /** - * Filters all participants by the given {@code group} - * AND by the given {@code gender}. - * - * Participants which are not related to the group AND not - * equal to the gender are not included in the returned list. - * - * @param group the group where the participants belongs to - * @param gender the gender of the participants - * - * @return the filtered participant list - */ override fun getParticipants(group: Group, gender: Gender) = participantRepository.findByGroupAndGender(group.name, gender.toString()).map { it.toParticipant() } - /** - * @param id the id of the participant - * - * @return an Optional containing the participant or empty if the participant could not be found - */ override fun getParticipant(id: Int): Optional = participantRepository.findById(id).map { it.toParticipant() } - /** - * Saves the given {@code participant}. - * - * If the {@link Participant#id} < 1, it will be created. - * - * If the participant exists already, it will be updated. - * - * If the {@link Participant#town} does not exist, it will be created - * - * The {@link Participant#group} relation will be created if it does not exist yet. - * - * The properties {@link Participant#absent} and {@link Participant#sport} will be ignored. - * To update those use {@link ParticipationManager#markAsAbsent}, {@link ParticipationManager#markAsPresent} - * or {@link ParticipationManager#participate}. - * - * @param participant the participant to save - * - * @return the created participant - */ override fun saveParticipant(participant: Participant): Participant { val participantEntity: ParticipantEntity = participantRepository.findById(participant.id) @@ -132,6 +78,9 @@ class ParticipantManagerImpl( val townEntity = townRepository.findByZipAndName(participant.town.zip, participant.town.name) .orElseGet { TownEntity(zip = participant.town.zip, name = participant.town.name) } + val groupEntity = groupRepository.findById(participant.group.name) + .orElseThrow { NoSuchElementException("Participant group does not exist: name=${participant.group.name}") } + participantEntity.apply { surname = participant.surname prename = participant.prename @@ -139,13 +88,7 @@ class ParticipantManagerImpl( birthday = participant.birthday.milliseconds address = participant.address town = townEntity - group = GroupEntity().apply { - name = participant.group.name - coach = CoachEntity().apply { - id = if (participant.group.coach.id < 1) null else participant.group.coach.id - name = participant.group.coach.name - } - } + group = groupEntity } return participantRepository.save(participantEntity).toParticipant() diff --git a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/entity/ParticipantEntity.kt b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/entity/ParticipantEntity.kt index 12c73130..71e58a8e 100644 --- a/src/main/kotlin/ch/schulealtendorf/sporttagpsa/entity/ParticipantEntity.kt +++ b/src/main/kotlin/ch/schulealtendorf/sporttagpsa/entity/ParticipantEntity.kt @@ -76,7 +76,7 @@ data class ParticipantEntity( @JoinColumn(name = "FK_TOWN_id", referencedColumnName = "id") var town: TownEntity = TownEntity(), - @ManyToOne(cascade = [CascadeType.PERSIST]) + @ManyToOne @JoinColumn(name = "FK_GROUP_name", referencedColumnName = "name") var group: GroupEntity = GroupEntity(), diff --git a/src/test/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManagerImplSpec.kt b/src/test/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManagerImplSpec.kt index 5900f24c..cc1262ef 100644 --- a/src/test/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManagerImplSpec.kt +++ b/src/test/kotlin/ch/schulealtendorf/sporttagpsa/business/participation/ParticipantManagerImplSpec.kt @@ -42,6 +42,7 @@ import ch.schulealtendorf.sporttagpsa.entity.ParticipantEntity import ch.schulealtendorf.sporttagpsa.entity.TownEntity import ch.schulealtendorf.sporttagpsa.model.* import ch.schulealtendorf.sporttagpsa.repository.AbsentParticipantRepository +import ch.schulealtendorf.sporttagpsa.repository.GroupRepository import ch.schulealtendorf.sporttagpsa.repository.ParticipantRepository import ch.schulealtendorf.sporttagpsa.repository.TownRepository import com.nhaarman.mockito_kotlin.* @@ -51,6 +52,9 @@ import org.jetbrains.spek.api.dsl.describe import org.jetbrains.spek.api.dsl.it import org.jetbrains.spek.api.dsl.on import java.util.* +import kotlin.NoSuchElementException +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith /** * @author nmaerchy @@ -63,18 +67,21 @@ object ParticipantManagerImplSpec: Spek({ val mockParticipantRepository: ParticipantRepository = mock() val mockAbsentRepository: AbsentParticipantRepository = mock() val mockTownRepository: TownRepository = mock() + val mockGroupRepository: GroupRepository = mock() val manager = ParticipantManagerImpl( mockParticipantRepository, mockAbsentRepository, - mockTownRepository + mockTownRepository, + mockGroupRepository ) beforeEachTest { reset( mockAbsentRepository, mockParticipantRepository, - mockTownRepository + mockTownRepository, + mockGroupRepository ) } @@ -142,6 +149,7 @@ object ParticipantManagerImplSpec: Spek({ whenever(mockParticipantRepository.findById(any())).thenReturn(Optional.empty()) whenever(mockTownRepository.findByZipAndName(any(), any())).thenReturn(Optional.of(townEntity)) + whenever(mockGroupRepository.findById(any())).thenReturn(Optional.of(groupEntity)) val participant = participantModel.copy(id = 0) // id 0 to create the participant @@ -158,6 +166,7 @@ object ParticipantManagerImplSpec: Spek({ whenever(mockParticipantRepository.findById(any())).thenReturn(Optional.of(participantEntity)) whenever(mockTownRepository.findByZipAndName(any(), any())).thenReturn(Optional.empty()) + whenever(mockGroupRepository.findById(any())).thenReturn(Optional.of(groupEntity)) val participant = participantModel.copy( @@ -178,27 +187,22 @@ object ParticipantManagerImplSpec: Spek({ } } - on("new coach") { + on("unkown group") { whenever(mockParticipantRepository.findById(any())).thenReturn(Optional.of(participantEntity)) + whenever(mockGroupRepository.findById(any())).thenReturn(Optional.empty()) val participant = participantModel.copy( group = Group("3a", Coach(0, "Müller")) // coach id 0 to create the coach ) - manager.saveParticipant(participant) - it("should create the coach") { - val expected = participantEntity.copy( - group = GroupEntity( - name = "3a", - coach = CoachEntity( // entity with id null will be created - name = "Müller" - ) - ) - ) - verify(mockParticipantRepository, times(1)).save(expected) + it("should throw a no such element exception, indicating that the group does not exist") { + val exception = assertFailsWith { + manager.saveParticipant(participant) + } + assertEquals("Participant group does not exist: name=3a", exception.message) } } }