@Test public void writeInputStreamToFile_fileNotExists_expectedFailOnMoveFileToBackup() throws IOException { Path file = Paths.get(DIR_PATH + File.separator + FILE_NAME); Path backup = Paths.get(INVALID_DIR_PATH + File.separator + FILE_NAME + ".org"); Writer logs = getLogs(); PKIUtil.writeInputStreamToFile(IOUtils.toInputStream(FILE_TEXT, "UTF-8"), INVALID_DIR_PATH, FILE_NAME); Assert.assertFalse("File should not exist: " + file, Files.exists(file)); Assert.assertFalse("File should not exist: " + file, Files.exists(backup)); Assert.assertTrue("Logs should contatin message: " + this.failedMessageInputStream, logs.toString().contains(this.failedMessageInputStream)); Assert.assertFalse("Logs should contatin message: " + this.renameMesage, logs.toString().contains(this.renameMesage)); }
public static void writeInputStreamToFile(InputStream is, String parentFolderName, String fileName) { StringBuilder sb = new StringBuilder(parentFolderName) .append(File.separator) .append(fileName); Path file = Paths.get(sb.toString()); Path backup = Paths.get(sb.append(".org").toString()); log.info("Start writing input stream to file: " + file); if(StringUtils.equals(String.valueOf(file.getFileName()),fileName)) { try { if (Files.exists(file)) { log.info("Renaming/backup existing file"); Files.move(file, backup, StandardCopyOption.REPLACE_EXISTING); } try { Files.copy(is, file); log.info("Successfully wrote input stream to file '" + file + "'"); } catch (Exception ex) { log.error("Failed to write input stream to file", ex); if (Files.exists(backup)) { Files.move(backup, file, StandardCopyOption.REPLACE_EXISTING); } } Files.deleteIfExists(backup); } catch (Exception e) { log.error("Failed to write input stream to file" + file, e); } } else { log.warn("Filename: " + fileName + " is not valid"); } }
PKIUtil { public static void writeInputStreamToFile(InputStream is, String parentFolderName, String fileName) { StringBuilder sb = new StringBuilder(parentFolderName) .append(File.separator) .append(fileName); Path file = Paths.get(sb.toString()); Path backup = Paths.get(sb.append(".org").toString()); log.info("Start writing input stream to file: " + file); if(StringUtils.equals(String.valueOf(file.getFileName()),fileName)) { try { if (Files.exists(file)) { log.info("Renaming/backup existing file"); Files.move(file, backup, StandardCopyOption.REPLACE_EXISTING); } try { Files.copy(is, file); log.info("Successfully wrote input stream to file '" + file + "'"); } catch (Exception ex) { log.error("Failed to write input stream to file", ex); if (Files.exists(backup)) { Files.move(backup, file, StandardCopyOption.REPLACE_EXISTING); } } Files.deleteIfExists(backup); } catch (Exception e) { log.error("Failed to write input stream to file" + file, e); } } else { log.warn("Filename: " + fileName + " is not valid"); } } }
PKIUtil { public static void writeInputStreamToFile(InputStream is, String parentFolderName, String fileName) { StringBuilder sb = new StringBuilder(parentFolderName) .append(File.separator) .append(fileName); Path file = Paths.get(sb.toString()); Path backup = Paths.get(sb.append(".org").toString()); log.info("Start writing input stream to file: " + file); if(StringUtils.equals(String.valueOf(file.getFileName()),fileName)) { try { if (Files.exists(file)) { log.info("Renaming/backup existing file"); Files.move(file, backup, StandardCopyOption.REPLACE_EXISTING); } try { Files.copy(is, file); log.info("Successfully wrote input stream to file '" + file + "'"); } catch (Exception ex) { log.error("Failed to write input stream to file", ex); if (Files.exists(backup)) { Files.move(backup, file, StandardCopyOption.REPLACE_EXISTING); } } Files.deleteIfExists(backup); } catch (Exception e) { log.error("Failed to write input stream to file" + file, e); } } else { log.warn("Filename: " + fileName + " is not valid"); } }  }
PKIUtil { public static void writeInputStreamToFile(InputStream is, String parentFolderName, String fileName) { StringBuilder sb = new StringBuilder(parentFolderName) .append(File.separator) .append(fileName); Path file = Paths.get(sb.toString()); Path backup = Paths.get(sb.append(".org").toString()); log.info("Start writing input stream to file: " + file); if(StringUtils.equals(String.valueOf(file.getFileName()),fileName)) { try { if (Files.exists(file)) { log.info("Renaming/backup existing file"); Files.move(file, backup, StandardCopyOption.REPLACE_EXISTING); } try { Files.copy(is, file); log.info("Successfully wrote input stream to file '" + file + "'"); } catch (Exception ex) { log.error("Failed to write input stream to file", ex); if (Files.exists(backup)) { Files.move(backup, file, StandardCopyOption.REPLACE_EXISTING); } } Files.deleteIfExists(backup); } catch (Exception e) { log.error("Failed to write input stream to file" + file, e); } } else { log.warn("Filename: " + fileName + " is not valid"); } }  static byte[] generateMD5Checksum(String fileName); static String toBase64EncodedString(byte[] bytes); static void writeInputStreamToFile(InputStream is, String parentFolderName, String fileName); static void writeBytesToFile(byte[] bytes, String parentFolderName, String fileName); static byte[] readBytesFromFile(File file); static byte[] generateKeyStore(); static boolean verifyKeyPair(byte[] keyStoreBytes, PublicKey pubKey); static PublicKey getPubKey(byte[] keyStoreBytes); static byte[] extractCertificate(byte[] keyStoreBytes); static byte[] extractPrivateKey(byte[] keyStoreBytes); }
PKIUtil { public static void writeInputStreamToFile(InputStream is, String parentFolderName, String fileName) { StringBuilder sb = new StringBuilder(parentFolderName) .append(File.separator) .append(fileName); Path file = Paths.get(sb.toString()); Path backup = Paths.get(sb.append(".org").toString()); log.info("Start writing input stream to file: " + file); if(StringUtils.equals(String.valueOf(file.getFileName()),fileName)) { try { if (Files.exists(file)) { log.info("Renaming/backup existing file"); Files.move(file, backup, StandardCopyOption.REPLACE_EXISTING); } try { Files.copy(is, file); log.info("Successfully wrote input stream to file '" + file + "'"); } catch (Exception ex) { log.error("Failed to write input stream to file", ex); if (Files.exists(backup)) { Files.move(backup, file, StandardCopyOption.REPLACE_EXISTING); } } Files.deleteIfExists(backup); } catch (Exception e) { log.error("Failed to write input stream to file" + file, e); } } else { log.warn("Filename: " + fileName + " is not valid"); } }  static byte[] generateMD5Checksum(String fileName); static String toBase64EncodedString(byte[] bytes); static void writeInputStreamToFile(InputStream is, String parentFolderName, String fileName); static void writeBytesToFile(byte[] bytes, String parentFolderName, String fileName); static byte[] readBytesFromFile(File file); static byte[] generateKeyStore(); static boolean verifyKeyPair(byte[] keyStoreBytes, PublicKey pubKey); static PublicKey getPubKey(byte[] keyStoreBytes); static byte[] extractCertificate(byte[] keyStoreBytes); static byte[] extractPrivateKey(byte[] keyStoreBytes);  }