Skip to content

Commit

Permalink
Merge pull request #35 from pastore99/34-testing-unit-testing
Browse files Browse the repository at this point in the history
Reached 59% Test Coverage
  • Loading branch information
panuozzo77 authored Jan 2, 2024
2 parents d5a65f9 + c6bb738 commit f6f15ae
Show file tree
Hide file tree
Showing 28 changed files with 1,547 additions and 28 deletions.
101 changes: 86 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<junit.version>5.10.0</junit.version>
<sonar.organization>panuozzo77</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
Expand All @@ -33,13 +33,6 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand All @@ -64,12 +57,6 @@
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
Expand All @@ -82,15 +69,99 @@
<version>1.1.4</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.2.1</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>
-Xmx1024M ${argLine}
</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
<append>false</append>
<formats>
<format>XML</format>
</formats>
</configuration>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.0</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
1 change: 1 addition & 0 deletions src/main/java/model/DAO/DAOConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public Connection getConnection() throws SQLException {
);
}


@Override
public Connection getConnection(String username, String password) throws SQLException {
return DriverManager.getConnection(props.getProperty("db.url"), username, password);
Expand Down
104 changes: 104 additions & 0 deletions src/main/java/model/DAO/DAOLicense.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,108 @@ public void activate(License license, int userId) {
}
}
}

/**
* Generate a new License to sell
* @return the sequence of the License if operation succeed, none otherwise
*/
public String generateLicense(){
final int length = 8;
License l = new License(length);
String insertQuery = "INSERT INTO TalkAID2.license (Sequence, ID_User, ExpirationDate) VALUES (?, ?, ?);";
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
connection = DAOConnection.getConnection();
preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, l.getSequence());
preparedStatement.setInt(2, 0);
preparedStatement.setDate(3, null);
preparedStatement.executeUpdate();
return l.getSequence();
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}
}


/**
* Generate an invitation code for a new patient
* @param userId the ID of a speech therapist
* @return the sequence of the invitation if operation succeed, none otherwise
*/
public String generateInvitation(int userId){
final int length = 4;
License l = new License(length);
String insertQuery = "INSERT INTO TalkAID2.license (Sequence, ID_User, ExpirationDate) VALUES (?,?,?);";
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
connection = DAOConnection.getConnection();
preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, l.getSequence());
preparedStatement.setInt(2, userId);
preparedStatement.setDate(3, l.getExpirationDate());
preparedStatement.executeUpdate();
return l.getSequence();
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}
}

public boolean deleteLicense(String code) {
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
// Get database connection
connection = DAOConnection.getConnection();

// Prepare the SQL query
String query = "DELETE FROM TalkAID2.license WHERE Sequence = ?";
preparedStatement = connection.prepareStatement(query);

// Set parameter for the prepared statement
preparedStatement.setString(1, code);

// Execute update and return boolean based on the affected rows
int rowsAffected = preparedStatement.executeUpdate();
return rowsAffected > 0;

} catch (SQLException e) {
e.printStackTrace();
} finally {
// Close the statement and release the connection
try {
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}

// If exception occurs, return false
return false;
}

}
38 changes: 34 additions & 4 deletions src/main/java/model/DAO/DAOMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import model.entity.Message;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.*;

public class DAOMessage {
Expand Down Expand Up @@ -203,4 +200,37 @@ public int countReceivedMessages(int recipientId) {

return count;
}

public void deleteLastInsertedMessage() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
conn = DAOConnection.getConnection();
stmt = conn.createStatement();

String sql = "SELECT MAX(ID_message) FROM message";

rs = stmt.executeQuery(sql);

if (rs.next()) {
int lastId = rs.getInt(1);
sql = "DELETE FROM message WHERE ID_message = " + lastId;
stmt.executeUpdate(sql);
}

} catch (SQLException e) {
// Handle exceptions (e.g., print stack trace, log error, etc.)
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
DAOConnection.releaseConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
40 changes: 40 additions & 0 deletions src/main/java/model/DAO/DAOPersonalInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,45 @@ public PersonalInfo getPersonalInfo(int id) {

return null; // Return null if personal_info does not exist
}

public boolean deleteRegistry(int createdUserId) {
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
connection = DAOConnection.getConnection();
connection.setAutoCommit(false); // Start a transaction

String sql = "DELETE FROM personal_info WHERE ID_user = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, createdUserId);
int rowAffected = preparedStatement.executeUpdate();

connection.commit(); // Commit the transaction

return rowAffected > 0; // Return true if the deletion was successful

} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
try {
if (connection != null) {
connection.rollback(); // Rollback the transaction in case of an exception
}
} catch (SQLException ex) {
ex.printStackTrace();
}
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}

return false; // Default to false if an exception occurs
}
}

41 changes: 41 additions & 0 deletions src/main/java/model/DAO/DAOUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,45 @@ public boolean updateEmailTime(String id, String value) {
// Default to false if an exception occurs
return false;
}

/**
* Deletes a user from the User table based on an ID or an email.
*
* @param idOrEmail Either an Integer representing the User's ID or a String representing the User's email.
* @return true if the user was successfully deleted; false otherwise.
*/
public boolean deleteUserByIdOrEmail(Object idOrEmail) {
Connection connection = null;
PreparedStatement preparedStatement = null;

try {
connection = DAOConnection.getConnection();
String query = null;

if (idOrEmail instanceof Integer) {
query = "DELETE FROM user WHERE ID = ?";
} else if (idOrEmail instanceof String) {
query = "DELETE FROM user WHERE Email = ?";
}

preparedStatement = connection.prepareStatement(query);
preparedStatement.setObject(1, idOrEmail);

int rowsDeleted = preparedStatement.executeUpdate();

return rowsDeleted > 0;
} catch (SQLException e) {
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
DAOConnection.releaseConnection(connection);
} catch (SQLException e) {
// Handle the exception (e.g., log or throw)
e.printStackTrace();
}
}

// Default to false if an exception occurs
return false;
}
}
Loading

0 comments on commit f6f15ae

Please sign in to comment.