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

Mutations

52

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

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

90

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

107

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

125

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

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

135

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

150

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

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

153

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