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.GetMapping;
18
import org.springframework.web.bind.annotation.PostMapping;
19
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.PutMapping;
21
import org.springframework.web.bind.annotation.RequestBody;
22
import org.springframework.web.bind.annotation.RequestParam;
23
import org.springframework.web.bind.annotation.DeleteMapping;
24
import org.springframework.web.bind.annotation.PutMapping;
25
26
import org.springframework.web.bind.annotation.RestController;
27
28
import jakarta.validation.Valid;
29
30
import java.time.LocalDateTime;
31
32
/**
33
 * This is a REST controller for Articles
34
 */
35
36
@Tag(name = "Articles")
37
@RequestMapping("/api/articles")
38
@RestController
39
@Slf4j
40
public class ArticlesController extends ApiController {
41
42
    @Autowired
43
    ArticlesRepository articlesRepository;
44
45
    /**
46
     * List all articles
47
     * 
48
     * @return an iterable of Article
49
     */
50
    @Operation(summary = "List all articles")
51
    @PreAuthorize("hasRole('ROLE_USER')")
52
    @GetMapping("/all")
53
    public Iterable<Article> allArticles() {
54
        Iterable<Article> 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
    /**
59
     * Create a new article
60
     * 
61
     * @param title         the title of the article
62
     * @param url           the url of the article
63
     * @param explanation   the explanation of the article
64
     * @param email         the email of the submitter
65
     * @param dateAdded     the date the article was added
66
     * @return the saved article
67
     */
68
    @Operation(summary = "Create a new article")
69
    @PreAuthorize("hasRole('ROLE_ADMIN')")
70
    @PostMapping("/post")
71
    public Article postArticle(
72
            @Parameter(name = "title") @RequestParam String title,
73
            @Parameter(name = "url") @RequestParam String url,
74
            @Parameter(name = "explanation") @RequestParam String explanation,
75
            @Parameter(name = "email") @RequestParam String email,
76
            @Parameter(name = "dateAdded", description = "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS)") 
77
            @RequestParam("dateAdded") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAdded)
78
            throws JsonProcessingException {
79
80
        log.info("dateAdded={}", dateAdded);
81
82
        Article article = new Article();
83 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
        article.setTitle(title);
84 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
        article.setUrl(url);
85 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
        article.setExplanation(explanation);
86 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
        article.setEmail(email);
87 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
        article.setDateAdded(dateAdded);
88
89
        Article savedArticle = articlesRepository.save(article); // <-- fixed here
90
91 1 1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED
        return savedArticle;
92
    }
93
        /**
94
     * Get a single article by id
95
     * 
96
     * @param id the id of the article
97
     * @return the Article, if found
98
     */
99
    @Operation(summary = "Get a single article by id")
100
    @PreAuthorize("hasRole('ROLE_USER')")
101
    @GetMapping("")
102
    public Article getById(
103
        @Parameter(name = "id") @RequestParam Long id) {
104
        Article article = articlesRepository.findById(id)
105 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));
106 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
        return article;
107
    }
108
        /**
109
     * Update a single article
110
     * 
111
     * @param id the id of the article to update
112
     * @param incoming the new values for the article
113
     * @return the updated article
114
     */
115
    @Operation(summary = "Update a single article")
116
    @PreAuthorize("hasRole('ROLE_ADMIN')")
117
    @PutMapping("")
118
    public Article updateArticle(
119
        @Parameter(name="id") @RequestParam Long id,
120
        @RequestBody @Valid Article incoming) {
121
122
        Article article = articlesRepository.findById(id)
123 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));
124
125 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
        article.setTitle(incoming.getTitle());
126 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
        article.setUrl(incoming.getUrl());
127 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
        article.setExplanation(incoming.getExplanation());
128 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
        article.setEmail(incoming.getEmail());
129 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
        article.setDateAdded(incoming.getDateAdded());
130
131
        articlesRepository.save(article);
132
133 1 1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED
        return article;
134
    }
135
        /**
136
     * Delete a single article by id
137
     * 
138
     * @param id the id of the article to delete
139
     * @return a message indicating success or failure
140
     */
141
    @Operation(summary = "Delete a single article by id")
142
    @PreAuthorize("hasRole('ROLE_ADMIN')")
143
    @DeleteMapping("")
144
    public String deleteArticle(
145
        @Parameter(name = "id") @RequestParam Long id) {
146
        
147
        Article article = articlesRepository.findById(id)
148 1 1. lambda$deleteArticle$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticle$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Article.class, id));
149
150 1 1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
        articlesRepository.delete(article);
151 1 1. deleteArticle : replaced return value with "" for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticle → KILLED
        return "record " + id + " deleted";
152
    }
153
154
}

Mutations

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

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::setTitle → 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::setUrl → 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::setExplanation → 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()]
removed call to edu/ucsb/cs156/example/entities/Article::setEmail → 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/Article::setDateAdded → 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()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED

105

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

123

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

125

1.1
Location : updateArticle
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:test_that_logged_in_admin_can_update_an_existing_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setTitle → 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:test_that_logged_in_admin_can_update_an_existing_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setUrl → 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:test_that_logged_in_admin_can_update_an_existing_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → 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:test_that_logged_in_admin_can_update_an_existing_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setEmail → 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:test_that_logged_in_admin_can_update_an_existing_article()]
removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED

133

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

148

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

150

1.1
Location : deleteArticle
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:test_that_logged_in_admin_can_delete_an_existing_article()]
removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED

151

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

Active mutators

Tests examined


Report generated by PIT 1.17.0