AddToCartServlet.java

1
package com.popx.presentazione;
2
3
import com.popx.modello.ProdottoBean;
4
import com.popx.modello.UserBean;
5
import com.popx.persistenza.*;
6
7
import javax.servlet.ServletException;
8
import javax.servlet.annotation.WebServlet;
9
import javax.servlet.http.*;
10
import java.io.IOException;
11
import java.sql.SQLException;
12
import java.util.ArrayList;
13
import java.util.List;
14
15
@WebServlet("/addToCart")
16
public class AddToCartServlet extends HttpServlet {
17
18
    private final ProdottoDAO prodottoDAO;
19
    private final UserDAO<UserBean> userDAO;
20
21
    // �� costruttore production
22
    public AddToCartServlet() {
23
        this.prodottoDAO = new ProdottoDAOImpl();
24
        this.userDAO = new UserDAOImpl();
25
    }
26
27
    // �� costruttore test
28
    public AddToCartServlet(ProdottoDAO prodottoDAO, UserDAO<UserBean> userDAO) {
29
        this.prodottoDAO = prodottoDAO;
30
        this.userDAO = userDAO;
31
    }
32
33
    @Override
34
    public void doPost(HttpServletRequest request, HttpServletResponse response)
35
            throws ServletException, IOException {
36
37
        String productId = request.getParameter("productId");
38
        String qtyParam = request.getParameter("quantity");
39
40
        int quantity;
41
        try {
42
            quantity = Integer.parseInt(qtyParam);
43 2 1. doPost : changed conditional boundary → SURVIVED
2. doPost : negated conditional → KILLED
            if (quantity <= 0) {
44 1 1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → NO_COVERAGE
                writeJson(response, false, "Quantità non valida.");
45
                return;
46
            }
47
        } catch (NumberFormatException e) {
48 1 1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → NO_COVERAGE
            writeJson(response, false, "Quantità non valida.");
49
            return;
50
        }
51
52
        HttpSession session = request.getSession();
53
        String userEmail = (String) session.getAttribute("userEmail");
54
55
        // �� controllo ruolo
56 1 1. doPost : negated conditional → KILLED
        if (userEmail != null) {
57
            try {
58
                UserBean userBean = userDAO.getUserByEmail(userEmail);
59 2 1. doPost : negated conditional → KILLED
2. doPost : negated conditional → KILLED
                if (userBean != null && !"User".equals(userBean.getRole())) {
60 1 1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED
                    writeJson(response, false,
61
                            "Accesso negato: solo gli utenti con il ruolo 'User' possono aggiungere prodotti al carrello.");
62
                    return;
63
                }
64
            } catch (SQLException e) {
65 1 1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED
                writeJson(response, false, "Errore interno durante il controllo dei permessi.");
66
                return;
67
            }
68
        }
69
70
        List<ProdottoBean> cart = (List<ProdottoBean>) session.getAttribute("cart");
71 1 1. doPost : negated conditional → KILLED
        if (cart == null) {
72
            cart = new ArrayList<>();
73 1 1. doPost : removed call to javax/servlet/http/HttpSession::setAttribute → KILLED
            session.setAttribute("cart", cart);
74
        }
75
76
        ProdottoBean prodotto = prodottoDAO.getProdottoById(productId);
77 1 1. doPost : negated conditional → KILLED
        if (prodotto == null) {
78 1 1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED
            writeJson(response, false, "Prodotto non trovato.");
79
            return;
80
        }
81
82
        for (ProdottoBean cartItem : cart) {
83 1 1. doPost : negated conditional → KILLED
            if (cartItem.getId().equals(prodotto.getId())) {
84
                int currentQty = prodottoDAO.getProductQtyInCart(session, productId);
85 3 1. doPost : changed conditional boundary → KILLED
2. doPost : Replaced integer addition with subtraction → KILLED
3. doPost : negated conditional → KILLED
                if (currentQty + quantity <= prodotto.getPiecesInStock()) {
86 2 1. doPost : Replaced integer addition with subtraction → KILLED
2. doPost : removed call to com/popx/persistenza/ProdottoDAO::updateProductQtyInCart → KILLED
                    prodottoDAO.updateProductQtyInCart(session, productId, currentQty + quantity);
87 1 1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED
                    writeJson(response, true, "Prodotto aggiunto al carrello!");
88
                    return;
89
                } else {
90 1 1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED
                    writeJson(response, false, "Quantità non disponibile nel magazzino.");
91
                    return;
92
                }
93
            }
94
        }
95
96
        cart.add(prodotto);
97 1 1. doPost : removed call to com/popx/persistenza/ProdottoDAO::updateProductQtyInCart → KILLED
        prodottoDAO.updateProductQtyInCart(session, productId, quantity);
98 1 1. doPost : removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED
        writeJson(response, true, "Prodotto aggiunto al carrello!");
99
    }
100
101
102
    private void writeJson(HttpServletResponse response, boolean success, String message) throws IOException {
103 1 1. writeJson : removed call to javax/servlet/http/HttpServletResponse::setContentType → SURVIVED
        response.setContentType("application/json");
104
        response.getWriter()
105 1 1. writeJson : removed call to java/io/PrintWriter::write → KILLED
                .write("{\"success\": " + success + ", \"message\": \"" + message + "\"}");
106
    }
107
}

Mutations

43

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

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

44

1.1
Location : doPost
Killed by : none
removed call to com/popx/presentazione/AddToCartServlet::writeJson → NO_COVERAGE

48

1.1
Location : doPost
Killed by : none
removed call to com/popx/presentazione/AddToCartServlet::writeJson → NO_COVERAGE

56

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

59

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

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

60

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_loggedUserWithWrongRole_denied()]
removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED

65

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_sqlExceptionDuringRoleCheck_returnsError()]
removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED

71

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

73

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_guestUser_addsProductSuccessfully()]
removed call to javax/servlet/http/HttpSession::setAttribute → KILLED

77

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

78

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_productNotFound_returnsError()]
removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED

83

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

85

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_quantityExactlyEqualsStock_allowed()]
changed conditional boundary → KILLED

2.2
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_quantityExceedsStock_returnsError()]
Replaced integer addition with subtraction → KILLED

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

86

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_quantityExactlyEqualsStock_allowed()]
Replaced integer addition with subtraction → KILLED

2.2
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_quantityExactlyEqualsStock_allowed()]
removed call to com/popx/persistenza/ProdottoDAO::updateProductQtyInCart → KILLED

87

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_quantityExactlyEqualsStock_allowed()]
removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED

90

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_quantityExceedsStock_returnsError()]
removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED

97

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_guestUser_addsProductSuccessfully()]
removed call to com/popx/persistenza/ProdottoDAO::updateProductQtyInCart → KILLED

98

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.AddToCartServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.AddToCartServletTest]/[method:addToCart_guestUser_addsProductSuccessfully()]
removed call to com/popx/presentazione/AddToCartServlet::writeJson → KILLED

103

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

105

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

Active mutators

Tests examined


Report generated by PIT 1.15.2