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
import edu.ucsb.cs156.example.repositories.UCSBDateRepository;
8
9
import io.swagger.v3.oas.annotations.Operation;
10
import io.swagger.v3.oas.annotations.Parameter;
11
import io.swagger.v3.oas.annotations.tags.Tag;
12
import lombok.extern.slf4j.Slf4j;
13
14
import com.fasterxml.jackson.core.JsonProcessingException;
15
16
import org.checkerframework.checker.units.qual.A;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.format.annotation.DateTimeFormat;
19
import org.springframework.http.MediaType;
20
import org.springframework.security.access.prepost.PreAuthorize;
21
import org.springframework.web.bind.annotation.DeleteMapping;
22
import org.springframework.web.bind.annotation.GetMapping;
23
import org.springframework.web.bind.annotation.PostMapping;
24
import org.springframework.web.bind.annotation.PutMapping;
25
import org.springframework.web.bind.annotation.RequestBody;
26
import org.springframework.web.bind.annotation.RequestMapping;
27
import org.springframework.web.bind.annotation.RequestParam;
28
import org.springframework.web.bind.annotation.RestController;
29
30
import jakarta.validation.Valid;
31
32
33
34
import java.time.LocalDateTime;
35
import java.time.ZonedDateTime;
36
import java.util.Map;
37
import java.util.Optional;
38
39
/**
40
 * This is a REST controller for Articles
41
 */
42
43
@Tag(name = "articles")
44
@RequestMapping("/api/articles")
45
@RestController
46
@Slf4j
47
public class ArticlesController extends ApiController {
48
49
    @Autowired
50
    ArticlesRepository articlesRepository;
51
52
    /**
53
     * List all Articles
54
     * 
55
     * @return an iterable of Articles
56
     */
57
    @Operation(summary = "List all Articles")
58
    @PreAuthorize("hasRole('ROLE_USER')")
59
    @GetMapping("/all")
60
    public Iterable<Articles> allArticles() {
61
        Iterable<Articles> Articles = articlesRepository.findAll();
62 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED
        return Articles;
63
    }
64
65
    /**
66
     * Get a new Articles
67
     * 
68
     * @param title       the title of the Articles
69
     * @param url         the url of the Articles
70
     * @param explanation the explanation of the Articles
71
     * @param email       the email of the Articles
72
     * @param dateAdded   the dateAdded of the Articles
73
     * @return the saved Articles
74
     */
75
    @Operation(summary = "Create a new Articles")
76
    @PreAuthorize("hasRole('ROLE_ADMIN')")
77
    @PostMapping("/post")
78
    public Articles postArticles(
79
            @Parameter(name = "title") @RequestParam String title,
80
            @Parameter(name = "url") @RequestParam String url,
81
            @Parameter(name = "explanation") @RequestParam String explanation,
82
            @Parameter(name = "email") @RequestParam String email,
83
            @Parameter(name = "dateAdded") @RequestParam("dateAdded") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAdded)
84
            throws JsonProcessingException {
85
86
        // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
87
        // See: https://www.baeldung.com/spring-date-parameters
88
89
        log.info("dateAdded={}", dateAdded);
90
91
        Articles Article = new Articles();
92 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
        Article.setTitle(title);
93 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
        Article.setUrl(url);
94 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
        Article.setExplanation(explanation);
95 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
        Article.setEmail(email);
96 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
        Article.setDateAdded(dateAdded);
97
98
        Articles savedArticles = articlesRepository.save(Article);
99
100 1 1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED
        return savedArticles;
101
    }
102
103
104
    /**
105
     * Get a single articles by id
106
     * 
107
     * @param id the id of the articles
108
     * @return a articles
109
     */
110
    @Operation(summary= "Get a single articles")
111
    @PreAuthorize("hasRole('ROLE_USER')")
112
    @GetMapping("")
113
    public Articles getById(
114
            @Parameter(name="id") @RequestParam Long id) {
115
        Articles articles = articlesRepository.findById(id)
116 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));
117
118 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
        return articles;
119
    }
120
121
    /**
122
     * Update a single articles
123
     * 
124
     * @param id       id of the articles to update
125
     * @param incoming the new articles
126
     * @return the updated articles object
127
     */
128
    @Operation(summary= "Update a single articles")
129
    @PreAuthorize("hasRole('ROLE_ADMIN')")
130
    @PutMapping("")
131
    public Articles updateArticles(
132
            @Parameter(name="id") @RequestParam Long id,
133
            @RequestBody @Valid Articles incoming) {
134
135
        Articles articles = articlesRepository.findById(id)
136 1 1. lambda$updateArticles$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticles$1 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
137
138 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
        articles.setTitle(incoming.getTitle());
139 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
        articles.setUrl(incoming.getUrl()); 
140 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
        articles.setExplanation(incoming.getExplanation());
141 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
        articles.setEmail(incoming.getEmail()); 
142 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
        articles.setDateAdded(incoming.getDateAdded());
143
144
        articlesRepository.save(articles);
145
146 1 1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED
        return articles;
147
    }
148
149
    /**
150
     * Delete a Articles
151
     * 
152
     * @param id the id of the articles to delete
153
     * @return a message indicating the articles was deleted
154
     */
155
    @Operation(summary= "Delete a Articles")
156
    @PreAuthorize("hasRole('ROLE_ADMIN')")
157
    @DeleteMapping("")
158
    public Object deleteArticles(
159
            @Parameter(name="id") @RequestParam Long id) {
160
        Articles articles = articlesRepository.findById(id)
161 1 1. lambda$deleteArticles$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticles$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
162
163 1 1. deleteArticles : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
        articlesRepository.delete(articles);
164 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));
165
    }
166
167
    
168
}

Mutations

62

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

92

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

93

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

94

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

95

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

96

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

100

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

116

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

118

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

136

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

138

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

139

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

140

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

141

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

142

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

146

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

161

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

163

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

164

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

Active mutators

Tests examined


Report generated by PIT 1.17.0