From a763bf2cf39d6a3946d1caacd3c31ac60058fcce Mon Sep 17 00:00:00 2001 From: Juraj Piar Date: Wed, 27 Mar 2024 15:30:54 +0000 Subject: [PATCH] test(genesis): adds file loader tests --- .../core/genesis/GenesisLoaderImpl.java | 17 ++--- .../core/genesis/GenesisLoaderImplTest.java | 74 +++++++++++++++++++ 2 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 rskj-core/src/test/java/org/ethereum/core/genesis/GenesisLoaderImplTest.java diff --git a/rskj-core/src/main/java/org/ethereum/core/genesis/GenesisLoaderImpl.java b/rskj-core/src/main/java/org/ethereum/core/genesis/GenesisLoaderImpl.java index 0c2bc0354bc..75ded31f74c 100644 --- a/rskj-core/src/main/java/org/ethereum/core/genesis/GenesisLoaderImpl.java +++ b/rskj-core/src/main/java/org/ethereum/core/genesis/GenesisLoaderImpl.java @@ -41,13 +41,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; @@ -237,21 +235,16 @@ private void setupPrecompiledContractsStorage(Repository repository) { public static InputStream loadGenesisFile(String fileName) { InputStream inputStream = GenesisLoaderImpl.class.getResourceAsStream("/genesis/" + fileName); - if (inputStream != null) { return inputStream; } - Path filePath = Paths.get(fileName); - if (Files.exists(filePath)) { - try { - return new FileInputStream(filePath.toFile()); - } catch (FileNotFoundException e) { - logger.error(e.getLocalizedMessage()); - } + try { + return Files.newInputStream(Paths.get(fileName)); + } catch (IOException e) { + logger.error("Could not create stream for genesis file: " + fileName + ". IOException: " + e); + return null; } - - return null; } public static void loadGenesisInitalState(Repository repository, Genesis genesis) { diff --git a/rskj-core/src/test/java/org/ethereum/core/genesis/GenesisLoaderImplTest.java b/rskj-core/src/test/java/org/ethereum/core/genesis/GenesisLoaderImplTest.java new file mode 100644 index 00000000000..932dba80ef7 --- /dev/null +++ b/rskj-core/src/test/java/org/ethereum/core/genesis/GenesisLoaderImplTest.java @@ -0,0 +1,74 @@ +package org.ethereum.core.genesis; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; +import java.util.Objects; + +class GenesisLoaderImplTest { + private final String GENESIS_FILE_NAME = "temp_genesis.json"; + + private final String RESOURCES_GENESIS_FILE_PATH = Objects.requireNonNull(GenesisLoaderImpl.class.getResource("/genesis")).getPath() + "/" + GENESIS_FILE_NAME; + + @SuppressWarnings("ResultOfMethodCallIgnored") + private boolean isStreamReadable(InputStream stream) { + try { + stream.read(); + return true; + } catch (IOException e) { + return false; + } + } + + @BeforeEach + @SuppressWarnings("ResultOfMethodCallIgnored") + public void createTempFiles() throws IOException { + (new File(RESOURCES_GENESIS_FILE_PATH)).createNewFile(); + } + + @AfterEach + @SuppressWarnings("ResultOfMethodCallIgnored") + public void cleanUpFiles() { + (new File(RESOURCES_GENESIS_FILE_PATH)).delete(); + } + + @Test + void loadGenesisFile_fromResourcesDir() { + InputStream genesisFileStream = GenesisLoaderImpl.loadGenesisFile(GENESIS_FILE_NAME); + assert genesisFileStream != null; + + Assertions.assertTrue(isStreamReadable(genesisFileStream)); + } + + @Test + void loadGenesisFile_missingFile_inResourcesDir() { + File genesisFile = new File("non-existent-file.json"); + + Assertions.assertNull(GenesisLoaderImpl.loadGenesisFile(genesisFile.getPath())); + } + + @Test + void loadGenesisFile_fromSystem(@TempDir Path tempGenesisDir) throws IOException { + File genesisFile = new File(tempGenesisDir + "/" + GENESIS_FILE_NAME); + Assertions.assertTrue(genesisFile.createNewFile()); + InputStream genesisFileStream = GenesisLoaderImpl.loadGenesisFile(genesisFile.getPath()); + assert genesisFileStream != null; + + Assertions.assertTrue(isStreamReadable(genesisFileStream)); + } + + @Test + void loadGenesisFile_missingFile_inSystem(@TempDir Path tempGenesisDir) { + File genesisFile = new File(tempGenesisDir + "/non-existent-file.json"); + + Assertions.assertNull(GenesisLoaderImpl.loadGenesisFile(genesisFile.getPath())); + } + +}