CommentaireController.java

1
package fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.controllers;
2
3
import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.dtos.CommentaireDto;
4
import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.exception.NoUpdateException;
5
import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.models.Commentaire;
6
import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.services.CommentaireService;
7
import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.utils.StringUtils;
8
import io.swagger.v3.oas.annotations.Operation;
9
import jakarta.persistence.NoResultException;
10
import org.modelmapper.ModelMapper;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.http.HttpStatus;
13
import org.springframework.web.bind.annotation.*;
14
import org.springframework.web.server.ResponseStatusException;
15
16
import java.util.List;
17
import java.util.NoSuchElementException;
18
19
@RestController
20
@RequestMapping(value = "/api/commentaire")
21
public class CommentaireController {
22
23
    @Autowired
24
    private CommentaireService service;
25
26
    @Autowired
27
    private ModelMapper mapper;
28
29
    /**
30
     *
31
     * @param id du sondage
32
     * @param commentaireDto à créer
33
     * @return commentaire créé
34
     */
35
    @PostMapping(value = "/{id}")
36
    @ResponseStatus(HttpStatus.CREATED)
37
    @Operation(summary = "Créer un commentaire", description = "Retourne le commentaire créé.")
38
    public CommentaireDto create(@PathVariable("id") Long id, @RequestBody CommentaireDto commentaireDto) {
39 1 1. create : negated conditional → KILLED
        if(StringUtils.isNullOrEmpty(commentaireDto.getCommentaire()))
40
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Précisez un commentaire.");
41 1 1. create : negated conditional → KILLED
        if(commentaireDto.getParticipant() == null)
42
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Précisez un participant.");
43
        try{
44
            var model = mapper.map(commentaireDto, Commentaire.class);
45
            var result = service.create(id, commentaireDto.getParticipant(), model);
46 1 1. create : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/CommentaireController::create → KILLED
            return mapper.map(result, CommentaireDto.class);
47
        }
48
        catch(NoSuchElementException e){
49
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
50
        }
51
        catch (Exception e) {
52
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la création du commentaire.");
53
        }
54
    }
55
56
    /**
57
     * Récupération des commentaires d'un sondage
58
     * @param id du sondage
59
     * @return la liste des commentaires récupérés
60
     */
61
    @GetMapping(value = "/{id}")
62
    @ResponseStatus(HttpStatus.OK)
63
    @Operation(summary = "Récupérer tous les commentaires d'un sondage", description = "Retourne la liste des commentaires récupérés.")
64
    public List<CommentaireDto> getAllCommentairesBySondageId(@PathVariable("id") Long id) {
65
        try {
66
            var models = service.getBySondageId(id);
67 1 1. getAllCommentairesBySondageId : replaced return value with Collections.emptyList for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/CommentaireController::getAllCommentairesBySondageId → KILLED
            return models.stream()
68 1 1. lambda$getAllCommentairesBySondageId$0 : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/CommentaireController::lambda$getAllCommentairesBySondageId$0 → KILLED
                    .map(model -> mapper.map(model, CommentaireDto.class))
69
                    .toList();
70
        }
71
        catch (NoSuchElementException e) {
72
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
73
        }
74
        catch (NoResultException e) {
75
            throw new ResponseStatusException(HttpStatus.NO_CONTENT, e.getMessage());
76
        }
77
        catch (Exception e) {
78
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la récupération des commentaires.");
79
        }
80
    }
81
82
    /**
83
     * Modifier un commentaire
84
     * @param id du commentaire
85
     * @param commentaireDto à modifier
86
     * @return commentaire modifié
87
     */
88
    @PutMapping(value = "/{id}")
89
    @ResponseStatus(HttpStatus.OK)
90
    @Operation(summary = "Modifier un commentaire", description = "Retourne le commentaire modifié.")
91
    public CommentaireDto update(@PathVariable("id") Long id, @RequestBody CommentaireDto commentaireDto) {
92 1 1. update : negated conditional → KILLED
        if(StringUtils.isNullOrEmpty(commentaireDto.getCommentaire()))
93
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Précisez un commentaire.");
94
        try {
95 1 1. update : negated conditional → KILLED
            if (mapper.map(service.getById(id), CommentaireDto.class).equals(commentaireDto))
96
                throw new NoUpdateException("Le commentaire n'a pas été modifié.");
97
            var model = mapper.map(commentaireDto, Commentaire.class);
98
            var result = service.update(id, model);
99 1 1. update : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/CommentaireController::update → KILLED
            return mapper.map(result, CommentaireDto.class);
100
        }
101
        catch (NoSuchElementException e) {
102
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
103
        }
104
        catch (NoUpdateException e) {
105
            throw new ResponseStatusException(HttpStatus.NO_CONTENT, e.getMessage());
106
        }
107
        catch (Exception e) {
108
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la modification du commentaire.");
109
        }
110
    }
111
112
    /**
113
     * Suppression d'un commentaire
114
     * @param id du commentaire
115
     */
116
    @Operation(summary = "Supprimer un commentaire", description = "Ne retourne rien.")
117
    @DeleteMapping(value = "/{id}")
118
    @ResponseStatus(HttpStatus.NO_CONTENT)
119
    public void delete(@PathVariable("id") Long id) {
120
        try {
121 1 1. delete : removed call to fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/services/CommentaireService::delete → KILLED
            service.delete(id);
122
        }
123
        catch (NoSuchElementException e) {
124
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
125
        }
126
        catch (Exception e) {
127
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la suppression du commentaire.");
128
        }
129
    }
130
}

