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 Articles
36
   *
37
   * @return an iterable of Articles
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 a Articles
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 article =
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 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
    return article;
62
  }
63
64
  /**
65
   * Create a new article
66
   *
67
   * @param title the title of the article
68
   * @param url the URL of the article
69
   * @param explanation the explanation of the article
70
   * @param email the email of the person who added the article
71
   * @param dateAdded the date added
72
   * @return the saved article
73
   */
74
  @Operation(summary = "Create a new article")
75
  @PreAuthorize("hasRole('ROLE_ADMIN')")
76
  @PostMapping("/post")
77
  public Articles postArticle(
78
      @Parameter(name = "title") @RequestParam String title,
79
      @Parameter(name = "url") @RequestParam String url,
80
      @Parameter(name = "explanation") @RequestParam String explanation,
81
      @Parameter(name = "email") @RequestParam String email,
82
      @Parameter(name = "dateAdded")
83
          @RequestParam("dateAdded")
84
          @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
85
          LocalDateTime dateAdded)
86
      throws JsonProcessingException {
87
88
    // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
89
    // See: https://www.baeldung.com/spring-date-parameters
90
91
    log.info("dateAdded={}", dateAdded);
92
93
    Articles article = new Articles();
94 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    article.setTitle(title);
95 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    article.setUrl(url);
96 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    article.setExplanation(explanation);
97 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    article.setEmail(email);
98 1 1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
    article.setDateAdded(dateAdded);
99
100
    Articles savedArticle = articlesRepository.save(article);
101
102 1 1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED
    return savedArticle;
103
  }
104
105
  /**
106
   * Delete an Articles
107
   *
108
   * @param id the id of the article to delete
109
   * @return a message indicating the article was deleted
110
   */
111
  @Operation(summary = "Delete an Articles")
112
  @PreAuthorize("hasRole('ROLE_ADMIN')")
113
  @DeleteMapping("")
114
  public Object deleteArticles(@Parameter(name = "id") @RequestParam Long id) {
115
    Articles articles =
116
        articlesRepository
117
            .findById(id)
118 1 1. lambda$deleteArticles$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticles$1 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
119
120 1 1. deleteArticles : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
    articlesRepository.delete(articles);
121 1 1. deleteArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticles → 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 updateArticles(
135
      @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Articles incoming) {
136
137
    Articles articles =
138
        articlesRepository
139
            .findById(id)
140 1 1. lambda$updateArticles$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticles$2 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
141
142 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    articles.setTitle(incoming.getTitle());
143 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    articles.setUrl(incoming.getUrl());
144 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    articles.setExplanation(incoming.getExplanation());
145 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    articles.setEmail(incoming.getEmail());
146 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
    articles.setDateAdded(incoming.getDateAdded());
147
148
    articlesRepository.save(articles);
149
150 1 1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED
    return articles;
151
  }
152
}

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

61

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

94

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

95

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

96

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

97

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

98

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

102

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

118

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

120

1.1
Location : deleteArticles
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

121

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

140

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

143

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

144

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

145

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

146

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

150

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

Active mutators

Tests examined


Report generated by PIT 1.17.0