Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit c624182f authored by Flavio Ferraioli's avatar Flavio Ferraioli
Browse files

Merge branch 'feature/SIMPL-coverage-implementation' into 'develop'

Coverage implementation

See merge request !105
parents d419699b b7fc6773
No related branches found
No related tags found
2 merge requests!106Release,!105Coverage implementation
Pipeline #220776 passed with warnings
package com.aruba.simpl.usersroles.configurations;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;
import com.aruba.simpl.client.SimplClient.Builder;
import com.aruba.simpl.client.adapters.EphemeralProofAdapter;
import com.aruba.simpl.client.feign.FeignSimplClient;
import com.aruba.simpl.common.interceptors.TierOneTokenPropagatorInterceptor;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@Import({MtlsClientFactory.class})
@MockBean(
classes = {
TierOneTokenPropagatorInterceptor.class,
MtlsClientProperties.class,
EphemeralProofAdapter.class,
})
public class MtlsClientFactoryTest {
@Autowired
MtlsClientFactory mtlsClientFactory;
@MockBean
private FeignSimplClient simplClient;
@Test
void buildAuthorityClient_success() throws Exception {
var url = "url";
var keyStore = mock(KeyStore.class);
var password = "pwd";
Certificate certificate = mock(Certificate.class);
Certificate[] certs = {certificate};
var simplClientBuilder = mock(Builder.class);
var feignBuilder = mock(feign.Feign.Builder.class);
given(keyStore.aliases()).willReturn(Collections.enumeration(List.of("keystore_alias")));
given(keyStore.getCertificateChain("keystore_alias")).willReturn(certs);
given(simplClient.builder(any())).willReturn(simplClientBuilder);
given(simplClientBuilder.setAuthorizationHeaderSupplier(any())).willReturn(simplClientBuilder);
given(simplClientBuilder.setEphemeralProofAdapter(any())).willReturn(simplClientBuilder);
given(simplClientBuilder.setAuthorityUrlSupplier(any())).willReturn(simplClientBuilder);
given(simplClientBuilder.build()).willReturn(feignBuilder);
try (MockedStatic<KeyStore> ks = Mockito.mockStatic(KeyStore.class)) {
ks.when(() -> KeyStore.getInstance("PKCS12")).thenReturn(keyStore);
mtlsClientFactory.buildAuthorityClient(url, keyStore, password);
}
}
}
......@@ -2,8 +2,11 @@ package com.aruba.simpl.usersroles.model.mappers;
import static com.aruba.simpl.common.test.TestUtil.a;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import com.aruba.simpl.common.model.dto.IdentityAttributeDTO;
import com.aruba.simpl.common.model.dto.IdentityAttributeWithOwnershipDTO;
import com.aruba.simpl.usersroles.model.entities.IdentityAttributeWithOwnership;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
......@@ -23,4 +26,68 @@ class IdentityAttributeWithOwnershipMapperTest {
entity = mapper.toEntity(null);
assertThat(entity).isNull();
}
@Test
void toEntityTest_withNullValues() {
var ia = a(IdentityAttributeWithOwnershipDTO.class);
ia.setIdentityAttribute(null);
ia.setAssignedToParticipant(null);
var entity = mapper.toEntity(ia);
assertThat(entity).isNotNull();
entity = mapper.toEntity(null);
assertThat(entity).isNull();
}
@Test
void toLightDtoWithOwnership_successMapper() {
var ia = a(IdentityAttributeWithOwnership.class);
var entity = mapper.toLightDtoWithOwnership(ia);
assertThat(entity).isNotNull();
}
@Test
void toLightDtoWithOwnership_nullEntity() {
var entity = mapper.toLightDtoWithOwnership(null);
assertThat(entity).isNull();
}
@Test
void toLightDto_successMapper() {
var ia = a(IdentityAttributeWithOwnership.class);
var entity = mapper.toLightDto(ia);
assertThat(entity).isNotNull();
}
@Test
void toLightDto_nullEntity() {
var entity = mapper.toLightDto(null);
assertThat(entity).isNull();
}
@Test
void updateIdentityAttribute_successMapper() {
var entity = new IdentityAttributeWithOwnership();
var ida = a(IdentityAttributeDTO.class);
assertDoesNotThrow(() -> mapper.updateIdentityAttribute(entity, ida));
}
@Test
void updateIdentityAttribute_withNullValues() {
var entity = new IdentityAttributeWithOwnership();
var ida = a(IdentityAttributeDTO.class);
ida.setAssignableToRoles(null);
ida.setEnabled(null);
entity.setParticipantTypes(null);
assertDoesNotThrow(() -> mapper.updateIdentityAttribute(entity, ida));
assertDoesNotThrow(() -> mapper.updateIdentityAttribute(new IdentityAttributeWithOwnership(), null));
ida.setParticipantTypes(null);
assertDoesNotThrow(() -> mapper.updateIdentityAttribute(new IdentityAttributeWithOwnership(), ida));
var entityWithNotParticipantTypes = a(IdentityAttributeWithOwnership.class);
entityWithNotParticipantTypes.setParticipantTypes(null);
assertDoesNotThrow(() -> mapper.updateIdentityAttribute(entityWithNotParticipantTypes, ida));
}
}
package com.aruba.simpl.usersroles.model.mappers;
import static com.aruba.simpl.common.test.TestUtil.a;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import com.aruba.simpl.common.model.dto.KeycloakUserDTO;
import java.util.ArrayList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.keycloak.representations.idm.UserRepresentation;
import org.mapstruct.factory.Mappers;
public class KeycloakMapperTest {
private KeycloakMapper mapper;
@BeforeEach
void setup() {
mapper = Mappers.getMapper(KeycloakMapper.class);
}
@Test
void updateEntity_successMapper() {
var dto = a(KeycloakUserDTO.class);
var entity = new UserRepresentation();
entity.setCredentials(new ArrayList<>());
assertDoesNotThrow(() -> mapper.updateEntity(dto, entity));
}
@Test
void updateEntity_nullValues() {
var dto = a(KeycloakUserDTO.class);
var entity = new UserRepresentation();
assertDoesNotThrow(() -> mapper.updateEntity(dto, entity));
assertDoesNotThrow(() -> mapper.updateEntity(null, entity));
}
}
......@@ -2,6 +2,7 @@ package com.aruba.simpl.usersroles.services.impl;
import static com.aruba.simpl.common.test.TestUtil.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.BDDMockito.*;
......@@ -93,6 +94,21 @@ class KeycloakUserServiceImplTest {
assertThat(keycloakUserDTO.getEmail()).isEqualTo(expectedUserEmail);
}
@Test
void getUserByEmail_ClientThrowException_ExpectToMapException() {
// Given
var expectedUserEmail = Gen.net().email().get();
var userRepresentation = Instancio.of(UserRepresentation.class).create();
userRepresentation.setEmail(expectedUserEmail);
var clientErrorException = generateClientErrorException();
given(keycloakService.getAppUsers().searchByEmail(eq(expectedUserEmail), any()))
.willThrow(clientErrorException);
// When
assertThrows(KeycloakException.class, () -> keycloakUserService.getUserByEmail(expectedUserEmail));
}
@Test
void updateRole_Success() {
var roleDTO = a(RoleDTO.class);
......@@ -179,6 +195,20 @@ class KeycloakUserServiceImplTest {
then(keycloakService.getClientRoles()).should(times(rolesCount)).create(any());
}
@Test
void importRole() {
var role = mock(KeycloakRoleDTO.class);
assertDoesNotThrow(() -> keycloakUserService.importRole(role));
}
@Test
void getRole_throwClientError_throwKeycloakException() {
var exception = generateClientErrorException();
given(keycloakService.getClientRoles()).willThrow(exception);
assertThrows(KeycloakException.class, () -> keycloakUserService.getRole("role-tests"));
}
@Test
void getUserRoles() {
......@@ -259,6 +289,15 @@ class KeycloakUserServiceImplTest {
.satisfies(exception -> assertThat(exception.getStatus()).isEqualTo(HttpStatus.NOT_FOUND));
}
@Test
void updateUserRoles_throwClientError_throwKeycloakException() {
var ex = generateClientErrorException();
given(keycloakService.getClientRoleScope(any())).willThrow(ex);
assertThrows(
KeycloakException.class, () -> keycloakUserService.updateUserRoles("test-uuid", List.of("TEST_ROLE")));
}
@ParameterizedTest
@InstancioSource
void search(List<UserRepresentation> userRepresentations, KeycloakUserFilter filter) {
......@@ -282,4 +321,12 @@ class KeycloakUserServiceImplTest {
// Then
assertThat(users.size()).isEqualTo(userRepresentations.size());
}
private ClientErrorException generateClientErrorException() {
var clientErrorException = mock(ClientErrorException.class);
var response = mock(Response.class);
when(clientErrorException.getResponse()).thenReturn(response);
when(response.getStatus()).thenReturn(500);
return clientErrorException;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment