ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import edu.ucsb.cs156.example.entities.Articles;
5
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.example.repositories.ArticlesRepository;
7
import io.swagger.v3.oas.annotations.Operation;
8
import io.swagger.v3.oas.annotations.Parameter;
9
import io.swagger.v3.oas.annotations.tags.Tag;
10
import jakarta.validation.Valid;
11
import java.time.LocalDateTime;
12
import lombok.extern.slf4j.Slf4j;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.format.annotation.DateTimeFormat;
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.RequestBody;
21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestParam;
23
import org.springframework.web.bind.annotation.RestController;
24
25
/** This is a REST controller for Articles */
26
@Tag(name = "Articles")
27
@RequestMapping("/api/articles")
28
@RestController
29
@Slf4j
30
public class ArticlesController extends ApiController {
31
32
  @Autowired ArticlesRepository articlesRepository;
33
34
  /**
35
   * List all Articles
36
   *
37
   * @return an iterable of Articles
38
   */
39
  @Operation(summary = "List all articles")
40
  @PreAuthorize("hasRole('ROLE_USER')")
41
  @GetMapping("/all")
42
  public Iterable<Articles> allUCSBDates() {
43
    Iterable<Articles> ret = articlesRepository.findAll();
44 1 1. allUCSBDates : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allUCSBDates → KILLED
    return ret;
45
  }
46
47
  /**
48
   * Get a single article by id
49
   *
50
   * @param id the id of the article
51
   * @return a article
52
   */
53
  @Operation(summary = "Get a single article by id")
54
  @PreAuthorize("hasRole('ROLE_USER')")
55
  @GetMapping("")
56
  public Articles getById(@Parameter(name = "id") @RequestParam Long id) {
57
    Articles ret =
58
        articlesRepository
59
            .findById(id)
60 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$getById$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
61
62 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
    return ret;
63
  }
64
65
  /**
66
   * Create a new article
67
   *
68
   * @param title title of article
69
   * @param url url of artucle
70
   * @param explanation brief explanation of the article
71
   * @param email email of author
72
   * @param dateAdded date of the article added
73
   * @return the saved article
74
   */
75
  @Operation(summary = "Create a new article")
76
  @PreAuthorize("hasRole('ROLE_ADMIN')")
77
  @PostMapping("/post")
78
  public Articles postUCSBDate(
79
      @Parameter(name = "title") @RequestParam String title,
80
      @Parameter(name = "url") @RequestParam String url,
81
      @Parameter(name = "explanation") @RequestParam String explanation,
82
      @Parameter(name = "email") @RequestParam String email,
83
      @Parameter(
84
              name = "dateAdded",
85
              description =
86
                  "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)")
87
          @RequestParam("dateAdded")
88
          @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
89
          LocalDateTime dateAdded)
90
      throws JsonProcessingException {
91
92
    // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
93
    // See: https://www.baeldung.com/spring-date-parameters
94
95
    log.info("localDateTime={}", dateAdded);
96
97
    Articles article = new Articles();
98 1 1. postUCSBDate : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    article.setTitle(title);
99 1 1. postUCSBDate : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
    article.setDateAdded(dateAdded);
100 1 1. postUCSBDate : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    article.setExplanation(explanation);
101 1 1. postUCSBDate : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    article.setEmail(email);
102 1 1. postUCSBDate : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    article.setUrl(url);
103
104
    Articles savedArticle = articlesRepository.save(article);
105
106 1 1. postUCSBDate : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postUCSBDate → KILLED
    return savedArticle;
107
  }
108
109
  /**
110
   * Delete a article
111
   *
112
   * @param id the id of the article to delete
113
   * @return a message indicating the article was deleted
114
   */
115
  @Operation(summary = "Delete a article")
116
  @PreAuthorize("hasRole('ROLE_ADMIN')")
117
  @DeleteMapping("")
118
  public Object deleteUCSBDate(@Parameter(name = "id") @RequestParam Long id) {
119
    Articles article =
120
        articlesRepository
121
            .findById(id)
122 1 1. lambda$deleteUCSBDate$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteUCSBDate$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
123
124 1 1. deleteUCSBDate : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
    articlesRepository.delete(article);
125 1 1. deleteUCSBDate : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteUCSBDate → KILLED
    return genericMessage("Article with id %s deleted".formatted(id));
126
  }
127
128
  /**
129
   * Update a single article
130
   *
131
   * @param id id of the article to update
132
   * @param incoming the new article
133
   * @return the updated article
134
   */
135
  @Operation(summary = "Update a single article")
136
  @PreAuthorize("hasRole('ROLE_ADMIN')")
137
  @PutMapping("")
138
  public Articles updateUCSBDate(
139
      @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Articles incoming) {
140
141
    Articles article =
142
        articlesRepository
143
            .findById(id)
144 1 1. lambda$updateUCSBDate$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateUCSBDate$2 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
145
146 1 1. updateUCSBDate : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
    article.setDateAdded(incoming.getDateAdded());
147 1 1. updateUCSBDate : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    article.setEmail(incoming.getEmail());
148 1 1. updateUCSBDate : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    article.setUrl(incoming.getUrl());
149 1 1. updateUCSBDate : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    article.setExplanation(incoming.getExplanation());
150 1 1. updateUCSBDate : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    article.setTitle(incoming.getTitle());
151
152
    articlesRepository.save(article);
153
154 1 1. updateUCSBDate : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateUCSBDate → KILLED
    return article;
155
  }
156
}

Mutations

44

1.1
Location : allUCSBDates
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:logged_in_user_can_get_all_ucsbdates()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allUCSBDates → KILLED

60

1.1
Location : lambda$getById$0
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:test_that_logged_in_user_can_get_by_id_when_the_id_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$getById$0 → KILLED

62

1.1
Location : getById
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:test_that_logged_in_user_can_get_by_id_when_the_id_exists()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED

98

1.1
Location : postUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:an_admin_user_can_post_a_new_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED

99

1.1
Location : postUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:an_admin_user_can_post_a_new_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED

100

1.1
Location : postUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:an_admin_user_can_post_a_new_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED

101

1.1
Location : postUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:an_admin_user_can_post_a_new_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED

102

1.1
Location : postUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:an_admin_user_can_post_a_new_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED

106

1.1
Location : postUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:an_admin_user_can_post_a_new_ucsbdate()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postUCSBDate → KILLED

122

1.1
Location : lambda$deleteUCSBDate$1
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_tries_to_delete_non_existant_ucsbdate_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteUCSBDate$1 → KILLED

124

1.1
Location : deleteUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_delete_a_date()]
removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED

125

1.1
Location : deleteUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_delete_a_date()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteUCSBDate → KILLED

144

1.1
Location : lambda$updateUCSBDate$2
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_cannot_edit_ucsbdate_that_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateUCSBDate$2 → KILLED

146

1.1
Location : updateUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED

147

1.1
Location : updateUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED

148

1.1
Location : updateUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED

149

1.1
Location : updateUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED

150

1.1
Location : updateUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED

154

1.1
Location : updateUCSBDate
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbdate()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateUCSBDate → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0