PersonalSectionsController.java

1
package edu.ucsb.cs156.courses.controllers;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.JsonNode;
5
import com.fasterxml.jackson.databind.ObjectMapper;
6
import edu.ucsb.cs156.courses.documents.Course;
7
import edu.ucsb.cs156.courses.entities.PSCourse;
8
import edu.ucsb.cs156.courses.entities.PersonalSchedule;
9
import edu.ucsb.cs156.courses.entities.User;
10
import edu.ucsb.cs156.courses.errors.EntityNotFoundException;
11
import edu.ucsb.cs156.courses.repositories.PSCourseRepository;
12
import edu.ucsb.cs156.courses.repositories.PersonalScheduleRepository;
13
import edu.ucsb.cs156.courses.services.UCSBCurriculumService;
14
import io.swagger.v3.oas.annotations.Operation;
15
import io.swagger.v3.oas.annotations.Parameter;
16
import io.swagger.v3.oas.annotations.tags.Tag;
17
import java.util.ArrayList;
18
import java.util.Iterator;
19
import lombok.extern.slf4j.Slf4j;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.security.access.prepost.PreAuthorize;
22
import org.springframework.web.bind.annotation.DeleteMapping;
23
import org.springframework.web.bind.annotation.GetMapping;
24
import org.springframework.web.bind.annotation.RequestMapping;
25
import org.springframework.web.bind.annotation.RequestParam;
26
import org.springframework.web.bind.annotation.RestController;
27
28
@Tag(name = "Personal Sections")
29
@RequestMapping("/api/personalSections")
30
@RestController
31
@Slf4j
32
public class PersonalSectionsController extends ApiController {
33
  @Autowired PersonalScheduleRepository personalScheduleRepository;
34
35
  @Autowired PSCourseRepository coursesRepository;
36
37
  @Autowired private ObjectMapper objectMapper;
38
39
  @Autowired UCSBCurriculumService ucsbCurriculumService;
40
41
  @Operation(summary = "List all sections given a psId")
42
  @PreAuthorize("hasRole('ROLE_USER')")
43
  @GetMapping(value = "/all", produces = "application/json")
44
  public ArrayList<Course> getSectionsByPsId(@Parameter(name = "psId") @RequestParam Long psId)
45
      throws JsonProcessingException {
46
    User us = getCurrentUser().getUser();
47
    PersonalSchedule ps =
48
        personalScheduleRepository
49
            .findByIdAndUser(psId, us)
50 1 1. lambda$getSectionsByPsId$0 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSectionsController::lambda$getSectionsByPsId$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, psId));
51
    ArrayList<Course> sections = new ArrayList<Course>();
52
    ArrayList<String> jsons = new ArrayList<String>();
53
    Iterable<PSCourse> courses = coursesRepository.findAllByPsId(psId);
54
    for (PSCourse crs : courses) {
55
56
      User u = crs.getUser();
57
      String qtr = ps.getQuarter();
58
      String responseBody = ucsbCurriculumService.getJSONbyQtrEnrollCd(qtr, crs.getEnrollCd());
59
      jsons.add(responseBody);
60
      Course course = objectMapper.readValue(responseBody, Course.class);
61
      sections.add(course);
62
    }
63 1 1. getSectionsByPsId : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSectionsController::getSectionsByPsId → KILLED
    return sections;
64
  }
65
66
  @Operation(summary = "Delete a schedule and associated lectures by enroll code and psId")
67
  @PreAuthorize("hasRole('ROLE_USER')")
68
  @DeleteMapping(value = "/delete")
69
  public Object deleteScheduleAndLectures(
70
      @Parameter(name = "psId") @RequestParam Long psId,
71
      @Parameter(name = "enrollCd") @RequestParam String enrollCd)
72
      throws JsonProcessingException {
73
    User currentUser = getCurrentUser().getUser();
74
75
    PersonalSchedule ps =
76
        personalScheduleRepository
77
            .findByIdAndUser(psId, currentUser)
78 1 1. lambda$deleteScheduleAndLectures$1 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSectionsController::lambda$deleteScheduleAndLectures$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, psId));
79
    Iterable<PSCourse> courses = coursesRepository.findAllByPsId(psId);
