Skip to content
Snippets Groups Projects
Commit 127dfc41 authored by Gwendal Margely's avatar Gwendal Margely :alembic:
Browse files

ÃMaking tests for IngredientDAO

parent e6a8d77d
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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;
}
}
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();
}
}
}
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment