StudentCsvFormat.java

1
package edu.ucsb.cs156.happiercows.helpers;
2
3
import edu.ucsb.cs156.happiercows.entities.Student;
4
import edu.ucsb.cs156.happiercows.utilities.CanonicalFormConverter;
5
import org.apache.commons.csv.CSVRecord;
6
7
import java.util.List;
8
import java.util.function.BiFunction;
9
10
/**
11
 * Different schools export their class rosters with different CSV column
12
 * layouts. Each format below knows how to recognize its own header row and
13
 * how to map a data row to a {@link Student}.
14
 *
15
 * <p>{@code UCSB_EGRADES} matches the header row exported by UCSB's
16
 * registrar (eGrades); see {@code docs/examples/egrades.csv}.
17
 * {@code CHICO_STATE_ROSTER} matches the Canvas roster export used at Chico
18
 * State (see {@code docs/examples/chico_canvas.csv}) and, along with
19
 * {@code GENERIC}, is included as a worked example of how to add support for
20
 * another school's roster format later (see issue #251); {@code GENERIC} is
21
 * also the format used for courses associated with the "Other" school.
22
 */
23
public enum StudentCsvFormat {
24
    UCSB_EGRADES(
25
            List.of(
26
                    "Enrl Cd", "Perm #", "Grade", "Final Units", "Student Last",
27
                    "Student First Middle", "Quarter", "Course ID", "Section",
28
                    "Meeting Time(s) / Location(s)", "Email", "ClassLevel", "Major1",
29
                    "Major2", "Date/Time", "Pronoun"),
30 1 1. lambda$static$0 : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::lambda$static$0 → KILLED
            (row, courseId) -> Student.builder()
31
                    .perm(row.get(1).trim())
32
                    .lastName(row.get(4).trim())
33
                    .firstMiddleName(row.get(5).trim())
34
                    .email(CanonicalFormConverter.convertToValidEmail(row.get(10).trim()))
35
                    .courseId(courseId)
36
                    .build()),
37
38
    CHICO_STATE_ROSTER(
39
            List.of("Student Name", "Student ID", "Student SIS ID", "Email", "Section Name"),
40
            (row, courseId) -> {
41
                String fullName = row.get(0).trim();
42 1 1. lambda$static$1 : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::lambda$static$1 → KILLED
                return Student.builder()
43
                        .perm(row.get(2).trim())
44
                        .lastName(lastNameFromFullName(fullName))
45
                        .firstMiddleName(firstNameFromFullName(fullName))
46
                        .email(CanonicalFormConverter.convertToValidEmail(row.get(3).trim()))
47
                        .courseId(courseId)
48
                        .build();
49
            }),
50
51
    GENERIC(
52
            List.of("lastName", "firstMiddleName", "email", "perm"),
53 1 1. lambda$static$2 : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::lambda$static$2 → KILLED
            (row, courseId) -> Student.builder()
54
                    .lastName(row.get(0).trim())
55
                    .firstMiddleName(row.get(1).trim())
56
                    .email(CanonicalFormConverter.convertToValidEmail(row.get(2).trim()))
57
                    .perm(row.get(3).trim())
58
                    .courseId(courseId)
59
                    .build());
60
61
    private final List<String> headers;
62
    private final BiFunction<CSVRecord, Long, Student> rowMapper;
63
64
    StudentCsvFormat(List<String> headers, BiFunction<CSVRecord, Long, Student> rowMapper) {
65
        this.headers = headers;
66
        this.rowMapper = rowMapper;
67
    }
68
69
    public List<String> getHeaders() {
70 1 1. getHeaders : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::getHeaders → KILLED
        return headers;
71
    }
72
73
    public boolean matchesHeaders(List<String> candidateHeaders) {
74 1 1. matchesHeaders : negated conditional → KILLED
        if (candidateHeaders.size() != headers.size()) {
75 1 1. matchesHeaders : replaced boolean return with true for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::matchesHeaders → KILLED
            return false;
76
        }
77 2 1. matchesHeaders : changed conditional boundary → KILLED
2. matchesHeaders : negated conditional → KILLED
        for (int i = 0; i < headers.size(); i++) {
78 1 1. matchesHeaders : negated conditional → KILLED
            if (!headers.get(i).equalsIgnoreCase(candidateHeaders.get(i).trim())) {
79 1 1. matchesHeaders : replaced boolean return with true for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::matchesHeaders → KILLED
                return false;
80
            }
81
        }
82 1 1. matchesHeaders : replaced boolean return with false for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::matchesHeaders → KILLED
        return true;
83
    }
84
85
    public Student toStudent(CSVRecord row, Long courseId) {
86 1 1. toStudent : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::toStudent → KILLED
        return rowMapper.apply(row, courseId);
87
    }
88
89
    /**
90
     * @param candidateHeaders the header row read from an uploaded CSV file
91
     * @return the matching format, or null if no known format matches
92
     */
93
    public static StudentCsvFormat detect(List<String> candidateHeaders) {
94
        for (StudentCsvFormat format : values()) {
95 1 1. detect : negated conditional → KILLED
            if (format.matchesHeaders(candidateHeaders)) {
96 1 1. detect : replaced return value with null for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::detect → KILLED
                return format;
97
            }
98
        }
99
        return null;
100
    }
101
102
    /**
103
     * Get everything after the last space in the full name. If the string
104
     * contains no spaces, return the entire input string as the result.
105
     *
106
     * @param fullName the full name, e.g. "Marge Simpson"
107
     * @return best estimate of last name, e.g. "Simpson"
108
     */
109
    static String lastNameFromFullName(String fullName) {
110
        int lastSpaceIndex = fullName.lastIndexOf(" ");
111 1 1. lastNameFromFullName : negated conditional → KILLED
        if (lastSpaceIndex == -1) {
112 1 1. lastNameFromFullName : replaced return value with "" for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::lastNameFromFullName → KILLED
            return fullName;
113
        }
114 2 1. lastNameFromFullName : Replaced integer addition with subtraction → KILLED
2. lastNameFromFullName : replaced return value with "" for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::lastNameFromFullName → KILLED
        return fullName.substring(lastSpaceIndex + 1).trim();
115
    }
116
117
    /**
118
     * Get everything up to and not including the last space in the full name.
119
     * If the string contains no spaces, return an empty string.
120
     *
121
     * @param fullName the full name, e.g. "Marge Simpson"
122
     * @return best estimate of first (and middle) name, e.g. "Marge"
123
     */
124
    static String firstNameFromFullName(String fullName) {
125
        int lastSpaceIndex = fullName.lastIndexOf(" ");
126 1 1. firstNameFromFullName : negated conditional → KILLED
        if (lastSpaceIndex == -1) {
127
            return "";
128
        }
129 1 1. firstNameFromFullName : replaced return value with "" for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::firstNameFromFullName → KILLED
        return fullName.substring(0, lastSpaceIndex).trim();
130
    }
131
}

