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

Test FIXXXXXXXED git add .git add .! (but still 404 on postman shhhhhhhh ...)

parent 6f0c5509
No related branches found
No related tags found
No related merge requests found
/.idea/
/WEB-INF/test-classes/
/WEB-INF/classes/
\ No newline at end of file
/WEB-INF/classes/
/target/
\ No newline at end of file
......@@ -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();
}
......
......@@ -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) {
......
......@@ -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();
......
<?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
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