|
| 1 | +/* |
| 2 | + * Copyright 2024 IEXEC BLOCKCHAIN TECH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.iexec.commons.poco.itest; |
| 18 | + |
| 19 | +import com.iexec.commons.poco.chain.SignerService; |
| 20 | +import org.apache.commons.lang3.RandomStringUtils; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Tag; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.testcontainers.containers.ComposeContainer; |
| 25 | +import org.testcontainers.junit.jupiter.Container; |
| 26 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 27 | +import org.web3j.crypto.CipherException; |
| 28 | +import org.web3j.crypto.Credentials; |
| 29 | +import org.web3j.crypto.WalletUtils; |
| 30 | +import org.web3j.tx.exceptions.ContractCallException; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.IOException; |
| 34 | +import java.math.BigInteger; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | + |
| 37 | +import static com.iexec.commons.poco.itest.ChainTests.SERVICE_NAME; |
| 38 | +import static com.iexec.commons.poco.itest.ChainTests.SERVICE_PORT; |
| 39 | +import static com.iexec.commons.poco.itest.Web3jTestService.BLOCK_TIME; |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 42 | +import static org.awaitility.Awaitility.await; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 44 | + |
| 45 | +@Tag("itest") |
| 46 | +@Testcontainers |
| 47 | +class AssetRegistriesTests { |
| 48 | + |
| 49 | + private SignerService signerService; |
| 50 | + private IexecHubTestService iexecHubService; |
| 51 | + private Web3jTestService web3jService; |
| 52 | + |
| 53 | + @Container |
| 54 | + static ComposeContainer environment = new ComposeContainer(new File("docker-compose.yml")) |
| 55 | + .withExposedService("poco-chain", 8545); |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + void init() throws CipherException, IOException { |
| 59 | + final Credentials credentials = WalletUtils.loadCredentials("whatever", "src/test/resources/wallet.json"); |
| 60 | + String chainNodeAddress = "http://" + environment.getServiceHost(SERVICE_NAME, SERVICE_PORT) + ":" + |
| 61 | + environment.getServicePort(SERVICE_NAME, SERVICE_PORT); |
| 62 | + web3jService = new Web3jTestService(chainNodeAddress); |
| 63 | + iexecHubService = new IexecHubTestService(credentials, web3jService); |
| 64 | + signerService = new SignerService(web3jService.getWeb3j(), web3jService.getChainId(), credentials); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void shouldCreateAndPredictCallsBeEqualWhenAssetNotDeployed() { |
| 69 | + final String appName = RandomStringUtils.randomAlphanumeric(16); |
| 70 | + final String datasetName = RandomStringUtils.randomAlphanumeric(16); |
| 71 | + final String workerpoolName = RandomStringUtils.randomAlphanumeric(16); |
| 72 | + |
| 73 | + assertAll( |
| 74 | + () -> assertThat(iexecHubService.callCreateApp(appName)) |
| 75 | + .isEqualTo(iexecHubService.callPredictApp(appName)), |
| 76 | + () -> assertThat(iexecHubService.callCreateDataset(datasetName)) |
| 77 | + .isEqualTo(iexecHubService.callPredictDataset(datasetName)), |
| 78 | + () -> assertThat(iexecHubService.callCreateWorkerpool(workerpoolName)) |
| 79 | + .isEqualTo(iexecHubService.callPredictWorkerpool(workerpoolName)) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void shouldCreateCallRevertWhenAssetDeployed() throws IOException { |
| 85 | + final String appName = RandomStringUtils.randomAlphanumeric(16); |
| 86 | + final String datasetName = RandomStringUtils.randomAlphanumeric(16); |
| 87 | + final String workerpoolName = RandomStringUtils.randomAlphanumeric(16); |
| 88 | + BigInteger nonce = signerService.getNonce(); |
| 89 | + final String appTxHash = iexecHubService.submitCreateAppTx(nonce, appName); |
| 90 | + nonce = nonce.add(BigInteger.ONE); |
| 91 | + final String datasetTxHash = iexecHubService.submitCreateDatasetTx(nonce, datasetName); |
| 92 | + nonce = nonce.add(BigInteger.ONE); |
| 93 | + final String workerpoolTxHash = iexecHubService.submitCreateWorkerpoolTx(nonce, workerpoolName); |
| 94 | + |
| 95 | + await().atMost(BLOCK_TIME, TimeUnit.SECONDS) |
| 96 | + .until(() -> web3jService.areTxMined(appTxHash, datasetTxHash, workerpoolTxHash)); |
| 97 | + assertThat(web3jService.areTxStatusOK(appTxHash, datasetTxHash, workerpoolTxHash)).isTrue(); |
| 98 | + |
| 99 | + // fetch asset addresses from call on predict assets |
| 100 | + final String predictedAppAddress = iexecHubService.callPredictApp(appName); |
| 101 | + final String predictedDatasetAddress = iexecHubService.callPredictDataset(datasetName); |
| 102 | + final String predictedWorkerpoolAddress = iexecHubService.callPredictWorkerpool(workerpoolName); |
| 103 | + |
| 104 | + // check assets are deployed |
| 105 | + assertAll( |
| 106 | + () -> assertThat(iexecHubService.isAppPresent(predictedAppAddress)).isTrue(), |
| 107 | + () -> assertThat(iexecHubService.isAppPresent(predictedDatasetAddress)).isFalse(), |
| 108 | + () -> assertThat(iexecHubService.isAppPresent(predictedWorkerpoolAddress)).isFalse(), |
| 109 | + () -> assertThat(iexecHubService.isDatasetPresent(predictedAppAddress)).isFalse(), |
| 110 | + () -> assertThat(iexecHubService.isDatasetPresent(predictedDatasetAddress)).isTrue(), |
| 111 | + () -> assertThat(iexecHubService.isDatasetPresent(predictedWorkerpoolAddress)).isFalse(), |
| 112 | + () -> assertThat(iexecHubService.isWorkerpoolPresent(predictedAppAddress)).isFalse(), |
| 113 | + () -> assertThat(iexecHubService.isWorkerpoolPresent(predictedDatasetAddress)).isFalse(), |
| 114 | + () -> assertThat(iexecHubService.isWorkerpoolPresent(predictedWorkerpoolAddress)).isTrue() |
| 115 | + ); |
| 116 | + |
| 117 | + // call on create assets should revert |
| 118 | + final String errorMessage = "Contract Call has been reverted by the EVM with the reason: 'VM execution error.'."; |
| 119 | + |
| 120 | + assertAll( |
| 121 | + () -> assertThatThrownBy(() -> iexecHubService.callCreateApp(appName), "Should have failed to call createApp") |
| 122 | + .isInstanceOf(ContractCallException.class) |
| 123 | + .hasMessage(errorMessage), |
| 124 | + () -> assertThatThrownBy(() -> iexecHubService.callCreateDataset(datasetName), "Should have failed to call createDataset") |
| 125 | + .isInstanceOf(ContractCallException.class) |
| 126 | + .hasMessage(errorMessage), |
| 127 | + () -> assertThatThrownBy(() -> iexecHubService.callCreateWorkerpool(workerpoolName), "Should have failed to call createWorkerpool") |
| 128 | + .isInstanceOf(ContractCallException.class) |
| 129 | + .hasMessage(errorMessage) |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
0 commit comments