ArticlesController.java

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

Mutations

44

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

60

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

62

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

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

101

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

102

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

107

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

123

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

125

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

126

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

145

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

147

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

148

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

150

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

151

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

155

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_articles()]
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