|
| 1 | +package org.cardanofoundation.lob.app.organisation.service; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | + |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import org.mockito.InjectMocks; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | + |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 19 | + |
| 20 | +import org.cardanofoundation.lob.app.organisation.domain.entity.Organisation; |
| 21 | +import org.cardanofoundation.lob.app.organisation.domain.entity.OrganisationCostCenter; |
| 22 | +import org.cardanofoundation.lob.app.organisation.domain.entity.OrganisationProject; |
| 23 | +import org.cardanofoundation.lob.app.organisation.domain.request.OrganisationUpsert; |
| 24 | +import org.cardanofoundation.lob.app.organisation.domain.view.OrganisationView; |
| 25 | +import org.cardanofoundation.lob.app.organisation.repository.*; |
| 26 | + |
| 27 | +@ExtendWith(MockitoExtension.class) |
| 28 | +class OrganisationServiceTest { |
| 29 | + |
| 30 | + @Mock |
| 31 | + private OrganisationRepository organisationRepository; |
| 32 | + |
| 33 | + @Mock |
| 34 | + private CostCenterService costCenterService; |
| 35 | + |
| 36 | + @Mock |
| 37 | + private ProjectMappingRepository projectMappingRepository; |
| 38 | + |
| 39 | + @Mock |
| 40 | + private OrganisationChartOfAccountTypeRepository organisationChartOfAccountTypeRepository; |
| 41 | + |
| 42 | + @Mock |
| 43 | + private ChartOfAccountRepository organisationChartOfAccountRepository; |
| 44 | + |
| 45 | + @Mock |
| 46 | + private OrganisationChartOfAccountSubTypeRepository organisationChartOfAccountSubTypeRepository; |
| 47 | + |
| 48 | + @Mock |
| 49 | + private AccountEventRepository accountEventRepository; |
| 50 | + |
| 51 | + @Mock |
| 52 | + private OrganisationCurrencyService organisationCurrencyService; |
| 53 | + |
| 54 | + @InjectMocks |
| 55 | + private OrganisationService organisationService; |
| 56 | + |
| 57 | + private Organisation organisation; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void setUp() { |
| 61 | + organisation = new Organisation(); |
| 62 | + organisation.setId("org-123"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testFindById_WhenOrganisationExists() { |
| 67 | + when(organisationRepository.findById("org-123")).thenReturn(Optional.of(organisation)); |
| 68 | + Optional<Organisation> result = organisationService.findById("org-123"); |
| 69 | + assertTrue(result.isPresent()); |
| 70 | + assertEquals("org-123", result.get().getId()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testFindById_WhenOrganisationDoesNotExist() { |
| 75 | + when(organisationRepository.findById("org-123")).thenReturn(Optional.empty()); |
| 76 | + Optional<Organisation> result = organisationService.findById("org-123"); |
| 77 | + assertFalse(result.isPresent()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testFindAll() { |
| 82 | + List<Organisation> organisations = List.of(organisation); |
| 83 | + when(organisationRepository.findAll()).thenReturn(organisations); |
| 84 | + List<Organisation> result = organisationService.findAll(); |
| 85 | + assertEquals(1, result.size()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testGetAllCostCenter() { |
| 90 | + Set<OrganisationCostCenter> costCenters = new HashSet<>(); |
| 91 | + when(costCenterService.getAllCostCenter("org-123")).thenReturn(costCenters); |
| 92 | + Set<OrganisationCostCenter> result = organisationService.getAllCostCenter("org-123"); |
| 93 | + assertEquals(costCenters, result); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void testGetAllProjects() { |
| 98 | + Set<OrganisationProject> projects = new HashSet<>(); |
| 99 | + when(projectMappingRepository.findAllByOrganisationId("org-123")).thenReturn(projects); |
| 100 | + Set<OrganisationProject> result = organisationService.getAllProjects("org-123"); |
| 101 | + assertEquals(projects, result); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testUpsertOrganisation_NewOrganisation() { |
| 106 | + OrganisationUpsert organisationUpsert = new OrganisationUpsert(); |
| 107 | + organisationUpsert.setCountryCode("US"); |
| 108 | + organisationUpsert.setTaxIdNumber("12345"); |
| 109 | + organisationUpsert.setAddress("Street"); |
| 110 | + organisationUpsert.setName("Company name"); |
| 111 | + organisationUpsert. setAdminEmail( "[email protected]"); |
| 112 | + organisationUpsert.setCity("City name"); |
| 113 | + organisationUpsert.setCountry("Country name"); |
| 114 | + organisationUpsert.setCurrencyId("ISO_4217:CHF"); |
| 115 | + organisationUpsert.setPostCode("A127"); |
| 116 | + organisationUpsert.setProvince("County co."); |
| 117 | + organisationUpsert.setReportCurrencyId("ISO_4217:CHF"); |
| 118 | + organisationUpsert.setPhoneNumber("0101010101"); |
| 119 | + |
| 120 | + when(organisationRepository.findById(any())).thenReturn(Optional.empty()); |
| 121 | + when(organisationRepository.saveAndFlush(any())).thenReturn(organisation); |
| 122 | + |
| 123 | + OrganisationView result = organisationService.upsertOrganisation(organisationUpsert); |
| 124 | + assertNotNull(result); |
| 125 | + assertEquals("6d50ed2208aba5047f54a0b4e603d77463db27f108de9a268bb1670fa9afef11",result.getId()); |
| 126 | + assertEquals("Street",result.getAddress()); |
| 127 | + assertEquals("City name",result.getCity()); |
| 128 | + assertEquals("Country name",result.getCountry()); |
| 129 | + assertEquals("County co.",result.getProvince()); |
| 130 | + } |
| 131 | +} |
0 commit comments