From 127dfc4155697a54c53614574b9fe493e092f51d Mon Sep 17 00:00:00 2001 From: Gwendal Margely <gwendal.margely.etu@univ-lille.fr> Date: Fri, 15 Mar 2024 13:27:06 +0100 Subject: [PATCH] =?UTF-8?q?=C3=83Making=20tests=20for=20IngredientDAO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WEB-INF/src/DAO/IngredientDAO.java | 9 +-- WEB-INF/src/POJO/Ingredient.java | 10 +++ WEB-INF/test/DAO/IngredientDAOTest.java | 85 +++++++++++++++++++++++++ pom.xml | 18 ++++++ 4 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 WEB-INF/test/DAO/IngredientDAOTest.java diff --git a/WEB-INF/src/DAO/IngredientDAO.java b/WEB-INF/src/DAO/IngredientDAO.java index db01497..ba48122 100644 --- a/WEB-INF/src/DAO/IngredientDAO.java +++ b/WEB-INF/src/DAO/IngredientDAO.java @@ -24,17 +24,17 @@ public class IngredientDAO { private static final String DELETE_QUERY = "DELETE FROM ingredients WHERE id = ?"; // Method to establish database connection - // Method to establish database connection - private static void connect() throws IngredientDAOException { + static Connection connect() throws IngredientDAOException { try { connection = DriverManager.getConnection(url, user, password); } catch (SQLException e) { throw new IngredientDAOException("Error establishing database connection", e); } + return connection; } // Method to close database connection - private static void close() throws IngredientDAOException { + public static void close() throws IngredientDAOException { try { if (resultSet != null) { resultSet.close(); @@ -74,7 +74,7 @@ public class IngredientDAO { } // Method to insert a new ingredient into the database - public static void insertIngredient(Ingredient ingredient) throws IngredientDAOException { + public static Ingredient insertIngredient(Ingredient ingredient) throws IngredientDAOException { try { connect(); preparedStatement = connection.prepareStatement(INSERT_QUERY); @@ -86,6 +86,7 @@ public class IngredientDAO { } finally { close(); } + return ingredient; } // Method to retrieve an ingredient by its ID diff --git a/WEB-INF/src/POJO/Ingredient.java b/WEB-INF/src/POJO/Ingredient.java index 1202b98..c560afa 100644 --- a/WEB-INF/src/POJO/Ingredient.java +++ b/WEB-INF/src/POJO/Ingredient.java @@ -14,6 +14,12 @@ public class Ingredient { public Ingredient(int id, String name) { this(id, name, 0.00); } + public Ingredient(int id) { + this(id, null); + } + public Ingredient() { + this(-1); + } public int getId() { return id; @@ -38,4 +44,8 @@ public class Ingredient { public void setPrice(double price) { this.price = price; } + + public boolean isEmpty() { + return id == -1 && name == null && price == 0.0; + } } diff --git a/WEB-INF/test/DAO/IngredientDAOTest.java b/WEB-INF/test/DAO/IngredientDAOTest.java new file mode 100644 index 0000000..cbb2c32 --- /dev/null +++ b/WEB-INF/test/DAO/IngredientDAOTest.java @@ -0,0 +1,85 @@ +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; + +public class IngredientDAOTest { + + private IngredientDAO dao; + private Connection connection; + + @BeforeEach + public void setUp() throws IngredientDAOException { + dao = new IngredientDAO(); + connection = IngredientDAO.connect(); + } + + @Test + public void testGetAllIngredients() throws IngredientDAOException { + List<Ingredient> ingredients = dao.getAllIngredients(); + assertFalse(ingredients.isEmpty()); + } + + @Test + public void testGetIngredient() throws IngredientDAOException { + Ingredient ingredient = IngredientDAO.getIngredient(1); + assertFalse(ingredient.isEmpty()); + } + + @Test + public void testInsertIngredient() throws IngredientDAOException { + String name = "Test Ingredient"; + double price = 1.5; + + Ingredient newIngredient = new Ingredient(); + newIngredient.setName(name); + newIngredient.setPrice(price); + + Ingredient savedIngredient = IngredientDAO.insertIngredient(newIngredient); + + assertTrue(savedIngredient.getId() > 0); + + Ingredient retrievedIngredient = IngredientDAO.getIngredient(savedIngredient.getId()); + assertEquals(name, retrievedIngredient.getName()); + assertEquals(price, retrievedIngredient.getPrice()); + } + + @Test + public void testDeleteIngredient() throws IngredientDAOException { + Ingredient ingredient = new Ingredient(); + ingredient.setName("Test Ingredient"); + ingredient.setPrice(1.5); + IngredientDAO.insertIngredient(ingredient); + dao.deleteIngredient(ingredient.getId()); + Ingredient deletedIngredient = IngredientDAO.getIngredient(ingredient.getId()); + assertTrue(deletedIngredient.isEmpty()); + } + + @Test + public void testConnect() throws IngredientDAOException { + assertNotNull(IngredientDAO.connect()); + } + + @Test + public void testClose() throws IngredientDAOException { + IngredientDAO.close(); + assertNull(IngredientDAO.connect()); + } + + @AfterEach + public void tearDown() throws SQLException { + if (connection != null) { + connection.close(); + } + } +} + diff --git a/pom.xml b/pom.xml index f87c195..4100924 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,24 @@ <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>RELEASE</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> + <version>RELEASE</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <version>5.10.2</version> + <scope>test</scope> + </dependency> </dependencies> <build> -- GitLab