-
Notifications
You must be signed in to change notification settings - Fork 24
tests for functionality of SmartContractClientImpl.createContract #187
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
Open
Ariho-Seth
wants to merge
9
commits into
OpenElements:main
Choose a base branch
from
Ariho-Seth:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3a1a22c
tests for functionality of SmartContractClientImpl.createContract
Ariho-Seth 7fcbe2f
Merge branch 'OpenElements:main' into master
Ariho-Seth 17b2a4c
tests for functionality of SmartContractClientImpl.createContract
Ariho-Seth 9d99ea6
Merge branch 'OpenElements:main' into master
Ariho-Seth ef67c67
Requested changes added
Ariho-Seth 66a9936
Merge remote-tracking branch 'origin/master'
Ariho-Seth 5e08103
Requested changes added
Ariho-Seth 32f81ae
Requested changes added
Ariho-Seth 205aaf0
Merge branch 'OpenElements:main' into master
Ariho-Seth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
191 changes: 191 additions & 0 deletions
191
...rise-base/src/test/java/com/openelements/hiero/base/test/SmartContractClientImplTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| package com.openelements.hiero.base.test; | ||
|
|
||
| import com.hedera.hashgraph.sdk.ContractId; | ||
| import com.hedera.hashgraph.sdk.FileId; | ||
| import com.openelements.hiero.base.FileClient; | ||
| import com.openelements.hiero.base.HieroException; | ||
| import com.openelements.hiero.base.data.ContractParam; | ||
| import com.openelements.hiero.base.implementation.SmartContractClientImpl; | ||
| import com.openelements.hiero.base.protocol.ProtocolLayerClient; | ||
| import com.openelements.hiero.base.protocol.data.ContractCreateRequest; | ||
| import com.openelements.hiero.base.protocol.data.ContractCreateResult; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
|
|
||
| public class SmartContractClientImplTest { | ||
|
|
||
| @Mock | ||
| private FileClient fileClient; | ||
|
|
||
| @Mock | ||
| private ProtocolLayerClient protocolLayerClient; | ||
|
|
||
| @InjectMocks | ||
| private SmartContractClientImpl smartContractClient; | ||
|
|
||
| private ContractId returnedContractId; | ||
|
|
||
| private ContractId contractId; | ||
|
|
||
| private FileId fileId; | ||
|
|
||
| private ContractParam<?> constructorParams; | ||
|
|
||
| private ContractCreateResult resultMock; | ||
|
|
||
| private Path tempPath; | ||
|
|
||
| private byte[] contents; | ||
|
|
||
|
|
||
| @BeforeEach | ||
| public void init() throws IOException { | ||
| fileClient = Mockito.mock(FileClient.class); | ||
| protocolLayerClient = Mockito.mock(ProtocolLayerClient.class); | ||
| smartContractClient = Mockito.spy(new SmartContractClientImpl(protocolLayerClient, fileClient)); | ||
| contractId = ContractId.fromString("0.0.123"); | ||
| fileId = FileId.fromString("0.0.123"); | ||
| constructorParams = ContractParam.string("paramValue"); | ||
| resultMock = Mockito.mock(ContractCreateResult.class); | ||
| tempPath= Files.createTempFile("testContract", ".bin"); | ||
| contents = "contractBytecode".getBytes(); | ||
| Files.write(tempPath, contents); | ||
|
|
||
| } | ||
|
|
||
| @AfterEach | ||
| public void doCleanUp() throws IOException{ | ||
| Files.deleteIfExists(tempPath); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingFileId_WithContractParams() throws HieroException { | ||
|
|
||
| when(resultMock.contractId()).thenReturn(contractId); | ||
| when(protocolLayerClient.executeContractCreateTransaction(any(ContractCreateRequest.class))).thenReturn(resultMock); | ||
|
|
||
| returnedContractId = smartContractClient.createContract(fileId, constructorParams); | ||
|
|
||
| assertEquals(contractId, returnedContractId); | ||
| verify(protocolLayerClient).executeContractCreateTransaction(any()); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingFileId_WithOutContractParams() throws HieroException { | ||
|
|
||
| when(resultMock.contractId()).thenReturn(contractId); | ||
| when(protocolLayerClient.executeContractCreateTransaction(any(ContractCreateRequest.class))).thenReturn(resultMock); | ||
|
|
||
| returnedContractId = smartContractClient.createContract(fileId); | ||
|
|
||
| assertEquals(contractId, returnedContractId); | ||
| verify(protocolLayerClient).executeContractCreateTransaction(any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingFileId_ThrowsException() throws HieroException { | ||
|
|
||
| when(protocolLayerClient.executeContractCreateTransaction(Mockito.any())).thenThrow(new RuntimeException("Failed")); | ||
| HieroException hieroException = assertThrows(HieroException.class, () -> smartContractClient.createContract(fileId, constructorParams)); | ||
|
|
||
| assertTrue(hieroException.getMessage().contains("Failed to create contract with fileId " + fileId)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingContents_WithContractParams() throws HieroException { | ||
|
|
||
| when(fileClient.createFile(contents)).thenReturn(fileId); | ||
| when(protocolLayerClient.executeContractCreateTransaction(any(ContractCreateRequest.class))).thenReturn(resultMock); | ||
| when(resultMock.contractId()).thenReturn(contractId); | ||
|
|
||
| returnedContractId = smartContractClient.createContract(contents, constructorParams); | ||
|
|
||
| assertEquals(contractId, returnedContractId); | ||
|
|
||
| verify(fileClient).createFile(contents); | ||
| verify(protocolLayerClient).executeContractCreateTransaction(any()); | ||
| verify(fileClient).deleteFile(fileId); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingContents_WithOutContractParams() throws HieroException { | ||
|
|
||
| when(fileClient.createFile(contents)).thenReturn(fileId); | ||
| when(protocolLayerClient.executeContractCreateTransaction(any(ContractCreateRequest.class))).thenReturn(resultMock); | ||
| when(resultMock.contractId()).thenReturn(contractId); | ||
|
|
||
| returnedContractId = smartContractClient.createContract(contents); | ||
|
|
||
| assertEquals(contractId, returnedContractId); | ||
|
|
||
| verify(fileClient).createFile(contents); | ||
| verify(protocolLayerClient).executeContractCreateTransaction(any()); | ||
| verify(fileClient).deleteFile(fileId); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingContents_ThrowsException() throws HieroException { | ||
| when(protocolLayerClient.executeContractCreateTransaction(Mockito.any())).thenThrow(new RuntimeException("Failed")); | ||
| HieroException hieroException = assertThrows(HieroException.class, () -> smartContractClient.createContract(contents, constructorParams)); | ||
|
|
||
| assertTrue(hieroException.getMessage().contains("Failed to create contract out of byte array")); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingPath_WithContractParams() throws HieroException, IOException { | ||
|
|
||
| when(fileClient.createFile(contents)).thenReturn(fileId); | ||
| when(protocolLayerClient.executeContractCreateTransaction(any(ContractCreateRequest.class))).thenReturn(resultMock); | ||
| when(resultMock.contractId()).thenReturn(contractId); | ||
|
|
||
| returnedContractId= smartContractClient.createContract(Files.readAllBytes(tempPath), constructorParams); | ||
|
|
||
| assertNotNull(returnedContractId); | ||
| assertEquals(contractId, returnedContractId); | ||
|
|
||
| verify(fileClient).createFile(contents); | ||
| verify(protocolLayerClient).executeContractCreateTransaction(any(ContractCreateRequest.class)); | ||
| verify(fileClient).deleteFile(fileId); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingPath_WithOutContractParams() throws HieroException, IOException { | ||
| when(fileClient.createFile(contents)).thenReturn(fileId); | ||
| when(protocolLayerClient.executeContractCreateTransaction(any(ContractCreateRequest.class))).thenReturn(resultMock); | ||
| when(resultMock.contractId()).thenReturn(contractId); | ||
|
|
||
| returnedContractId= smartContractClient.createContract(Files.readAllBytes(tempPath)); | ||
|
|
||
| assertNotNull(returnedContractId); | ||
| assertEquals(contractId, returnedContractId); | ||
|
|
||
| verify(fileClient).createFile(contents); | ||
| verify(protocolLayerClient).executeContractCreateTransaction(any(ContractCreateRequest.class)); | ||
| verify(fileClient).deleteFile(fileId); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingPath_ThrowsException() throws HieroException { | ||
| when(protocolLayerClient.executeContractCreateTransaction(Mockito.any())).thenThrow(new RuntimeException("Failed")); | ||
| HieroException hieroException = assertThrows(HieroException.class, () -> smartContractClient.createContract(tempPath, constructorParams)); | ||
|
|
||
| assertTrue(hieroException.getMessage().contains("Failed to create contract from path " + tempPath)); | ||
| } | ||
| } |
7 changes: 1 addition & 6 deletions
7
...rprise-microprofile/src/main/java/com/openelements/hiero/microprofile/ClientProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 1 addition & 7 deletions
8
...ng/src/main/java/com/openelements/hiero/spring/implementation/HieroAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,6 @@ | ||
| package com.openelements.hiero.spring.implementation; | ||
|
|
||
| import com.openelements.hiero.base.AccountClient; | ||
| import com.openelements.hiero.base.FileClient; | ||
| import com.openelements.hiero.base.FungibleTokenClient; | ||
| import com.openelements.hiero.base.HieroContext; | ||
| import com.openelements.hiero.base.NftClient; | ||
| import com.openelements.hiero.base.SmartContractClient; | ||
| import com.openelements.hiero.base.TopicClient; | ||
| import com.openelements.hiero.base.*; | ||
|
||
| import com.openelements.hiero.base.config.HieroConfig; | ||
| import com.openelements.hiero.base.implementation.AccountClientImpl; | ||
| import com.openelements.hiero.base.implementation.AccountRepositoryImpl; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use * imports