Skip to content
Snippets Groups Projects
Commit dc423288 authored by MEDEDJI Setondji's avatar MEDEDJI Setondji :speech_balloon:
Browse files

debut etape 2

parent bfe6b25d
No related branches found
No related tags found
No related merge requests found
package automaticmocking;
import authentication.AuthenticationService;
import authentication.Strength;
import interfacemanualmocking.DirectoryInterface;
public class AuthenticationByBiometryService implements AuthenticationService {
private interfacemanualmocking.DirectoryInterface directory;
private Strength strength;
public AuthenticationByBiometryService(interfacemanualmocking.DirectoryInterface directory, Strength strength) {
this.directory = directory;
this.strength = strength;
}
public void setDirectory(DirectoryInterface directory) {
this.directory = directory;
}
@Override
public boolean isAMatch(String identifier, String autenthicationData) {
String match = directory.getMatch(identifier);
return match.equals(autenthicationData);
}
@Override
public Strength dataStrenth(String autenthicationData) {
return Strength.STRONG;
}
}
package automaticmocking;
import authentication.AuthenticationService;
import authentication.Strength;
import interfacemanualmocking.DirectoryInterface;
import interfacemanualmocking.StrengthEstimationServiceInterface;
public class AuthenticationByPasswordService implements AuthenticationService {
private interfacemanualmocking.DirectoryInterface directory;
private interfacemanualmocking.StrengthEstimationServiceInterface strengthEstimationService;
public void setStrengthEstimationService(interfacemanualmocking.StrengthEstimationServiceInterface strengthEstimationService) {
this.strengthEstimationService = strengthEstimationService;
}
public AuthenticationByPasswordService(interfacemanualmocking.DirectoryInterface directory, StrengthEstimationServiceInterface strengthEstimationService) {
this.directory = directory;
this.strengthEstimationService = strengthEstimationService;
}
@Override
public boolean isAMatch(String identifier, String autenthicationData) {
String match = directory.getMatch(identifier);
return match.equals(autenthicationData);
}
@Override
public Strength dataStrenth(String autenthicationData) {
int datasize = autenthicationData.length();
int alphabetSize = 142;
int strength = strengthEstimationService.equivalentBitLength(alphabetSize,datasize);
Strength result = null ;
if( strength >= 0 && strength <= 49 ) {
result = Strength.VERY_WEAK;
}
else if ( strength >= 50 && strength <= 79 ) {
result = Strength.WEAK;
}
else if ( strength >= 80 && strength <= 99 ) {
result = Strength.REGULAR;
}
else if ( strength >= 100 ) {
result = Strength.STRONG;
}
return result;
}
public void setDirectory(DirectoryInterface directory) {
this.directory = directory;
}
}
//private StrengthEstimationServiceInterface dataStrenth(String autenthicationData) {
// int alphabetSize = 0;
// int datasize = autenthicationData.length();
//return strengthEstimationService.equivalentBitLength(alphabetSize, datasize);
//}
package automaticmocking;
import authentication.Strength;
public interface AuthenticationService {
boolean isAMatch(String identifier, String autenthicationData);
Strength dataStrenth(String autenthicationData);
}
package automaticmocking;
public interface DirectoryInterface {
String getMatch(String identifier);
}
package automaticmocking;
public interface StrengthEstimationServiceInterface {
int equivalentBitLength(int alphabetSize, int datasize);
}
import static authentication.Strength.STRONG;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.extension.ExtendWith;
import interfacemanualmocking.AuthenticationByBiometryService; import interfacemanualmocking.AuthenticationByBiometryService;
import interfacemanualmocking.AuthenticationByPasswordService;
import interfacemanualmocking.MockDirectory; import interfacemanualmocking.MockDirectory;
import interfacemanualmocking.MockStrengthEstimationService;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import static authentication.Strength.*; import org.mockito.InjectMocks;
import static authentication.Strength.STRONG; import org.mockito.Mock;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import org.mockito.Mockito;
//import org.mockito.junit.jupiter.MockitoExtension;
public class AuthenticationByBiometryServiceTest { public class AuthenticationByBiometryServiceTest {
@Mock
private MockDirectory directory; private MockDirectory directory;
@InjectMocks
private AuthenticationByBiometryService authenticationByBiometryService; private AuthenticationByBiometryService authenticationByBiometryService;
@BeforeAll @BeforeAll
...@@ -23,13 +32,11 @@ public class AuthenticationByBiometryServiceTest { ...@@ -23,13 +32,11 @@ public class AuthenticationByBiometryServiceTest {
} }
@BeforeEach @BeforeEach
void setUp() { void setUp() {
directory = new MockDirectory(); // Pas besoin de créer le mock manuellement, Mockito le fait pour vous avec @Mock
authenticationByBiometryService = new AuthenticationByBiometryService(directory, STRONG); authenticationByBiometryService = new AuthenticationByBiometryService(directory, STRONG);
authenticationByBiometryService.setDirectory(directory);
} }
@AfterEach @AfterEach
void tearDown() throws Exception { void tearDown() throws Exception {
System.out.println("After each"); System.out.println("After each");
...@@ -37,13 +44,41 @@ public class AuthenticationByBiometryServiceTest { ...@@ -37,13 +44,41 @@ public class AuthenticationByBiometryServiceTest {
@Test @Test
void testIsAMatch() { void testIsAMatch() {
// Définir un comportement attendu sur le mock
when(authenticationByBiometryService.isAMatch("regix", "azerty")).thenReturn(true);
assertThat(authenticationByBiometryService.isAMatch("regix", "azerty")).isTrue(); assertThat(authenticationByBiometryService.isAMatch("regix", "azerty")).isTrue();
// Vérifier que la méthode du mock a été appelée avec les bons arguments
verify(authenticationByBiometryService, times(1)).isAMatch("regix", "azerty");
// Vérifier qu'aucune autre méthode du mock n'a été appelée
verifyNoMoreInteractions(directory);
} }
@Test @Test
void testDataStrenth() { void testDataStrength() {
// Définir un comportement attendu sur le mock
when(authenticationByBiometryService.dataStrenth("azerty")).thenReturn(STRONG);
assertThat(authenticationByBiometryService.dataStrenth("azerty")).isEqualTo(STRONG); assertThat(authenticationByBiometryService.dataStrenth("azerty")).isEqualTo(STRONG);
// Vérifier que la méthode du mock a été appelée avec les bons arguments
verify(authenticationByBiometryService, times(1)).dataStrenth("azerty");
// Vérifier qu'aucune autre méthode du mock n'a été appelée
verifyNoMoreInteractions(directory);
// Ajouter un autre cas de test avec des données différentes
when(authenticationByBiometryService.dataStrenth("azertyuiopqsdfghjklmwxcvbn1234567890")).thenReturn(STRONG);
assertThat(authenticationByBiometryService.dataStrenth("azertyuiopqsdfghjklmwxcvbn1234567890")).isEqualTo(STRONG); assertThat(authenticationByBiometryService.dataStrenth("azertyuiopqsdfghjklmwxcvbn1234567890")).isEqualTo(STRONG);
// Vérifier que la méthode du mock a été appelée avec les bons arguments
verify(authenticationByBiometryService, times(1)).dataStrenth("azertyuiopqsdfghjklmwxcvbn1234567890");
// Vérifier qu'aucune autre méthode du mock n'a été appelée
verifyNoMoreInteractions(directory);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment