SectionTranslationService.java

1
package edu.ucsb.cs156.frontiers.services;
2
3
import edu.ucsb.cs156.frontiers.entities.CourseOption;
4
import edu.ucsb.cs156.frontiers.entities.Section;
5
import edu.ucsb.cs156.frontiers.enums.CourseOptions;
6
import edu.ucsb.cs156.frontiers.repositories.CourseOptionRepository;
7
import edu.ucsb.cs156.frontiers.repositories.SectionRepository;
8
import java.util.HashMap;
9
import java.util.Map;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.stereotype.Service;
12
13
/**
14
 * Centralizes the logic for checking whether the TRANSLATE_SECTIONS course option is enabled, and
15
 * for translating a raw roster student section value into its human readable label. This is the
16
 * single place where this logic lives, so that it behaves consistently everywhere a section value
17
 * is displayed or exported (e.g. the CATME CSV export, the roster student tables).
18
 */
19
@Service
20
public class SectionTranslationService {
21
22
  @Autowired private CourseOptionRepository courseOptionRepository;
23
24
  @Autowired private SectionRepository sectionRepository;
25
26
  /**
27
   * Returns the map from raw section value to label for the course, if the TRANSLATE_SECTIONS
28
   * course option is enabled; otherwise an empty map (raw values are used as-is).
29
   *
30
   * @param courseId the id of the course
31
   * @return map of raw section value to translated label
32
   */
33
  public Map<String, String> getSectionTranslations(Long courseId) {
34
    Map<String, String> translations = new HashMap<>();
35 1 1. getSectionTranslations : negated conditional → KILLED
    if (isTranslateSectionsEnabled(courseId)) {
36
      for (Section section : sectionRepository.findByCourseId(courseId)) {
37
        translations.put(section.getSection(), section.getLabel());
38
      }
39
    }
40 1 1. getSectionTranslations : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/services/SectionTranslationService::getSectionTranslations → KILLED
    return translations;
41
  }
42
43
  /**
44
   * Returns whether the TRANSLATE_SECTIONS course option is enabled for the given course.
45
   *
46
   * @param courseId the id of the course
47
   * @return true if the TRANSLATE_SECTIONS course option is enabled, false otherwise
48
   */
49
  public boolean isTranslateSectionsEnabled(Long courseId) {
50 2 1. isTranslateSectionsEnabled : replaced boolean return with false for edu/ucsb/cs156/frontiers/services/SectionTranslationService::isTranslateSectionsEnabled → KILLED
2. isTranslateSectionsEnabled : replaced boolean return with true for edu/ucsb/cs156/frontiers/services/SectionTranslationService::isTranslateSectionsEnabled → KILLED
    return courseOptionRepository
51
        .findByCourseIdAndOption(courseId, CourseOptions.TRANSLATE_SECTIONS.name())
52
        .map(CourseOption::getEnabled)
53
        .orElse(false);
54
  }
55
56
  /**
57
   * Translates a raw roster student section value using the given map. A null section is treated as
58
   * blank; a section with no translation is returned unchanged.
59
   *
60
   * @param rawSection the section value from the roster student row (may be null)
61
   * @param translations map of raw section value to label
62
   * @return the translated section value
63
   */
64
  public static String translateSection(String rawSection, Map<String, String> translations) {
65 1 1. translateSection : negated conditional → KILLED
    String section = rawSection == null ? "" : rawSection;
66 1 1. translateSection : replaced return value with "" for edu/ucsb/cs156/frontiers/services/SectionTranslationService::translateSection → KILLED
    return translations.getOrDefault(section, section);
67
  }
68
}

Mutations

35

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

40

1.1
Location : getSectionTranslations
Killed by : edu.ucsb.cs156.frontiers.controllers.CSVDownloadsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CSVDownloadsControllerTests]/[method:test_catme_csv_translates_sections_when_option_enabled()]
replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/services/SectionTranslationService::getSectionTranslations → KILLED

50

1.1
Location : isTranslateSectionsEnabled
Killed by : edu.ucsb.cs156.frontiers.controllers.CSVDownloadsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CSVDownloadsControllerTests]/[method:test_catme_csv_translates_sections_when_option_enabled()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/services/SectionTranslationService::isTranslateSectionsEnabled → KILLED

2.2
Location : isTranslateSectionsEnabled
Killed by : edu.ucsb.cs156.frontiers.controllers.CSVDownloadsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CSVDownloadsControllerTests]/[method:test_catme_csv_uses_raw_sections_when_option_explicitly_disabled()]
replaced boolean return with true for edu/ucsb/cs156/frontiers/services/SectionTranslationService::isTranslateSectionsEnabled → KILLED

65

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

66

1.1
Location : translateSection
Killed by : edu.ucsb.cs156.frontiers.controllers.CSVDownloadsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.CSVDownloadsControllerTests]/[method:translateSection_handles_null_blank_missing_and_present()]
replaced return value with "" for edu/ucsb/cs156/frontiers/services/SectionTranslationService::translateSection → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support