Skip to content

Commit

Permalink
Merge pull request #502 from catenax-ng/feat/administrative-areas-level1
Browse files Browse the repository at this point in the history
Feat: Create administrative areas level 1 endpoint
  • Loading branch information
nicoprow authored Oct 4, 2023
2 parents 2b138bb + 7f975fa commit 0fb6ad0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.eclipse.tractusx.bpdm.common.model.NamedType
import org.eclipse.tractusx.bpdm.common.model.NamedUrlType
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort

fun <T : NamedUrlType> T.toDto(): TypeKeyNameUrlDto<T> {
return TypeKeyNameUrlDto(this, getTypeName(), getUrl())
Expand All @@ -46,8 +47,8 @@ fun CountryCode.toDto(): TypeKeyNameVerboseDto<CountryCode> {
return TypeKeyNameVerboseDto(this, getName())
}

fun PaginationRequest.toPageRequest() =
PageRequest.of(page, size)
fun PaginationRequest.toPageRequest(sort: Sort = Sort.unsorted()) =
PageRequest.of(page, size, sort)

fun <T, R> Page<T>.toPageDto(contentMapper: (T) -> R): PageDto<R> =
PageDto(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ interface PoolMetadataApi {

@Operation(
summary = "Create new Region",
description = "Create a new region which can be referenced by business partner records. "
description = "Create a new region which can be referenced by business partner records.",
deprecated = true
)
@ApiResponses(
value = [
Expand All @@ -153,7 +154,8 @@ interface PoolMetadataApi {

@Operation(
summary = "Get page of regions",
description = "Lists all currently known regions in a paginated result"
description = "Lists all currently known regions in a paginated result",
deprecated = true
)
@ApiResponses(
value = [
Expand All @@ -165,5 +167,18 @@ interface PoolMetadataApi {
@GetExchange("/regions")
fun getRegions(@ParameterObject paginationRequest: PaginationRequest): PageDto<RegionDto>

@Operation(
summary = "Get page of regions suitable for the administrativeAreaLevel1 address property",
description = "Lists all currently known regions in a paginated result"
)
@ApiResponses(
value = [
ApiResponse(responseCode = "200", description = "Page of existing regions, may be empty"),
ApiResponse(responseCode = "400", description = "On malformed request parameters", content = [Content()])
]
)
@GetMapping("/administrative-areas-level1")
@GetExchange("/administrative-areas-level1")
fun getAdminAreasLevel1(@ParameterObject paginationRequest: PaginationRequest): PageDto<RegionDto>

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ class MetadataController(
return ResponseEntity(metadataService.getFieldQualityRules(country), HttpStatus.OK)
}

@PreAuthorize("hasAuthority(@poolSecurityConfigProperties.getReadMetaDataAsRole())")
override fun getAdminAreasLevel1(paginationRequest: PaginationRequest): PageDto<RegionDto> {
return metadataService.getRegions(paginationRequest)
}

@PreAuthorize("hasAuthority(@poolSecurityConfigProperties.getReadMetaDataAsRole())")
override fun getRegions(paginationRequest: PaginationRequest): PageDto<RegionDto> {
return metadataService.getRegions(PageRequest.of(paginationRequest.page, paginationRequest.size))
return metadataService.getRegions(paginationRequest)
}

@PreAuthorize("hasAuthority(@poolSecurityConfigProperties.getChangeMetaDataAsRole())")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
package org.eclipse.tractusx.bpdm.pool.repository

import org.eclipse.tractusx.bpdm.pool.entity.Region
import org.springframework.data.domain.Sort
import org.springframework.data.repository.CrudRepository
import org.springframework.data.repository.PagingAndSortingRepository

interface RegionRepository : PagingAndSortingRepository<Region, Long>, CrudRepository<Region, Long> {

companion object {
val DEFAULT_SORTING = Sort.by(Region::countryCode.name, Region::regionCode.name)
}

fun findByRegionCodeIn(regionCodes: Set<String>): Set<Region>

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ package org.eclipse.tractusx.bpdm.pool.service
import com.neovisionaries.i18n.CountryCode
import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.common.dto.*
import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.response.LegalFormDto
import org.eclipse.tractusx.bpdm.common.dto.response.PageDto
import org.eclipse.tractusx.bpdm.common.dto.response.RegionDto
import org.eclipse.tractusx.bpdm.common.service.toPageRequest
import org.eclipse.tractusx.bpdm.pool.api.model.request.LegalFormRequest
import org.eclipse.tractusx.bpdm.pool.dto.AddressMetadataDto
import org.eclipse.tractusx.bpdm.pool.dto.LegalEntityMetadataDto
Expand Down Expand Up @@ -188,7 +190,8 @@ class MetadataService(
return regionRepository.save(region).toDto()
}

fun getRegions(pageRequest: Pageable): PageDto<RegionDto> {
fun getRegions(paginationRequest: PaginationRequest): PageDto<RegionDto> {
val pageRequest = paginationRequest.toPageRequest(RegionRepository.DEFAULT_SORTING)
val page = regionRepository.findAll(pageRequest)
return page.toDto(page.content.map { it.toDto() })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private typealias GetFunction = (client: WebTestClient, page: Int, size: Int) ->
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = [Application::class, TestHelpers::class]
)
@ActiveProfiles("test")
@ContextConfiguration(initializers = [PostgreSQLContextInitializer::class,OpenSearchContextInitializer::class])
@ContextConfiguration(initializers = [PostgreSQLContextInitializer::class, OpenSearchContextInitializer::class])
class MetadataControllerIT @Autowired constructor(
private val testHelpers: TestHelpers,
private val webTestClient: WebTestClient,
Expand All @@ -71,7 +71,11 @@ class MetadataControllerIT @Autowired constructor(
) {
companion object {

private val identifierTypeDtos = listOf(BusinessPartnerNonVerboseValues.identifierTypeDto1, BusinessPartnerNonVerboseValues.identifierTypeDto2, BusinessPartnerNonVerboseValues.identifierTypeDto3)
private val identifierTypeDtos = listOf(
BusinessPartnerNonVerboseValues.identifierTypeDto1,
BusinessPartnerNonVerboseValues.identifierTypeDto2,
BusinessPartnerNonVerboseValues.identifierTypeDto3
)

private val legalFormRequests = listOf(
BusinessPartnerNonVerboseValues.legalForm1,
Expand Down Expand Up @@ -322,14 +326,14 @@ class MetadataControllerIT @Autowired constructor(
businessPartnerType = IdentifierBusinessPartnerType.LEGAL_ENTITY,
name = BusinessPartnerNonVerboseValues.identifierType2.name
)
identifierType2.details.add(IdentifierTypeDetail(identifierType2, CountryCode.UK, false))
identifierType2.details.add(IdentifierTypeDetail(identifierType2, UK, false))

val identifierType3 = IdentifierType(
technicalKey = BusinessPartnerNonVerboseValues.identifierType3.technicalKey,
businessPartnerType = IdentifierBusinessPartnerType.LEGAL_ENTITY,
name = BusinessPartnerNonVerboseValues.identifierType3.name
)
identifierType3.details.add(IdentifierTypeDetail(identifierType3, CountryCode.PL, false))
identifierType3.details.add(IdentifierTypeDetail(identifierType3, PL, false))

val givenIdentifierTypes = listOf(identifierType1, identifierType2, identifierType3)

Expand All @@ -343,7 +347,7 @@ class MetadataControllerIT @Autowired constructor(
val result = webTestClient.invokeGetEndpoint<PageDto<IdentifierTypeDto>>(
EndpointValues.CATENA_METADATA_IDENTIFIER_TYPE_PATH,
Pair("businessPartnerType", IdentifierBusinessPartnerType.LEGAL_ENTITY.name),
Pair("country", CountryCode.PL.alpha2)
Pair("country", PL.alpha2)
)

assertThat(result.content).containsExactlyInAnyOrderElementsOf(expected)
Expand Down Expand Up @@ -425,4 +429,31 @@ class MetadataControllerIT @Autowired constructor(
)
return rule
}
}

@Test
fun `get all administrative-areas-level1`() {
val firstPage = poolClient.metadata.getAdminAreasLevel1(PaginationRequest())
assertThat(firstPage.totalElements).isEqualTo(3478L)
assertThat(firstPage.totalPages).isEqualTo(348)
assertThat(firstPage.content.size).isEqualTo(10)

// INSERT INTO bpdm.regions (country_code, region_code, region_name) VALUES ('AD', 'AD-02', 'Canillo');
with(firstPage.content.first()) {
assertThat(countryCode).isEqualTo(AD)
assertThat(regionCode).isEqualTo("AD-02")
assertThat(regionName).isEqualTo("Canillo")
}

val lastPage = poolClient.metadata.getAdminAreasLevel1(PaginationRequest(347))
assertThat(lastPage.totalElements).isEqualTo(3478L)
assertThat(lastPage.totalPages).isEqualTo(348)
assertThat(lastPage.content.size).isEqualTo(8)

// INSERT INTO bpdm.regions (country_code, region_code, region_name) VALUES ('ZW', 'ZW-MW', 'Mashonaland West');
with(lastPage.content.last()) {
assertThat(countryCode).isEqualTo(ZW)
assertThat(regionCode).isEqualTo("ZW-MW")
assertThat(regionName).isEqualTo("Mashonaland West")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TestHelpers(
FOR table_names IN SELECT table_name
FROM information_schema.tables
WHERE table_schema='$BPDM_DB_SCHEMA_NAME'
AND table_name NOT IN ('flyway_schema_history')
AND table_name NOT IN ('flyway_schema_history','regions')
LOOP
EXECUTE format('TRUNCATE TABLE $BPDM_DB_SCHEMA_NAME.%I CONTINUE IDENTITY CASCADE;', table_names.table_name);
END LOOP;
Expand Down

0 comments on commit 0fb6ad0

Please sign in to comment.