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.ArticlesRepository;
6
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 lombok.extern.slf4j.Slf4j;
11
12
import com.fasterxml.jackson.core.JsonProcessingException;
13
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.format.annotation.DateTimeFormat;
16
import org.springframework.security.access.prepost.PreAuthorize;
17
import org.springframework.web.bind.annotation.DeleteMapping;
18
import org.springframework.web.bind.annotation.GetMapping;
19
import org.springframework.web.bind.annotation.PostMapping;
20
import org.springframework.web.bind.annotation.PutMapping;
21
import org.springframework.web.bind.annotation.RequestBody;
22
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestParam;
24
import org.springframework.web.bind.annotation.RestController;
25
26
import jakarta.validation.Valid;
27
28
import java.time.ZonedDateTime;
29
30
/**
31
 * This is a Rest Controller for Articles
32
 */
33
@Tag(name = "Articles")
34
@RequestMapping("/api/articles")
35
@RestController
36
@Slf4j
37
public class ArticlesController extends ApiController {
38
    @Autowired
39
    ArticlesRepository articlesRepository;
40
41
    /**
42
     * List all Articles
43
     * 
44
     * @return an iterable of Article
45
     */
46
    @Operation(summary = "List all Articles")
47
    @PreAuthorize("hasRole('ROLE_USER')")
48
    @GetMapping("/all")
49
    public Iterable<Article> allArticles() {
50
        Iterable<Article> articles = articlesRepository.findAll();
51 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED
        return articles;
52
    }
53
54
    /**
55
     * Create a new Article
56
     * 
57
     * @param title       the title of the article
58
     * @param url         the url of the article
59
     * @param explanation explanation for the article
60
     * @param email       the email of the author
61
     * @param dateAdded   the date
62
     * @return the saved article
63
     */
64
    @Operation(summary = "Create a new article")
65
    @PreAuthorize("hasRole('ROLE_ADMIN')")
66
    @PostMapping("/post")
67
    public Article 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(name = "dateAdded", description = "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SSZ)") @RequestParam("dateAdded") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime dateAdded)
73
            throws JsonProcessingException {
74
75
        // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
76
        // See: https://www.baeldung.com/spring-date-parameters
77
78
        log.info("dateAdded={}", dateAdded);
79
80
        Article article = new Article();
81 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
        article.setTitle(title);
82 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
        article.setUrl(url);
83 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
        article.setExplanation(explanation);
84 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
        article.setEmail(email);
85 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
        article.setDateAdded(dateAdded);
86
87
        Article savedarticle = articlesRepository.save(article);
88
89 1 1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED
        return savedarticle;
90
    }
91
92
    /**
93
     * Get an article by id
94
     * 
95
     * @param id the id of the article
96
     * @return an article
97
     */
98
    @Operation(summary = "Get a single article")
99
    @PreAuthorize("hasRole('ROLE_USER')")
100
    @GetMapping("")
101
    public Article getById(
102
            @Parameter(name = "id") @RequestParam Long id) {
103
        Article article = articlesRepository.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 article
111
     * 
112
     * @param id       id of the article to update
113
     * @param incoming the new article
114
     * @return the updated article 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,
121
            @RequestBody @Valid Article incoming) {
122
123
        Article article = articlesRepository.findById(id)
124 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));
125
126 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
        article.setTitle(incoming.getTitle());
127 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
        article.setUrl(incoming.getUrl());
128 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
        article.setExplanation(incoming.getExplanation());
129 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
        article.setEmail(incoming.getEmail());
130 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
        article.setDateAdded(incoming.getDateAdded());
131
132
        articlesRepository.save(article);
133
134 1 1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED
        return article;
135
    }
136
137
    /**
138
     * Delete an article
139
     * 
140
     * @param id the id of the article to delete
141
     * @return a message indicating the article was deleted
142
     */
143
    @Operation(summary = "Delete an article")
144
    @PreAuthorize("hasRole('ROLE_ADMIN')")
145
    @DeleteMapping("")
146
    public Object deletaArticle(
147
            @Parameter(name = "id") @RequestParam Long id) {
148
        Article article = articlesRepository.findById(id)
149 1 1. lambda$deletaArticle$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deletaArticle$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
150
151 1 1. deletaArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
        articlesRepository.delete(article);
152 1 1. deletaArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deletaArticle → KILLED
        return genericMessage("Article with id %s deleted".formatted(id));
153
    }
154
155
}

Mutations

51

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

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

85

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::setDateAdded → 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()]
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

124

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

126

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

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

134

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

149

1.1
Location : lambda$deletaArticle$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$deletaArticle$2 → KILLED

151

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

152

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

Active mutators

Tests examined


Report generated by PIT 1.17.0