ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
import edu.ucsb.cs156.example.entities.Articles;
3
import edu.ucsb.cs156.example.entities.UCSBDate;
4
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.example.repositories.ArticlesRepository;
6
import edu.ucsb.cs156.example.repositories.UCSBDateRepository;
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
import java.time.ZonedDateTime;
31
32
/**
33
 * This is a REST controller for Articles
34
 */
35
@Tag(name = "Articles")
36
@RequestMapping("/api/articles")
37
@RestController
38
@Slf4j
39
40
public class ArticlesController extends ApiController{
41
    
42
43
    @Autowired
44
    ArticlesRepository articlesRepository;
45
46
47
        /**
48
     * List all Articles
49
     * 
50
     * @return an iterable of Articles
51
     */
52
    @Operation(summary= "List all articles")
53
    @PreAuthorize("hasRole('ROLE_USER')")
54
    @GetMapping("/all")
55
    public Iterable<Articles> allArticles() {
56
        Iterable<Articles> articles = articlesRepository.findAll();
57 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED
        return articles;
58
    }
59
60
61
62
    /**
63
     * Create a new articles
64
     * 
65
     * @param title     the title of the articles
66
     * @param url        the url of the articles
67
     * @param explanation the explanation of the articles
68
     * @param email the email of the author
69
     * @param dateAdded the date we added the articles with time zone
70
     * @return the saved articles
71
     */
72
    @Operation(summary= "Create a new articles")
73
    @PreAuthorize("hasRole('ROLE_ADMIN')")
74
    @PostMapping("/post")
75
    public Articles postArticles(
76
            @Parameter(name="title") @RequestParam String title,
77
            @Parameter(name="url") @RequestParam String url,
78
            @Parameter(name="explanation") @RequestParam String explanation,
79
            @Parameter(name="email") @RequestParam String email,
80
            @Parameter(name="dateAdded") @RequestParam("dateAdded") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAdded)
81
            throws JsonProcessingException {
82
83
        // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
84
        // See: https://www.baeldung.com/spring-date-parameters
85
86
        log.info("dateAdded={}", dateAdded);
87
88
        Articles articles = new Articles();
89 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
        articles.setTitle(title);
90 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
        articles.setUrl(url);
91 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
        articles.setExplanation(explanation);
92 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
        articles.setEmail(email);
93 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
        articles.setDateAdded(dateAdded);
94
95
        Articles savedArticles = articlesRepository.save(articles);
96
97 1 1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED
        return savedArticles;
98
    }
99
100
    /**
101
    * Get a single article by id
102
    * 
103
    * @param id the id of the article
104
    * @return a Articles
105
    */
106
    @Operation(summary= "Get a single article")
107
    @PreAuthorize("hasRole('ROLE_USER')")
108
    @GetMapping("")
109
    public Articles getById(
110
            @Parameter(name="id") @RequestParam Long id) {
111
         Articles article = articlesRepository.findById(id)
112 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));
113
114 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
        return article;
115
  }
116
117
    /**
118
     * Update a single article
119
     * 
120
     * @param id       id of the article to update
121
     * @param incoming the new article
122
     * @return the updated article object
123
     */
124
    @Operation(summary= "Update a single article")
125
    @PreAuthorize("hasRole('ROLE_ADMIN')")
126
    @PutMapping("")
127
    public Articles updateArticles(
128
            @Parameter(name="id") @RequestParam Long id,
129
            @RequestBody @Valid Articles incoming) {
130
131
        Articles article = articlesRepository.findById(id)
132 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));
133
134 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
        article.setTitle(incoming.getTitle());
135 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
        article.setUrl(incoming.getUrl());
136 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
        article.setExplanation(incoming.getExplanation());
137 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
        article.setEmail(incoming.getEmail());
138 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
        article.setDateAdded(incoming.getDateAdded());
139
140
        articlesRepository.save(article);
141
142 1 1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED
        return article;
143
    }
144
145
    /**
146
     * Delete a Article
147
     * 
148
     * @param id the id of the article to delete
149
     * @return a message indicating the article was deleted
150
     */
151
    @Operation(summary= "Delete a Article")
152
    @PreAuthorize("hasRole('ROLE_ADMIN')")
153
    @DeleteMapping("")
154
    public Object deleteArticle(
155
            @Parameter(name="id") @RequestParam Long id) {
156
        Articles article = articlesRepository.findById(id)
157 1 1. lambda$deleteArticle$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticle$2 → KILLED
                .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
158
159 1 1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
                articlesRepository.delete(article);
160 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));
161
    }
162
163
164
}

Mutations

57

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

89

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

90

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

91

1.1
Location : postArticles
Killed by : edu.ucsb.cs156.example.controllers.ArticlesControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.example.controllers.ArticlesControllerTests]/[method:admin_user_can_post_new_article()]
removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → 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:admin_user_can_post_new_article()]
removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → 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:admin_user_can_post_new_article()]
removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED

97

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

112

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

114

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

132

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

134

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

135

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

136

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

137

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

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

157

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

159

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

160

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

Active mutators

Tests examined


Report generated by PIT 1.17.0