ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.Articles;
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.LocalDateTime;
30
31
@Tag(name = "Articles")
32
@RequestMapping("/api/articles")
33
@RestController
34
@Slf4j
35
public class ArticlesController extends ApiController {
36
37
    @Autowired
38
    ArticlesRepository articlesRepository;
39
40
    @Operation(summary= "List all articles")
41
    @PreAuthorize("hasRole('ROLE_USER')")
42
    @GetMapping("/all")
43
    public Iterable<Articles> allArticles() {
44
        Iterable<Articles> articles = articlesRepository.findAll();
45 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED
        return articles;
46
    }
47
48
    @Operation(summary= "Get a single article")
49
    @PreAuthorize("hasRole('ROLE_USER')")
50
    @GetMapping("")
51
    public Articles getById(
52
            @Parameter(name="id") @RequestParam Long id) {
53
        Articles articles = articlesRepository.findById(id)
54 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));
55
56 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
        return articles;
57
    }
58
59
    @Operation(summary= "Post new articles")
60
    @PreAuthorize("hasRole('ROLE_ADMIN')")
61
    @PostMapping("/post")
62
    public Articles postArticles(
63
            @Parameter(name="title") @RequestParam String title,
64
            @Parameter(name="url") @RequestParam String url,
65
            @Parameter(name="explanation") @RequestParam String explanation,
66
            @Parameter(name="email") @RequestParam String email,
67
            @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)
68
            throws JsonProcessingException {
69
70
        // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
71
        // See: https://www.baeldung.com/spring-date-parameters
72
73
        log.info("Posting new articles: title={}, url={}, explanation={}, email={}, dateAdded={}", title, url, explanation, email, dateAdded);
74
75
        Articles articles = new Articles();
76 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
        articles.setTitle(title);
77 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
        articles.setUrl(url);
78 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
        articles.setExplanation(explanation);
79 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
        articles.setEmail(email);
80 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
        articles.setDateAdded(dateAdded);
81
82
        Articles savedArticles = articlesRepository.save(articles);
83
84 1 1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED
        return savedArticles;
85
    }
86
87
    @Operation(summary= "Delete an Article")
88
    @PreAuthorize("hasRole('ROLE_ADMIN')")
89
    @DeleteMapping("")
90
    public Object deleteArticles(
91
            @Parameter(name="id") @RequestParam Long id) {
92
        Articles articles = articlesRepository.findById(id)
93 1 1. lambda$deleteArticles$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticles$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
94
95 1 1. deleteArticles : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
        articlesRepository.delete(articles);
96 1 1. deleteArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticles → KILLED
        return genericMessage("Articles with id %s deleted".formatted(id));
97
    }
98
99
    @Operation(summary= "Update a single article")
100
    @PreAuthorize("hasRole('ROLE_ADMIN')")
101
    @PutMapping("")
102
    public Articles updateArticles(
103
            @Parameter(name="id") @RequestParam Long id,
104
            @RequestBody @Valid Articles incoming) {
105
106
        Articles articles = articlesRepository.findById(id)
107 1 1. lambda$updateArticles$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticles$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
108
109 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
        articles.setTitle(incoming.getTitle());
110 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
        articles.setUrl(incoming.getUrl());
111 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
        articles.setExplanation(incoming.getExplanation());
112 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
        articles.setEmail(incoming.getEmail());
113 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
        articles.setDateAdded(incoming.getDateAdded());
114
115
        articlesRepository.save(articles);
116
117 1 1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED
        return articles;
118
    }
119
}

Mutations

45

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

54

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

56

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

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

77

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

78

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

80

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

84

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

93

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

95

1.1
Location : deleteArticles
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

96

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

107

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

109

1.1
Location : updateArticles
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

110

1.1
Location : updateArticles
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

111

1.1
Location : updateArticles
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

112

1.1
Location : updateArticles
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

113

1.1
Location : updateArticles
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

117

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

Active mutators

Tests examined


Report generated by PIT 1.17.0