44import com .hedera .hashgraph .sdk .FileId ;
55import com .openelements .hiero .base .FileClient ;
66import com .openelements .hiero .base .HieroException ;
7- import com .openelements .hiero .base .IFileReader ;
87import com .openelements .hiero .base .data .ContractParam ;
98import com .openelements .hiero .base .implementation .SmartContractClientImpl ;
109import com .openelements .hiero .base .protocol .ProtocolLayerClient ;
1110import com .openelements .hiero .base .protocol .data .ContractCreateRequest ;
1211import com .openelements .hiero .base .protocol .data .ContractCreateResult ;
12+ import org .junit .jupiter .api .AfterEach ;
1313import org .junit .jupiter .api .BeforeEach ;
1414import org .junit .jupiter .api .Test ;
1515import org .mockito .InjectMocks ;
1616import org .mockito .Mock ;
1717import org .mockito .Mockito ;
1818
1919import java .io .IOException ;
20+ import java .nio .file .Files ;
2021import java .nio .file .Path ;
2122
2223import 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}
0 commit comments