Mutations

30

1.1
Location : lambda$static$0
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:ucsb_egrades_maps_row_to_student()]
replaced return value with null for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::lambda$static$0 → KILLED

42

1.1
Location : lambda$static$1
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:chico_state_roster_handles_a_name_with_no_spaces()]
replaced return value with null for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::lambda$static$1 → KILLED

53

1.1
Location : lambda$static$2
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:generic_canonicalizes_umail_email()]
replaced return value with null for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::lambda$static$2 → KILLED

70

1.1
Location : getHeaders
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:getHeaders_returns_the_expected_header_list()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::getHeaders → KILLED

74

1.1
Location : matchesHeaders
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:returns_null_when_header_count_is_too_few()]
negated conditional → KILLED

75

1.1
Location : matchesHeaders
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:returns_null_when_header_count_is_too_few()]
replaced boolean return with true for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::matchesHeaders → KILLED

77

1.1
Location : matchesHeaders
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:detects_generic_format()]
changed conditional boundary → KILLED

2.2
Location : matchesHeaders
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:returns_null_when_header_count_is_too_many()]
negated conditional → KILLED

78

1.1
Location : matchesHeaders
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:detects_generic_format()]
negated conditional → KILLED

79

1.1
Location : matchesHeaders
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:returns_null_when_header_count_is_too_many()]
replaced boolean return with true for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::matchesHeaders → KILLED

82

1.1
Location : matchesHeaders
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:detects_generic_format()]
replaced boolean return with false for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::matchesHeaders → KILLED

86

1.1
Location : toStudent
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:generic_canonicalizes_umail_email()]
replaced return value with null for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::toStudent → KILLED

95

1.1
Location : detect
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:returns_null_when_header_count_is_too_few()]
negated conditional → KILLED

96

1.1
Location : detect
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:detects_generic_format()]
replaced return value with null for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::detect → KILLED

111

1.1
Location : lastNameFromFullName
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:chico_state_roster_maps_row_to_student()]
negated conditional → KILLED

112

1.1
Location : lastNameFromFullName
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:chico_state_roster_handles_a_name_with_no_spaces()]
replaced return value with "" for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::lastNameFromFullName → KILLED

114

1.1
Location : lastNameFromFullName
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:chico_state_roster_maps_row_to_student()]
Replaced integer addition with subtraction → KILLED

2.2
Location : lastNameFromFullName
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:chico_state_roster_maps_row_to_student()]
replaced return value with "" for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::lastNameFromFullName → KILLED

126

1.1
Location : firstNameFromFullName
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:chico_state_roster_handles_a_name_with_no_spaces()]
negated conditional → KILLED

129

1.1
Location : firstNameFromFullName
Killed by : edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.happiercows.helpers.StudentCsvFormatTests]/[method:chico_state_roster_maps_row_to_student()]
replaced return value with "" for edu/ucsb/cs156/happiercows/helpers/StudentCsvFormat::firstNameFromFullName → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1