Mutations

39

1.1
Location : create
Killed by : fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest.[engine:junit-jupiter]/[class:fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest]/[method:givenInvalidCommentaire_whenCreate_thenReturnBadRequest()]
negated conditional → KILLED

41

1.1
Location : create
Killed by : fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest.[engine:junit-jupiter]/[class:fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest]/[method:givenValidParameters_whenCreateButServerError_thenReturnInternalServerError()]
negated conditional → KILLED

46

1.1
Location : create
Killed by : fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest.[engine:junit-jupiter]/[class:fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest]/[method:givenValidParameters_whenCreate_thenReturnCreated()]
replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/CommentaireController::create → KILLED

67

1.1
Location : getAllCommentairesBySondageId
Killed by : fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest.[engine:junit-jupiter]/[class:fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest]/[method:givenValidParameters_whenGetAllCommentairesBySondageId_thenReturnOk()]
replaced return value with Collections.emptyList for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/CommentaireController::getAllCommentairesBySondageId → KILLED

68

1.1
Location : lambda$getAllCommentairesBySondageId$0
Killed by : fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest.[engine:junit-jupiter]/[class:fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest]/[method:givenValidParameters_whenGetAllCommentairesBySondageId_thenReturnOk()]
replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/CommentaireController::lambda$getAllCommentairesBySondageId$0 → KILLED

92

1.1
Location : update
Killed by : fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest.[engine:junit-jupiter]/[class:fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest]/[method:givenInvalidCommentaire_whenUpdateButCommentaireDoesNotExists_thenReturnBadRequest()]
negated conditional → KILLED

95

1.1
Location : update
Killed by : fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest.[engine:junit-jupiter]/[class:fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest]/[method:givenValidParameters_whenUpdateButNoChange_thenReturnNoContent()]
negated conditional → KILLED

99

1.1
Location : update
Killed by : fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest.[engine:junit-jupiter]/[class:fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest]/[method:givenValidParameters_whenUpdate_thenReturnOk()]
replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/CommentaireController::update → KILLED

121

1.1
Location : delete
Killed by : fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest.[engine:junit-jupiter]/[class:fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.unit.controllers.CommentaireControllerUnitTest]/[method:givenValidParameters_whenDelete_thenReturnNoContent()]
removed call to fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/services/CommentaireService::delete → KILLED

Active mutators

Tests examined


Report generated by PIT 1.15.3