ArticlesController.java

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

Mutations

43

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

52

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

53

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

72

1.1
Location : postArticles
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/Articles::setTitle → KILLED

73

1.1
Location : postArticles
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/Articles::setUrl → KILLED

74

1.1
Location : postArticles
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/Articles::setExplanation → KILLED

75

1.1
Location : postArticles
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/Articles::setEmail → KILLED

76

1.1
Location : postArticles
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/Articles::setDateAdded → KILLED

79

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

94

1.1
Location : lambda$deleteArticle$1
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$1 → KILLED

96

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

97

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

115

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

117

1.1
Location : updateaArticles
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/Articles::setTitle → KILLED

118

1.1
Location : updateaArticles
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/Articles::setUrl → KILLED

119

1.1
Location : updateaArticles
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/Articles::setExplanation → KILLED

120

1.1
Location : updateaArticles
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/Articles::setEmail → KILLED

121

1.1
Location : updateaArticles
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/Articles::setDateAdded → KILLED

125

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

Active mutators

Tests examined


Report generated by PIT 1.17.0