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
/** 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 ArticlesRepository articlesRepository;
33
34
  /**
35
   * List all articles
36
   *
37
   * @return an iterable of Articles
38
   */
39
  @Operation(summary = "List all articles")
40
  @PreAuthorize("hasRole('ROLE_USER')")
41
  @GetMapping("/all")
42
  public Iterable<Articles> allArticles() {
43
    Iterable<Articles> articles = articlesRepository.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 the title of the article
51
   * @param url the url of the article
52
   * @param explanation an explanation of the article
53
   * @param email the email of the person who submitted the article
54
   * @param dateAdded the date the article was added
55
   * @return the saved article
56
   */
57
  @Operation(summary = "Create a new article")
58
  @PreAuthorize("hasRole('ROLE_ADMIN')")
59
  @PostMapping("/post")
60
  public Articles 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 = "dateAdded",
67
              description =
68
                  "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)")
69
          @RequestParam("dateAdded")
70
          @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
71
          LocalDateTime dateAdded)
72
      throws JsonProcessingException {
73
74
    log.info("dateAdded={}", dateAdded);
75
76
    Articles article = new Articles();
77
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::setUrl → KILLED
    article.setUrl(url);
80 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    article.setExplanation(explanation);
81 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    article.setEmail(email);
82 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
    article.setDateAdded(dateAdded);
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
  @Operation(summary = "Get a single article")
90
  @PreAuthorize("hasRole('ROLE_USER')")
91
  @GetMapping("")
92
  public Articles getById(@Parameter(name = "id") @RequestParam Long id) {
93
    Articles articles =
94
        articlesRepository
95
            .findById(id)
96 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));
97
98 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
    return articles;
99
  }
100
101
  @Operation(summary = "Update a single article")
102
  @PreAuthorize("hasRole('ROLE_ADMIN')")
103
  @PutMapping("")
104
  public Articles updateArticles(
105
      @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Articles incoming) {
106
107
    Articles articles =
108
        articlesRepository
109
            .findById(id)
110 1 1. lambda$updateArticles$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticles$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
111
112 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    articles.setTitle(incoming.getTitle());
113 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    articles.setUrl(incoming.getUrl());
114 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    articles.setExplanation(incoming.getExplanation());
115 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    articles.setEmail(incoming.getEmail());
116 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
    articles.setDateAdded(incoming.getDateAdded());
117
118
    articlesRepository.save(articles);
119
120 1 1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED
    return articles;
121
  }
122
123
  @Operation(summary = "Delete an article")
124
  @PreAuthorize("hasRole('ROLE_ADMIN')")
125
  @DeleteMapping("")
126
  public Object deleteUCSBDate(@Parameter(name = "id") @RequestParam Long id) {
127
    Articles article =
128
        articlesRepository
129
            .findById(id)
130 1 1. lambda$deleteUCSBDate$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteUCSBDate$0 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
131
132 1 1. deleteUCSBDate : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
    articlesRepository.delete(article);
133 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));
134
  }
135
}

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

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::setUrl → 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::setExplanation → 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::setEmail → 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

96

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

98

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

110

1.1
Location : lambda$updateArticles$0
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$updateArticles$0 → KILLED

112

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

113

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

114

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

115

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

116

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

120

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

130

1.1
Location : lambda$deleteUCSBDate$0
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$deleteUCSBDate$0 → KILLED

132

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

133

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

Active mutators

Tests examined


Report generated by PIT 1.30.0 support