use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class ZeppelinRestApiTest method testUpdateParagraph.
@Test
public void testUpdateParagraph() throws IOException {
String noteId = null;
try {
noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testUpdateParagraph", anonymous);
String jsonRequest = "{\"title\": \"title1\", \"text\": \"text1\"}";
CloseableHttpResponse post = httpPost("/notebook/" + noteId + "/paragraph", jsonRequest);
Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
}.getType());
post.close();
String newParagraphId = (String) resp.get("body");
TestUtils.getInstance(Notebook.class).processNote(noteId, noteP -> {
Paragraph newParagraph = noteP.getParagraph(newParagraphId);
assertEquals("title1", newParagraph.getTitle());
assertEquals("text1", newParagraph.getText());
return null;
});
String updateRequest = "{\"text\": \"updated text\"}";
CloseableHttpResponse put = httpPut("/notebook/" + noteId + "/paragraph/" + newParagraphId, updateRequest);
assertThat("Test update method:", put, isAllowed());
put.close();
TestUtils.getInstance(Notebook.class).processNote(noteId, noteP -> {
Paragraph updatedParagraph = noteP.getParagraph(newParagraphId);
assertEquals("title1", updatedParagraph.getTitle());
assertEquals("updated text", updatedParagraph.getText());
return null;
});
String updateBothRequest = "{\"title\": \"updated title\", \"text\" : \"updated text 2\" }";
CloseableHttpResponse updatePut = httpPut("/notebook/" + noteId + "/paragraph/" + newParagraphId, updateBothRequest);
updatePut.close();
TestUtils.getInstance(Notebook.class).processNote(noteId, noteP -> {
Paragraph updatedBothParagraph = noteP.getParagraph(newParagraphId);
assertEquals("updated title", updatedBothParagraph.getTitle());
assertEquals("updated text 2", updatedBothParagraph.getText());
return null;
});
} finally {
// cleanup
if (null != noteId) {
TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
}
}
}
use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class ZeppelinRestApiTest method testJobs.
@Test
public void testJobs() throws Exception {
// create a note and a paragraph
String noteId = null;
try {
System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_CRON_ENABLE.getVarName(), "true");
noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testJobs", anonymous);
// Use write lock, because name is overwritten
TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
note.setName("note for run test");
Paragraph paragraph = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
paragraph.setText("%md This is test paragraph.");
Map<String, Object> config = paragraph.getConfig();
config.put("enabled", true);
paragraph.setConfig(config);
return null;
});
TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
try {
note.runAll(AuthenticationInfo.ANONYMOUS, false, false, new HashMap<>());
} catch (Exception e) {
fail();
}
return null;
});
String jsonRequest = "{\"cron\":\"* * * * * ?\" }";
// right cron expression but not exist note.
CloseableHttpResponse postCron = httpPost("/notebook/cron/notexistnote", jsonRequest);
assertThat("", postCron, isNotFound());
postCron.close();
// right cron expression.
postCron = httpPost("/notebook/cron/" + noteId, jsonRequest);
assertThat("", postCron, isAllowed());
postCron.close();
Thread.sleep(1000);
// wrong cron expression.
jsonRequest = "{\"cron\":\"a * * * * ?\" }";
postCron = httpPost("/notebook/cron/" + noteId, jsonRequest);
assertThat("", postCron, isBadRequest());
postCron.close();
Thread.sleep(1000);
// remove cron job.
CloseableHttpResponse deleteCron = httpDelete("/notebook/cron/" + noteId);
assertThat("", deleteCron, isAllowed());
deleteCron.close();
} finally {
// cleanup
if (null != noteId) {
TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
}
System.clearProperty(ConfVars.ZEPPELIN_NOTEBOOK_CRON_ENABLE.getVarName());
}
}
use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class ZeppelinRestApiTest method testGetNoteJob.
@Test
public void testGetNoteJob() throws Exception {
LOG.info("testGetNoteJob");
String noteId = null;
try {
// Create note to run test.
noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testGetNoteJob", anonymous);
// use write lock because name is overwritten
TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
assertNotNull("can't create new note", note);
note.setName("note for run test");
Paragraph paragraph = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
Map<String, Object> config = paragraph.getConfig();
config.put("enabled", true);
paragraph.setConfig(config);
paragraph.setText("%sh sleep 1");
paragraph.setAuthenticationInfo(anonymous);
TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
return null;
});
TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
try {
note.runAll(anonymous, true, false, new HashMap<>());
} catch (Exception e) {
fail();
}
return null;
});
// assume that status of the paragraph is running
CloseableHttpResponse get = httpGet("/notebook/job/" + noteId);
assertThat("test get note job: ", get, isAllowed());
String responseBody = EntityUtils.toString(get.getEntity(), StandardCharsets.UTF_8);
get.close();
LOG.info("test get note job: \n" + responseBody);
Map<String, Object> resp = gson.fromJson(responseBody, new TypeToken<Map<String, Object>>() {
}.getType());
NoteJobStatus noteJobStatus = NoteJobStatus.fromJson(gson.toJson(resp.get("body")));
assertEquals(1, noteJobStatus.getParagraphJobStatusList().size());
int progress = Integer.parseInt(noteJobStatus.getParagraphJobStatusList().get(0).getProgress());
assertTrue(progress >= 0 && progress <= 100);
// wait until job is finished or timeout.
int timeout = 1;
boolean terminated = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> note.getParagraph(0).isTerminated());
while (!terminated) {
Thread.sleep(100);
terminated = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> note.getParagraph(0).isTerminated());
if (timeout++ > 10) {
LOG.info("testGetNoteJob timeout job.");
break;
}
}
} finally {
// cleanup
if (null != noteId) {
TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
}
}
}
use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class ZeppelinRestApiTest method testExportNote.
@Test
public void testExportNote() throws IOException {
LOG.info("testExportNote");
String noteId = null;
try {
noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testExportNote", anonymous);
// use write lock because name is overwritten
TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
assertNotNull("can't create new note", note);
note.setName("source note for export");
Paragraph paragraph = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
Map<String, Object> config = paragraph.getConfig();
config.put("enabled", true);
paragraph.setConfig(config);
paragraph.setText("%md This is my new paragraph in my new note");
TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
return null;
});
// Call export Note REST API
CloseableHttpResponse get = httpGet("/notebook/export/" + noteId);
String getResponse = EntityUtils.toString(get.getEntity(), StandardCharsets.UTF_8);
LOG.info("testNoteExport \n" + getResponse);
assertThat("test note export method:", get, isAllowed());
Map<String, Object> resp = gson.fromJson(getResponse, new TypeToken<Map<String, Object>>() {
}.getType());
String exportJSON = (String) resp.get("body");
assertNotNull("Can not find new notejson", exportJSON);
LOG.info("export JSON:=" + exportJSON);
get.close();
} finally {
if (null != noteId) {
TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
}
}
}
use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class NotebookSecurityRestApiTest method deleteNoteForUser.
private void deleteNoteForUser(String noteId, String user, String pwd) throws IOException {
CloseableHttpResponse delete = httpDelete(("/notebook/" + noteId), user, pwd);
assertThat("Test delete method:", delete, isAllowed());
delete.close();
// make sure note is deleted
if (!noteId.isEmpty()) {
TestUtils.getInstance(Notebook.class).processNote(noteId, deletedNote -> {
assertNull("Deleted note should be null", deletedNote);
return null;
});
}
}
Aggregations