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