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 course with related lectures and sections")
67
  @PreAuthorize("hasRole('ROLE_USER')")
68
  @DeleteMapping("/delete")
69
  public Object deleteSchedule(
70
      @Parameter(name = "psId") @RequestParam Long psId,
71
      @Parameter(name = "enrollCd") @RequestParam String enrollCd)
72
      throws JsonProcessingException {
73
    User currentUser = getCurrentUser().getUser();
74
    PersonalSchedule ps =
75
        personalScheduleRepository
76
            .findByIdAndUser(psId, currentUser)
77 1 1. lambda$deleteSchedule$1 : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSectionsController::lambda$deleteSchedule$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(PersonalSchedule.class, psId));
78
79
    Iterable<PSCourse> courses = coursesRepository.findAllByPsId(psId);
80
    ArrayList<String> relatedEnrollCodes = new ArrayList<>();
81
    String body = ucsbCurriculumService.getAllSections(enrollCd, ps.getQuarter());
82
83 1 1. deleteSchedule : negated conditional → KILLED
    if (!body.equals("{\"error\": \"401: Unauthorized\"}")
84 1 1. deleteSchedule : 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
93
    boolean courseExist = false;
94
    for (PSCourse course : courses) {
95 1 1. deleteSchedule : negated conditional → KILLED
      if (relatedEnrollCodes.contains(course.getEnrollCd())) {
96
        courseExist = true;
97 1 1. deleteSchedule : removed call to edu/ucsb/cs156/courses/repositories/PSCourseRepository::delete → KILLED
        coursesRepository.delete(course);
98
      }
99
    }
100 1 1. deleteSchedule : negated conditional → KILLED
    if (!courseExist) {
101
      throw new EntityNotFoundException(PSCourse.class, "psId: " + psId + " enrollCd: " + enrollCd);
102
    }
103
104 1 1. deleteSchedule : replaced return value with null for edu/ucsb/cs156/courses/controllers/PersonalSectionsController::deleteSchedule → KILLED
    return genericMessage(
105
        "Personal Schedule with psId %s and lectures and affiliated sections with enrollCd %s deleted"
106
            .formatted(psId, enrollCd));
107
  }
108
}

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

77

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

83

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

84

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

95

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

97

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

100

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

104

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

Active mutators

Tests examined


Report generated by PIT 1.7.3