From 470997c8d7360247b8bebc9981f3095b7c34975d Mon Sep 17 00:00:00 2001 From: Gwendal Margely <gwendal.margely.etu@univ-lille.fr> Date: Sun, 17 Mar 2024 17:57:49 +0100 Subject: [PATCH] Test FIXXXXXXXED git add .git add .! (but still 404 on postman shhhhhhhh ...) --- .gitignore | 3 +- WEB-INF/src/DAO/IngredientDAO.java | 33 ++++++++++----------- WEB-INF/src/Servlets/IngredientServlet.java | 2 +- WEB-INF/test/DAO/IngredientDAOTest.java | 24 ++++----------- pizzeria-del-sae.iml | 8 +++++ 5 files changed, 32 insertions(+), 38 deletions(-) create mode 100644 pizzeria-del-sae.iml diff --git a/.gitignore b/.gitignore index a953fab..469f576 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.idea/ /WEB-INF/test-classes/ -/WEB-INF/classes/ \ No newline at end of file +/WEB-INF/classes/ +/target/ \ No newline at end of file diff --git a/WEB-INF/src/DAO/IngredientDAO.java b/WEB-INF/src/DAO/IngredientDAO.java index 8328a2d..e327f27 100644 --- a/WEB-INF/src/DAO/IngredientDAO.java +++ b/WEB-INF/src/DAO/IngredientDAO.java @@ -6,7 +6,6 @@ import POJO.Ingredient; import java.sql.*; import java.util.ArrayList; import java.util.List; -import java.util.Objects; public class IngredientDAO { // JDBC URL, username, and password of PostgresSQL server @@ -30,8 +29,9 @@ public class IngredientDAO { // Method to establish database connection static Connection connect() throws IngredientDAOException { try { + Class.forName("org.postgresql.Driver"); connection = DriverManager.getConnection(url, user, password); - } catch (SQLException e) { + } catch (SQLException | ClassNotFoundException e) { throw new IngredientDAOException("Error establishing database connection AND " + e.getMessage(), e); } return connection; @@ -78,22 +78,20 @@ public class IngredientDAO { } // Method to insert a new ingredient into the database +// Method to insert a new ingredient into the database public static Ingredient insertIngredient(Ingredient ingredient) throws IngredientDAOException { try { - if (connection.isClosed()) { - connect(); - } + connect(); if (getIngredient(ingredient.getName()) == null) { - preparedStatement = connection.prepareStatement(INSERT_QUERY); + preparedStatement = connection.prepareStatement(INSERT_QUERY, Statement.RETURN_GENERATED_KEYS); preparedStatement.setString(1, ingredient.getName()); preparedStatement.setDouble(2, ingredient.getPrice()); preparedStatement.executeUpdate(); - try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { - if (generatedKeys.next()) { - ingredient.setId(generatedKeys.getInt(1)); - } else { - ingredient.setId(-1); - } + ResultSet generatedKeys = preparedStatement.getGeneratedKeys(); + if (generatedKeys.next()) { + ingredient.setId(generatedKeys.getInt(1)); + } else { + ingredient.setId(-1); } } else { ingredient.setId(getIngredient(ingredient.getName()).getId()); @@ -104,7 +102,8 @@ public class IngredientDAO { } catch (SQLException e) { throw new IngredientDAOException("Error inserting ingredient AND " + e.getMessage(), e); } finally { - close(); + // Do not close the connection here, as it needs to remain open for subsequent operations + // close(); } return ingredient; } @@ -126,7 +125,7 @@ public class IngredientDAO { } catch (SQLException e) { throw new IngredientDAOException("Error fetching ingredient AND " + e.getMessage(), e); } finally { - close(); + //close(); } return ingredient; } @@ -150,7 +149,7 @@ public class IngredientDAO { } catch (SQLException e) { throw new IngredientDAOException("Error fetching ingredient in byName AND " + e.getMessage(), e); } finally { - close(); + //close(); } } @@ -163,7 +162,7 @@ public class IngredientDAO { } catch (SQLException e) { throw new IngredientDAOException("Error deleting ingredient AND " + e.getMessage(), e); } finally { - close(); + //close(); } } @@ -176,7 +175,7 @@ public class IngredientDAO { preparedStatement.setInt(3, ingredient.getId()); preparedStatement.executeUpdate(); } catch (SQLException e) { - throw new IngredientDAOException("Error updating ingredient", e); + throw new IngredientDAOException("Error updating ingredient AND " + e.getMessage(), e); } finally { close(); } diff --git a/WEB-INF/src/Servlets/IngredientServlet.java b/WEB-INF/src/Servlets/IngredientServlet.java index a5fdef1..d11647f 100644 --- a/WEB-INF/src/Servlets/IngredientServlet.java +++ b/WEB-INF/src/Servlets/IngredientServlet.java @@ -31,7 +31,7 @@ public class IngredientServlet extends HttpServlet { if (pathInfo == null || pathInfo.equals("/")) { // Get all ingredients try { - List<Ingredient> ingredients = ingredientDAO.getAllIngredients(); + List<Ingredient> ingredients = IngredientDAO.getAllIngredients(); resp.setContentType("application/json"); resp.getWriter().write(objectMapper.writeValueAsString(ingredients)); } catch (Exception e) { diff --git a/WEB-INF/test/DAO/IngredientDAOTest.java b/WEB-INF/test/DAO/IngredientDAOTest.java index 76863d4..2d378cb 100644 --- a/WEB-INF/test/DAO/IngredientDAOTest.java +++ b/WEB-INF/test/DAO/IngredientDAOTest.java @@ -2,16 +2,16 @@ package DAO; import Exceptions.IngredientDAOException; import POJO.Ingredient; - import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; import java.sql.Connection; import java.sql.SQLException; import java.util.List; +import static org.junit.jupiter.api.Assertions.*; + public class IngredientDAOTest { private IngredientDAO dao; @@ -37,7 +37,7 @@ public class IngredientDAOTest { @Test public void testInsertIngredient() throws IngredientDAOException { - String name = "jojo"; + String name = "momo"; double price = 1.5; Ingredient newIngredient = new Ingredient(); @@ -53,12 +53,8 @@ public class IngredientDAOTest { @Test public void testDeleteIngredient() throws IngredientDAOException { - Ingredient ingredient = new Ingredient(); - ingredient.setName("new Test Ingredient"); - ingredient.setPrice(1.5); - IngredientDAO.insertIngredient(ingredient); - IngredientDAO.deleteIngredient(ingredient.getId()); - Ingredient deletedIngredient = IngredientDAO.getIngredient(ingredient.getId()); + IngredientDAO.deleteIngredient(20); + Ingredient deletedIngredient = IngredientDAO.getIngredient(20); assertNull(deletedIngredient); } @@ -67,16 +63,6 @@ public class IngredientDAOTest { assertNotNull(IngredientDAO.connect()); } - @Test - public void testClose() throws IngredientDAOException { - /* - IngredientDAO.connect(); - IngredientDAO.close(); - assertNull(IngredientDAO.connection); - assertNull(IngredientDAO.resultSet); - */ - } - @AfterEach public void tearDown() throws SQLException, IngredientDAOException { IngredientDAO.close(); diff --git a/pizzeria-del-sae.iml b/pizzeria-del-sae.iml new file mode 100644 index 0000000..6831087 --- /dev/null +++ b/pizzeria-del-sae.iml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module version="4"> + <component name="AdditionalModuleElements"> + <content url="file://$MODULE_DIR$" dumb="true"> + <sourceFolder url="file://$MODULE_DIR$/WEB-INF/src" isTestSource="false" /> + </content> + </component> +</module> \ No newline at end of file -- GitLab