private void testProcessCommandWithValidInput( Command command, String[] parameters, String expected) throws Exception { Client client = new Client(new String[] {"-runmode", "gendatamodel"}); File tempFile = _folder.newFile("writer"); FileWriter writer = new FileWriter(tempFile); String[] args = ArrayUtils.addAll(new String[] {command.commandName()}, parameters); client._logger = new BatfishLogger("output", false); assertTrue(client.processCommand(args, writer)); assertThat(client.getLogger().getHistory().toString(500), equalTo(expected)); writer.close(); }
private synchronized void write(int level, String msg) { if (isActive(level)) { String outputMsg; if (_timestamp) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = df.format(new Date()); outputMsg = String.format("%s: %s", dateStr, msg); } else { outputMsg = msg; } if (_ps != null) { _ps.print(outputMsg); // logic for rotating log if (_rotateLog) { _numLinesSinceRotation++; if (_numLinesSinceRotation > LOG_ROTATION_THRESHOLD) { rotateLog(); _numLinesSinceRotation = 0; } } } else { _history.add(new HistoryItem(level, msg)); } } } }
@Test public void testLoadQuestionsNames() throws Exception { Client client = new Client(new String[] {"-runmode", "gendatamodel", "-prettyanswers", "false"}); JSONObject testQuestion = new JSONObject(); testQuestion.put( "instance", new JSONObject() .put("instanceName", "testQuestionName") .put("description", "test question description")); Path questionJsonPath = _folder.newFile("testquestion.json").toPath(); CommonUtil.writeFile(questionJsonPath, testQuestion.toString()); client._logger = new BatfishLogger("output", false); client.processCommand( new String[] {LOAD_QUESTIONS.commandName(), questionJsonPath.getParent().toString()}, null); // Reading the answer written by load-questions Answer answerLoadQuestions = _mapper.readValue( client.getLogger().getHistory().toString(BatfishLogger.LEVEL_OUTPUT), Answer.class); LoadQuestionAnswerElement ae = (LoadQuestionAnswerElement) answerLoadQuestions.getAnswerElements().get(0); // Checking that question name in answer element matches instanceName in file assertEquals("testQuestionName", ae.getAdded().first()); }
< JobResultT extends BatfishJobResult<OutputT, AnswerElementT>, AnswerElementT extends AnswerElement, OutputT> String getFailureMessage(JobResultT result) { String time = CommonUtil.getTime(result.getElapsedTime()); String failureMessage = String.format( "Failure running job after elapsed time: %s\n-----" + "BEGIN JOB LOG-----\n%s\n-----END JOB LOG-----", time, result.getHistory().toString(BatfishLogger.getLogLevel(_settings.getLogLevel()))); return failureMessage; }
private void checkProcessCommandErrorMessage( Command command, String[] parameters, String expected) throws Exception { Client client = new Client(new String[] {"-runmode", "gendatamodel"}); File tempFile = _folder.newFile("writer"); FileWriter writer = new FileWriter(tempFile); client._logger = new BatfishLogger("output", false); String[] args = ArrayUtils.addAll(new String[] {command.commandName()}, parameters); assertFalse(client.processCommand(args, writer)); assertThat(client.getLogger().getHistory().toString(500), equalTo(expected)); writer.close(); }
public BatfishLogger(String logLevel, boolean timestamp) { _timestamp = timestamp; setLogLevel(logLevel); _history = new BatfishLoggerHistory(); }
@Test public void testDefaultCase() throws Exception { Client client = new Client(new String[] {"-runmode", "gendatamodel"}); File tempFile = _folder.newFile("writer"); FileWriter writer = new FileWriter(tempFile); client._logger = new BatfishLogger("output", false); String[] args = new String[] {"non-exist command"}; String expected = "Command failed: Not a valid command: \"non-exist command\"\n"; assertFalse(client.processCommand(args, writer)); assertThat(client.getLogger().getHistory().toString(500), equalTo(expected)); }