Skip to content

Commit

Permalink
Fix participant group relation
Browse files Browse the repository at this point in the history
  • Loading branch information
BilledTrain380 committed Aug 31, 2018
1 parent 9729646 commit f48641d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,14 @@ 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}.
*
* @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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Participant> = 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)
Expand All @@ -132,20 +78,17 @@ 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
gender = participant.gender.toString()
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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 <billedtrain380@gmail.com>
Expand All @@ -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
)
}

Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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<NoSuchElementException> {
manager.saveParticipant(participant)
}
assertEquals("Participant group does not exist: name=3a", exception.message)
}
}
}
Expand Down

0 comments on commit f48641d

Please sign in to comment.