generated from pagopa/template-payments-java-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: P4ADEV-1753 get brokerId from UserInfo (#19)
- Loading branch information
1 parent
a2bf680
commit 30366dc
Showing
2 changed files
with
42 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 28 additions & 56 deletions
84
src/test/java/it/gov/pagopa/pu/bff/service/BrokerServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |