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 Article
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
   * Create a new article
49
   *
50
   * @param title of the article
51
   * @param url of the article
52
   * @param explanation of the article
53
   * @param email of the submitter
54
   * @param dateAdded the date
55
   * @return the saved Article
56
   */
57
  @Operation(summary = "Create a new article")
58
  @PreAuthorize("hasRole('ROLE_ADMIN')")
59
  @PostMapping("/post")
60
  public Articles postArticles(
61
      @Parameter(name = "title") @RequestParam String title,
62
      @Parameter(name = "url") @RequestParam String url,
63
      @Parameter(name = "explanation") @RequestParam String explanation,
64
      @Parameter(name = "email") @RequestParam String email,
65
      @Parameter(
66
              name = "dateAdded",
67
              description =
68
                  "dateAdded (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)")
69
          @RequestParam("dateAdded")
70
          @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
71
          LocalDateTime dateAdded)
72
      throws JsonProcessingException {
73
74
    // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
75
    // See: https://www.baeldung.com/spring-date-parameters
76
77
    log.info("dateAdded={}", dateAdded);
78
79
    Articles articles = new Articles();
80 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    articles.setTitle(title);
81 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    articles.setUrl(url);
82 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    articles.setExplanation(explanation);
83 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    articles.setEmail(email);
84 1 1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
    articles.setDateAdded(dateAdded);
85
    Articles savedArticles = articlesRepository.save(articles);
86
87 1 1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED
    return savedArticles;
88
  }
89
90
  /**
91
   * Get a single articles
92
   *
93
   * @param id the id of the articles
94
   * @return an Articles
95
   */
96
  @Operation(summary = "Get a single articles")
97
  @PreAuthorize("hasRole('ROLE_USER')")
98
  @GetMapping("")
99
  public Articles getById(@Parameter(name = "id") @RequestParam Long id) {
100
    Articles articles =
101
        articlesRepository
102
            .findById(id)
103 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));
104
105 1 1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED
    return articles;
106
  }
107
108
  @Operation(summary = "Update a single Articles")
109
  @PreAuthorize("hasRole('ROLE_ADMIN')")
110
  @PutMapping("")
111
  public Articles updateArticles(
112
      @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Articles incoming) {
113
114
    Articles articles =
115
        articlesRepository
116
            .findById(id)
117 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));
118
119 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED
    articles.setTitle(incoming.getTitle());
120 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED
    articles.setUrl(incoming.getUrl());
121 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED
    articles.setExplanation(incoming.getExplanation());
122 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED
    articles.setEmail(incoming.getEmail());
123 1 1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED
    articles.setDateAdded(incoming.getDateAdded());
124
125
    articlesRepository.save(articles);
126
127 1 1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED
    return articles;
128
  }
129
130
  @Operation(summary = "Delete a Articles")
131
  @PreAuthorize("hasRole('ROLE_ADMIN')")
132
  @DeleteMapping("")
133
  public Object deleteArticles(@Parameter(name = "id") @RequestParam Long id) {
134
    Articles articles =
135
        articlesRepository
136
            .findById(id)
137 1 1. lambda$deleteArticles$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticles$2 → KILLED
            .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));
138
139 1 1. deleteArticles : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED
    articlesRepository.delete(articles);
140 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));
141
  }
142
}

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

80

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

81

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

82

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

83

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

84

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

87

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

103

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

105

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

117

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

119

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

120

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

121

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

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

127

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

137

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

139

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

140

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

Active mutators

Tests examined


Report generated by PIT 1.17.0