UpdateProductServlet.java

1
package com.popx.presentazione;
2
3
import com.google.gson.JsonObject;
4
import com.popx.modello.ProdottoBean;
5
import com.popx.persistenza.ProdottoDAOImpl;
6
7
import javax.servlet.ServletException;
8
import javax.servlet.annotation.MultipartConfig;
9
import javax.servlet.annotation.WebServlet;
10
import javax.servlet.http.*;
11
import java.io.IOException;
12
import java.io.InputStream;
13
14
@WebServlet("/updateProductServlet")
15
@MultipartConfig(maxFileSize = 2 * 1024 * 1024)
16
public class UpdateProductServlet extends HttpServlet {
17
18
    private ProdottoDAOImpl prodottoDAO;
19
20
    // �� costruttore production
21
    public UpdateProductServlet() {
22
        this.prodottoDAO = new ProdottoDAOImpl();
23
    }
24
25
    // �� costruttore test
26
    public UpdateProductServlet(ProdottoDAOImpl prodottoDAO) {
27
        this.prodottoDAO = prodottoDAO;
28
    }
29
30
    @Override
31
    public void doPost(HttpServletRequest request, HttpServletResponse response)
32
            throws ServletException, IOException {
33
34 1 1. doPost : removed call to javax/servlet/http/HttpServletResponse::setContentType → SURVIVED
        response.setContentType("application/json");
35
        JsonObject jsonResponse = new JsonObject();
36
37
        try {
38
            String idProduct = request.getParameter("idProduct");
39
            String name = request.getParameter("name");
40
            String description = request.getParameter("description");
41
            double price = Double.parseDouble(request.getParameter("price"));
42
            int qty = Integer.parseInt(request.getParameter("qty"));
43
            String brand = request.getParameter("brand");
44
            String figure = request.getParameter("figure");
45
            Part imgPart = request.getPart("img_src");
46
            String currentImgSrc = request.getParameter("current_img_src");
47
48
            ProdottoBean prodotto =
49
                    new ProdottoBean(idProduct, name, description, price, qty, brand, null, figure);
50
51
            // �� gestione immagine
52 3 1. doPost : changed conditional boundary → SURVIVED
2. doPost : negated conditional → KILLED
3. doPost : negated conditional → KILLED
            if (imgPart != null && imgPart.getSize() > 0) {
53 1 1. doPost : negated conditional → SURVIVED
                if (!imgPart.getContentType().startsWith("image/")) {
54 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED
                    jsonResponse.addProperty("success", false);
55 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED
                    jsonResponse.addProperty("message", "Il file caricato non è un'immagine valida.");
56 1 1. doPost : removed call to java/io/PrintWriter::write → SURVIVED
                    response.getWriter().write(jsonResponse.toString());
57
                    return;
58
                }
59
60
                try (InputStream is = imgPart.getInputStream()) {
61 1 1. doPost : removed call to com/popx/modello/ProdottoBean::setImg → NO_COVERAGE
                    prodotto.setImg(is.readAllBytes());
62
                }
63 2 1. doPost : negated conditional → SURVIVED
2. doPost : negated conditional → SURVIVED
            } else if (currentImgSrc != null && !currentImgSrc.isEmpty()) {
64
                ProdottoBean existing = prodottoDAO.getProdottoById(idProduct);
65 1 1. doPost : negated conditional → SURVIVED
                if (existing != null) {
66 1 1. doPost : removed call to com/popx/modello/ProdottoBean::setImg → SURVIVED
                    prodotto.setImg(existing.getImg());
67
                }
68
            }
69
70
            boolean updated = prodottoDAO.updateProduct(prodotto);
71
72 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED
            jsonResponse.addProperty("success", updated);
73 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED
            jsonResponse.addProperty(
74
                    "message",
75 1 1. doPost : negated conditional → SURVIVED
                    updated ? "Prodotto aggiornato con successo."
76
                            : "Errore durante l'aggiornamento del prodotto."
77
            );
78
79
        } catch (Exception e) {
80 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED
            jsonResponse.addProperty("success", false);
81 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED
            jsonResponse.addProperty("message", "Errore interno: " + e.getMessage());
82
        }
83
84 1 1. doPost : removed call to java/io/PrintWriter::write → SURVIVED
        response.getWriter().write(jsonResponse.toString());
85
    }
86
}

Mutations

34

1.1
Location : doPost
Killed by : none
removed call to javax/servlet/http/HttpServletResponse::setContentType → SURVIVED

52

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.UpdateProductServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.UpdateProductServletTest]/[method:invalidImage_returnsError()]
negated conditional → KILLED

2.2
Location : doPost
Killed by : none
changed conditional boundary → SURVIVED

3.3
Location : doPost
Killed by : com.popx.integration.presentazione.UpdateProductServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.UpdateProductServletTest]/[method:invalidImage_returnsError()]
negated conditional → KILLED

53

1.1
Location : doPost
Killed by : none
negated conditional → SURVIVED

54

1.1
Location : doPost
Killed by : none
removed call to com/google/gson/JsonObject::addProperty → SURVIVED

55

1.1
Location : doPost
Killed by : none
removed call to com/google/gson/JsonObject::addProperty → SURVIVED

56

1.1
Location : doPost
Killed by : none
removed call to java/io/PrintWriter::write → SURVIVED

61

1.1
Location : doPost
Killed by : none
removed call to com/popx/modello/ProdottoBean::setImg → NO_COVERAGE

63

1.1
Location : doPost
Killed by : none
negated conditional → SURVIVED

2.2
Location : doPost
Killed by : none
negated conditional → SURVIVED

65

1.1
Location : doPost
Killed by : none
negated conditional → SURVIVED

66

1.1
Location : doPost
Killed by : none
removed call to com/popx/modello/ProdottoBean::setImg → SURVIVED

72

1.1
Location : doPost
Killed by : none
removed call to com/google/gson/JsonObject::addProperty → SURVIVED

73

1.1
Location : doPost
Killed by : none
removed call to com/google/gson/JsonObject::addProperty → SURVIVED

75

1.1
Location : doPost
Killed by : none
negated conditional → SURVIVED

80

1.1
Location : doPost
Killed by : none
removed call to com/google/gson/JsonObject::addProperty → SURVIVED

81

1.1
Location : doPost
Killed by : none
removed call to com/google/gson/JsonObject::addProperty → SURVIVED

84

1.1
Location : doPost
Killed by : none
removed call to java/io/PrintWriter::write → SURVIVED

Active mutators

Tests examined


Report generated by PIT 1.15.2