Skip to content

Commit

Permalink
fix(Gate): Resolve bug on page requesting and content on other pages
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiodmota committed Mar 31, 2023
1 parent 36b4099 commit bae9c8c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ interface GateChangelogApi {
fun getChangelogEntriesExternalId(
@ParameterObject @Valid paginationRequest: PaginationRequest,
@Parameter(description = "From Time", example = "2023-03-20T10:23:28.194Z") @RequestParam(required = false) fromTime: Instant?,
@RequestBody(required = true) @NotEmpty(message = "Input externalIds list cannot be empty.") externalIds: Collection<String>
@RequestBody(required = true) @NotEmpty(message = "Input externalIds list cannot be empty.") externalIds: Set<String>
): PageChangeLogResponse<ChangelogResponse>

@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ChangelogController(
) : GateChangelogApi {

override fun getChangelogEntriesExternalId(
paginationRequest: PaginationRequest, fromTime: Instant?, externalIds: Collection<String>
paginationRequest: PaginationRequest, fromTime: Instant?, externalIds: Set<String>
): PageChangeLogResponse<ChangelogResponse> {
return changelogService.getChangeLogByExternalId(externalIds, fromTime, paginationRequest.page, paginationRequest.size)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

package org.eclipse.tractusx.bpdm.gate.repository

import io.swagger.v3.oas.annotations.Parameter
import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType
import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntity
import org.springframework.data.jpa.domain.Specification
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.JpaSpecificationExecutor
import org.springframework.data.jpa.repository.Query
import java.time.Instant


Expand Down Expand Up @@ -64,5 +66,8 @@ interface ChangelogRepository : JpaRepository<ChangelogEntity, Long>, JpaSpecifi
}
}

@Query("select distinct u.externalId from ChangelogEntity u where u.externalId in :externalIdList")
fun findExternalIdsInListDistinct(externalIdList: Collection<String>): Set<String>


}
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,45 @@ import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository.Specs.byCreatedAtGreaterThan
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository.Specs.byExternalIdsIn
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository.Specs.byLsaType
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.PageRequest
import org.springframework.data.jpa.domain.Specification
import org.springframework.stereotype.Service

import java.time.Instant
import kotlin.math.ceil

@Service
class ChangelogService(private val changelogRepository: ChangelogRepository) {

fun getChangeLogByExternalId(externalIds: Collection<String>, createdAt: Instant?, page: Int, pageSize: Int): PageChangeLogResponse<ChangelogResponse> {
fun getChangeLogByExternalId(externalIds: Set<String>, createdAt: Instant?, page: Int, pageSize: Int): PageChangeLogResponse<ChangelogResponse> {

val spec = Specification.allOf(byExternalIdsIn(externalIds = externalIds), byCreatedAtGreaterThan(createdAt = createdAt))
val pageable = PageRequest.of(page, pageSize)
val pageResponse = changelogRepository.findAll(spec, pageable)
val setDistinctList = changelogRepository.findExternalIdsInListDistinct(externalIds)


val pageDto = pageResponse.map {
it.toGateDto()
}

val errorInfoSet = externalIds.filterNot { id ->
pageDto.content.any { it.externalId == id }
}.map {
val errorList = (externalIds - setDistinctList).map {
ErrorInfo(
ChangeLogOutputError.ExternalIdNotFound,
"$it not found",
it
)
}.toSet()
}


return PageChangeLogResponse(
page = page, totalElements = pageDto.totalElements,
totalPages = pageDto.totalPages,
contentSize = pageDto.content.size,
content = pageDto.content,
invalidEntries = errorInfoSet.size,
errors = errorInfoSet
invalidEntries = errorList.size,
errors = errorList
)
}

Expand All @@ -86,4 +89,5 @@ class ChangelogService(private val changelogRepository: ChangelogRepository) {
content = pageDto.content,
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal class ChangeLogControllerIT @Autowired constructor(
@Test
fun `get changeLog by external id`() {

val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), null, listOf(CommonValues.externalIdAddress1))
val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), null, setOf(CommonValues.externalIdAddress1))

assertRecursively(searchResult.content)
.ignoringFieldsMatchingRegexes(".*${ChangelogResponse::modifiedAt.name}")
Expand All @@ -133,7 +133,7 @@ internal class ChangeLogControllerIT @Autowired constructor(
@Test
fun `get changeLog by external id not found`() {

val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), null, listOf("NONEXIST"))
val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), null, setOf("NONEXIST"))

assertThat(searchResult.content)
.usingRecursiveComparison()
Expand Down Expand Up @@ -164,7 +164,7 @@ internal class ChangeLogControllerIT @Autowired constructor(
@Test
fun `get changeLog by external id and timeStamp`() {

val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), instant, listOf(CommonValues.externalIdAddress1))
val searchResult = gateClient.changelog().getChangelogEntriesExternalId(PaginationRequest(), instant, setOf(CommonValues.externalIdAddress1))


assertRecursively(searchResult.content).ignoringFieldsMatchingRegexes(".*${ChangelogResponse::modifiedAt.name}")
Expand Down

0 comments on commit bae9c8c

Please sign in to comment.