ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import edu.ucsb.cs156.example.entities.Articles;
5
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.example.repositories.ArticlesRepository;
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 = "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 a Articles
51
   */
52
  @Operation(summary = "Get a single article")
53
  @PreAuthorize("hasRole('ROLE_USER')")
54
  @GetMapping("")
55
  public Articles getById(@Parameter(name = "id") @RequestParam Long id) {
56
    Articles article =
57
        articlesRepository
58
            .findById(id)
59 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));
60
61 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
    return article;
62
  }
63
64
  @Operation(summary = "Create a new article")
65
  @PreAuthorize("hasRole('ROLE_ADMIN')")
66
  @PostMapping("/post")
67
  public Articles postArticle(
68
      @Parameter(name = "title") @RequestParam String title,
69
      @Parameter(name = "url") @RequestParam String url,
70
      @Parameter(name = "explanation") @RequestParam String explanation,
71
      @Parameter(name = "email") @RequestParam String email,
72
      @Parameter(
73
              name = "localDateTime",
74
              description =
75
                  "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)")
76
          @RequestParam("localDateTime")
77
          @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
78
          LocalDateTime localDateTime)
79
      throws JsonProcessingException {
80
81
    // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
82
    // See: https://www.baeldung.com/spring-date-parameters
83
84
    log.info("localDateTime={}", localDateTime);
85
86
    Articles article = new Articles();
87 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    article.setTitle(title);
88 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    article.setUrl(url);
89 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    article.setExplanation(explanation);
90 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    article.setEmail(email);
91 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setLocalDateTime → KILLED
    article.setLocalDateTime(localDateTime);
92
93 1 1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED
    return articlesRepository.save(article);
94
  }
95
96
  /**
97
   * Delete a Article
98
   *
99
   * @param id the id of the article to delete
100
   * @return a message indicating the article was deleted
101
   */
102
  @Operation(summary = "Delete an Article")
103
  @PreAuthorize("hasRole('ROLE_ADMIN')")
104
  @DeleteMapping("")
105
  public Object deleteArticles(@Parameter(name = "id") @RequestParam Long id) {
106
    Articles article =
107
        articlesRepository
108
            .findById(id)
109 1 1. lambda$deleteArticles$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticles$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
110
111 1 1. deleteArticles : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
    articlesRepository.delete(article);
112 1 1. deleteArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticles → KILLED
    return genericMessage("Articles with id %s deleted".formatted(id));
113
  }
114
115
  /**
116
   * Update a single article
117
   *
118
   * @param id id of the article to update
119
   * @param incoming the new article
120
   * @return the updated article object
121
   */
122
  @Operation(summary = "Update a single article")
123
  @PreAuthorize("hasRole('ROLE_ADMIN')")
124
  @PutMapping("")
125
  public Articles updateArticles(
126
      @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Articles incoming) {
127
128
    Articles article =
129
        articlesRepository
130
            .findById(id)
131 1 1. lambda$updateArticles$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticles$2 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
132
133 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    article.setTitle(incoming.getTitle());
134 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    article.setExplanation(incoming.getExplanation());
135 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    article.setEmail(incoming.getEmail());
136 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    article.setUrl(incoming.getUrl());
137 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setLocalDateTime → KILLED
    article.setLocalDateTime(incoming.getLocalDateTime());
138
139
    articlesRepository.save(article);
140
141 1 1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED
    return article;
142
  }
143
}

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

59

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

61

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

87

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

89

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/Articles::setExplanation → KILLED

90

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/Articles::setEmail → KILLED

91

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

93

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

109

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

111

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

112

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

131

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

133

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

134

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

135

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

136

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

137

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_articles()]
removed call to edu/ucsb/cs156/example/entities/Articles::setLocalDateTime → KILLED

141

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

Active mutators

Tests examined


Report generated by PIT 1.17.0