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

Mutations

44

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

64

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

65

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

66

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

67

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

68

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_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → 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_ucsbdate()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED

81

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

83

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

92

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

94

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

95

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

106

1.1
Location : lambda$updateArticle$2
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_cannot_edit_ucsbdate_that_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticle$2 → 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_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → 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_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → 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_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → 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_ucsbdate()]
removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED

112

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

116

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

Active mutators

Tests examined


Report generated by PIT 1.7.3