This repository was archived by the owner on May 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
tests for functionality of SmartContractClientImpl.createContract #187
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 1 commit
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
8 changes: 8 additions & 0 deletions
8
hiero-enterprise-base/src/main/java/com/openelements/hiero/base/IFileReader.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,8 @@ | ||
| package com.openelements.hiero.base; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
|
|
||
| public interface IFileReader { | ||
| byte[] readAllBytes(Path path) throws IOException; | ||
| } | ||
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
14 changes: 14 additions & 0 deletions
14
...prise-base/src/main/java/com/openelements/hiero/base/implementation/SystemFileReader.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,14 @@ | ||
| package com.openelements.hiero.base.implementation; | ||
|
|
||
| import com.openelements.hiero.base.IFileReader; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class SystemFileReader implements IFileReader { | ||
| @Override | ||
| public byte[] readAllBytes(Path path) throws IOException { | ||
| return Files.readAllBytes(path); | ||
| } | ||
| } |
187 changes: 187 additions & 0 deletions
187
...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,187 @@ | ||
| 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.IFileReader; | ||
| 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.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.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; | ||
|
|
||
| @Mock | ||
| private IFileReader fileReader; | ||
|
|
||
| @InjectMocks | ||
| private SmartContractClientImpl smartContractClient; | ||
|
|
||
| private ContractId returnedContractId; | ||
|
|
||
| private ContractId contractId; | ||
|
|
||
| private FileId fileId; | ||
|
|
||
| private ContractParam<?> constructorParams; | ||
|
|
||
| private ContractCreateResult resultMock; | ||
|
|
||
| private Path path; | ||
|
|
||
| private byte[] contents; | ||
|
|
||
|
|
||
| @BeforeEach | ||
| public void init() throws IOException { | ||
| fileClient = Mockito.mock(FileClient.class); | ||
| protocolLayerClient = Mockito.mock(ProtocolLayerClient.class); | ||
| fileReader= Mockito.mock(IFileReader.class); | ||
| smartContractClient = Mockito.spy(new SmartContractClientImpl(protocolLayerClient, fileClient, fileReader)); | ||
| contractId = ContractId.fromString("0.0.123"); | ||
| fileId = FileId.fromString("0.0.123"); | ||
| constructorParams = ContractParam.string("paramValue"); | ||
| resultMock = Mockito.mock(ContractCreateResult.class); | ||
| path= Mockito.mock(Path.class); | ||
| contents = "contractBytecode".getBytes(); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| @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_UsingContent_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_UsingContent_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_UsingContent_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(fileReader.readAllBytes(path)).thenReturn(contents); | ||
| doReturn(contractId).when(smartContractClient).createContract(contents, constructorParams); | ||
|
|
||
| returnedContractId = smartContractClient.createContract(path, constructorParams); | ||
|
|
||
| assertNotNull(returnedContractId); | ||
| assertEquals(contractId, returnedContractId); | ||
|
|
||
| verify(fileReader).readAllBytes(path); | ||
| verify(smartContractClient).createContract(contents, constructorParams); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingPath_WithOutContractParams() throws HieroException, IOException { | ||
| when(fileReader.readAllBytes(path)).thenReturn(contents); | ||
| doReturn(contractId).when(smartContractClient).createContract(contents); | ||
|
|
||
| returnedContractId = smartContractClient.createContract(path); | ||
|
|
||
| assertNotNull(returnedContractId); | ||
| assertEquals(contractId, returnedContractId); | ||
|
|
||
| verify(fileReader).readAllBytes(path); | ||
| verify(smartContractClient).createContract(contents); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContract_UsingPath_ThrowsException() throws HieroException { | ||
| when(protocolLayerClient.executeContractCreateTransaction(Mockito.any())).thenThrow(new RuntimeException("Failed")); | ||
| HieroException hieroException = assertThrows(HieroException.class, () -> smartContractClient.createContract(path, constructorParams)); | ||
|
|
||
| assertTrue(hieroException.getMessage().contains("Failed to create contract from path " + path)); | ||
|
|
||
| } | ||
|
|
||
| } |
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,11 +1,6 @@ | ||
| package com.openelements.hiero.microprofile; | ||
|
|
||
| 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.*; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not use * imports |
||
| import com.openelements.hiero.base.config.HieroConfig; | ||
| import com.openelements.hiero.base.implementation.AccountClientImpl; | ||
| import com.openelements.hiero.base.implementation.AccountRepositoryImpl; | ||
|
|
@@ -79,8 +74,8 @@ FileClient createFileClient(@NonNull final ProtocolLayerClient protocolLayerClie | |
| @Produces | ||
| @ApplicationScoped | ||
| SmartContractClient createSmartContractClient(@NonNull final ProtocolLayerClient protocolLayerClient, | ||
| @NonNull final FileClient fileClient) { | ||
| return new SmartContractClientImpl(protocolLayerClient, fileClient); | ||
| @NonNull final FileClient fileClient, @NonNull IFileReader fileReader) { | ||
| return new SmartContractClientImpl(protocolLayerClient, fileClient, fileReader); | ||
| } | ||
|
|
||
| @NonNull | ||
|
|
||
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.*; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not use * imports |
||
| import com.openelements.hiero.base.config.HieroConfig; | ||
| import com.openelements.hiero.base.implementation.AccountClientImpl; | ||
| import com.openelements.hiero.base.implementation.AccountRepositoryImpl; | ||
|
|
@@ -81,8 +75,8 @@ FileClient fileClient(final ProtocolLayerClient protocolLayerClient) { | |
| } | ||
|
|
||
| @Bean | ||
| SmartContractClient smartContractClient(final ProtocolLayerClient protocolLayerClient, FileClient fileClient) { | ||
| return new SmartContractClientImpl(protocolLayerClient, fileClient); | ||
| SmartContractClient smartContractClient(final ProtocolLayerClient protocolLayerClient, FileClient fileClient, IFileReader fileReader) { | ||
| return new SmartContractClientImpl(protocolLayerClient, fileClient, fileReader); | ||
| } | ||
|
|
||
| @Bean | ||
|
|
||
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.
I disagree with this approach. Instead of mocking the Fiel read in the test you can easily create a test file and use that one in the tests
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.
Thanks for the review, and I've worked on them as requested.