use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class NotebookSecurityRestApiTest method testThatWriterCannotRemoveNote.
@Test
public void testThatWriterCannotRemoveNote() throws IOException {
String noteId = createNoteForUser("test_4", "admin", "password1");
// set permission
String payload = "{ \"owners\": [\"admin\", \"user1\"], \"readers\": [\"user2\"], " + "\"runners\": [\"user2\"], \"writers\": [\"user2\"] }";
CloseableHttpResponse put = httpPut("/notebook/" + noteId + "/permissions", payload, "admin", "password1");
assertThat("test set note permission method:", put, isAllowed());
put.close();
userTryRemoveNote(noteId, "user2", "password3", isForbidden());
userTryRemoveNote(noteId, "user1", "password2", isAllowed());
TestUtils.getInstance(Notebook.class).processNote(noteId, deletedNote -> {
assertNull("Deleted note should be null", deletedNote);
return null;
});
}
use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class NotebookSecurityRestApiTest method createNoteForUser.
private String createNoteForUser(String noteName, String user, String pwd) throws IOException {
String jsonRequest = "{\"name\":\"" + noteName + "\"}";
CloseableHttpResponse post = httpPost("/notebook/", jsonRequest, user, pwd);
assertThat("test note create method:", post, isAllowed());
Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
}.getType());
post.close();
String newNoteId = (String) resp.get("body");
Notebook notebook = TestUtils.getInstance(Notebook.class);
notebook.processNote(newNoteId, newNote -> {
assertNotNull("Can not find new note by id", newNote);
return null;
});
return newNoteId;
}
use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class NotebookRestApiTest method testRunNoteNonBlocking_Isolated.
@Test
public void testRunNoteNonBlocking_Isolated() throws IOException, InterruptedException {
LOG.info("Running testRunNoteNonBlocking_Isolated");
String note1Id = null;
try {
InterpreterSettingManager interpreterSettingManager = TestUtils.getInstance(InterpreterSettingManager.class);
InterpreterSetting interpreterSetting = interpreterSettingManager.getInterpreterSettingByName("python");
int pythonProcessNum = interpreterSetting.getAllInterpreterGroups().size();
note1Id = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
// 2 paragraphs
// P1:
// %python
// from __future__ import print_function
// import time
// time.sleep(1)
// user='abc'
// P2:
// %python
// print(user)
//
TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
Paragraph p2 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
p1.setText("%python from __future__ import print_function\nimport time\ntime.sleep(1)\nuser='abc'");
p2.setText("%python print(user)");
return null;
});
CloseableHttpResponse post = httpPost("/notebook/job/" + note1Id + "?blocking=false&isolated=true", "");
assertThat(post, isAllowed());
Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
}.getType());
assertEquals("OK", resp.get("status"));
post.close();
// wait for all the paragraphs are done
boolean isRunning = TestUtils.getInstance(Notebook.class).processNote(note1Id, Note::isRunning);
while (isRunning) {
Thread.sleep(1000);
isRunning = TestUtils.getInstance(Notebook.class).processNote(note1Id, Note::isRunning);
}
TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
Paragraph p1 = note1.getParagraph(0);
Paragraph p2 = note1.getParagraph(1);
assertEquals(Job.Status.FINISHED, p1.getStatus());
assertEquals(Job.Status.FINISHED, p2.getStatus());
assertEquals("abc\n", p2.getReturn().message().get(0).getData());
return null;
});
// no new python process is created because it is isolated mode.
assertEquals(pythonProcessNum, interpreterSetting.getAllInterpreterGroups().size());
} finally {
// cleanup
if (null != note1Id) {
TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous);
}
}
}
use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class NotebookRestApiTest method testRenameNote.
@Test
public void testRenameNote() throws IOException {
LOG.info("Running testRenameNote");
String noteId = null;
try {
String oldName = "old_name";
noteId = TestUtils.getInstance(Notebook.class).createNote(oldName, anonymous);
assertEquals(oldName, TestUtils.getInstance(Notebook.class).processNote(noteId, Note::getName));
final String newName = "testName";
String jsonRequest = "{\"name\": " + newName + "}";
CloseableHttpResponse put = httpPut("/notebook/" + noteId + "/rename/", jsonRequest);
assertThat("test testRenameNote:", put, isAllowed());
put.close();
assertEquals(newName, TestUtils.getInstance(Notebook.class).processNote(noteId, Note::getName));
} 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 NotebookRestApiTest method testGetNoteByPath.
@Test
public void testGetNoteByPath() throws IOException {
LOG.info("Running testGetNoteByPath");
String note1Id = null;
try {
String notePath = "dir1/note1";
note1Id = TestUtils.getInstance(Notebook.class).createNote(notePath, anonymous);
TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
TestUtils.getInstance(Notebook.class).saveNote(note1, anonymous);
return null;
});
CloseableHttpResponse post = httpPost("/notebook/getByPath", "{\"notePath\":\"" + notePath + "\"}");
assertThat(post, isAllowed());
Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
}.getType());
Map<String, Object> noteObject = (Map<String, Object>) resp.get("body");
assertEquals(notePath, ((String) noteObject.get("path")).substring(1));
post.close();
} finally {
// cleanup
if (null != note1Id) {
TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous);
}
}
}
Aggregations