| 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 2.2 |
|
| 44 |
1.1 |
|
| 48 |
1.1 |
|
| 56 |
1.1 |
|
| 59 |
1.1 2.2 |
|
| 60 |
1.1 |
|
| 65 |
1.1 |
|
| 71 |
1.1 |
|
| 73 |
1.1 |
|
| 77 |
1.1 |
|
| 78 |
1.1 |
|
| 83 |
1.1 |
|
| 85 |
1.1 2.2 3.3 |
|
| 86 |
1.1 2.2 |
|
| 87 |
1.1 |
|
| 90 |
1.1 |
|
| 97 |
1.1 |
|
| 98 |
1.1 |
|
| 103 |
1.1 |
|
| 105 |
1.1 |