ProductServlet.java

1
package com.popx.presentazione;
2
3
import com.google.gson.JsonObject;
4
import com.popx.modello.ProdottoBean;
5
import com.popx.persistenza.DataSourceSingleton;
6
import com.popx.persistenza.ProdottoDAOImpl;
7
8
import javax.servlet.ServletException;
9
import javax.servlet.annotation.MultipartConfig;
10
import javax.servlet.annotation.WebServlet;
11
import javax.servlet.http.HttpServlet;
12
import javax.servlet.http.HttpServletRequest;
13
import javax.servlet.http.HttpServletResponse;
14
import javax.servlet.http.Part;
15
import java.io.IOException;
16
import java.io.InputStream;
17
18
@WebServlet("/addProductServlet")
19
@MultipartConfig(maxFileSize = 2 * 1024 * 1024)
20
public class ProductServlet extends HttpServlet {
21
22
    private static final long serialVersionUID = 1L;
23
24
    private ProdottoDAOImpl prodottoDAO;
25
26
    // �� costruttore production (Tomcat)
27
    public ProductServlet() {
28
        this.prodottoDAO = new ProdottoDAOImpl(DataSourceSingleton.getInstance());
29
    }
30
31
    // �� costruttore test
32
    public ProductServlet(ProdottoDAOImpl prodottoDAO) {
33
        this.prodottoDAO = prodottoDAO;
34
    }
35
36
    @Override
37
    public void doPost(HttpServletRequest request, HttpServletResponse response)
38
            throws IOException {
39
40 1 1. doPost : removed call to javax/servlet/http/HttpServletResponse::setContentType → SURVIVED
        response.setContentType("application/json");
41
        JsonObject jsonResponse = new JsonObject();
42
43
        try {
44
            String id = request.getParameter("idProduct");
45
            String name = request.getParameter("name");
46
            String description = request.getParameter("description");
47
            String costStr = request.getParameter("price");
48
            String piecesInStockStr = request.getParameter("qty");
49
            String brand = request.getParameter("brand");
50
            String figure = request.getParameter("figure");
51
            Part imgPart = request.getPart("img_src");
52
53 3 1. doPost : negated conditional → KILLED
2. doPost : negated conditional → KILLED
3. doPost : negated conditional → KILLED
            if (id == null || id.isEmpty() ||
54 2 1. doPost : negated conditional → KILLED
2. doPost : negated conditional → KILLED
                    name == null || name.isEmpty() ||
55 2 1. doPost : negated conditional → KILLED
2. doPost : negated conditional → KILLED
                    costStr == null || costStr.isEmpty() ||
56 1 1. doPost : negated conditional → KILLED
                    piecesInStockStr == null || piecesInStockStr.isEmpty()) {
57
58 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED
                jsonResponse.addProperty("success", false);
59 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → KILLED
                jsonResponse.addProperty("message", "Campi obbligatori mancanti.");
60 1 1. doPost : removed call to java/io/PrintWriter::write → KILLED
                response.getWriter().write(jsonResponse.toString());
61
                return;
62
            }
63
64
            double cost = Double.parseDouble(costStr);
65
            int piecesInStock = Integer.parseInt(piecesInStockStr);
66
67
            byte[] imgBytes = null;
68 2 1. doPost : negated conditional → KILLED
2. doPost : negated conditional → KILLED
            if (imgPart != null && imgPart.getContentType().startsWith("image/")) {
69
                try (InputStream imgInputStream = imgPart.getInputStream()) {
70
                    imgBytes = imgInputStream.readAllBytes();
71
                }
72 1 1. doPost : negated conditional → KILLED
            } else if (imgPart != null) {
73 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED
                jsonResponse.addProperty("success", false);
74 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → KILLED
                jsonResponse.addProperty("message", "Il file caricato non è un'immagine valida.");
75 1 1. doPost : removed call to java/io/PrintWriter::write → KILLED
                response.getWriter().write(jsonResponse.toString());
76
                return;
77
            }
78
79
            ProdottoBean prodotto = new ProdottoBean(
80
                    id, name, description, cost, piecesInStock, brand, imgBytes, figure
81
            );
82
83
            boolean isSaved = prodottoDAO.saveProdotto(prodotto);
84
85 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → KILLED
            jsonResponse.addProperty("success", isSaved);
86 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → KILLED
            jsonResponse.addProperty(
87
                    "message",
88 1 1. doPost : negated conditional → KILLED
                    isSaved ? "Prodotto aggiunto con successo." : "Errore durante il salvataggio del prodotto."
89
            );
90
91
        } catch (Exception e) {
92 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → NO_COVERAGE
            jsonResponse.addProperty("success", false);
93 1 1. doPost : removed call to com/google/gson/JsonObject::addProperty → NO_COVERAGE
            jsonResponse.addProperty("message", "Errore interno.");
94
        }
95
96 1 1. doPost : removed call to java/io/PrintWriter::write → KILLED
        response.getWriter().write(jsonResponse.toString());
97
    }
98
}

Mutations

40

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

53

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

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

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

54

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

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

55

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

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

56

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

58

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

59

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.ProductServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.ProductServletTest]/[method:addProduct_missingRequiredFields_returnsError()]
removed call to com/google/gson/JsonObject::addProperty → KILLED

60

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.ProductServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.ProductServletTest]/[method:addProduct_missingRequiredFields_returnsError()]
removed call to java/io/PrintWriter::write → KILLED

68

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

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

72

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

73

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

74

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.ProductServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.ProductServletTest]/[method:addProduct_invalidImage_returnsError()]
removed call to com/google/gson/JsonObject::addProperty → KILLED

75

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.ProductServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.ProductServletTest]/[method:addProduct_invalidImage_returnsError()]
removed call to java/io/PrintWriter::write → KILLED

85

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.ProductServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.ProductServletTest]/[method:addProduct_success_returnsSuccessJson()]
removed call to com/google/gson/JsonObject::addProperty → KILLED

86

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.ProductServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.ProductServletTest]/[method:addProduct_daoFails_returnsErrorJson()]
removed call to com/google/gson/JsonObject::addProperty → KILLED

88

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

92

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

93

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

96

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.ProductServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.ProductServletTest]/[method:addProduct_daoFails_returnsErrorJson()]
removed call to java/io/PrintWriter::write → KILLED

Active mutators

Tests examined


Report generated by PIT 1.15.2