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 UCSBDates
33
 */
34
35
@Tag(name = "Articles")
36
@RequestMapping("/api/articles")
37
@RestController
38
@Slf4j
39
public class ArticlesController extends ApiController {
40
41
    @Autowired
42
    ArticlesRepository ArticlesRepository;
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<Articles> allArticles() {
53
        Iterable<Articles> articles = ArticlesRepository.findAll();
54 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → 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 Articles getById(
67
            @Parameter(name="id") @RequestParam Long id) {
68
        Articles Article = ArticlesRepository.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(Articles.class, id));
70
71 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
        return Article;
72
    }
73
74
    /**
75
     
76
     */
77
    @Operation(summary= "Create a new article")
78
    @PreAuthorize("hasRole('ROLE_ADMIN')")
79
    @PostMapping("/post")
80
    public Articles 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
        Articles Article = new Articles();
91 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
        Article.setTitle(title);
92 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
        Article.setUrl(url);
93 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
        Article.setExplanation(explanation);
94 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
        Article.setEmail(email);
95 1 1. postUCSBArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setLocalDateTime → KILLED
        Article.setLocalDateTime(localDateTime);
96
97
        Articles savedUcsbArticle = ArticlesRepository.save(Article);
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 Articles updateArticles(
114
            @Parameter(name="id") @RequestParam Long id,
115
            @RequestBody @Valid Articles incoming) {
116
117
            Articles Article = ArticlesRepository.findById(id)
118 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));
119
120 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
        Article.setTitle(incoming.getTitle());
121 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
        Article.setUrl(incoming.getUrl());
122 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
        Article.setExplanation(incoming.getExplanation());
123 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
        Article.setEmail(incoming.getEmail());
124 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setLocalDateTime → KILLED
        Article.setLocalDateTime(incoming.getLocalDateTime());
125
126
        ArticlesRepository.save(Article);
127
128 1 1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED
        return Article;
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 Article")
138
    @PreAuthorize("hasRole('ROLE_ADMIN')")
139
    @DeleteMapping("")
140
    public Object deleteArticle(
141
            @Parameter(name="id") @RequestParam Long id) {
142
        Articles Article = ArticlesRepository.findById(id)
143 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));
144
145 1 1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
        ArticlesRepository.delete(Article);
146 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));
147
    }
148
149
}
150
//

Mutations

54

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

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

118

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

120

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

121

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

122

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

123

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

124

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

128

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

143

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

145

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

146

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_existing_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