Skip to content

Commit 17b2a4c

Browse files
committed
tests for functionality of SmartContractClientImpl.createContract
Signed-off-by: Atwijukire Ariho Seth <[email protected]>
1 parent 7fcbe2f commit 17b2a4c

File tree

6 files changed

+37
-58
lines changed

6 files changed

+37
-58
lines changed

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/IFileReader.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/SmartContractClientImpl.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.hedera.hashgraph.sdk.FileId;
66
import com.openelements.hiero.base.FileClient;
77
import com.openelements.hiero.base.HieroException;
8-
import com.openelements.hiero.base.IFileReader;
98
import com.openelements.hiero.base.SmartContractClient;
109
import com.openelements.hiero.base.data.ContractCallResult;
1110
import com.openelements.hiero.base.data.ContractParam;
@@ -29,12 +28,10 @@ public class SmartContractClientImpl implements SmartContractClient {
2928
private final ProtocolLayerClient protocolLayerClient;
3029

3130
private final FileClient fileClient;
32-
private final IFileReader fileReader;
3331

34-
public SmartContractClientImpl(@NonNull final ProtocolLayerClient protocolLayerClient, FileClient fileClient, IFileReader fileReader) {
32+
public SmartContractClientImpl(@NonNull final ProtocolLayerClient protocolLayerClient, FileClient fileClient) {
3533
this.protocolLayerClient = Objects.requireNonNull(protocolLayerClient, "protocolLevelClient must not be null");
3634
this.fileClient = Objects.requireNonNull(fileClient, "fileClient must not be null");
37-
this.fileReader = Objects.requireNonNull(fileReader, "fileReader must not be null");
3835
}
3936

4037
@NonNull
@@ -77,7 +74,7 @@ public ContractId createContract(@NonNull final Path pathToBin,
7774
@Nullable final ContractParam<?>... constructorParams)
7875
throws HieroException {
7976
try {
80-
final byte[] bytes = fileReader.readAllBytes(pathToBin);
77+
final byte[] bytes = Files.readAllBytes(pathToBin);
8178
return createContract(bytes, constructorParams);
8279
} catch (Exception e) {
8380
throw new HieroException("Failed to create contract from path " + pathToBin, e);

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/SystemFileReader.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/SmartContractClientImplTest.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
import com.hedera.hashgraph.sdk.FileId;
55
import com.openelements.hiero.base.FileClient;
66
import com.openelements.hiero.base.HieroException;
7-
import com.openelements.hiero.base.IFileReader;
87
import com.openelements.hiero.base.data.ContractParam;
98
import com.openelements.hiero.base.implementation.SmartContractClientImpl;
109
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
1110
import com.openelements.hiero.base.protocol.data.ContractCreateRequest;
1211
import com.openelements.hiero.base.protocol.data.ContractCreateResult;
12+
import org.junit.jupiter.api.AfterEach;
1313
import org.junit.jupiter.api.BeforeEach;
1414
import org.junit.jupiter.api.Test;
1515
import org.mockito.InjectMocks;
1616
import org.mockito.Mock;
1717
import org.mockito.Mockito;
1818

1919
import java.io.IOException;
20+
import java.nio.file.Files;
2021
import java.nio.file.Path;
2122

2223
import static org.junit.jupiter.api.Assertions.*;
@@ -32,9 +33,6 @@ public class SmartContractClientImplTest {
3233
@Mock
3334
private ProtocolLayerClient protocolLayerClient;
3435

35-
@Mock
36-
private IFileReader fileReader;
37-
3836
@InjectMocks
3937
private SmartContractClientImpl smartContractClient;
4038

@@ -48,7 +46,7 @@ public class SmartContractClientImplTest {
4846

4947
private ContractCreateResult resultMock;
5048

51-
private Path path;
49+
private Path tempPath;
5250

5351
private byte[] contents;
5452

@@ -57,16 +55,20 @@ public class SmartContractClientImplTest {
5755
public void init() throws IOException {
5856
fileClient = Mockito.mock(FileClient.class);
5957
protocolLayerClient = Mockito.mock(ProtocolLayerClient.class);
60-
fileReader= Mockito.mock(IFileReader.class);
61-
smartContractClient = Mockito.spy(new SmartContractClientImpl(protocolLayerClient, fileClient, fileReader));
58+
smartContractClient = Mockito.spy(new SmartContractClientImpl(protocolLayerClient, fileClient));
6259
contractId = ContractId.fromString("0.0.123");
6360
fileId = FileId.fromString("0.0.123");
6461
constructorParams = ContractParam.string("paramValue");
6562
resultMock = Mockito.mock(ContractCreateResult.class);
66-
path= Mockito.mock(Path.class);
63+
tempPath= Files.createTempFile("testContract", ".bin");
6764
contents = "contractBytecode".getBytes();
65+
Files.write(tempPath, contents);
6866

67+
}
6968

69+
@AfterEach
70+
public void doCleanUp() throws IOException{
71+
Files.deleteIfExists(tempPath);
7072
}
7173

7274
@Test
@@ -104,7 +106,7 @@ public void testCreateContract_UsingFileId_ThrowsException() throws HieroExcepti
104106
}
105107

106108
@Test
107-
public void testCreateContract_UsingContent_WithContractParams() throws HieroException {
109+
public void testCreateContract_UsingContents_WithContractParams() throws HieroException {
108110

109111
when(fileClient.createFile(contents)).thenReturn(fileId);
110112
when(protocolLayerClient.executeContractCreateTransaction(any(ContractCreateRequest.class))).thenReturn(resultMock);
@@ -113,14 +115,15 @@ public void testCreateContract_UsingContent_WithContractParams() throws HieroExc
113115
returnedContractId = smartContractClient.createContract(contents, constructorParams);
114116

115117
assertEquals(contractId, returnedContractId);
118+
116119
verify(fileClient).createFile(contents);
117120
verify(protocolLayerClient).executeContractCreateTransaction(any());
118121
verify(fileClient).deleteFile(fileId);
119122

120123
}
121124

122125
@Test
123-
public void testCreateContract_UsingContent_WithOutContractParams() throws HieroException {
126+
public void testCreateContract_UsingContents_WithOutContractParams() throws HieroException {
124127

125128
when(fileClient.createFile(contents)).thenReturn(fileId);
126129
when(protocolLayerClient.executeContractCreateTransaction(any(ContractCreateRequest.class))).thenReturn(resultMock);
@@ -129,13 +132,14 @@ public void testCreateContract_UsingContent_WithOutContractParams() throws Hiero
129132
returnedContractId = smartContractClient.createContract(contents);
130133

131134
assertEquals(contractId, returnedContractId);
135+
132136
verify(fileClient).createFile(contents);
133137
verify(protocolLayerClient).executeContractCreateTransaction(any());
134138
verify(fileClient).deleteFile(fileId);
135139
}
136140

137141
@Test
138-
public void testCreateContract_UsingContent_ThrowsException() throws HieroException {
142+
public void testCreateContract_UsingContents_ThrowsException() throws HieroException {
139143
when(protocolLayerClient.executeContractCreateTransaction(Mockito.any())).thenThrow(new RuntimeException("Failed"));
140144
HieroException hieroException = assertThrows(HieroException.class, () -> smartContractClient.createContract(contents, constructorParams));
141145

@@ -146,42 +150,42 @@ public void testCreateContract_UsingContent_ThrowsException() throws HieroExcept
146150
@Test
147151
public void testCreateContract_UsingPath_WithContractParams() throws HieroException, IOException {
148152

149-
when(fileReader.readAllBytes(path)).thenReturn(contents);
150-
doReturn(contractId).when(smartContractClient).createContract(contents, constructorParams);
153+
when(fileClient.createFile(contents)).thenReturn(fileId);
154+
when(protocolLayerClient.executeContractCreateTransaction(any(ContractCreateRequest.class))).thenReturn(resultMock);
155+
when(resultMock.contractId()).thenReturn(contractId);
151156

152-
returnedContractId = smartContractClient.createContract(path, constructorParams);
157+
returnedContractId= smartContractClient.createContract(Files.readAllBytes(tempPath), constructorParams);
153158

154159
assertNotNull(returnedContractId);
155160
assertEquals(contractId, returnedContractId);
156161

157-
verify(fileReader).readAllBytes(path);
158-
verify(smartContractClient).createContract(contents, constructorParams);
159-
162+
verify(fileClient).createFile(contents);
163+
verify(protocolLayerClient).executeContractCreateTransaction(any(ContractCreateRequest.class));
164+
verify(fileClient).deleteFile(fileId);
160165

161166
}
162167

163168
@Test
164169
public void testCreateContract_UsingPath_WithOutContractParams() throws HieroException, IOException {
165-
when(fileReader.readAllBytes(path)).thenReturn(contents);
166-
doReturn(contractId).when(smartContractClient).createContract(contents);
170+
when(fileClient.createFile(contents)).thenReturn(fileId);
171+
when(protocolLayerClient.executeContractCreateTransaction(any(ContractCreateRequest.class))).thenReturn(resultMock);
172+
when(resultMock.contractId()).thenReturn(contractId);
167173

168-
returnedContractId = smartContractClient.createContract(path);
174+
returnedContractId= smartContractClient.createContract(Files.readAllBytes(tempPath));
169175

170176
assertNotNull(returnedContractId);
171177
assertEquals(contractId, returnedContractId);
172178

173-
verify(fileReader).readAllBytes(path);
174-
verify(smartContractClient).createContract(contents);
175-
179+
verify(fileClient).createFile(contents);
180+
verify(protocolLayerClient).executeContractCreateTransaction(any(ContractCreateRequest.class));
181+
verify(fileClient).deleteFile(fileId);
176182
}
177183

178184
@Test
179185
public void testCreateContract_UsingPath_ThrowsException() throws HieroException {
180186
when(protocolLayerClient.executeContractCreateTransaction(Mockito.any())).thenThrow(new RuntimeException("Failed"));
181-
HieroException hieroException = assertThrows(HieroException.class, () -> smartContractClient.createContract(path, constructorParams));
182-
183-
assertTrue(hieroException.getMessage().contains("Failed to create contract from path " + path));
187+
HieroException hieroException = assertThrows(HieroException.class, () -> smartContractClient.createContract(tempPath, constructorParams));
184188

189+
assertTrue(hieroException.getMessage().contains("Failed to create contract from path " + tempPath));
185190
}
186-
187191
}

hiero-enterprise-microprofile/src/main/java/com/openelements/hiero/microprofile/ClientProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ FileClient createFileClient(@NonNull final ProtocolLayerClient protocolLayerClie
7474
@Produces
7575
@ApplicationScoped
7676
SmartContractClient createSmartContractClient(@NonNull final ProtocolLayerClient protocolLayerClient,
77-
@NonNull final FileClient fileClient, @NonNull IFileReader fileReader) {
78-
return new SmartContractClientImpl(protocolLayerClient, fileClient, fileReader);
77+
@NonNull final FileClient fileClient) {
78+
return new SmartContractClientImpl(protocolLayerClient, fileClient);
7979
}
8080

8181
@NonNull

hiero-enterprise-spring/src/main/java/com/openelements/hiero/spring/implementation/HieroAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ FileClient fileClient(final ProtocolLayerClient protocolLayerClient) {
7575
}
7676

7777
@Bean
78-
SmartContractClient smartContractClient(final ProtocolLayerClient protocolLayerClient, FileClient fileClient, IFileReader fileReader) {
79-
return new SmartContractClientImpl(protocolLayerClient, fileClient, fileReader);
78+
SmartContractClient smartContractClient(final ProtocolLayerClient protocolLayerClient, FileClient fileClient) {
79+
return new SmartContractClientImpl(protocolLayerClient, fileClient);
8080
}
8181

8282
@Bean

0 commit comments

Comments
 (0)