DokkuAccountTranslationsController.java

1
package edu.ucsb.cs156.frontiers.controllers;
2
3
import edu.ucsb.cs156.frontiers.entities.Course;
4
import edu.ucsb.cs156.frontiers.entities.DokkuAccountTranslation;
5
import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.frontiers.repositories.CourseRepository;
7
import edu.ucsb.cs156.frontiers.repositories.DokkuAccountTranslationRepository;
8
import edu.ucsb.cs156.frontiers.utilities.CanonicalFormConverter;
9
import io.swagger.v3.oas.annotations.Operation;
10
import io.swagger.v3.oas.annotations.Parameter;
11
import io.swagger.v3.oas.annotations.tags.Tag;
12
import java.util.List;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.http.HttpStatus;
15
import org.springframework.security.access.prepost.PreAuthorize;
16
import org.springframework.web.bind.annotation.DeleteMapping;
17
import org.springframework.web.bind.annotation.GetMapping;
18
import org.springframework.web.bind.annotation.PostMapping;
19
import org.springframework.web.bind.annotation.PutMapping;
20
import org.springframework.web.bind.annotation.RequestMapping;
21
import org.springframework.web.bind.annotation.RequestParam;
22
import org.springframework.web.bind.annotation.RestController;
23
import org.springframework.web.server.ResponseStatusException;
24
25
/**
26
 * CRUD for {@link DokkuAccountTranslation}: the table of email addresses whose dokku username is
27
 * not simply the part of the email before the {@code @}.
28
 *
29
 * <p>The table is global (not per-course), but it is managed from the Dokku tab of a course, so
30
 * every endpoint takes a {@code courseId} that is used only to check that the caller can manage
31
 * that course.
32
 */
33
@Tag(name = "Dokku")
34
@RequestMapping("/api/dokku/translations")
35
@RestController
36
public class DokkuAccountTranslationsController extends ApiController {
37
38
  @Autowired private DokkuAccountTranslationRepository dokkuAccountTranslationRepository;
39
40
  @Autowired private CourseRepository courseRepository;
41
42
  @Operation(summary = "List all dokku account translations, sorted by email")
43
  @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)")
44
  @GetMapping("")
45
  public List<DokkuAccountTranslation> getTranslations(
46
      @Parameter(name = "courseId") @RequestParam Long courseId) {
47 1 1. getTranslations : removed call to edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::ensureCourseExists → KILLED
    ensureCourseExists(courseId);
48 1 1. getTranslations : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::getTranslations → KILLED
    return dokkuAccountTranslationRepository.findAllByOrderByEmailAsc();
49
  }
50
51
  @Operation(summary = "Create a dokku account translation")
52
  @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)")
53
  @PostMapping("")
54
  public DokkuAccountTranslation postTranslation(
55
      @Parameter(name = "courseId") @RequestParam Long courseId,
56
      @Parameter(name = "email") @RequestParam String email,
57
      @Parameter(name = "username") @RequestParam String username) {
58 1 1. postTranslation : removed call to edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::ensureCourseExists → KILLED
    ensureCourseExists(courseId);
59
    String normalizedEmail = normalizeEmail(email);
60
    String normalizedUsername = normalizeRequired("username", username);
61
62 1 1. postTranslation : negated conditional → KILLED
    if (dokkuAccountTranslationRepository.findById(normalizedEmail).isPresent()) {
63
      throw new ResponseStatusException(
64
          HttpStatus.CONFLICT,
65
          "Dokku account translation for %s already exists".formatted(normalizedEmail));
66
    }
67
68
    DokkuAccountTranslation translation =
69
        DokkuAccountTranslation.builder()
70
            .email(normalizedEmail)
71
            .username(normalizedUsername)
72
            .build();
73 1 1. postTranslation : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::postTranslation → KILLED
    return dokkuAccountTranslationRepository.save(translation);
74
  }
75
76
  @Operation(summary = "Update the username of a dokku account translation")
77
  @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)")
78
  @PutMapping("")
79
  public DokkuAccountTranslation putTranslation(
80
      @Parameter(name = "courseId") @RequestParam Long courseId,
81
      @Parameter(name = "email") @RequestParam String email,
82
      @Parameter(name = "username") @RequestParam String username) {
83 1 1. putTranslation : removed call to edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::ensureCourseExists → KILLED
    ensureCourseExists(courseId);
84
    String normalizedEmail = normalizeEmail(email);
85
    String normalizedUsername = normalizeRequired("username", username);
86
87
    DokkuAccountTranslation existing = findTranslation(normalizedEmail);
88 1 1. putTranslation : removed call to edu/ucsb/cs156/frontiers/entities/DokkuAccountTranslation::setUsername → KILLED
    existing.setUsername(normalizedUsername);
89 1 1. putTranslation : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::putTranslation → KILLED
    return dokkuAccountTranslationRepository.save(existing);
90
  }