80
    boolean courseFound = false;
81
    ArrayList<String> relatedEnrollCodes = new ArrayList<>();
82
    String body = ucsbCurriculumService.getAllSections(enrollCd, ps.getQuarter());
83 1 1. deleteScheduleAndLectures : negated conditional → KILLED
    if (!body.equals("{\"error\": \"401: Unauthorized\"}")
84 1 1. deleteScheduleAndLectures : negated conditional → KILLED
        && !body.equals("{\"error\": \"Enroll code doesn't exist in that quarter.\"}")) {
85
      Iterator<JsonNode> it = objectMapper.readTree(body).path("classSections").elements();
86
      while (it.hasNext()) {
87
        JsonNode classSection = it.next();
88
        String sectionEnrollCd = classSection.path("enrollCode").asText();
89
        relatedEnrollCodes.add(sectionEnrollCd);
90
      }
91
    }
92
    for (PSCourse crs : courses) {
93 1 1. deleteScheduleAndLectures : negated conditional → KILLED
      if (relatedEnrollCodes.contains(crs.getEnrollCd())) {
94
        courseFound = true;
95 1 1. deleteScheduleAndLectures : removed call to edu/ucsb/cs156/courses/repositories/PSCourseRepository::delete → KILLED
        coursesRepository.delete(crs);
96
      }
97
    }
98 1 1. deleteScheduleAndLectures : negated conditional → KILLED
    if (!courseFound) {
99
      throw new EntityNotFoundException(
100
          PSCourse.class, "enrollCd: " + enrollCd + " and psId: " + psId);
101
    }
102 1 1. deleteScheduleAndLectures : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSectionsController::deleteScheduleAndLectures → KILLED
    return genericMessage(
103
        "Schedule with psId %s and associated lectures with enrollCd %s deleted"
104
            .formatted(psId, enrollCd));
105
  }
106
}

Mutations

50

1.1
Location : lambda$getSectionsByPsId$0
Killed by : edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests]/[method:api_psid_sections__user_logged_in__no_personal_schedule()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSectionsController::lambda$getSectionsByPsId$0 → KILLED

63

1.1
Location : getSectionsByPsId
Killed by : edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests]/[method:api_psid_sections__user_logged_in__returns_existing_course()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSectionsController::getSectionsByPsId → KILLED

78

1.1
Location : lambda$deleteScheduleAndLectures$1
Killed by : edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests]/[method:api_deleteScheduleAndLectures__user_logged_in__no_such_schedule()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSectionsController::lambda$deleteScheduleAndLectures$1 → KILLED

83

1.1
Location : deleteScheduleAndLectures
Killed by : edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests]/[method:api_deleteScheduleAndLectures__user_logged_in__successful_deletion()]
negated conditional → KILLED

84

1.1
Location : deleteScheduleAndLectures
Killed by : edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests]/[method:api_deleteScheduleAndLectures__user_logged_in__successful_deletion()]
negated conditional → KILLED

93

1.1
Location : deleteScheduleAndLectures
Killed by : edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests]/[method:api_deleteScheduleAndLectures__user_logged_in__unauthorized_error_from_service()]
negated conditional → KILLED

95

1.1
Location : deleteScheduleAndLectures
Killed by : edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests]/[method:api_deleteScheduleAndLectures__user_logged_in__successful_deletion()]
removed call to edu/ucsb/cs156/courses/repositories/PSCourseRepository::delete → KILLED

98

1.1
Location : deleteScheduleAndLectures
Killed by : edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests]/[method:api_deleteScheduleAndLectures__user_logged_in__unauthorized_error_from_service()]
negated conditional → KILLED

102

1.1
Location : deleteScheduleAndLectures
Killed by : edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.controllers.PersonalSectionsControllerTests]/[method:api_deleteScheduleAndLectures__user_logged_in__successful_deletion()]
replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSectionsController::deleteScheduleAndLectures → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3