From 2564995bf97583744b41d616e26eeffada92af86 Mon Sep 17 00:00:00 2001 From: panuozzo77 Date: Mon, 1 Jan 2024 12:57:25 +0100 Subject: [PATCH] First Unit Tests to checkout on SonarCloud --- pom.xml | 19 ++++ .../service/license/LicenseActivation.java | 3 + .../service/encryption/EncryptionTest.java | 38 ++++++++ .../license/LicenseActivationTest.java | 94 +++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 src/test/java/model/service/encryption/EncryptionTest.java create mode 100644 src/test/java/model/service/license/LicenseActivationTest.java diff --git a/pom.xml b/pom.xml index 751c7dc..986c7bb 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,25 @@ javax.json 1.1.4 + + junit + junit + 4.13.2 + test + + + + org.junit.jupiter + junit-jupiter-engine + 5.9.2 + test + + + org.mockito + mockito-core + 5.3.1 + test + diff --git a/src/main/java/model/service/license/LicenseActivation.java b/src/main/java/model/service/license/LicenseActivation.java index a1a43c9..4b6ed54 100644 --- a/src/main/java/model/service/license/LicenseActivation.java +++ b/src/main/java/model/service/license/LicenseActivation.java @@ -6,6 +6,9 @@ public class LicenseActivation implements LicenseActivationInterface { DAOLicense daoLicense = new DAOLicense(); + public void setDAOLicense(DAOLicense daoLicense) { + this.daoLicense = daoLicense; + } public License getLicense(String code) { return daoLicense.getLicenseByCode(code); } diff --git a/src/test/java/model/service/encryption/EncryptionTest.java b/src/test/java/model/service/encryption/EncryptionTest.java new file mode 100644 index 0000000..ebb6c59 --- /dev/null +++ b/src/test/java/model/service/encryption/EncryptionTest.java @@ -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)); + } +} diff --git a/src/test/java/model/service/license/LicenseActivationTest.java b/src/test/java/model/service/license/LicenseActivationTest.java new file mode 100644 index 0000000..1c8984c --- /dev/null +++ b/src/test/java/model/service/license/LicenseActivationTest.java @@ -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)); + } +}