StudentInfoController.java

1
package edu.ucsb.cs156.frontiers.controllers;
2
3
import edu.ucsb.cs156.frontiers.config.AllowApiKeyAccess;
4
import edu.ucsb.cs156.frontiers.entities.RosterStudent;
5
import edu.ucsb.cs156.frontiers.enums.RosterStatus;
6
import edu.ucsb.cs156.frontiers.errors.StudentNotOnRosterException;
7
import edu.ucsb.cs156.frontiers.models.NameAndTeam;
8
import edu.ucsb.cs156.frontiers.models.StudentInfo;
9
import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository;
10
import edu.ucsb.cs156.frontiers.services.TeamCsvService;
11
import io.swagger.v3.oas.annotations.Operation;
12
import io.swagger.v3.oas.annotations.Parameter;
13
import io.swagger.v3.oas.annotations.tags.Tag;
14
import java.util.ArrayList;
15
import java.util.Comparator;
16
import java.util.List;
17
import java.util.stream.StreamSupport;
18
import lombok.extern.slf4j.Slf4j;
19
import org.springframework.security.access.prepost.PreAuthorize;
20
import org.springframework.web.bind.annotation.GetMapping;
21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestParam;
23
import org.springframework.web.bind.annotation.RestController;
24
25
/**
26
 * Looks up one roster student by email and returns what an autograder needs to check their
27
 * submission: their GitHub login, their team, and the names of their teammates. The endpoint
28
 * accepts a course-scoped API key (see docs/api-keys.md), so that a Gradescope autograder or a
29
 * GitHub Action can call it without a logged-in user.
30
 */
31
@Tag(name = "Student Info")
32
@RestController
33
@RequestMapping("/api/courses/studentInfo")
34
@Slf4j
35
public class StudentInfoController extends ApiController {
36
37
  private static final String UMAIL_SUFFIX = "@umail.ucsb.edu";
38
  private static final String UCSB_SUFFIX = "@ucsb.edu";
39
40
  private final RosterStudentRepository rosterStudentRepository;
41
  private final TeamCsvService teamCsvService;
42
43
  public StudentInfoController(
44
      RosterStudentRepository rosterStudentRepository, TeamCsvService teamCsvService) {
45
    this.rosterStudentRepository = rosterStudentRepository;
46
    this.teamCsvService = teamCsvService;
47
  }
48
49
  /**
50
   * Puts an email into the form used for comparison: surrounding whitespace removed, lower case,
51
   * and the legacy <code>@umail.ucsb.edu</code> domain replaced by <code>@ucsb.edu</code> (both
52
   * reach the same mailbox, and Gradescope may report either one).
53
   *
54
   * @param email the email as supplied
55
   * @return the normalized email
56
   */
57
  public static String normalizeEmail(String email) {
58
    String normalized = email.strip().toLowerCase();
59 1 1. normalizeEmail : negated conditional → KILLED
    if (normalized.endsWith(UMAIL_SUFFIX)) {
60
      normalized =
61 1 1. normalizeEmail : Replaced integer subtraction with addition → KILLED
          normalized.substring(0, normalized.length() - UMAIL_SUFFIX.length()) + UCSB_SUFFIX;
62
    }
63 1 1. normalizeEmail : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::normalizeEmail → KILLED
    return normalized;
64
  }
65
66
  private static String lower(String s) {
67 2 1. lower : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::lower → KILLED
2. lower : negated conditional → KILLED
    return s == null ? "" : s.toLowerCase();
68
  }
69
70
  @Operation(
71
      summary = "Get a student's GitHub login, team and teammates (for autograders)",
72
      description =
73
          """
74
          Looks up a non-dropped roster student of the course by email (case-insensitive;
75
          @umail.ucsb.edu is treated as @ucsb.edu) and returns their GitHub login, their team,
76
          and everyone on that team. The firstName fields are the shortest names that are
77
          unique within the course (e.g. "Chris L"), as on the team CSVs; legalFirstName is the
78
          roster's first name field. Accepts a course-scoped API key in the X-API-KEY header.
79
          """)
80
  @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)")
81
  @AllowApiKeyAccess
82
  @GetMapping("")
83
  public StudentInfo getStudentInfo(
84
      @Parameter(name = "courseId") @RequestParam Long courseId,
85
      @Parameter(name = "email") @RequestParam String email) {
86
87
    String wantedEmail = normalizeEmail(email);
88
89
    // Dropped students are not part of the current roster: they cannot be looked up, and they are
90
    // not listed as anyone's teammate. The rest are sorted by first name (first word only, as in
91
    // the short names), last name and email, so that the teammates come back in a stable,
92
    // human-friendly order.
93
    List<RosterStudent> students =
94
        StreamSupport.stream(rosterStudentRepository.findByCourseId(courseId).spliterator(), false)
95 2 1. lambda$getStudentInfo$0 : negated conditional → KILLED
2. lambda$getStudentInfo$0 : replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::lambda$getStudentInfo$0 → KILLED
            .filter(student -> student.getRosterStatus() != RosterStatus.DROPPED)
96
            .sorted(
97
                Comparator.comparing(
98
                        (RosterStudent student) ->
99 1 1. lambda$getStudentInfo$1 : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::lambda$getStudentInfo$1 → KILLED
                            teamCsvService.firstWord(student.getFirstName()).toLowerCase())
100 1 1. lambda$getStudentInfo$2 : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::lambda$getStudentInfo$2 → KILLED
                    .thenComparing(student -> lower(student.getLastName()))
101 1 1. lambda$getStudentInfo$3 : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::lambda$getStudentInfo$3 → KILLED
                    .thenComparing(student -> lower(student.getEmail())))
102
            .toList();
103
104
    // Unique short names must be worked out over the whole roster, not just the team, so that they
105
    // match the names students see on the team CSVs.
106
    List<NameAndTeam> namesAndTeams = teamCsvService.nameAndTeams(students);
107
108
    int index = -1;
109 2 1. getStudentInfo : changed conditional boundary → KILLED
2. getStudentInfo : negated conditional → KILLED
    for (int i = 0; i < students.size(); i++) {
110
      String studentEmail = students.get(i).getEmail();
111 2 1. getStudentInfo : negated conditional → KILLED
2. getStudentInfo : negated conditional → KILLED
      if (studentEmail != null && normalizeEmail(studentEmail).equals(wantedEmail)) {
112
        index = i;
113
        break;
114
      }
115
    }
116 2 1. getStudentInfo : changed conditional boundary → KILLED
2. getStudentInfo : negated conditional → KILLED
    if (index < 0) {
117
      throw new StudentNotOnRosterException(wantedEmail, courseId);
118
    }
119
120
    RosterStudent student = students.get(index);
121
    String team = namesAndTeams.get(index).team();
122
123
    List<StudentInfo.Member> teamMembers = new ArrayList<>();
124 1 1. getStudentInfo : negated conditional → KILLED
    if (!team.isEmpty()) {
125 2 1. getStudentInfo : negated conditional → KILLED
2. getStudentInfo : changed conditional boundary → KILLED
      for (int i = 0; i < students.size(); i++) {
126
        RosterStudent candidate = students.get(i);
127 1 1. getStudentInfo : negated conditional → KILLED
        if (candidate.getTeams().contains(team)) {
128
          teamMembers.add(
129
              new StudentInfo.Member(
130
                  candidate.getEmail(),
131
                  namesAndTeams.get(i).name(),
132
                  candidate.getFirstName(),
133
                  candidate.getGithubLogin()));
134
        }
135
      }
136
    }
137
138 1 1. getStudentInfo : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::getStudentInfo → KILLED
    return new StudentInfo(
139
        student.getEmail(),
140
        namesAndTeams.get(index).name(),
141
        student.getFirstName(),
142
        student.getLastName(),
143
        student.getGithubLogin(),
144 1 1. getStudentInfo : negated conditional → KILLED
        team.isEmpty() ? null : team,
145
        student.getTeams(),
146
        teamMembers);
147
  }
148
}

Mutations

59

1.1
Location : normalizeEmail
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:normalizeEmail_lowercases_strips_and_maps_umail()]
negated conditional → KILLED

61

1.1
Location : normalizeEmail
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:normalizeEmail_lowercases_strips_and_maps_umail()]
Replaced integer subtraction with addition → KILLED

63

1.1
Location : normalizeEmail
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:normalizeEmail_lowercases_strips_and_maps_umail()]
replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::normalizeEmail → KILLED

67

1.1
Location : lower
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:roster_rows_without_email_are_skipped_and_missing_github_login_is_null()]
replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::lower → KILLED

2.2
Location : lower
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:roster_rows_without_email_are_skipped_and_missing_github_login_is_null()]
negated conditional → KILLED

95

1.1
Location : lambda$getStudentInfo$0
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:dropped_student_is_not_found()]
negated conditional → KILLED

2.2
Location : lambda$getStudentInfo$0
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:dropped_student_is_not_found()]
replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::lambda$getStudentInfo$0 → KILLED

99

1.1
Location : lambda$getStudentInfo$1
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:student_on_several_teams_reports_first_team_and_its_members()]
replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::lambda$getStudentInfo$1 → KILLED

100

1.1
Location : lambda$getStudentInfo$2
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:returns_student_with_unique_short_names_and_sorted_teammates()]
replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::lambda$getStudentInfo$2 → KILLED

101

1.1
Location : lambda$getStudentInfo$3
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:roster_rows_without_email_are_skipped_and_missing_github_login_is_null()]
replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::lambda$getStudentInfo$3 → KILLED

109

1.1
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:empty_roster_is_not_found()]
changed conditional boundary → KILLED

2.2
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:empty_roster_is_not_found()]
negated conditional → KILLED

111

1.1
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:unknown_email_is_not_found()]
negated conditional → KILLED

2.2
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:student_on_several_teams_reports_first_team_and_its_members()]
negated conditional → KILLED

116

1.1
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:api_key_with_course_permissions_can_look_up_students()]
changed conditional boundary → KILLED

2.2
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:empty_roster_is_not_found()]
negated conditional → KILLED

124

1.1
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:student_on_several_teams_reports_first_team_and_its_members()]
negated conditional → KILLED

125

1.1
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:student_on_several_teams_reports_first_team_and_its_members()]
negated conditional → KILLED

2.2
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:student_on_several_teams_reports_first_team_and_its_members()]
changed conditional boundary → KILLED

127

1.1
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:student_on_several_teams_reports_first_team_and_its_members()]
negated conditional → KILLED

138

1.1
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:student_on_several_teams_reports_first_team_and_its_members()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/StudentInfoController::getStudentInfo → KILLED

144

1.1
Location : getStudentInfo
Killed by : edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.StudentInfoControllerTests]/[method:student_on_several_teams_reports_first_team_and_its_members()]
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support