ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.UCSBArticles;
4
import edu.ucsb.cs156.example.entities.UCSBDate;
5
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
6
import edu.ucsb.cs156.example.repositories.UCSBArticlesRepository;
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
/**
32
 * This is a REST controller for UCSBDates
33
 */
34
35
@Tag(name = "UCSBArticles")
36
@RequestMapping("/api/ucsbarticles")
37
@RestController
38
@Slf4j
39
public class ArticlesController extends ApiController {
40
41
    @Autowired
42
    UCSBArticlesRepository ucsbArticlesRepository;
43
44
    /**
45
     * List all UCSB articles
46
     * 
47
     * @return an iterable of UCSBArticles
48
     */
49
    @Operation(summary= "List all ucsb articles")
50
    @PreAuthorize("hasRole('ROLE_USER')")
51
    @GetMapping("/all")
52
    public Iterable<UCSBArticles> allUCSBArticles() {
53
        Iterable<UCSBArticles> articles = ucsbArticlesRepository.findAll();
54 1 1. allUCSBArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allUCSBArticles → KILLED
        return articles;
55
    }
56
57
    /**
58
     * Get a single article by id
59
     * 
60
     * @param id the id of the article
61
     * @return a UCSBArticle
62
     */
63
    @Operation(summary= "Get a single article")
64
    @PreAuthorize("hasRole('ROLE_USER')")
65
    @GetMapping("")
66
    public UCSBArticles getById(
67
            @Parameter(name="id") @RequestParam Long id) {
68
        UCSBArticles ucsbArticle = ucsbArticlesRepository.findById(id)
69 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));
70
71 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
        return ucsbArticle;
72
    }
73
74
    /**
75
     
76
     */
77
    @Operation(summary= "Create a new article")
78
    @PreAuthorize("hasRole('ROLE_ADMIN')")
79
    @PostMapping("/post")
80
    public UCSBArticles postUCSBArticles(
81
            @Parameter(name="title") @RequestParam String title,
82
            @Parameter(name="url") @RequestParam String url,
83
            @Parameter(name="explanation") @RequestParam String explanation,
84
            @Parameter(name="email") @RequestParam String email,
85
            @Parameter(name="localDateTime", description="date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") @RequestParam("localDateTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime localDateTime)
86
            throws JsonProcessingException {
87
88
        log.info("localDateTime={}", localDateTime);
89
90
        UCSBArticles ucsbArticle = new UCSBArticles();
91 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setTitle → KILLED
        ucsbArticle.setTitle(title);
92 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setUrl → KILLED
        ucsbArticle.setUrl(url);
93 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setExplanation → KILLED
        ucsbArticle.setExplanation(explanation);
94 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setEmail → KILLED
        ucsbArticle.setEmail(email);
95 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setLocalDateTime → KILLED
        ucsbArticle.setLocalDateTime(localDateTime);
96
97
        UCSBArticles savedUcsbArticle = ucsbArticlesRepository.save(ucsbArticle);
98
99 1 1. postUCSBArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postUCSBArticles → KILLED
        return savedUcsbArticle;
100
    }
101
102
     /**
103
     * Update a single date
104
     * private String title;
105
        private String url;
106
        private String explanation;
107
        private String email;
108
        private LocalDateTime localDateTime;
109
     */
110
    @Operation(summary= "Update a single article")
111
    @PreAuthorize("hasRole('ROLE_ADMIN')")
112
    @PutMapping("")
113
    public UCSBArticles updateUCSBArticles(
114
            @Parameter(name="id") @RequestParam Long id,
115
            @RequestBody @Valid UCSBArticles incoming) {
116
117
            UCSBArticles ucsbArticle = ucsbArticlesRepository.findById(id)
118 1 1. lambda$updateUCSBArticles$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateUCSBArticles$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(UCSBArticles.class, id));
119
120 1 1. updateUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setTitle → KILLED
        ucsbArticle.setTitle(incoming.getTitle());
121 1 1. updateUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setUrl → KILLED
        ucsbArticle.setUrl(incoming.getUrl());
122 1 1. updateUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setExplanation → KILLED
        ucsbArticle.setExplanation(incoming.getExplanation());
123 1 1. updateUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setEmail → KILLED
        ucsbArticle.setEmail(incoming.getEmail());
124 1 1. updateUCSBArticles : removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setLocalDateTime → KILLED
        ucsbArticle.setLocalDateTime(incoming.getLocalDateTime());
125
126
        ucsbArticlesRepository.save(ucsbArticle);
127
128 1 1. updateUCSBArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateUCSBArticles → KILLED
        return ucsbArticle;
129
    }
130
131
     /**
132
     * Delete an Article
133
     * 
134
     * @param id the id of the article to delete
135
     * @return a message indicating the date was deleted
136
     */
137
    @Operation(summary= "Delete a UCSBArticle")
138
    @PreAuthorize("hasRole('ROLE_ADMIN')")
139
    @DeleteMapping("")
140
    public Object deleteUCSBArticle(
141
            @Parameter(name="id") @RequestParam Long id) {
142
        UCSBArticles ucsbArticles = ucsbArticlesRepository.findById(id)
143 1 1. lambda$deleteUCSBArticle$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteUCSBArticle$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(UCSBArticles.class, id));
144
145 1 1. deleteUCSBArticle : removed call to edu/ucsb/cs156/example/repositories/UCSBArticlesRepository::delete → KILLED
        ucsbArticlesRepository.delete(ucsbArticles);
146 1 1. deleteUCSBArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteUCSBArticle → KILLED
        return genericMessage("UCSBArticles with id %s deleted".formatted(id));
147
    }
148
149
}

Mutations

54

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 Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allUCSBArticles → KILLED

69

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

71

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

91

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

92

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

93

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

94

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

95

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_ucsbarticle()]
removed call to edu/ucsb/cs156/example/entities/UCSBArticles::setLocalDateTime → KILLED

99

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

118

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

120

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

121

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

122

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

123

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

124

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

128

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

143

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

145

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

146

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

Active mutators

Tests examined


Report generated by PIT 1.17.0