Skip to content

Commit

Permalink
Merge pull request #165 from cardano-foundation/feat/organisation_crud
Browse files Browse the repository at this point in the history
feat: organisation create and edit endpoint
  • Loading branch information
M4rc0Russ0 authored Feb 27, 2025
2 parents 20cadfc + b04ccfc commit 342a750
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.cardanofoundation.lob.app.organisation.service;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.cardanofoundation.lob.app.organisation.domain.entity.Organisation;
import org.cardanofoundation.lob.app.organisation.domain.entity.OrganisationCostCenter;
import org.cardanofoundation.lob.app.organisation.domain.entity.OrganisationProject;
import org.cardanofoundation.lob.app.organisation.domain.request.OrganisationUpsert;
import org.cardanofoundation.lob.app.organisation.domain.view.OrganisationView;
import org.cardanofoundation.lob.app.organisation.repository.*;

@ExtendWith(MockitoExtension.class)
class OrganisationServiceTest {

@Mock
private OrganisationRepository organisationRepository;

@Mock
private CostCenterService costCenterService;

@Mock
private ProjectMappingRepository projectMappingRepository;

@Mock
private OrganisationChartOfAccountTypeRepository organisationChartOfAccountTypeRepository;

@Mock
private ChartOfAccountRepository organisationChartOfAccountRepository;

@Mock
private OrganisationChartOfAccountSubTypeRepository organisationChartOfAccountSubTypeRepository;

@Mock
private AccountEventRepository accountEventRepository;

@Mock
private OrganisationCurrencyService organisationCurrencyService;

@InjectMocks
private OrganisationService organisationService;

private Organisation organisation;

@BeforeEach
void setUp() {
organisation = new Organisation();
organisation.setId("org-123");
}

@Test
void testFindById_WhenOrganisationExists() {
when(organisationRepository.findById("org-123")).thenReturn(Optional.of(organisation));
Optional<Organisation> result = organisationService.findById("org-123");
assertTrue(result.isPresent());
assertEquals("org-123", result.get().getId());
}

@Test
void testFindById_WhenOrganisationDoesNotExist() {
when(organisationRepository.findById("org-123")).thenReturn(Optional.empty());
Optional<Organisation> result = organisationService.findById("org-123");
assertFalse(result.isPresent());
}

@Test
void testFindAll() {
List<Organisation> organisations = List.of(organisation);
when(organisationRepository.findAll()).thenReturn(organisations);
List<Organisation> result = organisationService.findAll();
assertEquals(1, result.size());
}

@Test
void testGetAllCostCenter() {
Set<OrganisationCostCenter> costCenters = new HashSet<>();
when(costCenterService.getAllCostCenter("org-123")).thenReturn(costCenters);
Set<OrganisationCostCenter> result = organisationService.getAllCostCenter("org-123");
assertEquals(costCenters, result);
}

@Test
void testGetAllProjects() {
Set<OrganisationProject> projects = new HashSet<>();
when(projectMappingRepository.findAllByOrganisationId("org-123")).thenReturn(projects);
Set<OrganisationProject> result = organisationService.getAllProjects("org-123");
assertEquals(projects, result);
}

@Test
void testUpsertOrganisation_NewOrganisation() {
OrganisationUpsert organisationUpsert = new OrganisationUpsert();
organisationUpsert.setCountryCode("US");
organisationUpsert.setTaxIdNumber("12345");
organisationUpsert.setAddress("Street");
organisationUpsert.setName("Company name");
organisationUpsert.setAdminEmail("[email protected]");
organisationUpsert.setCity("City name");
organisationUpsert.setCountry("Country name");
organisationUpsert.setCurrencyId("ISO_4217:CHF");
organisationUpsert.setPostCode("A127");
organisationUpsert.setProvince("County co.");
organisationUpsert.setReportCurrencyId("ISO_4217:CHF");
organisationUpsert.setPhoneNumber("0101010101");

when(organisationRepository.findById(any())).thenReturn(Optional.empty());
when(organisationRepository.saveAndFlush(any())).thenReturn(organisation);

OrganisationView result = organisationService.upsertOrganisation(organisationUpsert);
assertNotNull(result);
assertEquals("6d50ed2208aba5047f54a0b4e603d77463db27f108de9a268bb1670fa9afef11",result.getId());
assertEquals("Street",result.getAddress());
assertEquals("City name",result.getCity());
assertEquals("Country name",result.getCountry());
assertEquals("County co.",result.getProvince());
}
}
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ subprojects {
}

property("sonar.exclusions", "" +
"organisation/**, " +
"**/views/**, " +
"**/requests/**, " +
"**/entity/**, " +
Expand Down
6 changes: 1 addition & 5 deletions organisation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ plugins {
id("org.sonarqube")
}

// Skipping the project from SonarQube analysis, since it doesn't have tests
sonarqube {
isSkipProject = false
}

dependencies {

implementation("org.springframework.modulith:spring-modulith-api")
implementation("org.springframework.boot:spring-boot-starter-security")

implementation(project(":support"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ public class Organisation extends CommonEntity implements Persistable<String> {
@Column(name = "name", nullable = false)
private String name;

@Column(name = "city", nullable = false)
private String city;

@Column(name = "post_code", nullable = false)
private String postCode;

@Column(name = "province", nullable = false)
private String province;

@Column(name = "country", nullable = false)
private String country;

@Column(name = "address", nullable = false)
private String address;

@Column(name = "phone_number")
private String phoneNumber;

@Column(name = "tax_id_number", nullable = false)
private String taxIdNumber;

Expand All @@ -58,6 +76,12 @@ public class Organisation extends CommonEntity implements Persistable<String> {
@Column(name = "currency_id", nullable = false)
private String currencyId;

@Column(name = "report_currency_id", nullable = false)
private String reportCurrencyId;

@Column(name = "website")
private String webSite;

@Column(name = "admin_email", nullable = false)
private String adminEmail;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.cardanofoundation.lob.app.organisation.domain.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import io.swagger.v3.oas.annotations.media.Schema;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class OrganisationUpsert {

@Schema(example = "My company name")
private String name;

@Schema(example = "Ballyhealy")
private String city;

@Schema(example = "Y35 C6KC")
private String postCode;

@Schema(example = "Co. Wexford")
private String province;

@Schema(example = "Ireland")
private String country;

@Schema(example = "IE")
private String countryCode;

@Schema(example = "Ballyhealy Cottage")
private String address;

@Schema(example = "0035863286566")
private String phoneNumber;

@Schema(example = "[email protected]")
private String adminEmail;

@Schema(example = "http://cardanofoundation.org")
private String websiteUrl;

@Schema(example = "ISO_4217:CHF")
private String currencyId;

@Schema(example = "ISO_4217:CHF")
private String reportCurrencyId;

@Schema(example = "CHE-184477354")
private String taxIdNumber;

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ public class OrganisationView {
@Schema(example = "[email protected]")
private String adminEmail;

@Schema(example = "0035863286566")
private String phoneNumber;

@Schema(example = "Street name and number")
private String address;

@Schema(example = "Ballyhealy")
private String city;

@Schema(example = "Y35 C6KC")
private String postCode;

@Schema(example = "Co. Wexford")
private String province;

@Schema(example = "Ireland")
private String country;

private Set<OrganisationCostCenterView> costCenter;

private Set<OrganisationCostCenterView> projects;
Expand Down
Loading

0 comments on commit 342a750

Please sign in to comment.