Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate language-support codegen tests to sandbox #11508

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions language-support/java/codegen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ java_test(
deps = [
":integration-tests-model-%s.jar" % target,
"//language-support/java/bindings:bindings-java",
"//ledger/sandbox-classic:sandbox-classic-scala-tests-lib",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:org_junit_jupiter_junit_jupiter_api",
"@maven//:org_junit_jupiter_junit_jupiter_engine",
Expand Down Expand Up @@ -408,6 +407,8 @@ da_scala_test(
resource_strip_prefix = "language-support/java/codegen/src/ledger-tests/resources/",
resources = ["src/ledger-tests/resources/logback-test.xml"],
scala_deps = [
"@maven//:com_typesafe_akka_akka_actor",
"@maven//:com_typesafe_akka_akka_stream",
"@maven//:org_scalatest_scalatest_core",
"@maven//:org_scalatest_scalatest_flatspec",
"@maven//:org_scalatest_scalatest_matchers_core",
Expand All @@ -423,16 +424,21 @@ da_scala_test(
"//bazel_tools/runfiles:scala_runfiles",
"//daml-lf/data",
"//language-support/java/bindings:bindings-java",
"//language-support/scala/bindings-akka",
"//ledger-api/rs-grpc-bridge",
"//ledger-api/testing-utils",
"//ledger/caching",
"//ledger/ledger-api-auth",
"//ledger/ledger-api-common",
"//ledger/ledger-api-domain",
"//ledger/ledger-configuration",
"//ledger/ledger-resources",
"//ledger/ledger-resources:ledger-resources-test-lib",
"//ledger/participant-integration-api",
"//ledger/participant-integration-api:participant-integration-api-tests-lib",
"//ledger/sandbox-classic",
"//ledger/sandbox",
"//ledger/sandbox:sandbox-scala-tests-lib",
"//ledger/sandbox-common",
"//ledger/sandbox-common:sandbox-common-scala-tests-lib",
"//libs-scala/ports",
"//libs-scala/resources",
"@maven//:ch_qos_logback_logback_classic",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,183 +8,208 @@ import java.time.temporal.ChronoField
import java.time.{Instant, LocalDate, ZoneOffset}

import com.daml.ledger.javaapi.data.{Unit => DamlUnit}
import com.daml.ledger.resources.TestResourceContext
import com.daml.ledger.resources.{ResourceContext, TestResourceContext}
import com.daml.lf.data.Numeric
import org.scalatest.flatspec.AsyncFlatSpec
import org.scalatest.matchers.should.Matchers
import wolpertinger.color.Grey
import wolpertinger.{Color, Wolpertinger}
import alltests.MultiParty
import com.daml.ledger.api.testing.utils.SuiteResourceManagementAroundAll
import io.grpc.Channel
import org.scalatest.Assertion

import scala.jdk.CollectionConverters._
import scala.concurrent.Future

import java.util.Arrays.asList

class CodegenLedgerTest extends AsyncFlatSpec with Matchers with TestResourceContext {
class CodegenLedgerTest
extends AsyncFlatSpec
with SandboxFixture
with Matchers
with TestResourceContext
with SuiteResourceManagementAroundAll {

import TestUtil._

val glookofly = new Wolpertinger(
Alice,
3L,
new BigDecimal("17.4200000000"),
"Glookofly",
true,
LocalDate.of(1583, 12, 8),
LocalDate.of(1583, 12, 8).atStartOfDay().toInstant(ZoneOffset.UTC),
List[Wolpertinger.ContractId]().asJava,
List[Color](new Grey(DamlUnit.getInstance())).asJava,
)

val sruquito = new Wolpertinger(
Alice,
1L,
new BigDecimal("8.2000000000"),
"Sruquito",
true,
LocalDate.of(1303, 3, 19),
LocalDate.of(1303, 3, 19).atStartOfDay().toInstant(ZoneOffset.UTC),
List[Wolpertinger.ContractId]().asJava,
List[Color](new Grey(DamlUnit.getInstance())).asJava,
)
def withUniqueParty(
testCode: (String, Wolpertinger, Wolpertinger, Channel) => Assertion
)(implicit resourceContext: ResourceContext): Future[Assertion] = {
val alice = getUniqueParty(Alice)
val glookofly = new Wolpertinger(
alice,
3L,
new BigDecimal("17.4200000000"),
"Glookofly",
true,
LocalDate.of(1583, 12, 8),
LocalDate.of(1583, 12, 8).atStartOfDay().toInstant(ZoneOffset.UTC),
List[Wolpertinger.ContractId]().asJava,
List[Color](new Grey(DamlUnit.getInstance())).asJava,
)
val sruquito = new Wolpertinger(
alice,
1L,
new BigDecimal("8.2000000000"),
"Sruquito",
true,
LocalDate.of(1303, 3, 19),
LocalDate.of(1303, 3, 19).atStartOfDay().toInstant(ZoneOffset.UTC),
List[Wolpertinger.ContractId]().asJava,
List[Color](new Grey(DamlUnit.getInstance())).asJava,
)
val fn = testCode(alice, glookofly, sruquito, _)
withClient(fn)
}

behavior of "Generated Java code"

it should "create correct create commands" in withClient { client =>
sendCmd(client, glookofly.create())
it should "create correct create commands" in withUniqueParty { (alice, glookofly, _, client) =>
sendCmd(client, alice, glookofly.create())

val glookoflyContract :: Nil =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)

glookoflyContract.data shouldEqual glookofly
}

it should "create correct exercise choice commands" in withClient { client =>
sendCmd(client, Alice, glookofly.create(), sruquito.create())
it should "create correct exercise choice commands" in withUniqueParty {
(alice, glookofly, sruquito, client) =>
import java.util.Arrays.asList
sendCmd(client, asList(alice), asList[String](), glookofly.create(), sruquito.create())

val glookoflyContract :: sruquitoContract :: Nil =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
val glookoflyContract :: sruquitoContract :: Nil =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)

glookoflyContract.data shouldEqual glookofly
sruquitoContract.data shouldEqual sruquito
glookoflyContract.data shouldEqual glookofly
sruquitoContract.data shouldEqual sruquito

val tob = Instant.now().`with`(ChronoField.NANO_OF_SECOND, 0)
val reproduceCmd = glookoflyContract.id
.exerciseReproduce(sruquitoContract.id, tob)
sendCmd(client, reproduceCmd)
val tob = Instant.now().`with`(ChronoField.NANO_OF_SECOND, 0)
val reproduceCmd = glookoflyContract.id
.exerciseReproduce(sruquitoContract.id, tob)
sendCmd(client, alice, reproduceCmd)

val wolpertingers = readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
wolpertingers should have length 2
val wolpertingers = readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)
wolpertingers should have length 2

println(wolpertingers)
println(wolpertingers)

val sruq :: glookosruq :: Nil = wolpertingers
val sruq :: glookosruq :: Nil = wolpertingers

sruq.data.name shouldEqual sruquito.name
glookosruq.data.name shouldEqual s"${glookofly.name}-${sruquito.name}"
glookosruq.data.timeOfBirth shouldEqual tob
sruq.data.name shouldEqual sruquito.name
glookosruq.data.name shouldEqual s"${glookofly.name}-${sruquito.name}"
glookosruq.data.timeOfBirth shouldEqual tob
}

it should "create correct createAndExercise choice commands" in withClient { client =>
sendCmd(client, glookofly.create())
it should "create correct createAndExercise choice commands" in withUniqueParty {
(alice, glookofly, sruquito, client) =>
sendCmd(client, alice, glookofly.create())

val glookoflyContract :: Nil =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
val glookoflyContract :: Nil =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)

glookoflyContract.data shouldEqual glookofly
glookoflyContract.data shouldEqual glookofly

val tob = Instant.now().`with`(ChronoField.NANO_OF_SECOND, 0)
val reproduceCmd = sruquito.createAndExerciseReproduce(glookoflyContract.id, tob)
sendCmd(client, reproduceCmd)
val tob = Instant.now().`with`(ChronoField.NANO_OF_SECOND, 0)
val reproduceCmd = sruquito.createAndExerciseReproduce(glookoflyContract.id, tob)
sendCmd(client, alice, reproduceCmd)

val wolpertingers = readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
wolpertingers should have length 2
val wolpertingers = readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)
wolpertingers should have length 2

val glook :: glookosruq :: Nil = wolpertingers
val glook :: glookosruq :: Nil = wolpertingers

glook.data.name shouldEqual glookofly.name
glookosruq.data.name shouldEqual s"${sruquito.name}-${glookofly.name}"
glookosruq.data.timeOfBirth shouldEqual tob
glook.data.name shouldEqual glookofly.name
glookosruq.data.name shouldEqual s"${sruquito.name}-${glookofly.name}"
glookosruq.data.timeOfBirth shouldEqual tob
}

it should "provide the agreement text" in withClient { client =>
sendCmd(client, glookofly.create())
it should "provide the agreement text" in withUniqueParty { (alice, glookofly, _, client) =>
sendCmd(client, alice, glookofly.create())

val wolpertinger :: _ = readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
val wolpertinger :: _ =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)

wolpertinger.agreementText.isPresent shouldBe true
wolpertinger.agreementText.get shouldBe s"${wolpertinger.data.name} has ${wolpertinger.data.wings} wings and is ${Numeric
.toUnscaledString(Numeric.assertFromUnscaledBigDecimal(wolpertinger.data.age))} years old."
}

it should "provide the key" in withClient { client =>
sendCmd(client, glookofly.create())
it should "provide the key" in withUniqueParty { (alice, glookofly, _, client) =>
sendCmd(client, alice, glookofly.create())

val wolpertinger :: _ = readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
val wolpertinger :: _ =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)

wolpertinger.key.isPresent shouldBe true
wolpertinger.key.get.owner shouldEqual "Alice"
wolpertinger.key.get.owner shouldEqual alice
wolpertinger.key.get.age shouldEqual new BigDecimal("17.4200000000")
}

it should "be able to exercise by key" in withClient { client =>
sendCmd(client, glookofly.create(), sruquito.create())
it should "be able to exercise by key" in withUniqueParty {
(alice, glookofly, sruquito, client) =>
sendCmd(client, alice, glookofly.create(), sruquito.create())

// We'll exercise by key, no need to get the handles
val glookoflyContract :: sruquitoContract :: Nil =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
// We'll exercise by key, no need to get the handles
val glookoflyContract :: sruquitoContract :: Nil =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)

val tob = Instant.now().`with`(ChronoField.NANO_OF_SECOND, 0)
val reproduceByKeyCmd =
Wolpertinger.exerciseByKeyReproduce(glookoflyContract.key.get, sruquitoContract.id, tob)
sendCmd(client, reproduceByKeyCmd)
val tob = Instant.now().`with`(ChronoField.NANO_OF_SECOND, 0)
val reproduceByKeyCmd =
Wolpertinger.exerciseByKeyReproduce(glookoflyContract.key.get, sruquitoContract.id, tob)
sendCmd(client, alice, reproduceByKeyCmd)

val wolpertingers = readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
wolpertingers should have length 2
val wolpertingers = readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)
wolpertingers should have length 2

val sruq :: glookosruq :: Nil = wolpertingers
val sruq :: glookosruq :: Nil = wolpertingers

sruq.data.name shouldEqual sruquito.name
glookosruq.data.name shouldEqual s"${glookofly.name}-${sruquito.name}"
glookosruq.data.timeOfBirth shouldEqual tob
sruq.data.name shouldEqual sruquito.name
glookosruq.data.name shouldEqual s"${glookofly.name}-${sruquito.name}"
glookosruq.data.timeOfBirth shouldEqual tob
}

it should "provide the correct signatories" in withClient { client =>
sendCmd(client, glookofly.create())
it should "provide the correct signatories" in withUniqueParty { (alice, glookofly, _, client) =>
sendCmd(client, alice, glookofly.create())

val wolpertinger :: _ = readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
val wolpertinger :: _ =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)

// as stated explicitly in src/ledger-tests/daml/Wolpertinger.daml
wolpertinger.signatories should contain only glookofly.owner
}

it should "provide the correct observers" in withClient { client =>
sendCmd(client, glookofly.create())
it should "provide the correct observers" in withUniqueParty { (alice, glookofly, _, client) =>
sendCmd(client, alice, glookofly.create())

val wolpertinger :: _ = readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client)
val wolpertinger :: _ =
readActiveContracts(Wolpertinger.Contract.fromCreatedEvent)(client, alice)

// no explicit observers and the only choice controller is a signatory
wolpertinger.observers shouldBe empty
}

it should "be able to create multi-party templates" in withClient { client =>
val multi = new MultiParty(Alice, Bob)
sendCmd(client, asList(Alice, Bob), asList[String](), multi.create());
val List(alice, bob) = List(Alice, Bob).map(getUniqueParty)
val multi = new MultiParty(alice, bob)
sendCmd(client, asList(alice, bob), asList[String](), multi.create());

val read = readActiveContracts(MultiParty.Contract.fromCreatedEvent)(client).head
val read = readActiveContracts(MultiParty.Contract.fromCreatedEvent)(client, alice).head

read.data.p1 shouldBe Alice
read.data.p2 shouldBe Bob
read.data.p1 shouldBe alice
read.data.p2 shouldBe bob
}

it should "be able to read as other parties" in withClient { client =>
sendCmd(client, asList(Charlie, Bob), asList[String](), new MultiParty(Charlie, Bob).create())
sendCmd(client, asList(Alice, Bob), asList[String](), new MultiParty(Alice, Bob).create())
val List(alice, bob, charlie) = List(Alice, Bob, Charlie).map(getUniqueParty)
sendCmd(client, asList(charlie, bob), asList[String](), new MultiParty(charlie, bob).create())
sendCmd(client, asList(alice, bob), asList[String](), new MultiParty(alice, bob).create())
sendCmd(
client,
asList(Alice),
asList(Charlie),
MultiParty.exerciseByKeyMPFetchOtherByKey(new da.types.Tuple2(Alice, Bob), Charlie, Bob),
asList(alice),
asList(charlie),
MultiParty.exerciseByKeyMPFetchOtherByKey(new da.types.Tuple2(alice, bob), charlie, bob),
)

succeed
Expand Down
Loading