ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.Article;
4
import edu.ucsb.cs156.example.entities.UCSBDate;
5
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.example.repositories.ArticleRepository;
7
import edu.ucsb.cs156.example.repositories.UCSBDateRepository;
8
9
import io.swagger.v3.oas.annotations.Operation;
10
import io.swagger.v3.oas.annotations.Parameter;
11
import io.swagger.v3.oas.annotations.tags.Tag;
12
import lombok.extern.slf4j.Slf4j;
13
14
import com.fasterxml.jackson.core.JsonProcessingException;
15
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.format.annotation.DateTimeFormat;
18
import org.springframework.security.access.prepost.PreAuthorize;
19
import org.springframework.web.bind.annotation.DeleteMapping;
20
import org.springframework.web.bind.annotation.GetMapping;
21
import org.springframework.web.bind.annotation.PostMapping;
22
import org.springframework.web.bind.annotation.PutMapping;
23
import org.springframework.web.bind.annotation.RequestBody;
24
import org.springframework.web.bind.annotation.RequestMapping;
25
import org.springframework.web.bind.annotation.RequestParam;
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
    ArticleRepository articleRepository;
44
45
    /**
46
     * List all Articles
47
     * 
48
     * @return an iterable of Articles
49
     */
50
    @Operation(summary = "List all articles")
51
    @PreAuthorize("hasRole('ROLE_USER')")
52
    @GetMapping("/all")
53
    public Iterable<Article> allArticles() {
54
        Iterable<Article> articles = articleRepository.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
     * Get a single article by id
60
     * 
61
     * @param id the id of the date
62
     * @return a Article
63
     */
64
    @Operation(summary = "Get a single article")
65
    @PreAuthorize("hasRole('ROLE_USER')")
66
    @GetMapping("")
67
    public Article getById(
68
            @Parameter(name = "id") @RequestParam Long id) {
69
        Article article = articleRepository.findById(id)
70 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));
71
72 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
        return article;
73
    }
74
75
    /**
76
     * Create a new article
77
     * 
78
     * @param title       the article title
79
     * @param url         the article url
80
     * @param explanation the explanation
81
     * @param email       the associated email
82
     * @param dateAdded   the date added
83
     * @return the saved article
84
     */
85
    @Operation(summary = "Create a new article")
86
    @PreAuthorize("hasRole('ROLE_ADMIN')")
87
    @PostMapping("/post")
88
    public Article postArticle(
89
            @Parameter(name = "title") @RequestParam String title,
90
            @Parameter(name = "url") @RequestParam String url,
91
            @Parameter(name = "explanation") @RequestParam String explanation,
92
            @Parameter(name = "email") @RequestParam String email,
93
            @Parameter(name = "dateAdded", description = "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") @RequestParam("dateAdded") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAdded)
94
            throws JsonProcessingException {
95
96
        // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
97
        // See: https://www.baeldung.com/spring-date-parameters
98
99
        log.info("localDateTime={}", dateAdded);
100
101
        Article article = new Article();
102 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
        article.setTitle(title);
103 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
        article.setUrl(url);
104 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
        article.setExplanation(explanation);
105 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
        article.setEmail(email);
106 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
        article.setDateAdded(dateAdded);
107
108
        Article savedArticle = articleRepository.save(article);
109
110 1 1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED
        return savedArticle;
111
    }
112
113
    /**
114
     * Update a single article
115
     * 
116
     * @param id       id of the article to update
117
     * @param incoming the new article
118
     * @return the updated article object
119
     */
120
    @Operation(summary = "Update a single article")
121
    @PreAuthorize("hasRole('ROLE_ADMIN')")
122
    @PutMapping("")
123
    public Article updateArticle(
124
            @Parameter(name = "id") @RequestParam Long id,
125
            @RequestBody @Valid Article incoming) {
126
127
        Article article = articleRepository.findById(id)
128 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));
129
130 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED
        article.setDateAdded(incoming.getDateAdded());
131 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED
        article.setExplanation(incoming.getExplanation());
132 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED
        article.setUrl(incoming.getUrl());
133 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED
        article.setTitle(incoming.getTitle());
134 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED
        article.setEmail(incoming.getEmail());
135
136
        articleRepository.save(article);
137
138 1 1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED
        return article;
139
    }
140
141
    /**
142
     * Delete an Article
143
     * 
144
     * @param id the id of the date to delete
145
     * @return a message indicating the date was deleted
146
     */
147
    @Operation(summary = "Delete an Article")
148
    @PreAuthorize("hasRole('ROLE_ADMIN')")
149
    @DeleteMapping("")
150
    public Object deleteArticle(
151
            @Parameter(name = "id") @RequestParam Long id) {
152
        Article article = articleRepository.findById(id)
153 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));
154
155 1 1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticleRepository::delete → KILLED
        articleRepository.delete(article);
156 1 1. deleteArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticle → KILLED
        return genericMessage("Article with id %s deleted".formatted(id));
157
    }
158
}

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

70

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

72

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

102

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

103

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

104

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

105

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

106

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

110

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

128

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

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

131

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

132

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

133

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

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

138

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

153

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

155

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

156

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_a_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