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

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

73

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

75

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

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

98

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

99

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

103

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

118

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

120

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

121

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

139

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

141

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

142

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

143

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

144

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

145

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

149

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

Active mutators

Tests examined


Report generated by PIT 1.17.0