use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class ZeppelinRestApiTest method testGetNoteInfo.
@Test
public void testGetNoteInfo() throws IOException {
LOG.info("testGetNoteInfo");
String noteId = null;
try {
// Create note to get info
noteId = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
assertNotNull("can't create new note", noteId);
// use write lock because name is overwritten
String paragraphText = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
note.setName("note");
Paragraph paragraph = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
Map<String, Object> config = paragraph.getConfig();
config.put("enabled", true);
paragraph.setConfig(config);
String paragraphTextTmp = "%md This is my new paragraph in my new note";
paragraph.setText(paragraphTextTmp);
TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
return paragraphTextTmp;
});
CloseableHttpResponse get = httpGet("/notebook/" + noteId);
String getResponse = EntityUtils.toString(get.getEntity(), StandardCharsets.UTF_8);
LOG.info("testGetNoteInfo \n" + getResponse);
assertThat("test note get method:", get, isAllowed());
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");
List<Map<String, Object>> paragraphs = (List<Map<String, Object>>) body.get("paragraphs");
assertTrue(paragraphs.size() > 0);
assertEquals(paragraphText, paragraphs.get(0).get("text"));
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 ZeppelinRestApiTest method testMoveParagraph.
@Test
public void testMoveParagraph() throws IOException {
String noteId = null;
try {
noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testMoveParagraph", anonymous);
Paragraph p2 = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
p.setTitle("title1");
p.setText("text1");
Paragraph p2tmp = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
p2tmp.setTitle("title2");
p2tmp.setText("text2");
TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
return p2tmp;
});
CloseableHttpResponse post = httpPost("/notebook/" + noteId + "/paragraph/" + p2.getId() + "/move/" + 0, "");
assertThat("Test post method: ", post, isAllowed());
post.close();
TestUtils.getInstance(Notebook.class).processNote(noteId, retrNote -> {
Paragraph paragraphAtIdx0 = retrNote.getParagraphs().get(0);
assertEquals(p2.getId(), paragraphAtIdx0.getId());
assertEquals(p2.getTitle(), paragraphAtIdx0.getTitle());
assertEquals(p2.getText(), paragraphAtIdx0.getText());
return null;
});
CloseableHttpResponse post2 = httpPost("/notebook/" + noteId + "/paragraph/" + p2.getId() + "/move/" + 10, "");
assertThat("Test post method: ", post2, isBadRequest());
post.close();
} 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 ClusterEventTest method init.
@BeforeClass
public static void init() throws Exception {
ZeppelinConfiguration zconf = genZeppelinConf();
ZeppelinServerMock.startUp("ClusterEventTest", zconf);
notebook = TestUtils.getInstance(Notebook.class);
authorizationService = TestUtils.getInstance(AuthorizationService.class);
schedulerService = new QuartzSchedulerService(zconf, notebook);
schedulerService.waitForFinishInit();
notebookServer = spy(NotebookServer.getInstance());
notebookService = new NotebookService(notebook, authorizationService, zconf, schedulerService);
ConfigurationService configurationService = new ConfigurationService(notebook.getConf());
when(notebookServer.getNotebookService()).thenReturn(notebookService);
when(notebookServer.getConfigurationService()).thenReturn(configurationService);
startOtherZeppelinClusterNode(zconf);
// wait zeppelin cluster startup
Thread.sleep(10000);
// mock cluster manager client
clusterClient = ClusterManagerClient.getInstance(zconf);
clusterClient.start(metaKey);
// Waiting for cluster startup
int wait = 0;
while (wait++ < 100) {
if (clusterIsStartup() && clusterClient.raftInitialized()) {
LOGGER.info("wait {}(ms) found cluster leader", wait * 500);
break;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
}
}
Thread.sleep(3000);
assertEquals(true, clusterIsStartup());
getClusterServerMeta();
}
use of org.apache.zeppelin.notebook.Notebook in project zeppelin by apache.
the class ClusterEventTest method testRenameNoteEvent.
@Test
public void testRenameNoteEvent() throws IOException {
String noteId = null;
try {
String oldName = "old_name";
noteId = TestUtils.getInstance(Notebook.class).createNote(oldName, anonymous);
TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
assertEquals(note.getName(), oldName);
return null;
});
final String newName = "testName";
String jsonRequest = "{\"name\": " + newName + "}";
CloseableHttpResponse put = AbstractTestRestApi.httpPut("/notebook/" + noteId + "/rename/", jsonRequest);
assertThat("test testRenameNote:", put, AbstractTestRestApi.isAllowed());
put.close();
TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
assertEquals(note.getName(), newName);
return null;
});
// wait cluster sync event
Thread.sleep(1000);
checkClusterNoteEventListener();
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
} 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 NotebookServerTest method testAngularObjectSaveToNote.
@Test
public void testAngularObjectSaveToNote() throws IOException, InterruptedException {
// create a notebook
String note1Id = null;
try {
note1Id = notebook.createNote("note1", "angular", anonymous);
// get reference to interpreterGroup
InterpreterGroup interpreterGroup = null;
List<InterpreterSetting> settings = notebook.processNote(note1Id, note1 -> note1.getBindedInterpreterSettings(new ArrayList<>()));
for (InterpreterSetting setting : settings) {
if (setting.getName().equals("angular")) {
interpreterGroup = setting.getOrCreateInterpreterGroup("anonymous", note1Id);
break;
}
}
String p1Id = notebook.processNote(note1Id, note1 -> {
// start interpreter process
Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
p1.setText("%angular <h2>Bind here : {{COMMAND_TYPE}}</h2>");
p1.setAuthenticationInfo(anonymous);
note1.run(p1.getId());
return p1.getId();
});
// wait for paragraph finished
Status status = notebook.processNote(note1Id, note1 -> note1.getParagraph(p1Id).getStatus());
while (true) {
if (status == Job.Status.FINISHED) {
break;
}
Thread.sleep(100);
status = notebook.processNote(note1Id, note1 -> note1.getParagraph(p1Id).getStatus());
}
// sleep for 1 second to make sure job running thread finish to fire event. See ZEPPELIN-3277
Thread.sleep(1000);
// create two sockets and open it
NotebookSocket sock1 = createWebSocket();
notebookServer.onOpen(sock1);
// getNote, getAngularObject
verify(sock1, times(0)).send(anyString());
// open the same notebook from sockets
notebookServer.onMessage(sock1, new Message(OP.GET_NOTE).put("id", note1Id).toJson());
reset(sock1);
// bind object from sock1
notebookServer.onMessage(sock1, new Message(OP.ANGULAR_OBJECT_CLIENT_BIND).put("noteId", note1Id).put("paragraphId", p1Id).put("name", "COMMAND_TYPE").put("value", "COMMAND_TYPE_VALUE").put("interpreterGroupId", interpreterGroup.getId()).toJson());
List<AngularObject> list = notebook.processNote(note1Id, note1 -> note1.getAngularObjects("angular-shared_process"));
assertEquals(1, list.size());
assertEquals(note1Id, list.get(0).getNoteId());
assertEquals(p1Id, list.get(0).getParagraphId());
assertEquals("COMMAND_TYPE", list.get(0).getName());
assertEquals("COMMAND_TYPE_VALUE", list.get(0).get());
// Check if the interpreterGroup AngularObjectRegistry is updated
Map<String, Map<String, AngularObject>> mapRegistry = interpreterGroup.getAngularObjectRegistry().getRegistry();
AngularObject ao = mapRegistry.get(note1Id + "_" + p1Id).get("COMMAND_TYPE");
assertEquals("COMMAND_TYPE", ao.getName());
assertEquals("COMMAND_TYPE_VALUE", ao.get());
// update bind object from sock1
notebookServer.onMessage(sock1, new Message(OP.ANGULAR_OBJECT_UPDATED).put("noteId", note1Id).put("paragraphId", p1Id).put("name", "COMMAND_TYPE").put("value", "COMMAND_TYPE_VALUE_UPDATE").put("interpreterGroupId", interpreterGroup.getId()).toJson());
list = notebook.processNote(note1Id, note1 -> note1.getAngularObjects("angular-shared_process"));
assertEquals(1, list.size());
assertEquals(note1Id, list.get(0).getNoteId());
assertEquals(p1Id, list.get(0).getParagraphId());
assertEquals("COMMAND_TYPE", list.get(0).getName());
assertEquals("COMMAND_TYPE_VALUE_UPDATE", list.get(0).get());
// Check if the interpreterGroup AngularObjectRegistry is updated
mapRegistry = interpreterGroup.getAngularObjectRegistry().getRegistry();
AngularObject ao1 = mapRegistry.get(note1Id + "_" + p1Id).get("COMMAND_TYPE");
assertEquals("COMMAND_TYPE", ao1.getName());
assertEquals("COMMAND_TYPE_VALUE_UPDATE", ao1.get());
// unbind object from sock1
notebookServer.onMessage(sock1, new Message(OP.ANGULAR_OBJECT_CLIENT_UNBIND).put("noteId", note1Id).put("paragraphId", p1Id).put("name", "COMMAND_TYPE").put("value", "COMMAND_TYPE_VALUE").put("interpreterGroupId", interpreterGroup.getId()).toJson());
list = notebook.processNote(note1Id, note1 -> note1.getAngularObjects("angular-shared_process"));
assertEquals(0, list.size());
// Check if the interpreterGroup AngularObjectRegistry is delete
mapRegistry = interpreterGroup.getAngularObjectRegistry().getRegistry();
AngularObject ao2 = mapRegistry.get(note1Id + "_" + p1Id).get("COMMAND_TYPE");
assertNull(ao2);
} finally {
if (note1Id != null) {
notebook.removeNote(note1Id, anonymous);
}
}
}
Aggregations