ArticlesController.java

1
package edu.ucsb.cs156.example.controllers;
2
3
import edu.ucsb.cs156.example.entities.*;
4
import edu.ucsb.cs156.example.errors.EntityNotFoundException;
5
import edu.ucsb.cs156.example.repositories.*;
6
7
import io.swagger.v3.oas.annotations.Operation;
8
import io.swagger.v3.oas.annotations.Parameter;
9
import io.swagger.v3.oas.annotations.tags.Tag;
10
import lombok.extern.slf4j.Slf4j;
11
12
import com.fasterxml.jackson.core.JsonProcessingException;
13
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.format.annotation.DateTimeFormat;
16
import org.springframework.security.access.prepost.PreAuthorize;
17
import org.springframework.web.bind.annotation.DeleteMapping;
18
import org.springframework.web.bind.annotation.GetMapping;
19
import org.springframework.web.bind.annotation.PostMapping;
20
import org.springframework.web.bind.annotation.PutMapping;
21
import org.springframework.web.bind.annotation.RequestBody;
22
import org.springframework.web.bind.annotation.RequestMapping;
23
import org.springframework.web.bind.annotation.RequestParam;
24
import org.springframework.web.bind.annotation.RestController;
25
26
import jakarta.validation.Valid;
27
28
import java.time.LocalDateTime;
29
/**
30
 * This is a REST controller for Articles
31
 */
32
33
@Tag(name = "Articles")
34
@RequestMapping("/api/articles")
35
@RestController
36
@Slf4j
37
public class ArticlesController extends ApiController {
38
    @Autowired
39
    articlesRepository articlesRepository;
40
41
    /**
42
     * returns list of all articles
43
     * 
44
     * @return articles
45
     */
46
    @Operation(summary= "List all Articles")
47
    @PreAuthorize("hasRole('ROLE_USER')")
48
    @GetMapping("/all")
49
    public Iterable<Articles> allArticles() {
50
        Iterable<Articles> articles = articlesRepository.findAll();
51 1 1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED
        return articles;
52
    }
53
54
    /**
55
     * This method creates a new article. Accessible only to users with the role "ROLE_ADMIN".
56
     * @param id long id
57
     * @param title of the article
58
     * @param url article url
59
     * @param explanation an explanation of article
60
     * @param email email of the sender
61
     * @param date added date of article
62
     * @return the save article
63
     */
64
    @Operation(summary= "Create a new article")
65
    @PreAuthorize("hasRole('ROLE_ADMIN')")
66
    @PostMapping("/post")
67
    public Articles postArticle(
68
        @Parameter(name="title") @RequestParam String title,
69
        @Parameter(name="url") @RequestParam String url,
70
        @Parameter(name="explanation") @RequestParam String explanation,
71
        @Parameter(name="email") @RequestParam String email,
72
        @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)
73
            throws JsonProcessingException {
74
        log.info("dateAdded={}", dateAdded);
75
76
        Articles articles = Articles.builder()
77
            .title(title)
78
            .url(url)
79
            .explanation(explanation)
80
            .email(email)
81
            .dateAdded(dateAdded)
82
            .build();
83
84
        Articles savedArticles = articlesRepository.save(articles);
85
86 1 1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED
        return savedArticles;
87
    }
88
    /**
89
     * Get a single article by id
90
     * 
91
     * @param id the id of the article
92
     * @return article
93
     */
94
    @Operation(summary= "Get a single article")
95
    @PreAuthorize("hasRole('ROLE_USER')")
96
    @GetMapping("")
97
    public Articles getById(
98
    @Parameter(name="id") @RequestParam Long id) {
99
            Articles article = articlesRepository.findById(id)
100 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));
101
       
102 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
            return article;
103
    
104
    }
105
    
106
    /**
107
     * update a single article, accessible by only admins
108
     * @param id
109
     * @param incoming
110
     * @return articles 
111
     */
112
    @Operation(summary= "Update a single article")
113
    @PreAuthorize("hasRole('ROLE_ADMIN')")
114
    @PutMapping("")
115
    public Articles updateArticles(
116
            @Parameter(name="id") @RequestParam Long id,
117
            @RequestBody @Valid Articles incoming) {
118
119
        Articles articles = articlesRepository.findById(id)
120 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));
121
122 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
        articles.setTitle(incoming.getTitle());
123 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
        articles.setUrl (incoming.getUrl());
124 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
        articles.setExplanation(incoming.getExplanation());
125 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
        articles.setEmail(incoming.getEmail());
126 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
        articles.setDateAdded(incoming.getDateAdded());
127
        
128
129
        articlesRepository.save(articles);
130
131 1 1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED
        return articles;
132
    }
133
134
    /**
135
     * Deletes an article bassed off of id
136
     * 
137
     * @param id the id of thearticle to delete
138
     * @return a message indicating the article was deleted
139
     */
140
    @Operation(summary= "Delete an article")
141
    @PreAuthorize("hasRole('ROLE_ADMIN')")
142
    @DeleteMapping("")
143
    public Object deleteArticle(
144
            @Parameter(name="id") @RequestParam Long id) {
145
        Articles article = articlesRepository.findById(id)
146 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));
147
148 1 1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/articlesRepository::delete → KILLED
        articlesRepository.delete(article);
149 1 1. deleteArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticle → KILLED
        return genericMessage("Article with id %s deleted".formatted(id));
150
    }
151
152
}

Mutations

51

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

86

1.1
Location : postArticle
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::postArticle → KILLED

100

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

102

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

120

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

122

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

123

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

124

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

125

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

126

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

131

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

146

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

148

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

149

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