Skip to content

Commit

Permalink
feat: P4ADEV-1753 get brokerId from UserInfo (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElisKina-dev authored Jan 13, 2025
1 parent a2bf680 commit 30366dc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@

import it.gov.pagopa.pu.bff.config.DefaultConfigFe;
import it.gov.pagopa.pu.bff.connector.organization.client.BrokerEntityClient;
import it.gov.pagopa.pu.bff.connector.organization.client.OrganizationSearchClient;
import it.gov.pagopa.pu.bff.dto.generated.ConfigFE;
import it.gov.pagopa.pu.bff.mapper.PersonalisationFE2ConfigFEMapper;
import it.gov.pagopa.pu.p4pa_organization.dto.generated.Broker;
import it.gov.pagopa.pu.p4pa_organization.dto.generated.Organization;
import it.gov.pagopa.pu.p4paauth.dto.generated.UserInfo;
import it.gov.pagopa.pu.p4paauth.dto.generated.UserOrganizationRoles;
import org.apache.commons.lang3.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@Slf4j
public class BrokerServiceImpl implements BrokerService {

private final OrganizationSearchClient organizationSearchClient;
private final BrokerEntityClient brokerEntityClient;
private final PersonalisationFE2ConfigFEMapper personalisationFE2ConfigFEMapper;
private final DefaultConfigFe defaultConfigFe;
private final ConfigFE defaultFEConfig;

public BrokerServiceImpl(OrganizationSearchClient organizationSearchClient, BrokerEntityClient brokerEntityClient,
public BrokerServiceImpl(BrokerEntityClient brokerEntityClient,
DefaultConfigFe defaultConfigFe,
PersonalisationFE2ConfigFEMapper personalisationFE2ConfigFEMapper) {
this.organizationSearchClient = organizationSearchClient;
this.brokerEntityClient = brokerEntityClient;
this.defaultConfigFe = defaultConfigFe;
this.personalisationFE2ConfigFEMapper = personalisationFE2ConfigFEMapper;
Expand All @@ -35,22 +29,20 @@ public BrokerServiceImpl(OrganizationSearchClient organizationSearchClient, Brok

@Override
public ConfigFE getBrokerConfig(UserInfo user, String accessToken) {
String orgIpaCode = Optional.ofNullable(user.getOrganizationAccess())
.orElseGet(() -> user.getOrganizations().stream()
.findFirst()
.map(UserOrganizationRoles::getOrganizationIpaCode)
.orElse(null)
);

if (StringUtils.isNotBlank(orgIpaCode)) {
Organization organization = organizationSearchClient.getOrganizationByIpaCode(orgIpaCode, accessToken);
if (organization != null && organization.getBrokerId() != null) {
return getFEConfiguration(brokerEntityClient.getBrokerById(organization.getBrokerId(), accessToken));
}
if (user.getBrokerId() == null) {
log.warn("BrokerId is null, returning default configuration.");
return this.defaultFEConfig;
}
return this.defaultFEConfig;

log.info("BrokerId retrieved from UserInfo: {}", user.getBrokerId());
Broker broker = brokerEntityClient.getBrokerById(user.getBrokerId(), accessToken);
ConfigFE configFE = getFEConfiguration(broker);
configFE.setCanManageUsers(user.getCanManageUsers());

return configFE;
}


public ConfigFE getFEConfiguration(Broker broker) {
if (broker != null) {
return personalisationFE2ConfigFEMapper.mapPersonalisationFE2ConfigFE(broker.getPersonalisationFe());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,115 +1,87 @@
package it.gov.pagopa.pu.bff.service;

import static org.junit.jupiter.api.Assertions.assertEquals;

import it.gov.pagopa.pu.bff.config.DefaultConfigFe;
import it.gov.pagopa.pu.bff.connector.organization.client.BrokerEntityClient;
import it.gov.pagopa.pu.bff.connector.organization.client.OrganizationSearchClient;
import it.gov.pagopa.pu.bff.dto.generated.ConfigFE;
import it.gov.pagopa.pu.bff.mapper.PersonalisationFE2ConfigFEMapper;
import it.gov.pagopa.pu.bff.service.broker.BrokerServiceImpl;
import it.gov.pagopa.pu.bff.util.TestUtils;
import it.gov.pagopa.pu.p4pa_organization.dto.generated.Broker;
import it.gov.pagopa.pu.p4pa_organization.dto.generated.Organization;
import it.gov.pagopa.pu.p4pa_organization.dto.generated.PersonalisationFe;
import it.gov.pagopa.pu.p4paauth.dto.generated.UserInfo;
import it.gov.pagopa.pu.p4paauth.dto.generated.UserOrganizationRoles;
import java.util.Collection;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.util.ReflectionTestUtils;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(MockitoExtension.class)
class BrokerServiceImplTest {

@Mock
private OrganizationSearchClient organizationSearchClientMock;
@Mock
private BrokerEntityClient brokerEntityClientMock;
@Mock
private DefaultConfigFe defaultConfigFeMock;

@Mock
private PersonalisationFE2ConfigFEMapper personalisationFE2ConfigFEMapperMock;
private BrokerServiceImpl brokerService;

private Organization entityModelOrganization;
private Broker entityModelBroker;
private PersonalisationFe personalisationFe;

private final String accessToken = "TOKEN";

private ConfigFE defaultFEConfig;

@BeforeEach
void setUp() {
UserOrganizationRoles userOrganizationRoles = new UserOrganizationRoles();
userOrganizationRoles.setOrganizationIpaCode("testIpaCode");

UserInfo userInfo = new UserInfo();
userInfo.setOrganizations(Collections.singletonList(userOrganizationRoles));

entityModelOrganization = new Organization();
entityModelOrganization.setBrokerId(1L);

entityModelBroker = new Broker();
personalisationFe = new PersonalisationFe();
entityModelBroker.setPersonalisationFe(personalisationFe);

defaultFEConfig = new ConfigFE();

// Mock SecurityUtils.getLoggedUser() to return userInfo
Collection<? extends GrantedAuthority> authorities = null;
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(userInfo, null, authorities);
SecurityContextHolder.getContext().setAuthentication(authToken);
brokerService = new BrokerServiceImpl(organizationSearchClientMock,brokerEntityClientMock,defaultConfigFeMock,personalisationFE2ConfigFEMapperMock);
brokerService = new BrokerServiceImpl(
brokerEntityClientMock,
defaultConfigFeMock,
personalisationFE2ConfigFEMapperMock
);
}

@Test
void givenGetBrokerConfigWhenValidDataThenOK() {
TestUtils.addSampleUserIntoSecurityContext();
ConfigFE configFE = new ConfigFE();
Mockito.when(organizationSearchClientMock.getOrganizationByIpaCode("ORG",accessToken)).thenReturn(entityModelOrganization);
Mockito.when(brokerEntityClientMock.getBrokerById(1L,accessToken)).thenReturn(entityModelBroker);
UserInfo userInfo = new UserInfo();
userInfo.setBrokerId(1L);
userInfo.setCanManageUsers(true);

Mockito.when(brokerEntityClientMock.getBrokerById(1L, accessToken)).thenReturn(entityModelBroker);
Mockito.when(personalisationFE2ConfigFEMapperMock.mapPersonalisationFE2ConfigFE(personalisationFe)).thenReturn(configFE);

ConfigFE result = brokerService.getBrokerConfig(TestUtils.getSampleUser(),accessToken);
ConfigFE result = brokerService.getBrokerConfig(userInfo, accessToken);

assertEquals(personalisationFe.getFooterAccessibilityUrl(), result.getFooterAccessibilityUrl());
assertEquals(personalisationFe.getFooterGDPRUrl(),result.getFooterGDPRUrl());
assertEquals(personalisationFe.getFooterDescText(),result.getFooterDescText());
assertEquals(personalisationFe.getFooterTermsCondUrl(),result.getFooterTermsCondUrl());
assertEquals(personalisationFe.getHeaderAssistanceUrl(),result.getHeaderAssistanceUrl());
assertEquals(personalisationFe.getLogoFooterImg(),result.getLogoFooterImg());
assertEquals(personalisationFe.getFooterGDPRUrl(), result.getFooterGDPRUrl());
assertEquals(personalisationFe.getFooterDescText(), result.getFooterDescText());
assertEquals(personalisationFe.getFooterTermsCondUrl(), result.getFooterTermsCondUrl());
assertEquals(personalisationFe.getHeaderAssistanceUrl(), result.getHeaderAssistanceUrl());
assertEquals(personalisationFe.getLogoFooterImg(), result.getLogoFooterImg());
}


@Test
void givenGetBrokerConfigWhenNoOrganizationThenOkDefault() {
ConfigFE configFE = new ConfigFE();
ReflectionTestUtils.setField(brokerService, "defaultFEConfig", configFE);
void givenGetBrokerConfigWhenBrokerNotFoundThenDefaultConfig() {
UserInfo userInfo = new UserInfo();
userInfo.setBrokerId(1L);
userInfo.setCanManageUsers(false);

ConfigFE result = brokerService.getBrokerConfig(TestUtils.getSampleUser(false),accessToken);
assertEquals(defaultFEConfig, result);
}
Mockito.when(brokerEntityClientMock.getBrokerById(1L, accessToken)).thenReturn(null);
Mockito.when(personalisationFE2ConfigFEMapperMock.mapPersonalisationFE2ConfigFE(defaultConfigFeMock)).thenReturn(defaultFEConfig);

@Test
void givenGetBrokerConfigWhenNoBrokerThenOkDefault() {
TestUtils.addSampleUserIntoSecurityContext();
ConfigFE defaultConf = new ConfigFE();
Mockito.when(organizationSearchClientMock.getOrganizationByIpaCode("ORG",accessToken)).thenReturn(entityModelOrganization);
Mockito.when(brokerEntityClientMock.getBrokerById(1L,accessToken)).thenReturn(null);
Mockito.when(personalisationFE2ConfigFEMapperMock.mapPersonalisationFE2ConfigFE(defaultConfigFeMock)).thenReturn(defaultConf);
ConfigFE result = brokerService.getBrokerConfig(userInfo, accessToken);

ConfigFE result = brokerService.getBrokerConfig(TestUtils.getSampleUser(),accessToken);
assertEquals(defaultConf, result);
assertEquals(defaultFEConfig, result);
assertEquals(userInfo.getCanManageUsers(), result.getCanManageUsers());
}

}

0 comments on commit 30366dc

Please sign in to comment.