use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class ZeppelinRestApiTest method testDeleteParagraph.
@Test
public void testDeleteParagraph() throws IOException {
String noteId = null;
try {
noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testDeleteParagraph", anonymous);
Paragraph p = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
Paragraph ptmp = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
ptmp.setTitle("title1");
ptmp.setText("text1");
TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
return ptmp;
});
CloseableHttpResponse delete = httpDelete("/notebook/" + noteId + "/paragraph/" + p.getId());
assertThat("Test delete method: ", delete, isAllowed());
delete.close();
TestUtils.getInstance(Notebook.class).processNote(noteId, retrNote -> {
Paragraph retrParagrah = retrNote.getParagraph(p.getId());
assertNull("paragraph should be deleted", retrParagrah);
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 testGetParagraph.
@Test
public void testGetParagraph() throws IOException {
String noteId = null;
try {
noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testGetParagraph", anonymous);
String pId = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
p.setTitle("hello");
p.setText("world");
TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
return p.getId();
});
CloseableHttpResponse get = httpGet("/notebook/" + noteId + "/paragraph/" + pId);
String getResponse = EntityUtils.toString(get.getEntity(), StandardCharsets.UTF_8);
LOG.info("testGetParagraph response\n" + getResponse);
assertThat("Test get method: ", get, isAllowed());
get.close();
Map<String, Object> resp = gson.fromJson(getResponse, new TypeToken<Map<String, Object>>() {
}.getType());
assertNotNull(resp);
assertEquals("OK", resp.get("status"));
Map<String, Object> body = (Map<String, Object>) resp.get("body");
assertEquals(pId, body.get("id"));
assertEquals("hello", body.get("title"));
assertEquals("world", body.get("text"));
} 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 testNoteJobs.
@Test
public void testNoteJobs() throws Exception {
LOG.info("testNoteJobs");
String noteId = null;
try {
// Create note to run test.
noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testNoteJobs", anonymous);
// use write lock because name is overwritten
String paragraphId = 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("%md This is test paragraph.");
TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
return paragraph.getId();
});
TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
try {
note.runAll(anonymous, true, false, new HashMap<>());
} catch (Exception e) {
fail();
}
return null;
});
// 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(1000);
terminated = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> note.getParagraph(0).isTerminated());
if (timeout++ > 10) {
LOG.info("testNoteJobs timeout job.");
break;
}
}
// Call Run note jobs REST API
CloseableHttpResponse postNoteJobs = httpPost("/notebook/job/" + noteId + "?blocking=true", "");
assertThat("test note jobs run:", postNoteJobs, isAllowed());
postNoteJobs.close();
// Call Stop note jobs REST API
CloseableHttpResponse deleteNoteJobs = httpDelete("/notebook/job/" + noteId);
assertThat("test note stop:", deleteNoteJobs, isAllowed());
deleteNoteJobs.close();
Thread.sleep(1000);
// Call Run paragraph REST API
CloseableHttpResponse postParagraph = httpPost("/notebook/job/" + noteId + "/" + paragraphId, "");
assertThat("test paragraph run:", postParagraph, isAllowed());
postParagraph.close();
Thread.sleep(1000);
// Call Stop paragraph REST API
CloseableHttpResponse deleteParagraph = httpDelete("/notebook/job/" + noteId + "/" + paragraphId);
assertThat("test paragraph stop:", deleteParagraph, isAllowed());
deleteParagraph.close();
Thread.sleep(1000);
} 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 testImportNotebook.
@Test
public void testImportNotebook() throws IOException {
String noteId = null;
Map<String, Object> resp;
String oldJson;
String noteName;
String importId = null;
try {
noteName = "source note for import";
LOG.info("testImportNote");
// create test note
noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testImportNotebook", anonymous);
// use write lock because name is overwritten
int paragraphSize = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
assertNotNull("can't create new note", note);
note.setName(noteName);
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 note.getParagraphs().size();
});
// get note content as JSON
oldJson = getNoteContent(noteId);
// delete it first then import it
TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
// call note post
CloseableHttpResponse importPost = httpPost("/notebook/import/", oldJson);
assertThat(importPost, isAllowed());
resp = gson.fromJson(EntityUtils.toString(importPost.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
}.getType());
importId = (String) resp.get("body");
assertNotNull("Did not get back a note id in body", importId);
TestUtils.getInstance(Notebook.class).processNote(importId, newNote -> {
assertEquals("Compare note names", noteName, newNote.getName());
assertEquals("Compare paragraphs count", paragraphSize, newNote.getParagraphs().size());
return null;
});
importPost.close();
} finally {
if (null != noteId) {
TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
}
TestUtils.getInstance(Notebook.class).removeNote(importId, anonymous);
}
}
use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class ZeppelinRestApiTest method testTitleSearch.
@Test
public void testTitleSearch() throws IOException, InterruptedException {
String noteId = null;
try {
noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testTitleSearch", anonymous);
String jsonRequest = "{\"title\": \"testTitleSearchOfParagraph\", " + "\"text\": \"ThisIsToTestSearchMethodWithTitle \"}";
CloseableHttpResponse postNoteText = httpPost("/notebook/" + noteId + "/paragraph", jsonRequest);
postNoteText.close();
Thread.sleep(3000);
CloseableHttpResponse searchNote = httpGet("/notebook/search?q='testTitleSearchOfParagraph'");
Map<String, Object> respSearchResult = gson.fromJson(EntityUtils.toString(searchNote.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
}.getType());
ArrayList searchBody = (ArrayList) respSearchResult.get("body");
int numberOfTitleHits = 0;
for (int i = 0; i < searchBody.size(); i++) {
Map<String, String> searchResult = (Map<String, String>) searchBody.get(i);
if (searchResult.get("header").contains("testTitleSearchOfParagraph")) {
numberOfTitleHits++;
}
}
assertEquals("Paragraph title hits must be at-least one", true, numberOfTitleHits >= 1);
searchNote.close();
} finally {
// cleanup
if (null != noteId) {
TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
}
}
}
Aggregations