ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.Article;
4
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.example.repositories.ArticleRepository;
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
/** 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 ArticleRepository articleRepository;
32
33
  /** List all articles. */
34
  @Operation(summary = "List all articles")
35
  @PreAuthorize("hasRole('ROLE_USER')")
36
  @GetMapping("/all")
37
  public Iterable<Article> allArticles() {
38
    Iterable<Article> articles = articleRepository.findAll();
39 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED
    return articles;
40
  }
41
42
  /**
43
   * Get a single article by id.
44
   *
45
   * @param id the id of the article
46
   * @return the article
47
   */
48
  @Operation(summary = "Get a single article")
49
  @PreAuthorize("hasRole('ROLE_USER')")
50
  @GetMapping("")
51
  public Article getById(@Parameter(name = "id") @RequestParam Long id) {
52
    Article article =
53
        articleRepository
54
            .findById(id)
55 1 1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$getById$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
56
57 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
    return article;
58
  }
59
60
  /**
61
   * Update an existing article.
62
   *
63
   * @param id the id of the article to update
64
   * @param incoming the updated article data
65
   * @return the updated article
66
   */
67
  @Operation(summary = "Update an article")
68
  @PreAuthorize("hasRole('ROLE_ADMIN')")
69
  @PutMapping("")
70
  public Article updateArticle(
71
      @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Article incoming) {
72
73
    Article article =
74
        articleRepository
75
            .findById(id)
76 1 1. lambda$updateArticle$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticle$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
77
78 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
    article.setTitle(incoming.getTitle());
79 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
    article.setUrl(incoming.getUrl());
80 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
    article.setExplanation(incoming.getExplanation());
81 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
    article.setEmail(incoming.getEmail());
82 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
    article.setDateAdded(incoming.getDateAdded());
83
84
    articleRepository.save(article);
85
86 1 1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED
    return article;
87
  }
88
89
  /**
90
   * Delete an article.
91
   *
92
   * @param id the id of the article to delete
93
   * @return a message indicating the article was deleted
94
   */
95
  @Operation(summary = "Delete an article")
96
  @PreAuthorize("hasRole('ROLE_ADMIN')")
97
  @DeleteMapping("")
98
  public Object deleteArticle(
99
      @Parameter(name = "id", description = "id of the article to delete") @RequestParam Long id) {
100
    Article article =
101
        articleRepository
102
            .findById(id)
103 1 1. lambda$deleteArticle$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticle$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
104
105 1 1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticleRepository::delete → KILLED
    articleRepository.delete(article);
106 1 1. deleteArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticle → KILLED
    return genericMessage("Article with id %s deleted".formatted(id));
107
  }
108
109
  /** Create a new article. */
110
  @Operation(summary = "Create a new article")
111
  @PreAuthorize("hasRole('ROLE_ADMIN')")
112
  @PostMapping("/post")
113
  public Article postArticle(
114
      @Parameter(name = "title") @RequestParam String title,
115
      @Parameter(name = "url") @RequestParam String url,
116
      @Parameter(name = "explanation") @RequestParam String explanation,
117
      @Parameter(name = "email") @RequestParam String email,
118
      @Parameter(name = "dateAdded", description = "Date added in ISO format (YYYY-MM-DDTHH:MM:SS)")
119
          @RequestParam("dateAdded")
120
          @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
121
          LocalDateTime dateAdded) {
122
123
    Article article = new Article();
124 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
    article.setTitle(title);
125 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
    article.setUrl(url);
126 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
    article.setExplanation(explanation);
127 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
    article.setEmail(email);
128 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
    article.setDateAdded(dateAdded);
129
130
    Article savedArticle = articleRepository.save(article);
131
132 1 1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED
    return savedArticle;
133
  }
134
}

Mutations

39

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

55

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

57

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

76

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

78

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

79

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

80

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

81

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

82

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

86

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

103

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

105

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

106

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/ArticlesController::deleteArticle → KILLED

124

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

125

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

126

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

127

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

128

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

132

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/ArticlesController::postArticle → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support