Skip to content

Commit

Permalink
chore: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilczaja committed Nov 28, 2024
1 parent eea6eab commit dfb7526
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {
// https://github.com/quarkusio/quarkus/wiki/Migration-Guide-3.0#fixation-of-the-mockito-subclass-mockmaker
exclude(group = libs.mockito.subclass.get().group, module = libs.mockito.subclass.get().name)
}
testImplementation("io.quarkus:quarkus-test-security")
testImplementation(libs.bundles.assertj)
testImplementation(libs.awaitility)
testImplementation(libs.bundles.mockito)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import jakarta.annotation.security.PermitAll
import jakarta.enterprise.context.ApplicationScoped
import jakarta.transaction.Transactional

@PermitAll // auth checks will be in code in this unit
@PermitAll
@ApplicationScoped
class UiResourceImpl(
val authUtils: AuthUtils,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class CaasManagementApiService(
@ConfigProperty(name = "quarkus.oidc-client.sovity.client-enabled") val isCaasClientEnabled: Boolean
) {

@Transactional
@Transactional(Transactional.TxType.REQUIRES_NEW)
fun createCaas(
organizationId: String,
userId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,29 @@ import de.sovity.authorityportal.web.thirdparty.caas.CaasClient
import de.sovity.authorityportal.web.thirdparty.caas.model.CaasDetails
import de.sovity.authorityportal.web.thirdparty.caas.model.CaasPortalResponse
import de.sovity.authorityportal.web.utils.idmanagement.ClientIdUtils
import io.quarkus.security.identity.SecurityIdentity
import io.quarkus.test.InjectMock
import io.quarkus.test.TestTransaction
import io.quarkus.test.junit.QuarkusTest
import io.quarkus.test.security.TestSecurity
import jakarta.enterprise.context.control.ActivateRequestContext
import jakarta.inject.Inject
import org.assertj.core.api.Assertions.assertThat
import org.flywaydb.core.Flyway
import org.jooq.DSLContext
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.whenever
import java.time.OffsetDateTime
import java.util.concurrent.CountDownLatch
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

@QuarkusTest
@ExtendWith(MockitoExtension::class)
Expand All @@ -65,9 +75,20 @@ class CaasManagementApiServiceTest {
@Inject
lateinit var clientIdUtils: ClientIdUtils

@Inject
lateinit var executorService: ExecutorService

@Inject
lateinit var flyway: Flyway

@InjectMock
lateinit var caasClient: CaasClient

@AfterEach
fun cleanup() {
flyway.clean()
}

@Test
@TestTransaction
fun `create caas creates connector with caas configuration`() {
Expand Down Expand Up @@ -166,4 +187,54 @@ class CaasManagementApiServiceTest {
assertThat(result.limit).isEqualTo(0)
assertThat(result.current).isEqualTo(0)
}

@Test
@TestSecurity(authorizationEnabled = false)
fun `create caas endpoint should lock correctly`() {
// arrange
val now = OffsetDateTime.now()
val i = AtomicInteger(0)

useDevUser(0, 0, setOf(Roles.UserRoles.PARTICIPANT_CURATOR))
useMockNow(now)

ScenarioData().apply {
organization(0, 0)
user(0, 0)

scenarioInstaller.install(this)
}

whenever(caasClient.validateSubdomain(any())).thenReturn(true)
whenever(caasClient.requestCaas(any())).thenReturn(
CaasPortalResponse().apply {
value = CaasDetails(connectorId = dummyDevConnectorId(0, 0))
}
)

// act
val count = 10
val latch = CountDownLatch(count)

repeat(count) {
executorService.execute {
uiResource.createCaas(
"test", CreateCaasRequest(
connectorSubdomain = "test-caas-${i.addAndGet(1)}",
connectorTitle = "Test CaaS-${i.addAndGet(1)}",
connectorDescription = "Connector-as-a-service for testing purposes"
)
)
latch.countDown()
}
}
latch.await(20, TimeUnit.SECONDS)

// assert
val connectors = dsl.selectFrom(Tables.CONNECTOR)
.where(Tables.CONNECTOR.TYPE.eq(ConnectorType.CAAS))
.fetch()

assertThat(connectors).hasSize(1)
}
}

0 comments on commit dfb7526

Please sign in to comment.