FrontiersStartup.java

1
package edu.ucsb.cs156.frontiers.startup;
2
3
import edu.ucsb.cs156.frontiers.entities.Admin;
4
import edu.ucsb.cs156.frontiers.entities.Course;
5
import edu.ucsb.cs156.frontiers.repositories.AdminRepository;
6
import edu.ucsb.cs156.frontiers.repositories.CourseRepository;
7
import edu.ucsb.cs156.frontiers.services.CourseStaffService;
8
import java.util.List;
9
import lombok.extern.slf4j.Slf4j;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Value;
12
import org.springframework.stereotype.Component;
13
14
/** This class contains a `run` method that is called once at application startup time. */
15
@Slf4j
16
@Component
17
public class FrontiersStartup {
18
19
  @Value("#{'${app.admin.emails}'.split(',')}")
20
  List<String> adminEmails;
21
22
  @Value("${app.webhook.secret}")
23
  String webhookSecret;
24
25
  @Autowired AdminRepository adminRepository;
26
27
  @Autowired CourseRepository courseRepository;
28
29
  @Autowired CourseStaffService courseStaffService;
30
31
  /**
32
   * Called once at application startup time . Put code here if you want it to run once each time
33
   * the Spring Boot application starts up in all environments.
34
   */
35
  public void alwaysRunOnStartup() {
36
    log.info("alwaysRunOnStartup called");
37
38 1 1. alwaysRunOnStartup : removed call to edu/ucsb/cs156/frontiers/startup/FrontiersStartup::validateWebhookSecret → KILLED
    validateWebhookSecret();
39
40
    try {
41 1 1. alwaysRunOnStartup : removed call to java/util/List::forEach → KILLED
      adminEmails.forEach(
42
          (email) -> {
43
            Admin admin = new Admin(email);
44
            adminRepository.save(admin);
45
          });
46
    } catch (Exception e) {
47
      log.error("Error in loading all ADMIN_EMAILS:", e);
48
    }
49
50 1 1. alwaysRunOnStartup : removed call to edu/ucsb/cs156/frontiers/startup/FrontiersStartup::addInstructorsAsStaff → KILLED
    addInstructorsAsStaff();
51
  }
52
53
  /**
54
   * Makes sure the instructor of every course is on that course's staff list, adding them where
55
   * they are missing. Courses without an instructor email are skipped. An error on one course is
56
   * logged and does not stop the others or the application from starting.
57
   */
58
  void addInstructorsAsStaff() {
59
    try {
60
      for (Course course : courseRepository.findAll()) {
61
        String instructorEmail = course.getInstructorEmail();
62 2 1. addInstructorsAsStaff : negated conditional → KILLED
2. addInstructorsAsStaff : negated conditional → KILLED
        if (instructorEmail == null || instructorEmail.isBlank()) {
63
          log.warn(
64
              "Course id {} has no instructor email; not adding an instructor as staff",
65
              course.getId());
66
          continue;
67
        }
68
        try {
69 1 1. addInstructorsAsStaff : removed call to edu/ucsb/cs156/frontiers/services/CourseStaffService::ensureInstructorIsStaff → KILLED
          courseStaffService.ensureInstructorIsStaff(course);
70
        } catch (Exception e) {
71
          log.error("Error adding instructor as staff for course id {}:", course.getId(), e);
72
        }
73
      }
74
    } catch (Exception e) {
75
      log.error("Error adding instructors as staff for all courses:", e);
76
    }
77
  }
78
79
  private void validateWebhookSecret() {
80 3 1. validateWebhookSecret : negated conditional → KILLED
2. validateWebhookSecret : changed conditional boundary → KILLED
3. validateWebhookSecret : negated conditional → KILLED
    if (webhookSecret == null || webhookSecret.length() < 10) {
81
      String error =
82
          String.format(
83
              "WEBHOOK_SECRET must be at least 10 characters long. Current length: %d",
84 1 1. validateWebhookSecret : negated conditional → KILLED
              webhookSecret == null ? 0 : webhookSecret.length());
85
      log.error(error);
86
      throw new IllegalStateException(error);
87
    }
88
    log.info("Webhook secret validation passed");
89
  }
90
}

Mutations

38

1.1
Location : alwaysRunOnStartup
Killed by : edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests]/[method:test_AlwaysRunOnStartup_fails_with_null_webhook_secret()]
removed call to edu/ucsb/cs156/frontiers/startup/FrontiersStartup::validateWebhookSecret → KILLED

41

1.1
Location : alwaysRunOnStartup
Killed by : edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests]/[method:test_AlwaysRunOnStartup_succeeds_with_minimum_length_webhook_secret()]
removed call to java/util/List::forEach → KILLED

50

1.1
Location : alwaysRunOnStartup
Killed by : edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests]/[method:test_AlwaysRunOnStartup_adds_instructors_even_when_saving_admins_fails()]
removed call to edu/ucsb/cs156/frontiers/startup/FrontiersStartup::addInstructorsAsStaff → KILLED

62

1.1
Location : addInstructorsAsStaff
Killed by : edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests]/[method:test_AlwaysRunOnStartup_adds_instructors_even_when_saving_admins_fails()]
negated conditional → KILLED

2.2
Location : addInstructorsAsStaff
Killed by : edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests]/[method:test_AlwaysRunOnStartup_adds_instructors_even_when_saving_admins_fails()]
negated conditional → KILLED

69

1.1
Location : addInstructorsAsStaff
Killed by : edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests]/[method:test_AlwaysRunOnStartup_adds_instructors_even_when_saving_admins_fails()]
removed call to edu/ucsb/cs156/frontiers/services/CourseStaffService::ensureInstructorIsStaff → KILLED

80

1.1
Location : validateWebhookSecret
Killed by : edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests]/[method:test_AlwaysRunOnStartup_fails_with_short_webhook_secret()]
negated conditional → KILLED

2.2
Location : validateWebhookSecret
Killed by : edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests]/[method:test_AlwaysRunOnStartup_succeeds_with_minimum_length_webhook_secret()]
changed conditional boundary → KILLED

3.3
Location : validateWebhookSecret
Killed by : edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests]/[method:test_AlwaysRunOnStartup_fails_with_null_webhook_secret()]
negated conditional → KILLED

84

1.1
Location : validateWebhookSecret
Killed by : edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.startup.FrontiersStartupTests]/[method:test_AlwaysRunOnStartup_fails_with_null_webhook_secret()]
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support