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

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

80

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

81

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

82

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

83

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

84

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::setLocalDateTime → KILLED

88

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

104

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

106

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

125

1.1
Location : lambda$updateArticle$1
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$updateArticle$1 → KILLED

127

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

128

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

129

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

130

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

131

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::setLocalDateTime → KILLED

135

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

151

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

153

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_an_article()]
removed call to edu/ucsb/cs156/example/repositories/ArticleRepository::delete → KILLED

154

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

Active mutators

Tests examined


Report generated by PIT 1.17.0