CommandLinesInterpretor { public static ResponseObject interpretCommandLines(String[] args){ logger.traceEntry("params: {}", args); if (args == null){ return logger.traceExit(new ResponseObject(true, "null arguments!", null)); } if (args.length == 0){ return logger.traceExit(new ResponseObject(true, "empty arguments", null)); } boolean isHelp = false; String configFileName = null; String generateFor = ""; for (int i = 0; i < args.length; i++){ isHelp = isHelp || isHelpArgument(args[i]); if (isConfig(args[i])){ configFileName = args[i].substring(args[i].indexOf("=") + 1); } if(isGenerate(args[i])){ generateFor = args[i].substring(args[i].indexOf("=") + 1); } } if (isHelp){ displayHelp(); return logger.traceExit(new ResponseObject(false, "displayed help", null)); } if (configFileName == null){ return logger.traceExit(new ResponseObject(true, "no file specified", null)); } else { Map<String, Object> properties = parseFileProperties(configFileName); if (properties == null) { System.out.println("Error parsing file! Can not start!"); return logger.traceExit(new ResponseObject(false, "config file parse error", null)); } if(generateFor == null || generateFor.isEmpty()){ return logger.traceExit(new ResponseObject(true, "autostart", properties)); } else{ String[] generateSplit = generateFor.split(";"); if(generateSplit.length !=3){ System.out.println("Error generating! Can not start!"); return logger.traceExit(new ResponseObject(false, "Error generating!", null)); } Integer nrShards = Integer.parseInt(generateSplit[0]); Integer nodesPerShard = Integer.parseInt(generateSplit[1]); Boolean shouldStartAutomatically = Boolean.parseBoolean(generateSplit[2]); Map<Integer, List<String>> shardPrivateKeys = new HashMap<>(); GeneratePrivateKeysPerShard(nrShards, nodesPerShard, shardPrivateKeys); List<String> configs = GenerateConfigFiles(shardPrivateKeys); String fileName = GenerateCommandFile(configs); if(shouldStartAutomatically) { try { Runtime.getRuntime().exec("cmd /c start " + fileName); } catch (IOException e) { e.printStackTrace(); } } return logger.traceExit(new ResponseObject(false, "generate", properties)); } } }  static ResponseObject interpretCommandLines(String[] args); static void GeneratePrivateKeysPerShard(Integer nrShards, Integer nodesPerShard, Map<Integer, List<String>> shardPrivateKeys);  }
@Test public void testInterpretWithEmpty(){ ResponseObject result = CommandLinesInterpretor.interpretCommandLines(new String[0]); TestCase.assertTrue(result.isSuccess()); TestCase.assertNull(result.getPayload()); }
@Test public void testInterpretWithHelp(){ ResponseObject result = CommandLinesInterpretor.interpretCommandLines(new String[]{"--h"}); TestCase.assertFalse(result.isSuccess()); TestCase.assertNull(result.getPayload()); }
@Test public void testInterpretWithConfigNOK(){ ResponseObject result = CommandLinesInterpretor.interpretCommandLines(new String[]{"--config=aaa"}); TestCase.assertFalse(result.isSuccess()); TestCase.assertNull(result.getPayload()); }
@Test public void testInterpretWithConfigOK() throws IOException { FileWriter fileWriter = new FileWriter("test.config", false); fileWriter.append("node_name=elrond-node-1\n" + "port=4001\n" + "master_peer_port=4000\n" + "peer_ip=127.0.0.1\n" + "node_private_key=00e073884464d8d887568d6e4e66344db01334436c817bce17653eaf3e428b6ef5\n" + "startup_type=START_FROM_SCRATCH\n" + "blockchain_path=elrond-node-1\n" + "blockchain_restore_path=elrond-node-1"); fileWriter.close(); ResponseObject result = CommandLinesInterpretor.interpretCommandLines(new String[]{"--config=test.config"}); ThreadUtil.sleep(1000); FileUtils.forceDelete(new File("test.config")); TestCase.assertTrue(result.isSuccess()); TestCase.assertNotNull(result.getPayload()); @SuppressWarnings("unchecked") Map<String, Object> data = (Map<String, Object>)result.getPayload(); for (String key : data.keySet()){ System.out.println(key + ": " + data.get(key).toString()); } }
@Test public void testInterpretWithConfigOK2() throws IOException { FileWriter fileWriter = new FileWriter("test.config", false); fileWriter.append("node_name=AUTO\n" + "port=4001\n" + "master_peer_port=4000\n" + "peer_ip=127.0.0.1\n" + "node_private_key=AUTO\n" + "startup_type=START_FROM_SCRATCH\n" + "blockchain_path=AUTO\n" + "blockchain_restore_path=AUTO"); fileWriter.close(); ResponseObject result = CommandLinesInterpretor.interpretCommandLines(new String[]{"--config=test.config"}); ThreadUtil.sleep(1000); FileUtils.forceDelete(new File("test.config")); TestCase.assertTrue(result.isSuccess()); TestCase.assertNotNull(result.getPayload()); @SuppressWarnings("unchecked") Map<String, Object> data = (Map<String, Object>)result.getPayload(); for (String key : data.keySet()){ System.out.println(key + ": " + data.get(key).toString()); } }
@Test public void testInterpretWithNull(){ ResponseObject result = CommandLinesInterpretor.interpretCommandLines(null); TestCase.assertTrue(result.isSuccess()); TestCase.assertNull(result.getPayload()); }