Skip to content

Commit

Permalink
review note #5
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls committed Aug 3, 2022
1 parent 7babf58 commit 2167d02
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 20 deletions.
2 changes: 1 addition & 1 deletion db/test-data/git-insert.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<column name="url" value="https://github.com/saveourtool/save-cli" />
<column name="username" value="null" />
<column name="password" value="null" />
<column name="organization_id" value="1" />
<column name="organization_id" value="3" />
</insert>
</changeSet>
</databaseChangeLog>
3 changes: 2 additions & 1 deletion db/test-data/sqlRequests/organization.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
id;name;owner_id;date_created;can_create_contests
1;"Huawei";1;"2021-01-01 00:00:00";true
2;"Example.com";1;"2021-01-01 00:00:00";false
2;"Example.com";1;"2021-01-01 00:00:00";false
3;"CQFN.org";1;"2022-01-01 00:00:00";false
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ class ExecutionController(private val executionService: ExecutionService,
)) {
throw ResponseStatusException(HttpStatus.FORBIDDEN)
}
val executionType = execution.type
val git = gitService.getByOrganizationAndUrl(execution.project.organization, execution.getTestSuiteRepoUrl())
.toDto()
val executionType = execution.type
val testRootPath = if (executionType == ExecutionType.GIT) {
execution.getTestRootPathByTestSuites()
.distinct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class TestSuitesController(
* @return response with list of test suite dtos
*/
@GetMapping(path = ["/api/$v1/allStandardTestSuites", "/internal/allStandardTestSuites"])
fun getAllStandardTestSuites(): ResponseListTestSuites =
ResponseEntity.status(HttpStatus.OK).body(testSuitesService.getStandardTestSuites())
fun getAllStandardTestSuites(): Mono<ResponseListTestSuites> =
testSuitesService.getStandardTestSuites().map { ResponseEntity.status(HttpStatus.OK).body(it) }

/**
* @param id id of the test suite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toFlux
import reactor.kotlin.core.publisher.toMono
import reactor.kotlin.core.util.function.component1
import reactor.kotlin.core.util.function.component2

typealias TestSuiteList = List<TestSuite>

Expand Down Expand Up @@ -307,14 +309,19 @@ class TestSuitesSourceController(
)
@ApiResponse(responseCode = "200", description = "Successfully get or create test suites source with requested values.")
@ApiResponse(responseCode = "409", description = "Organization was not found by provided name.")
@ApiResponse(responseCode = "409", description = "Git credentials was not found by provided url.")
fun getOrCreate(
@PathVariable organizationName: String,
@RequestParam gitUrl: String,
@RequestParam testRootPath: String,
@RequestParam branch: String,
): Mono<TestSuitesSourceDto> = getOrganization(organizationName)
.map { organization ->
organization to gitService.getByOrganizationAndUrl(organization, gitUrl)
.zipWhen { organization ->
gitService.findByOrganizationAndUrl(organization, gitUrl)
.toMono()
.switchIfEmptyToResponseException(HttpStatus.CONFLICT) {
"There is no git credential with url $gitUrl in $organizationName"
}
}
.map { (organization, git) ->
testSuitesSourceService.getOrCreate(organization, git, testRootPath, branch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,4 @@ interface TestSuiteRepository : BaseEntityRepository<TestSuite>, QueryByExampleE
source: TestSuitesSource,
version: String
): List<TestSuite>

/**
* @param source source of the test suite
* @return matched test suites
*/
fun findAllBySource(
source: TestSuitesSource,
): List<TestSuite>
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ class GitService(private val gitRepository: GitRepository) {
*/
fun getAllByOrganization(organization: Organization): List<Git> = gitRepository.findAllByOrganizationId(organization.requiredId())

/**
* @param organization
* @param url
* @return list of gits by organization if exists or null
*/
fun findByOrganizationAndUrl(organization: Organization, url: String): Git? = gitRepository.findByOrganizationAndUrl(organization, url)


/**
* @param organization
* @param url
* @return list of gits by organization if exists
* @throws NoSuchElementException
*/
fun getByOrganizationAndUrl(organization: Organization, url: String): Git = gitRepository.findByOrganizationAndUrl(organization, url)
fun getByOrganizationAndUrl(organization: Organization, url: String): Git = findByOrganizationAndUrl(organization, url)
?: throw NoSuchElementException("There is no git credential with url $url in ${organization.name}")

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.saveourtool.save.backend.service
import com.saveourtool.save.backend.repository.TestExecutionRepository
import com.saveourtool.save.backend.repository.TestRepository
import com.saveourtool.save.backend.repository.TestSuiteRepository
import com.saveourtool.save.backend.storage.TestSuitesSourceSnapshotStorage
import com.saveourtool.save.backend.utils.blockingToFlux
import com.saveourtool.save.entities.TestSuite
import com.saveourtool.save.entities.TestSuitesSource
import com.saveourtool.save.testsuite.TestSuiteDto
Expand All @@ -13,8 +15,12 @@ import org.springframework.data.domain.Example
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException
import reactor.core.publisher.Mono
import reactor.kotlin.extra.math.max
import java.time.LocalDateTime

typealias TestSuiteDtoList = List<TestSuiteDto>

/**
* Service for test suites
*/
Expand All @@ -24,6 +30,7 @@ class TestSuitesService(
private val testRepository: TestRepository,
private val testExecutionRepository: TestExecutionRepository,
private val testSuitesSourceService: TestSuitesSourceService,
private val testSuitesSourceSnapshotStorage: TestSuitesSourceSnapshotStorage,
) {
/**
* Save new test suites to DB
Expand Down Expand Up @@ -76,9 +83,17 @@ class TestSuitesService(
/**
* @return all standard test suites
*/
fun getStandardTestSuites() = testSuitesSourceService.getStandardTestSuitesSources()
.flatMap { testSuitesSource -> testSuiteRepository.findAllBySource(testSuitesSource) }
.map { it.toDto() }
fun getStandardTestSuites(): Mono<TestSuiteDtoList> = blockingToFlux { testSuitesSourceService.getStandardTestSuitesSources() }
.flatMap { testSuitesSource ->
testSuitesSourceSnapshotStorage.list(testSuitesSource.organization.name, testSuitesSource.name)
.max { max, next -> max.creationTimeInMills.compareTo(next.creationTimeInMills) }
.map { testSuitesSource to it.version }
}
.flatMap { (testSuitesSource, version) ->
blockingToFlux { testSuiteRepository.findAllBySourceAndVersion(testSuitesSource, version) }
}
.map { it.toDto() }
.collectList()

/**
* @param id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class TestSuitesSourceService(
fun getStandardTestSuitesSources(): List<TestSuitesSource> {
// FIXME: a hardcoded values for standard test suites
// Will be removed in phase 3
val organizationName = "Huawei"
val organizationName = "CQFN.org"
val gitUrl = "https://github.com/saveourtool/save-cli"
val branch = "main"
val testRootPaths = listOf("examples/kotlin-diktat", "examples/discovery-test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.core.scheduler.Schedulers
import reactor.kotlin.core.publisher.toFlux
import reactor.kotlin.core.publisher.toMono

Expand Down Expand Up @@ -92,3 +93,15 @@ fun <T : Any> T?.toMonoOrNotFound(message: String? = null) = toMono<T>().switchI
fun <T> justOrNotFound(data: Optional<T>, message: String? = null) = Mono.justOrEmpty(data).switchIfEmptyToNotFound {
message
}

/**
* Taking from https://projectreactor.io/docs/core/release/reference/#faq.wrap-blocking
*
* @param supplier blocking operation like JDBC
*/
fun <T> blockingToMono(supplier: () -> T): Mono<T> = Mono.fromCallable(supplier).subscribeOn(Schedulers.boundedElastic())

/**
* @param supplier blocking operation like JDBC
*/
fun <T> blockingToFlux(supplier: () -> Iterable<T>): Flux<T> = blockingToMono(supplier).flatMapIterable { it }

0 comments on commit 2167d02

Please sign in to comment.