StudentDropService.java

1
package edu.ucsb.cs156.frontiers.services;
2
3
import edu.ucsb.cs156.frontiers.entities.RosterStudent;
4
import edu.ucsb.cs156.frontiers.entities.TeamMember;
5
import edu.ucsb.cs156.frontiers.repositories.TeamMemberRepository;
6
import java.util.ArrayList;
7
import java.util.Collection;
8
import java.util.List;
9
import org.springframework.stereotype.Service;
10
import org.springframework.transaction.annotation.Transactional;
11
12
/**
13
 * Single place for everything that must happen when a student is dropped from a course. Currently
14
 * this removes the student from all teams.
15
 */
16
@Service
17
public class StudentDropService {
18
19
  private final TeamMemberRepository teamMemberRepository;
20
21
  public StudentDropService(TeamMemberRepository teamMemberRepository) {
22
    this.teamMemberRepository = teamMemberRepository;
23
  }
24
25
  @Transactional
26
  public void handleStudentsDropped(Collection<RosterStudent> students) {
27
    for (RosterStudent student : students) {
28 1 1. handleStudentsDropped : removed call to edu/ucsb/cs156/frontiers/services/StudentDropService::handleStudentDropped → KILLED
      handleStudentDropped(student);
29
    }
30
  }
31
32
  @Transactional
33
  public void handleStudentDropped(RosterStudent student) {
34 1 1. handleStudentDropped : removed call to edu/ucsb/cs156/frontiers/services/StudentDropService::removeFromTeams → KILLED
    removeFromTeams(student);
35
  }
36
37
  private void removeFromTeams(RosterStudent student) {
38 1 1. removeFromTeams : negated conditional → KILLED
    if (student.getTeamMembers() == null) {
39
      return;
40
    }
41
    List<TeamMember> memberships = new ArrayList<>(student.getTeamMembers());
42
    for (TeamMember membership : memberships) {
43
      // Delete through the persistent (managed) copy, removing it from its managed parents first.
44
      // The parents' collections cascade, so otherwise Hibernate re-saves the deleted row.
45
      teamMemberRepository
46
          .findById(membership.getId())
47 1 1. removeFromTeams : removed call to java/util/Optional::ifPresent → KILLED
          .ifPresent(
48
              managed -> {
49
                managed.getTeam().getTeamMembers().remove(managed);
50
                managed.getRosterStudent().getTeamMembers().remove(managed);
51 1 1. lambda$removeFromTeams$0 : removed call to edu/ucsb/cs156/frontiers/repositories/TeamMemberRepository::delete → KILLED
                teamMemberRepository.delete(managed);
52
              });
53
      membership.getTeam().getTeamMembers().remove(membership);
54
    }
55
    // Replace the list instead of removing from it: a loaded Hibernate collection keeps a snapshot
56
    // of its old contents, and merging a detached student whose snapshot still lists the deleted
57
    // memberships fails with "Unable to find TeamMember".
58 1 1. removeFromTeams : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setTeamMembers → KILLED
    student.setTeamMembers(new ArrayList<>());
59
  }
60
}

Mutations

28

1.1
Location : handleStudentsDropped
Killed by : edu.ucsb.cs156.frontiers.services.StudentDropServiceDatabaseTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.StudentDropServiceDatabaseTests]/[method:droppedStudentIsRemovedFromAllTeamsInTheDatabase()]
removed call to edu/ucsb/cs156/frontiers/services/StudentDropService::handleStudentDropped → KILLED

34

1.1
Location : handleStudentDropped
Killed by : edu.ucsb.cs156.frontiers.services.StudentDropServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.StudentDropServiceTests]/[method:stillCleansDetachedCopiesIfRowAlreadyGone()]
removed call to edu/ucsb/cs156/frontiers/services/StudentDropService::removeFromTeams → KILLED

38

1.1
Location : removeFromTeams
Killed by : edu.ucsb.cs156.frontiers.services.StudentDropServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.StudentDropServiceTests]/[method:studentWithNullTeamMembersIsNoOp()]
negated conditional → KILLED

47

1.1
Location : removeFromTeams
Killed by : edu.ucsb.cs156.frontiers.services.StudentDropServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.StudentDropServiceTests]/[method:leavesOtherStudentsOnTheTeam()]
removed call to java/util/Optional::ifPresent → KILLED

51

1.1
Location : lambda$removeFromTeams$0
Killed by : edu.ucsb.cs156.frontiers.services.StudentDropServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.StudentDropServiceTests]/[method:leavesOtherStudentsOnTheTeam()]
removed call to edu/ucsb/cs156/frontiers/repositories/TeamMemberRepository::delete → KILLED

58

1.1
Location : removeFromTeams
Killed by : edu.ucsb.cs156.frontiers.services.StudentDropServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.StudentDropServiceTests]/[method:stillCleansDetachedCopiesIfRowAlreadyGone()]
removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setTeamMembers → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support