ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.Articles;
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 org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.format.annotation.DateTimeFormat;
14
import org.springframework.security.access.prepost.PreAuthorize;
15
import org.springframework.web.bind.annotation.*;
16
17
import jakarta.validation.Valid;
18
import java.time.LocalDateTime;
19
20
/**
21
 * REST controller for managing articles.
22
 */
23
24
@Tag(name = "articles")
25
@RequestMapping("/api/articles")
26
@RestController
27
@Slf4j
28
public class ArticlesController extends ApiController {
29
30
    @Autowired
31
    ArticlesRepository articlesRepository;
32
33
    /**
34
     * List all articles.
35
     * 
36
     * @return iterable of all articles
37
     */
38
    @Operation(summary = "List all articles")
39
    @PreAuthorize("hasRole('ROLE_USER')")
40
    @GetMapping("/all")
41
    public Iterable<Articles> allArticles() {
42 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED
        return articlesRepository.findAll();
43
    }
44
45
    /**
46
     * Get a single article by ID.
47
     * 
48
     * @param id ID of the article to retrieve
49
     * @return the requested article
50
     */
51
    @Operation(summary = "Get a single article by ID")
52
    @PreAuthorize("hasRole('ROLE_USER')")
53
    @GetMapping("")
54
    public Articles getById(@Parameter(name = "id") @RequestParam Long id) {
55 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
        return articlesRepository.findById(id)
56 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));
57
    }
58
59
    /**
60
     * Create a new article.
61
     * 
62
     * @param title       title of the article
63
     * @param url         URL of the article
64
     * @param explanation explanation of the article
65
     * @param email       email of the article creator
66
     * @param dateAdded   date the article was added
67
     * @return the newly created article
68
     */
69
    @Operation(summary = "Create a new article")
70
    @PreAuthorize("hasRole('ROLE_ADMIN')")
71
    @PostMapping("/post")
72
    public Articles postArticle(
73
            @Parameter(name = "title") @RequestParam String title,
74
            @Parameter(name = "url") @RequestParam String url,
75
            @Parameter(name = "explanation") @RequestParam String explanation,
76
            @Parameter(name = "email") @RequestParam String email,
77
            @Parameter(name = "dateAdded") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAdded) {
78
79
        Articles article = Articles.builder()
80
                .title(title)
81
                .url(url)
82
                .explanation(explanation)
83
                .email(email)
84
                .dateAdded(dateAdded)
85
                .build();
86
87 1 1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED
        return articlesRepository.save(article);
88
    }
89
90
    /**
91
     * Update an existing article.
92
     * 
93
     * @param id       ID of the article to update
94
     * @param incoming article data to update with
95
     * @return the updated article
96
     */
97
    @Operation(summary = "Update an existing article")
98
    @PreAuthorize("hasRole('ROLE_ADMIN')")
99
    @PutMapping("")
100
    public Articles updateArticle(
101
            @Parameter(name = "id") @RequestParam Long id,
102
            @RequestBody @Valid Articles incoming) {
103
104
        Articles article = articlesRepository.findById(id)
105 1 1. lambda$updateArticle$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticle$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
106
107 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
        article.setTitle(incoming.getTitle());
108 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
        article.setUrl(incoming.getUrl());
109 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
        article.setExplanation(incoming.getExplanation());
110 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
        article.setEmail(incoming.getEmail());
111 1 1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
        article.setDateAdded(incoming.getDateAdded());
112
113 1 1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED
        return articlesRepository.save(article);
114
    }
115
116
    /**
117
     * Delete an article by ID.
118
     * 
119
     * @param id ID of the article to delete
120
     * @return confirmation message
121
     */
122
    @Operation(summary = "Delete an article by ID")
123
    @PreAuthorize("hasRole('ROLE_ADMIN')")
124
    @DeleteMapping("")
125
    public Object deleteArticle(@Parameter(name = "id") @RequestParam Long id) {
126
        Articles article = articlesRepository.findById(id)
127 1 1. lambda$deleteArticle$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticle$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
128
129 1 1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
        articlesRepository.delete(article);
130 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));
131
    }
132
}

Mutations

42

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

55

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

56

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

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

105

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

107

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

108

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

109

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

110

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

111

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

113

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

127

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

129

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

130

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