import aQute.bnd.gradle.Bundle

plugins {
    id 'corda.osgi-test-conventions'
    id 'corda.common-library'
}

description "Sandbox stress tests"

configurations {
    cpis {
        canBeConsumed = false
        transitive = false
    }
}

dependencies {
    compileOnly 'org.osgi:org.osgi.service.component.annotations'
    compileOnly "org.osgi:org.osgi.service.component:$osgiServiceComponentVersion"

    implementation platform("net.corda:corda-api:$cordaApiVersion")

    implementation project(':components:configuration:configuration-read-service')
    implementation project(':components:db:db-connection-manager')

    implementation project(':components:membership:group-policy')
    implementation project(':components:persistence:entity-processor-service')
    implementation project(':components:persistence:persistence-service-common')
    implementation project(':components:virtual-node:cpi-info-read-service')
    implementation project(":components:virtual-node:cpk-read-service")
    implementation project(':components:virtual-node:sandbox-group-context-service')
    implementation project(':components:virtual-node:virtual-node-info-read-service')

    implementation project(':libs:crypto:merkle-impl')
    implementation project(':libs:flows:external-event-responses')
    implementation project(':libs:flows:flow-utils')
    implementation project(':libs:flows:flow-api')
    implementation project(":libs:messaging:messaging")
    implementation project(':libs:metrics')
    implementation project(':libs:serialization:serialization-checkpoint-api')
    implementation project(":libs:tracing")
    implementation project(":libs:utilities")
    implementation project(':libs:virtual-node:sandbox-group-context')
    implementation project(":libs:virtual-node:virtual-node-datamodel")
    implementation project(":libs:serialization:serialization-avro")

    implementation 'net.corda:corda-application'
    implementation 'net.corda:corda-avro-schema'
    implementation 'net.corda:corda-config-schema'
    implementation 'net.corda:corda-db-schema'
    implementation 'net.corda:corda-topic-schema'
    implementation 'org.jetbrains.kotlin:kotlin-osgi-bundle'
    implementation 'org.slf4j:slf4j-api'
    implementation libs.micrometer.registry.prometheus

    testImplementation project(':testing:db-testkit')
    testImplementation project(':testing:persistence-testkit')
    testImplementation project(':testing:sandboxes-testkit')

    testImplementation project(':libs:crypto:merkle-impl')
    testImplementation project(':libs:flows:session-manager-impl')
    testImplementation project(':libs:lifecycle:lifecycle-test-impl')
    testImplementation project(':libs:lifecycle:lifecycle-impl')
    testImplementation project(':libs:lifecycle:registry')
    testImplementation project(':testing:flow:flow-utilities')
    testImplementation project(':testing:test-utilities')

    testRuntimeOnly "org.ops4j.pax.jdbc:pax-jdbc-hsqldb:$paxJdbcVersion"
    testRuntimeOnly libs.postgresql.jdbc

    // Integration test resources using cpis configuration
    cpis project(path: ':testing:cpbs:extendable-cpb', configuration: 'cordaCPB')
    cpis project(path: ':testing:bundles:testing-fish', configuration: 'cordaCPB')

    // IMPORTANT: do NOT include cpbs (or cpks) as dependencies of the integration test.
    // This would cause OSGi to load the bundle twice (once as part of the test) and again
    // as part of the CPB loading.  This introduces *two* unique instances of classes in the CPKs (remember
    // the class is unique by name **and** classloader).

    // IMPORTANT:  do NOT attempt to use mockito-kotlin in the integration tests.
    // It's not an OSGi bundle, so you will get errors (despite Intellij appearing to allow you to use it).

    integrationTestImplementation libs.mockito.core

    integrationTestImplementation project(':components:flow:flow-service')
    integrationTestImplementation project(":testing:db-message-bus-testkit")
    integrationTestImplementation project(':libs:crypto:merkle-impl')
    integrationTestImplementation project(':libs:db:db-admin-impl')
    integrationTestImplementation project(':testing:sandboxes')
    integrationTestImplementation project(':testing:test-utilities')
    integrationTestImplementation project(':testing:virtual-node-info-read-service-fake')
    integrationTestImplementation project(':testing:cpi-info-read-service-fake')
    integrationTestImplementation project(':testing:group-policy-test-common')

    // needed to import serialization libs
    integrationTestRuntimeOnly "org.apache.aries.spifly:org.apache.aries.spifly.dynamic.framework.extension:$ariesDynamicFrameworkExtensionVersion"

    integrationTestRuntimeOnly project(':libs:flows:external-event-responses-impl')
    integrationTestRuntimeOnly project(':libs:lifecycle:lifecycle-impl')
    integrationTestRuntimeOnly project(':libs:messaging:db-message-bus-impl')
    integrationTestRuntimeOnly project(':libs:messaging:messaging-impl')
    integrationTestRuntimeOnly project(':libs:state-manager:state-manager-db-impl')
    integrationTestRuntimeOnly project(':libs:serialization:serialization-checkpoint-api')
    integrationTestRuntimeOnly project(':components:db:db-connection-manager-impl')
    integrationTestRuntimeOnly project(':components:membership:membership-group-read-impl')
    integrationTestRuntimeOnly project(':components:virtual-node:cpk-read-service-impl')

    integrationTestRuntimeOnly project(':libs:application:application-impl')
    integrationTestRuntimeOnly project(':libs:flows:session-manager-impl')
    integrationTestRuntimeOnly project(':libs:serialization:serialization-kryo')

    integrationTestRuntimeOnly "com.sun.activation:javax.activation:$activationVersion"
    integrationTestRuntimeOnly "org.hsqldb:hsqldb:$hsqldbVersion"
    integrationTestRuntimeOnly libs.postgresql.jdbc
}

//  Copy the cpi builds declared in the cpis configuration into our resources so we find and load them
def integrationTestResources = tasks.named('processIntegrationTestResources', ProcessResources) {
    from(configurations.cpis) {
        into 'META-INF'
        rename "(.*)-\\Q${version}\\E-package.cpb", "\$1.cpb"
    }
}

tasks.named('testingBundle', Bundle) {
    dependsOn integrationTestResources
}