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

Mutations

42

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_ucsbdates()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED

76

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

77

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

78

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

79

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

80

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

82

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

97

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

99

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

118

1.1
Location : lambda$updateArticle$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$updateArticle$1 → KILLED

120

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

121

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

122

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

123

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

124

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

127

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

142

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

144

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

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