91
92
  @Operation(summary = "Delete a dokku account translation")
93
  @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)")
94
  @DeleteMapping("")
95
  public Object deleteTranslation(
96
      @Parameter(name = "courseId") @RequestParam Long courseId,
97
      @Parameter(name = "email") @RequestParam String email) {
98 1 1. deleteTranslation : removed call to edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::ensureCourseExists → KILLED
    ensureCourseExists(courseId);
99
    String normalizedEmail = normalizeEmail(email);
100
101
    DokkuAccountTranslation existing = findTranslation(normalizedEmail);
102 1 1. deleteTranslation : removed call to edu/ucsb/cs156/frontiers/repositories/DokkuAccountTranslationRepository::delete → KILLED
    dokkuAccountTranslationRepository.delete(existing);
103 1 1. deleteTranslation : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::deleteTranslation → KILLED
    return genericMessage("Dokku account translation for %s deleted".formatted(normalizedEmail));
104
  }
105
106
  private void ensureCourseExists(Long courseId) {
107
    courseRepository
108
        .findById(courseId)
109 1 1. lambda$ensureCourseExists$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::lambda$ensureCourseExists$0 → KILLED
        .orElseThrow(() -> new EntityNotFoundException(Course.class, courseId));
110
  }
111
112
  private DokkuAccountTranslation findTranslation(String email) {
113 1 1. findTranslation : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::findTranslation → KILLED
    return dokkuAccountTranslationRepository
114
        .findById(email)
115 1 1. lambda$findTranslation$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::lambda$findTranslation$0 → KILLED
        .orElseThrow(() -> new EntityNotFoundException(DokkuAccountTranslation.class, email));
116
  }
117
118
  /** Strips and canonicalizes an email; a blank email is a 400. */
119
  private String normalizeEmail(String email) {
120 1 1. normalizeEmail : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::normalizeEmail → KILLED
    return CanonicalFormConverter.convertToValidEmail(normalizeRequired("email", email));
121
  }
122
123
  private String normalizeRequired(String fieldName, String value) {
124
    String normalized = value.strip();
125 1 1. normalizeRequired : negated conditional → KILLED
    if (normalized.isEmpty()) {
126
      throw new IllegalArgumentException("%s must not be blank".formatted(fieldName));
127
    }
128 1 1. normalizeRequired : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::normalizeRequired → KILLED
    return normalized;
129
  }
130
}

Mutations

47

1.1
Location : getTranslations
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:get_returns_404_when_course_does_not_exist()]
removed call to edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::ensureCourseExists → KILLED

48

1.1
Location : getTranslations
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:get_returns_all_translations_sorted_by_email()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::getTranslations → KILLED

58

1.1
Location : postTranslation
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:post_returns_404_when_course_does_not_exist()]
removed call to edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::ensureCourseExists → KILLED

62

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

73

1.1
Location : postTranslation
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:post_creates_a_translation_with_canonical_email_and_stripped_fields()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::postTranslation → KILLED

83

1.1
Location : putTranslation
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:put_returns_404_when_course_does_not_exist()]
removed call to edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::ensureCourseExists → KILLED

88

1.1
Location : putTranslation
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:put_updates_the_username_of_an_existing_translation()]
removed call to edu/ucsb/cs156/frontiers/entities/DokkuAccountTranslation::setUsername → KILLED

89

1.1
Location : putTranslation
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:put_updates_the_username_of_an_existing_translation()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::putTranslation → KILLED

98

1.1
Location : deleteTranslation
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:delete_returns_404_when_course_does_not_exist()]
removed call to edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::ensureCourseExists → KILLED

102

1.1
Location : deleteTranslation
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:delete_removes_an_existing_translation()]
removed call to edu/ucsb/cs156/frontiers/repositories/DokkuAccountTranslationRepository::delete → KILLED

103

1.1
Location : deleteTranslation
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:delete_removes_an_existing_translation()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::deleteTranslation → KILLED

109

1.1
Location : lambda$ensureCourseExists$0
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:get_returns_404_when_course_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::lambda$ensureCourseExists$0 → KILLED

113

1.1
Location : findTranslation
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:delete_removes_an_existing_translation()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::findTranslation → KILLED

115

1.1
Location : lambda$findTranslation$0
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:put_returns_404_when_translation_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::lambda$findTranslation$0 → KILLED

120

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

125

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

128

1.1
Location : normalizeRequired
Killed by : edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.controllers.DokkuAccountTranslationsControllerTests]/[method:put_returns_404_when_translation_does_not_exist()]
replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/DokkuAccountTranslationsController::normalizeRequired → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support