ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.UCSBArticles;
4
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.example.repositories.UCSBArticlesRepository;
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 = "UCSBArticles")
31
@RequestMapping("/api/ucsbarticles")
32
@RestController
33
@Slf4j
34
public class ArticlesController extends ApiController {
35
36
    @Autowired
37
    UCSBArticlesRepository ucsbArticlesRepository;
38
39
    @Operation(summary= "List all ucsb articles")
40
    @PreAuthorize("hasRole('ROLE_USER')")
41
    @GetMapping("/all")
42
    public Iterable<UCSBArticles> allUCSBArticles() {
43
        Iterable<UCSBArticles> articles = ucsbArticlesRepository.findAll();
44 1 1. allUCSBArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::allUCSBArticles → KILLED
        return articles;
45
    }
46
47
    @Operation(summary= "Create a new article")
48
    @PreAuthorize("hasRole('ROLE_ADMIN')")
49
    @PostMapping("/post")
50
    public UCSBArticles postUCSBArticles(
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="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)
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("localDateTime={}", dateAdded);
62
63
        UCSBArticles ucsbArticle = new UCSBArticles();
64 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setTitle → KILLED
        ucsbArticle.setTitle(title);
65 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setUrl → KILLED
        ucsbArticle.setUrl(url);
66 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setExplanation → KILLED
        ucsbArticle.setExplanation(explanation);
67 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setEmail → KILLED
        ucsbArticle.setEmail(email);
68 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setDateAdded → KILLED
        ucsbArticle.setDateAdded(dateAdded);
69
70
        UCSBArticles savedUcsbArticle = ucsbArticlesRepository.save(ucsbArticle);
71
72 1 1. postUCSBArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postUCSBArticles → KILLED
        return savedUcsbArticle;
73
    }
74
75
    @Operation(summary= "Get a single article")
76
    @PreAuthorize("hasRole('ROLE_USER')")
77
    @GetMapping("")
78
    public UCSBArticles getById(
79
            @Parameter(name="id") @RequestParam Long id) {
80
        UCSBArticles ucsbArticle = ucsbArticlesRepository.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(UCSBArticles.class, id));
82
83 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
        return ucsbArticle;
84
    }
85
86
    @Operation(summary= "Delete a UCSBArticles")
87
    @PreAuthorize("hasRole('ROLE_ADMIN')")
88
    @DeleteMapping("")
89
    public Object deleteUCSBArticles(
90
            @Parameter(name="id") @RequestParam Long id) {
91
        UCSBArticles ucsbArticle = ucsbArticlesRepository.findById(id)
92 1 1. lambda$deleteUCSBArticles$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteUCSBArticles$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(UCSBArticles.class, id));
93
94 1 1. deleteUCSBArticles : removed call to edu/ucsb/cs156/example/repositories/UCSBArticlesRepository::delete → KILLED
        ucsbArticlesRepository.delete(ucsbArticle);
95 1 1. deleteUCSBArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteUCSBArticles → KILLED
        return genericMessage("UCSBArticles with id %s deleted".formatted(id));
96
    }
97
98
    @Operation(summary= "Update a single article")
99
    @PreAuthorize("hasRole('ROLE_ADMIN')")
100
    @PutMapping("")
101
    public UCSBArticles updateUCSBArticles(
102
            @Parameter(name="id") @RequestParam Long id,
103
            @RequestBody @Valid UCSBArticles incoming) {
104
105
        UCSBArticles ucsbArticle = ucsbArticlesRepository.findById(id)
106 1 1. lambda$updateUCSBArticles$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateUCSBArticles$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(UCSBArticles.class, id));
107
108 1 1. updateUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setTitle → KILLED
        ucsbArticle.setTitle(incoming.getTitle());
109 1 1. updateUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setUrl → KILLED
        ucsbArticle.setUrl(incoming.getUrl());
110 1 1. updateUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setExplanation → KILLED
        ucsbArticle.setExplanation(incoming.getExplanation());
111 1 1. updateUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setEmail → KILLED
        ucsbArticle.setEmail(incoming.getEmail());
112 1 1. updateUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setDateAdded → KILLED
        ucsbArticle.setDateAdded(incoming.getDateAdded());
113
        ucsbArticlesRepository.save(ucsbArticle);
114
115 1 1. updateUCSBArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateUCSBArticles → KILLED
        return ucsbArticle;
116
    }
117
}

Mutations

44

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

64

1.1
Location : postUCSBArticles
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_ucsbarticles()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setTitle → KILLED

65

1.1
Location : postUCSBArticles
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_ucsbarticles()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setUrl → KILLED

66

1.1
Location : postUCSBArticles
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_ucsbarticles()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setExplanation → KILLED

67

1.1
Location : postUCSBArticles
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_ucsbarticles()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setEmail → KILLED

68

1.1
Location : postUCSBArticles
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_ucsbarticles()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setDateAdded → KILLED

72

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

94

1.1
Location : deleteUCSBArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_delete_a_article()]
removed call to edu/ucsb/cs156/example/repositories/UCSBArticlesRepository::delete → KILLED

95

1.1
Location : deleteUCSBArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_delete_a_article()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteUCSBArticles → KILLED

106

1.1
Location : lambda$updateUCSBArticles$2
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_cannot_edit_ucsbarticle_that_does_not_exist()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateUCSBArticles$2 → KILLED

108

1.1
Location : updateUCSBArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbarticle()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setTitle → KILLED

109

1.1
Location : updateUCSBArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbarticle()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setUrl → KILLED

110

1.1
Location : updateUCSBArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbarticle()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setExplanation → KILLED

111

1.1
Location : updateUCSBArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbarticle()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setEmail → KILLED

112

1.1
Location : updateUCSBArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbarticle()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setDateAdded → KILLED

115

1.1
Location : updateUCSBArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_can_edit_an_existing_ucsbarticle()]
replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateUCSBArticles → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3