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/ /.idea/
/WEB-INF/test-classes/ /WEB-INF/test-classes/
/WEB-INF/classes/ /WEB-INF/classes/
\ No newline at end of file /target/
\ No newline at end of file
...@@ -6,7 +6,6 @@ import POJO.Ingredient; ...@@ -6,7 +6,6 @@ import POJO.Ingredient;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
public class IngredientDAO { public class IngredientDAO {
// JDBC URL, username, and password of PostgresSQL server // JDBC URL, username, and password of PostgresSQL server
...@@ -30,8 +29,9 @@ public class IngredientDAO { ...@@ -30,8 +29,9 @@ public class IngredientDAO {
// Method to establish database connection // Method to establish database connection
static Connection connect() throws IngredientDAOException { static Connection connect() throws IngredientDAOException {
try { try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(url, user, password); connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) { } catch (SQLException | ClassNotFoundException e) {
throw new IngredientDAOException("Error establishing database connection AND " + e.getMessage(), e); throw new IngredientDAOException("Error establishing database connection AND " + e.getMessage(), e);
} }
return connection; return connection;
...@@ -78,22 +78,20 @@ public class IngredientDAO { ...@@ -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
// Method to insert a new ingredient into the database
public static Ingredient insertIngredient(Ingredient ingredient) throws IngredientDAOException { public static Ingredient insertIngredient(Ingredient ingredient) throws IngredientDAOException {
try { try {
if (connection.isClosed()) { connect();
connect();
}
if (getIngredient(ingredient.getName()) == null) { 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.setString(1, ingredient.getName());
preparedStatement.setDouble(2, ingredient.getPrice()); preparedStatement.setDouble(2, ingredient.getPrice());
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();
try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
if (generatedKeys.next()) { if (generatedKeys.next()) {
ingredient.setId(generatedKeys.getInt(1)); ingredient.setId(generatedKeys.getInt(1));
} else { } else {
ingredient.setId(-1); ingredient.setId(-1);
}
} }
} else { } else {
ingredient.setId(getIngredient(ingredient.getName()).getId()); ingredient.setId(getIngredient(ingredient.getName()).getId());
...@@ -104,7 +102,8 @@ public class IngredientDAO { ...@@ -104,7 +102,8 @@ public class IngredientDAO {
} catch (SQLException e) { } catch (SQLException e) {
throw new IngredientDAOException("Error inserting ingredient AND " + e.getMessage(), e); throw new IngredientDAOException("Error inserting ingredient AND " + e.getMessage(), e);
} finally { } finally {
close(); // Do not close the connection here, as it needs to remain open for subsequent operations
// close();
} }
return ingredient; return ingredient;
} }
...@@ -126,7 +125,7 @@ public class IngredientDAO { ...@@ -126,7 +125,7 @@ public class IngredientDAO {
} catch (SQLException e) { } catch (SQLException e) {
throw new IngredientDAOException("Error fetching ingredient AND " + e.getMessage(), e); throw new IngredientDAOException("Error fetching ingredient AND " + e.getMessage(), e);
} finally { } finally {
close(); //close();
} }
return ingredient; return ingredient;
} }
...@@ -150,7 +149,7 @@ public class IngredientDAO { ...@@ -150,7 +149,7 @@ public class IngredientDAO {
} catch (SQLException e) { } catch (SQLException e) {
throw new IngredientDAOException("Error fetching ingredient in byName AND " + e.getMessage(), e); throw new IngredientDAOException("Error fetching ingredient in byName AND " + e.getMessage(), e);
} finally { } finally {
close(); //close();
} }
} }
...@@ -163,7 +162,7 @@ public class IngredientDAO { ...@@ -163,7 +162,7 @@ public class IngredientDAO {
} catch (SQLException e) { } catch (SQLException e) {
throw new IngredientDAOException("Error deleting ingredient AND " + e.getMessage(), e); throw new IngredientDAOException("Error deleting ingredient AND " + e.getMessage(), e);
} finally { } finally {
close(); //close();
} }
} }
...@@ -176,7 +175,7 @@ public class IngredientDAO { ...@@ -176,7 +175,7 @@ public class IngredientDAO {
preparedStatement.setInt(3, ingredient.getId()); preparedStatement.setInt(3, ingredient.getId());
preparedStatement.executeUpdate(); preparedStatement.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new IngredientDAOException("Error updating ingredient", e); throw new IngredientDAOException("Error updating ingredient AND " + e.getMessage(), e);
} finally { } finally {
close(); close();
} }
......
...@@ -31,7 +31,7 @@ public class IngredientServlet extends HttpServlet { ...@@ -31,7 +31,7 @@ public class IngredientServlet extends HttpServlet {
if (pathInfo == null || pathInfo.equals("/")) { if (pathInfo == null || pathInfo.equals("/")) {
// Get all ingredients // Get all ingredients
try { try {
List<Ingredient> ingredients = ingredientDAO.getAllIngredients(); List<Ingredient> ingredients = IngredientDAO.getAllIngredients();
resp.setContentType("application/json"); resp.setContentType("application/json");
resp.getWriter().write(objectMapper.writeValueAsString(ingredients)); resp.getWriter().write(objectMapper.writeValueAsString(ingredients));
} catch (Exception e) { } catch (Exception e) {
......
...@@ -2,16 +2,16 @@ package DAO; ...@@ -2,16 +2,16 @@ package DAO;
import Exceptions.IngredientDAOException; import Exceptions.IngredientDAOException;
import POJO.Ingredient; import POJO.Ingredient;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class IngredientDAOTest { public class IngredientDAOTest {
private IngredientDAO dao; private IngredientDAO dao;
...@@ -37,7 +37,7 @@ public class IngredientDAOTest { ...@@ -37,7 +37,7 @@ public class IngredientDAOTest {
@Test @Test
public void testInsertIngredient() throws IngredientDAOException { public void testInsertIngredient() throws IngredientDAOException {
String name = "jojo"; String name = "momo";
double price = 1.5; double price = 1.5;
Ingredient newIngredient = new Ingredient(); Ingredient newIngredient = new Ingredient();
...@@ -53,12 +53,8 @@ public class IngredientDAOTest { ...@@ -53,12 +53,8 @@ public class IngredientDAOTest {
@Test @Test
public void testDeleteIngredient() throws IngredientDAOException { public void testDeleteIngredient() throws IngredientDAOException {
Ingredient ingredient = new Ingredient(); IngredientDAO.deleteIngredient(20);
ingredient.setName("new Test Ingredient"); Ingredient deletedIngredient = IngredientDAO.getIngredient(20);
ingredient.setPrice(1.5);
IngredientDAO.insertIngredient(ingredient);
IngredientDAO.deleteIngredient(ingredient.getId());
Ingredient deletedIngredient = IngredientDAO.getIngredient(ingredient.getId());
assertNull(deletedIngredient); assertNull(deletedIngredient);
} }
...@@ -67,16 +63,6 @@ public class IngredientDAOTest { ...@@ -67,16 +63,6 @@ public class IngredientDAOTest {
assertNotNull(IngredientDAO.connect()); assertNotNull(IngredientDAO.connect());
} }
@Test
public void testClose() throws IngredientDAOException {
/*
IngredientDAO.connect();
IngredientDAO.close();
assertNull(IngredientDAO.connection);
assertNull(IngredientDAO.resultSet);
*/
}
@AfterEach @AfterEach
public void tearDown() throws SQLException, IngredientDAOException { public void tearDown() throws SQLException, IngredientDAOException {
IngredientDAO.close(); 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