ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.Articles;
4
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.example.repositories.ArticlesRepository;
6
import io.swagger.v3.oas.annotations.Operation;
7
import io.swagger.v3.oas.annotations.Parameter;
8
import io.swagger.v3.oas.annotations.tags.Tag;
9
import jakarta.validation.Valid;
10
import java.time.LocalDateTime;
11
import lombok.extern.slf4j.Slf4j;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.format.annotation.DateTimeFormat;
14
import org.springframework.security.access.prepost.PreAuthorize;
15
import org.springframework.web.bind.annotation.DeleteMapping;
16
import org.springframework.web.bind.annotation.GetMapping;
17
import org.springframework.web.bind.annotation.PostMapping;
18
import org.springframework.web.bind.annotation.PutMapping;
19
import org.springframework.web.bind.annotation.RequestBody;
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
24
/** This is a REST controller for Articles. */
25
@Tag(name = "Articles")
26
@RequestMapping("/api/Articles")
27
@RestController
28
@Slf4j
29
public class ArticlesController extends ApiController {
30
31
  @Autowired ArticlesRepository articlesRepository;
32
33
  /**
34
   * List all articles.
35
   *
36
   * @return an iterable of Articles
37
   */
38
  @Operation(summary = "List all articles")
39
  @PreAuthorize("hasRole('ROLE_USER')")
40
  @GetMapping("/all")
41
  public Iterable<Articles> allArticles() {
42
    Iterable<Articles> articles = articlesRepository.findAll();
43 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED
    return articles;
44
  }
45
46
  /**
47
   * Get a single article by id.
48
   *
49
   * @param id the id of the article
50
   * @return the article with the given id
51
   * @throws EntityNotFoundException if no article has the given id
52
   */
53
  @Operation(
54
      summary = "Get a single article by id",
55
      description = "Returns the article with the given id, or HTTP 404 if it does not exist.")
56
  @PreAuthorize("hasRole('ROLE_USER')")
57
  @GetMapping("")
58
  public Articles getById(
59
      @Parameter(name = "id", description = "ID of the article to retrieve", example = "123")
60
          @RequestParam
61
          Long id) {
62
    Articles articles =
63
        articlesRepository
64
            .findById(id)
65 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));
66
67 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
    return articles;
68
  }
69
70
  /**
71
   * Create a new article. Accessible only to administrators.
72
   *
73
   * @param title the article title
74
   * @param url the URL of the article
75
   * @param explanation a description of the article
76
   * @param email the email of the person who submitted the article
77
   * @param dateAdded the date and time the article was added
78
   * @return the saved article
79
   */
80
  @Operation(summary = "Create a new article")
81
  @PreAuthorize("hasRole('ROLE_ADMIN')")
82
  @PostMapping("/post")
83
  public Articles postArticles(
84
      @Parameter(name = "title", description = "Article title") @RequestParam String title,
85
      @Parameter(name = "url", description = "URL of the article") @RequestParam String url,
86
      @Parameter(name = "explanation", description = "Description of the article") @RequestParam
87
          String explanation,
88
      @Parameter(name = "email", description = "Email of the person who submitted the article")
89
          @RequestParam
90
          String email,
91
      @Parameter(
92
              name = "dateAdded",
93
              description = "Date added in ISO format, e.g. 2026-10-21T12:00:00")
94
          @RequestParam("dateAdded")
95
          @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
96
          LocalDateTime dateAdded) {
97
98
    Articles articles = new Articles();
99 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    articles.setTitle(title);
100 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    articles.setUrl(url);
101 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    articles.setExplanation(explanation);
102 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    articles.setEmail(email);
103 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
    articles.setDateAdded(dateAdded);
104
105
    Articles savedArticles = articlesRepository.save(articles);
106
107 1 1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED
    return savedArticles;
108
  }
109
110
  /**
111
   * Delete a single article. Accessible only to administrators.
112
   *
113
   * @param id the id of the article to delete
114
   * @return a message indicating the record was deleted
115
   * @throws EntityNotFoundException if no article has the given id
116
   */
117
  @Operation(
118
      summary = "Delete a single article",
119
      description =
120
          "Deletes the article with the given id and returns a message such as record 123 deleted. "
121
              + "Returns HTTP 404 with Articles with id 123 not found if it does not exist. "
122
              + "Requires an administrator.")
123
  @PreAuthorize("hasRole('ROLE_ADMIN')")
124
  @DeleteMapping("")
125
  public Object deleteArticles(
126
      @Parameter(name = "id", description = "ID of the article to delete", example = "123")
127
          @RequestParam
128
          Long id) {
129
    Articles articles =
130
        articlesRepository
131
            .findById(id)
132 1 1. lambda$deleteArticles$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticles$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
133
134 1 1. deleteArticles : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
    articlesRepository.delete(articles);
135 1 1. deleteArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticles → KILLED
    return genericMessage("record %s deleted".formatted(id));
136
  }
137
138
  /**
139
   * Update a single article. Accessible only to administrators.
140
   *
141
   * @param id the id of the article to update
142
   * @param incoming the new values for the article fields
143
   * @return the updated article
144
   * @throws EntityNotFoundException if no article has the given id
145
   */
146
  @Operation(
147
      summary = "Update a single article",
148
      description =
149
          "Updates title, url, explanation, email, and dateAdded from the JSON body. "
150
              + "The id is preserved; any id in the body is ignored. "
151
              + "Use ISO format for dateAdded, e.g. 2026-10-21T12:00:00. "
152
              + "Returns HTTP 404 if the article does not exist. Requires an administrator.")
153
  @PreAuthorize("hasRole('ROLE_ADMIN')")
154
  @PutMapping("")
155
  public Articles updateArticles(
156
      @Parameter(name = "id", description = "ID of the article to update", example = "123")
157
          @RequestParam
158
          Long id,
159
      @RequestBody @Valid Articles incoming) {
160
161
    Articles articles =
162
        articlesRepository
163
            .findById(id)
164 1 1. lambda$updateArticles$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticles$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
165
166 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    articles.setTitle(incoming.getTitle());
167 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    articles.setUrl(incoming.getUrl());
168 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    articles.setExplanation(incoming.getExplanation());
169 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    articles.setEmail(incoming.getEmail());
170 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
    articles.setDateAdded(incoming.getDateAdded());
171
172
    articlesRepository.save(articles);
173
174 1 1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED
    return articles;
175
  }
176
}

Mutations

43

1.1
Location : allArticles
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_articles()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED

65

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:logged_in_user_gets_not_found_when_the_id_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$getById$0 → KILLED

67

1.1
Location : getById
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method: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

99

1.1
Location : postArticles
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_article()]
removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED

100

1.1
Location : postArticles
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_article()]
removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED

101

1.1
Location : postArticles
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_article()]
removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED

102

1.1
Location : postArticles
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_article()]
removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED

103

1.1
Location : postArticles
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_article()]
removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED

107

1.1
Location : postArticles
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_article()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED

132

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

134

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

135

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

164

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

166

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

167

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

168

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

169

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

170

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

174

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

Active mutators

Tests examined


Report generated by PIT 1.30.0 support