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
@Tag(name = "Article")
26
@RequestMapping("/api/article")
27
@RestController
28
@Slf4j
29
public class ArticleController extends ApiController {
30
  // Autowired
31
  // Springboot auto invoke the constructor
32
  // also auto-switch between localhost and production environment
33
  @Autowired ArticleRepository articleRepository;
34
35
  // #region Create
36
  @Operation(summary = "Create a new article")
37
  @PreAuthorize("hasRole('ROLE_ADMIN')")
38
  @PostMapping("/post")
39
  public Article postArticles(
40
      @Parameter(name = "title") @RequestParam String title,
41
      @Parameter(name = "url") @RequestParam String url,
42
      @Parameter(name = "explanation") @RequestParam String explanation,
43
      @Parameter(name = "email", description = "Author email: should be ...@ucsb.edu") @RequestParam
44
          String email,
45
      @Parameter(
46
              name = "localDateTime",
47
              description =
48
                  "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)")
49
          @RequestParam("localDateTime")
50
          @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
51
          LocalDateTime localDateTime)
52
      throws JsonProcessingException {
53
54
    // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
55
    // See: https://www.baeldung.com/spring-date-parameters
56
57
    log.info("localDateTime={}", localDateTime);
58
59
    Article article = new Article();
60 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
    article.setTitle(title);
61 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
    article.setUrl(url);
62 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
    article.setExplanation(explanation);
63 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
    article.setEmail(email);
64 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
    article.setDateAdded(localDateTime); // might need updating?
65
66
    Article savedArticle = articleRepository.save(article);
67
68 1 1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::postArticles → KILLED
    return savedArticle;
69
  }
70
71
  // #endregion
72
73
  // #region Read
74
  /**
75
   * List all article
76
   *
77
   * @return an iterable of Article
78
   */
79
  @Operation(summary = "List all article")
80
  @PreAuthorize("hasRole('ROLE_USER')")
81
  @GetMapping("/all")
82
  public Iterable<Article> allArticles() {
83
    Iterable<Article> articles = articleRepository.findAll();
84 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticleController::allArticles → KILLED
    return articles;
85
  }
86
87
  /**
88
   * Get a single date by id
89
   *
90
   * @param id the id of the date
91
   * @return an article
92
   */
93
  @Operation(summary = "Get a single article by id")
94
  @PreAuthorize("hasRole('ROLE_USER')")
95
  @GetMapping("")
96
  public Article getById(@Parameter(name = "id") @RequestParam Long id) {
97
    Article article =
98
        articleRepository
99
            .findById(id)
100 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));
101
102 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::getById → KILLED
    return article;
103
  }
104
105
  // #endregion
106
107
  // #region Update
108
  /**
109
   * Update a single date
110
   *
111
   * @param id id of the article to update
112
   * @param incoming the new article
113
   * @return the updated date object
114
   */
115
  @Operation(summary = "Update a single article")
116
  @PreAuthorize("hasRole('ROLE_ADMIN')")
117
  @PutMapping("")
118
  public Article updateArticle(
119
      @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Article incoming) {
120
121
    Article article =
122
        articleRepository
123
            .findById(id)
124 1 1. lambda$updateArticle$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::lambda$updateArticle$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
125
126 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
    article.setTitle(incoming.getTitle());
127 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
    article.setUrl(incoming.getUrl());
128 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
    article.setExplanation(incoming.getExplanation());
129 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
    article.setEmail(incoming.getEmail());
130 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
    article.setDateAdded(incoming.getDateAdded());
131
132
    articleRepository.save(article);
133
134 1 1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::updateArticle → KILLED
    return article;
135
  }
136
137
  // #endregion
138
139
  // #region Delete
140
  /**
141
   * Delete an Article
142
   *
143
   * @param id the id of the article to delete
144
   * @return a message indicating the article was deleted
145
   */
146
  @Operation(summary = "Delete an Article")
147
  @PreAuthorize("hasRole('ROLE_ADMIN')")
148
  @DeleteMapping("")
149
  public Object deleteArticle(@Parameter(name = "id") @RequestParam Long id) {
150
    Article article =
151
        articleRepository
152
            .findById(id)
153 1 1. lambda$deleteArticle$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticleController::lambda$deleteArticle$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
154
155 1 1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticleRepository::delete → KILLED
    articleRepository.delete(article);
156 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));
157
  }
158
  // #endregion
159
}

Mutations

60

1.1
Location : postArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:an_admin_user_can_post_a_new_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED

61

1.1
Location : postArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:an_admin_user_can_post_a_new_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED

62

1.1
Location : postArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:an_admin_user_can_post_a_new_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED

63

1.1
Location : postArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:an_admin_user_can_post_a_new_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED

64

1.1
Location : postArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:an_admin_user_can_post_a_new_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED

68

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

84

1.1
Location : allArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:logged_in_user_can_get_all_articles()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticleController::allArticles → KILLED

100

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

102

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

124

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

126

1.1
Location : updateArticle
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:admin_can_edit_an_existing_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED

127

1.1
Location : updateArticle
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:admin_can_edit_an_existing_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED

128

1.1
Location : updateArticle
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:admin_can_edit_an_existing_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED

129

1.1
Location : updateArticle
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:admin_can_edit_an_existing_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED

130

1.1
Location : updateArticle
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[method:admin_can_edit_an_existing_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED

134

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

153

1.1
Location : lambda$deleteArticle$0
Killed by : edu.ucsb.cs156.example.controllers.ArticleControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticleControllerTests]/[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$0 → KILLED

155

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

156

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

Active mutators

Tests examined


Report generated by PIT 1.30.0 support