ArticleController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import edu.ucsb.cs156.example.entities.Article;
5
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.example.repositories.ArticleRepository;
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 ArticleController extends ApiController {
31
32
  @Autowired ArticleRepository articleRepository;
33
34
  /**
35
   * List all articles
36
   *
37
   * @return an iterable of Article
38
   */
39
  @Operation(summary = "List all articles")
40
  @PreAuthorize("hasRole('ROLE_USER')")
41
  @GetMapping("/all")
42
  public Iterable<Article> allArticles() {
43
    Iterable<Article> articles = articleRepository.findAll();
44 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticleController::allArticles → KILLED
    return articles;
45
  }
46
47
  /**
48
   * Get a single article by id
49
   *
50
   * @param id the id of the article
51
   * @return an Article
52
   */
53
  @Operation(summary = "Get a single article")
54
  @PreAuthorize("hasRole('ROLE_USER')")
55
  @GetMapping("")
56
  public Article getById(@Parameter(name = "id") @RequestParam Long id) {
57
    Article article =
58
        articleRepository
59
            .findById(id)
60 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::lambda$getById$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
61
62 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::getById → KILLED
    return article;
63
  }
64
65
  /**
66
   * Create a new article
67
   *
68
   * @param title the title of the article
69
   * @param url the URL of the article
70
   * @param explanation the explanation of the article
71
   * @param email the email associated with the article
72
   * @param dateAdded the date the article was added
73
   * @return the saved article
74
   */
75
  @Operation(summary = "Create a new article")
76
  @PreAuthorize("hasRole('ROLE_ADMIN')")
77
  @PostMapping("/post")
78
  public Article postArticle(
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 added (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
    log.info("dateAdded={}", dateAdded);
93
94
    Article article = new Article();
95 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
    article.setTitle(title);
96 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
    article.setUrl(url);
97 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
    article.setExplanation(explanation);
98 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
    article.setEmail(email);
99 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
    article.setDateAdded(dateAdded);
100
101
    Article savedArticle = articleRepository.save(article);
102
103 1 1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::postArticle → KILLED
    return savedArticle;
104
  }
105
106
  /**
107
   * Delete an Article
108
   *
109
   * @param id the id of the article to delete
110
   * @return a message indicating the article was deleted
111
   */
112
  @Operation(summary = "Delete an Article")
113
  @PreAuthorize("hasRole('ROLE_ADMIN')")
114
  @DeleteMapping("")
115
  public Object deleteArticle(@Parameter(name = "id") @RequestParam Long id) {
116
    Article article =
117
        articleRepository
118
            .findById(id)
119 1 1. lambda$deleteArticle$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::lambda$deleteArticle$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
120
121 1 1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticleRepository::delete → KILLED
    articleRepository.delete(article);
122 1 1. deleteArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::deleteArticle → KILLED
    return genericMessage("Article with id %s deleted".formatted(id));
123
  }
124
125
  /**
126
   * Update a single article
127
   *
128
   * @param id id of the article to update
129
   * @param incoming the new article
130
   * @return the updated article object
131
   */
132
  @Operation(summary = "Update a single article")
133
  @PreAuthorize("hasRole('ROLE_ADMIN')")
134
  @PutMapping("")
135
  public Article updateArticle(
136
      @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Article incoming) {
137
138
    Article article =
139
        articleRepository
140
            .findById(id)
141 1 1. lambda$updateArticle$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::lambda$updateArticle$2 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
142
143 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
    article.setTitle(incoming.getTitle());
144 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
    article.setUrl(incoming.getUrl());
145 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
    article.setExplanation(incoming.getExplanation());
146 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
    article.setEmail(incoming.getEmail());
147 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
    article.setDateAdded(incoming.getDateAdded());
148
149
    articleRepository.save(article);
150
151 1 1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::updateArticle → KILLED
    return article;
152
  }
153
}

Mutations

44

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/ArticleController::allArticles → 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/ArticleController::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/ArticleController::getById → KILLED

95

1.1
Location : postArticle
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/Article::setTitle → KILLED

96

1.1
Location : postArticle
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/Article::setUrl → KILLED

97

1.1
Location : postArticle
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/Article::setExplanation → KILLED

98

1.1
Location : postArticle
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/Article::setEmail → KILLED

99

1.1
Location : postArticle
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/Article::setDateAdded → KILLED

103

1.1
Location : postArticle
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/ArticleController::postArticle → KILLED

119

1.1
Location : lambda$deleteArticle$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_article_and_gets_right_error_message()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::lambda$deleteArticle$1 → KILLED

121

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

122

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

141

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

143

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

144

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

145

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

146

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

147

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

151

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

Active mutators

Tests examined


Report generated by PIT 1.17.0