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

Mutations

45

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

47

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

55

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

78

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

79

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

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

86

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

105

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

107

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

108

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

109

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

110

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

111

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

115

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

131

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

133

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/ArticlesRepository::delete → KILLED

134

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

Active mutators

Tests examined


Report generated by PIT 1.17.0