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 java.time.LocalDateTime;
10
import lombok.extern.slf4j.Slf4j;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.format.annotation.DateTimeFormat;
13
import org.springframework.http.HttpStatus;
14
import org.springframework.http.ResponseEntity;
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 private ArticleRepository articleRepository;
31
32
  @Operation(summary = "List all articles")
33
  @PreAuthorize("hasRole('ROLE_USER')")
34
  @GetMapping("/all")
35
  public Iterable<Article> allArticles() {
36 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED
    return articleRepository.findAll();
37
  }
38
39
  @Operation(summary = "Create a new article")
40
  @PreAuthorize("hasRole('ROLE_ADMIN')")
41
  @PostMapping("/post")
42
  public Article postArticle(
43
      @Parameter(name = "title") @RequestParam String title,
44
      @Parameter(name = "url") @RequestParam String url,
45
      @Parameter(name = "explanation") @RequestParam String explanation,
46
      @Parameter(name = "email") @RequestParam String email,
47
      @Parameter(name = "dateAdded", description = "ISO datetime, e.g. 2025-10-27T13:45:00")
48
          @RequestParam("dateAdded")
49
          @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
50
          LocalDateTime dateAdded) {
51
    Article a = new Article();
52 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
    a.setTitle(title);
53 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
    a.setUrl(url);
54 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
    a.setExplanation(explanation);
55 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
    a.setEmail(email);
56 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
    a.setDateAdded(dateAdded);
57
58 1 1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED
    return articleRepository.save(a);
59
  }
60
61
  @Operation(summary = "Get a single article")
62
  @PreAuthorize("hasRole('ROLE_USER')")
63
  @GetMapping("")
64
  public Article getById(@Parameter(name = "id") @RequestParam Long id) {
65 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
    return articleRepository
66
        .findById(id)
67 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));
68
  }
69
70
  @Operation(summary = "Update a single article")
71
  @PreAuthorize("hasRole('ROLE_ADMIN')")
72
  @PutMapping("")
73
  public Article updateArticleById(
74
      @Parameter(name = "id") @RequestParam Long id, @RequestBody Article incoming) {
75
    Article article =
76
        articleRepository
77
            .findById(id)
78 1 1. lambda$updateArticleById$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticleById$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
79
80 1 1. updateArticleById : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
    article.setTitle(incoming.getTitle());
81 1 1. updateArticleById : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
    article.setUrl(incoming.getUrl());
82 1 1. updateArticleById : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
    article.setExplanation(incoming.getExplanation());
83 1 1. updateArticleById : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
    article.setEmail(incoming.getEmail());
84 1 1. updateArticleById : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
    article.setDateAdded(incoming.getDateAdded());
85
86 1 1. updateArticleById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticleById → KILLED
    return articleRepository.save(article);
87
  }
88
89
  @Operation(summary = "Delete a single article")
90
  @PreAuthorize("hasRole('ROLE_ADMIN')")
91
  @DeleteMapping("")
92
  public ResponseEntity<String> deleteArticleById(@Parameter(name = "id") @RequestParam Long id) {
93
94 1 1. deleteArticleById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticleById → KILLED
    return articleRepository
95
        .findById(id)
96
        .map(
97
            existing -> {
98 1 1. lambda$deleteArticleById$2 : removed call to edu/ucsb/cs156/example/repositories/ArticleRepository::delete → KILLED
              articleRepository.delete(existing);
99 1 1. lambda$deleteArticleById$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticleById$2 → KILLED
              return ResponseEntity.ok("record " + id + " deleted");
100
            })
101
        .orElseGet(
102 1 1. lambda$deleteArticleById$3 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticleById$3 → KILLED
            () -> ResponseEntity.status(HttpStatus.NOT_FOUND).body("record " + id + " not found"));
103
  }
104
}

Mutations

36

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

52

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

53

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

54

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

55

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

56

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

58

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

65

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

67

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

78

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

80

1.1
Location : updateArticleById
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

81

1.1
Location : updateArticleById
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

82

1.1
Location : updateArticleById
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

83

1.1
Location : updateArticleById
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

84

1.1
Location : updateArticleById
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::setDateAdded → KILLED

86

1.1
Location : updateArticleById
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::updateArticleById → KILLED

94

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

98

1.1
Location : lambda$deleteArticleById$2
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_delete_existing_article()]
removed call to edu/ucsb/cs156/example/repositories/ArticleRepository::delete → KILLED

99

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

102

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

Active mutators

Tests examined


Report generated by PIT 1.17.0