use of org.apache.zeppelin.service.ServiceContext in project zeppelin by apache.
the class NotebookServerTest method testRuntimeInfos.
@Test
public void testRuntimeInfos() throws IOException {
// mock note
String msg = "{\"op\":\"IMPORT_NOTE\",\"data\":" + "{\"note\":{\"paragraphs\": [{\"text\": \"Test " + "paragraphs import\"," + "\"progressUpdateIntervalMs\":500," + "\"config\":{},\"settings\":{}}]," + "\"name\": \"Test RuntimeInfos\",\"config\": " + "{}}}}";
Message messageReceived = notebookServer.deserializeMessage(msg);
String noteId = null;
ServiceContext context = new ServiceContext(AuthenticationInfo.ANONYMOUS, new HashSet<>());
try {
noteId = notebookServer.importNote(null, context, messageReceived);
} catch (NullPointerException e) {
// broadcastNoteList(); failed nothing to worry.
LOG.error("Exception in NotebookServerTest while testImportNotebook, failed nothing to " + "worry ", e);
} catch (IOException e) {
e.printStackTrace();
}
// update RuntimeInfos
Map<String, String> infos = new java.util.HashMap<>();
String paragraphId = notebook.processNote(noteId, note -> {
assertNotNull(note);
assertNotNull(note.getParagraph(0));
infos.put("jobUrl", "jobUrl_value");
infos.put("jobLabel", "jobLabel_value");
infos.put("label", "SPARK JOB");
infos.put("tooltip", "View in Spark web UI");
infos.put("noteId", note.getId());
infos.put("paraId", note.getParagraph(0).getId());
return note.getParagraph(0).getId();
});
notebookServer.onParaInfosReceived(noteId, paragraphId, "spark", infos);
notebook.processNote(noteId, note -> {
Paragraph paragraph = note.getParagraph(paragraphId);
// check RuntimeInfos
assertTrue(paragraph.getRuntimeInfos().containsKey("jobUrl"));
List<Object> list = paragraph.getRuntimeInfos().get("jobUrl").getValue();
assertEquals(1, list.size());
Map<String, String> map = (Map<String, String>) list.get(0);
assertEquals(2, map.size());
assertEquals(map.get("jobUrl"), "jobUrl_value");
assertEquals(map.get("jobLabel"), "jobLabel_value");
return null;
});
}
use of org.apache.zeppelin.service.ServiceContext in project zeppelin by apache.
the class NotebookServerTest method testImportJupyterNote.
@Test
public void testImportJupyterNote() throws IOException {
String jupyterNoteJson = IOUtils.toString(getClass().getResourceAsStream("/Lecture-4.ipynb"), StandardCharsets.UTF_8);
String msg = "{\"op\":\"IMPORT_NOTE\",\"data\":" + "{\"note\": " + jupyterNoteJson + "}}";
Message messageReceived = notebookServer.deserializeMessage(msg);
String noteId = null;
ServiceContext context = new ServiceContext(AuthenticationInfo.ANONYMOUS, new HashSet<>());
try {
try {
noteId = notebookServer.importNote(null, context, messageReceived);
} catch (NullPointerException e) {
// broadcastNoteList(); failed nothing to worry.
LOG.error("Exception in NotebookServerTest while testImportJupyterNote, failed nothing to " + "worry ", e);
}
notebook.processNote(noteId, note -> {
assertNotNull(note);
assertTrue(note.getName(), note.getName().startsWith("Note converted from Jupyter_"));
assertEquals("md", note.getParagraphs().get(0).getIntpText());
assertEquals("\n# matplotlib - 2D and 3D plotting in Python", note.getParagraphs().get(0).getScriptText());
return null;
});
} finally {
if (noteId != null) {
notebook.removeNote(noteId, anonymous);
}
}
}
use of org.apache.zeppelin.service.ServiceContext in project zeppelin by apache.
the class AbstractRestApi method getServiceContext.
protected ServiceContext getServiceContext() {
AuthenticationInfo authInfo = new AuthenticationInfo(authenticationService.getPrincipal());
Set<String> userAndRoles = new HashSet<>();
userAndRoles.add(authenticationService.getPrincipal());
userAndRoles.addAll(authenticationService.getAssociatedRoles());
return new ServiceContext(authInfo, userAndRoles);
}
use of org.apache.zeppelin.service.ServiceContext in project zeppelin by apache.
the class NotebookRepoRestApi method getServiceContext.
private ServiceContext getServiceContext() {
AuthenticationInfo authInfo = new AuthenticationInfo(authenticationService.getPrincipal());
Set<String> userAndRoles = new HashSet<>();
userAndRoles.add(authenticationService.getPrincipal());
userAndRoles.addAll(authenticationService.getAssociatedRoles());
return new ServiceContext(authInfo, userAndRoles);
}
use of org.apache.zeppelin.service.ServiceContext in project zeppelin by apache.
the class NotebookRestApi method cloneNote.
/**
* Clone note REST API.
*
* @param noteId ID of Note
* @return JSON with status.OK
* @throws IOException
* @throws CloneNotSupportedException
* @throws IllegalArgumentException
*/
@POST
@Path("{noteId}")
@ZeppelinApi
public Response cloneNote(@PathParam("noteId") String noteId, String message) throws IOException, IllegalArgumentException {
LOGGER.info("Clone note by JSON {}", message);
checkIfUserCanWrite(noteId, "Insufficient privileges you cannot clone this note");
NewNoteRequest request = NewNoteRequest.fromJson(message);
String newNoteName = null;
if (request != null) {
newNoteName = request.getName();
}
AuthenticationInfo subject = new AuthenticationInfo(authenticationService.getPrincipal());
String newNoteId = notebookService.cloneNote(noteId, newNoteName, getServiceContext(), new RestServiceCallback<Note>() {
@Override
public void onSuccess(Note newNote, ServiceContext context) throws IOException {
notebookServer.broadcastNote(newNote);
notebookServer.broadcastNoteList(subject, context.getUserAndRoles());
}
});
return new JsonResponse<>(Status.OK, "", newNoteId).build();
}
Aggregations