CheckoutServlet.java

1
package com.popx.presentazione;
2
3
import com.popx.modello.*;
4
import com.popx.persistenza.*;
5
6
import javax.servlet.ServletException;
7
import javax.servlet.annotation.WebServlet;
8
import javax.servlet.http.*;
9
import java.io.IOException;
10
import java.sql.Date;
11
import java.util.List;
12
13
@WebServlet(name = "CheckoutServlet", value = "/CheckoutServlet")
14
public class CheckoutServlet extends HttpServlet {
15
16
    private CarrelloDAO carrelloDAO;
17
    private OrdineDAO ordineDAO;
18
    private RigaOrdineDAO rigaOrdineDAO;
19
    private ProdottoDAO prodottoDAO;
20
21
    // �� costruttore production
22
    public CheckoutServlet() {
23
        this.carrelloDAO = new CarrelloDAOImpl();
24
        this.ordineDAO = new OrdineDAOImpl();
25
        this.rigaOrdineDAO = new RigaOrdineDAOImpl();
26
        this.prodottoDAO = new ProdottoDAOImpl();
27
    }
28
29
    // �� costruttore test
30
    public CheckoutServlet(
31
            CarrelloDAO carrelloDAO,
32
            OrdineDAO ordineDAO,
33
            RigaOrdineDAO rigaOrdineDAO,
34
            ProdottoDAO prodottoDAO
35
    ) {
36
        this.carrelloDAO = carrelloDAO;
37
        this.ordineDAO = ordineDAO;
38
        this.rigaOrdineDAO = rigaOrdineDAO;
39
        this.prodottoDAO = prodottoDAO;
40
    }
41
42
    @Override
43
    public void doPost(HttpServletRequest request, HttpServletResponse response)
44
            throws ServletException, IOException {
45
46
        HttpSession session = request.getSession();
47
        String userEmail = (String) session.getAttribute("userEmail");
48
49 1 1. doPost : negated conditional → KILLED
        if (userEmail == null) {
50 1 1. doPost : removed call to javax/servlet/http/HttpServletResponse::setStatus → KILLED
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
51 1 1. doPost : removed call to java/io/PrintWriter::write → SURVIVED
            response.getWriter().write("<script>alert('Utente non autenticato');</script>");
52
            return;
53
        }
54
55
        List<ProdottoBean> cart =
56
                (List<ProdottoBean>) session.getAttribute("cart");
57
58 2 1. doPost : negated conditional → KILLED
2. doPost : negated conditional → KILLED
        if (cart == null || cart.isEmpty()) {
59 1 1. doPost : removed call to javax/servlet/http/HttpServletResponse::setStatus → KILLED
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
60 1 1. doPost : removed call to java/io/PrintWriter::write → SURVIVED
            response.getWriter().write("<script>alert('Il carrello è vuoto.');</script>");
61
            return;
62
        }
63
64
        double subtotal = 0;
65
        for (ProdottoBean p : cart) {
66 2 1. doPost : Replaced double addition with subtraction → SURVIVED
2. doPost : Replaced double multiplication with division → SURVIVED
            subtotal += p.getQty() * p.getCost();
67
        }
68
69
        OrdineBean ordine = new OrdineBean(
70
                (float) subtotal,
71
                userEmail,
72
                new Date(System.currentTimeMillis())
73
        );
74
75
        try {
76
            ordineDAO.insertOrdine(ordine);
77
            int ordineId = ordine.getId();
78
79
            for (ProdottoBean p : cart) {
80 1 1. doPost : removed call to com/popx/persistenza/RigaOrdineDAO::addRigaOrdine → KILLED
                rigaOrdineDAO.addRigaOrdine(
81
                        new RigaOrdineBean(
82
                                ordineId,
83
                                p.getId(),
84
                                p.getQty(),
85
                                (float) p.getCost()
86
                        )
87
                );
88
89 1 1. doPost : removed call to com/popx/persistenza/ProdottoDAO::updateStock → KILLED
                prodottoDAO.updateStock(
90
                        p.getId(),
91 1 1. doPost : Replaced integer subtraction with addition → KILLED
                        p.getPiecesInStock() - p.getQty()
92
                );
93
            }
94
95 1 1. doPost : removed call to com/popx/persistenza/CarrelloDAO::clearCartByUserEmail → KILLED
            carrelloDAO.clearCartByUserEmail(userEmail);
96 1 1. doPost : removed call to javax/servlet/http/HttpSession::setAttribute → KILLED
            session.setAttribute("cart", null);
97
98 1 1. doPost : removed call to javax/servlet/http/HttpServletResponse::setStatus → SURVIVED
            response.setStatus(HttpServletResponse.SC_OK);
99 1 1. doPost : removed call to java/io/PrintWriter::write → KILLED
            response.getWriter().write(
100
                    "<script>alert('Ordine completato con successo!');</script>"
101
            );
102
103
        } catch (Exception e) {
104 1 1. doPost : removed call to javax/servlet/http/HttpServletResponse::setStatus → KILLED
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
105 1 1. doPost : removed call to java/io/PrintWriter::write → SURVIVED
            response.getWriter().write(
106
                    "<script>alert('Errore interno nel completamento dell'ordine.');</script>"
107
            );
108
        }
109
    }
110
}

Mutations

49

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

50

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.CheckoutServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.CheckoutServletTest]/[method:checkout_notAuthenticated_returns401()]
removed call to javax/servlet/http/HttpServletResponse::setStatus → KILLED

51

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

58

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

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

59

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.CheckoutServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.CheckoutServletTest]/[method:checkout_emptyCart_returns400()]
removed call to javax/servlet/http/HttpServletResponse::setStatus → KILLED

60

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

66

1.1
Location : doPost
Killed by : none
Replaced double addition with subtraction → SURVIVED

2.2
Location : doPost
Killed by : none
Replaced double multiplication with division → SURVIVED

80

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.CheckoutServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.CheckoutServletTest]/[method:checkout_successfulOrder()]
removed call to com/popx/persistenza/RigaOrdineDAO::addRigaOrdine → KILLED

89

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

91

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

95

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.CheckoutServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.CheckoutServletTest]/[method:checkout_successfulOrder()]
removed call to com/popx/persistenza/CarrelloDAO::clearCartByUserEmail → KILLED

96

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

98

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

99

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

104

1.1
Location : doPost
Killed by : com.popx.integration.presentazione.CheckoutServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.CheckoutServletTest]/[method:checkout_exception_returns500()]
removed call to javax/servlet/http/HttpServletResponse::setStatus → KILLED

105

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