Skip to content

Commit

Permalink
First Unit Tests to checkout on SonarCloud
Browse files Browse the repository at this point in the history
  • Loading branch information
panuozzo77 committed Jan 1, 2024
1 parent d5a65f9 commit 2564995
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/model/service/license/LicenseActivation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/model/service/encryption/EncryptionTest.java
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 src/test/java/model/service/license/LicenseActivationTest.java
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));
}
}

0 comments on commit 2564995

Please sign in to comment.