-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First Unit Tests to checkout on SonarCloud
- Loading branch information
1 parent
d5a65f9
commit 2564995
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/test/java/model/service/encryption/EncryptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package model.service.encryption; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
|
||
public class EncryptionTest { | ||
|
||
@Test | ||
public void testEncryptPassword() { | ||
Encryption encryption = new Encryption(); | ||
String plainTextPassword = "password"; | ||
|
||
// Ensure that the encrypted password is not null or empty | ||
String encryptedPassword = encryption.encryptPassword(plainTextPassword); | ||
assertNotNull(encryptedPassword); | ||
assertFalse(encryptedPassword.isEmpty()); | ||
|
||
// Ensure that each encryption of the same password produces different results | ||
String secondEncryptedPassword = encryption.encryptPassword(plainTextPassword); | ||
assertNotEquals(encryptedPassword, secondEncryptedPassword); | ||
} | ||
|
||
@Test | ||
public void testVerifyPassword() { | ||
Encryption encryption = new Encryption(); | ||
String plainTextPassword = "password"; | ||
String hashedPassword = encryption.encryptPassword(plainTextPassword); | ||
|
||
// Ensure that the password is verified correctly | ||
assertTrue(encryption.verifyPassword(plainTextPassword, hashedPassword)); | ||
|
||
// Ensure that an incorrect password is not verified | ||
String incorrectPassword = "wrongpassword"; | ||
assertFalse(encryption.verifyPassword(incorrectPassword, hashedPassword)); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/test/java/model/service/license/LicenseActivationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package model.service.license; | ||
|
||
import model.DAO.DAOLicense; | ||
import org.junit.jupiter.api.Test; | ||
import model.service.license.LicenseActivation; | ||
import model.entity.License; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
class LicenseActivationTest { | ||
|
||
@Test | ||
void testGetLicense() { | ||
// Mock the DAOLicense class | ||
DAOLicense daoLicenseMock = mock(DAOLicense.class); | ||
|
||
// Set up the LicenseActivation class with the mocked DAO | ||
LicenseActivation licenseActivation = new LicenseActivation(); | ||
licenseActivation.setDAOLicense(daoLicenseMock); | ||
|
||
// Create a sample license | ||
License sampleLicense = new License(); | ||
sampleLicense.setSequence("1234"); | ||
sampleLicense.setIdUser(1); | ||
|
||
// Mock the behavior of the DAO method | ||
when(daoLicenseMock.getLicenseByCode(anyString())).thenReturn(sampleLicense); | ||
|
||
// Test the getLicense method | ||
License resultLicense = licenseActivation.getLicense("sampleCode"); | ||
assertNotNull(resultLicense); | ||
assertEquals(sampleLicense, resultLicense); | ||
} | ||
|
||
@Test | ||
void testIsActivable() { | ||
LicenseActivation licenseActivation = new LicenseActivation(); | ||
|
||
// Test with an active license | ||
License activeLicense = new License(); | ||
activeLicense.setActive(true); | ||
assertFalse(licenseActivation.isActivable(activeLicense)); | ||
|
||
// Test with an inactive license | ||
License inactiveLicense = new License(); | ||
inactiveLicense.setActive(false); | ||
assertTrue(licenseActivation.isActivable(inactiveLicense)); | ||
|
||
// Test with a null license | ||
assertFalse(licenseActivation.isActivable(null)); | ||
} | ||
|
||
@Test | ||
void testIsForTherapist() { | ||
LicenseActivation licenseActivation = new LicenseActivation(); | ||
|
||
// Test with a valid therapist license | ||
License therapistLicense = new License(); | ||
therapistLicense.setSequence("1234"); | ||
therapistLicense.setIdUser(1); | ||
assertEquals(1, licenseActivation.isForTherapist(therapistLicense)); | ||
|
||
// Test with an invalid therapist license | ||
License invalidTherapistLicense = new License(); | ||
invalidTherapistLicense.setSequence("123"); | ||
invalidTherapistLicense.setIdUser(2); | ||
assertEquals(0, licenseActivation.isForTherapist(invalidTherapistLicense)); | ||
|
||
// Test with a null license | ||
assertEquals(0, licenseActivation.isForTherapist(null)); | ||
} | ||
|
||
@Test | ||
void testActivate() { | ||
// Mock the DAOLicense class | ||
DAOLicense daoLicenseMock = mock(DAOLicense.class); | ||
|
||
// Set up the LicenseActivation class with the mocked DAO | ||
LicenseActivation licenseActivation = new LicenseActivation(); | ||
licenseActivation.setDAOLicense(daoLicenseMock); | ||
|
||
// Create a sample license | ||
License sampleLicense = new License(); | ||
sampleLicense.setSequence("1234"); | ||
sampleLicense.setIdUser(1); | ||
|
||
// Test the activate method | ||
licenseActivation.activate(sampleLicense, 1); | ||
|
||
// Verify that the DAO's activate method was called with the correct parameters | ||
verify(daoLicenseMock, times(1)).activate(eq(sampleLicense), eq(1)); | ||
} | ||
} |