Search in sources :

Example 61 with Paragraph

use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.

the class GitNotebookRepoTest method getRevisionTest.

@Test
public void getRevisionTest() throws IOException {
    // initial checks
    notebookRepo = new GitNotebookRepo(conf);
    assertThat(notebookRepo.list(null)).isNotEmpty();
    assertThat(containsNote(notebookRepo.list(null), TEST_NOTE_ID)).isTrue();
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID, null)).isEmpty();
    // add first checkpoint
    Revision revision_1 = notebookRepo.checkpoint(TEST_NOTE_ID, "first commit", null);
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID, null).size()).isEqualTo(1);
    int paragraphCount_1 = notebookRepo.get(TEST_NOTE_ID, null).getParagraphs().size();
    // add paragraph and save
    Note note = notebookRepo.get(TEST_NOTE_ID, null);
    Paragraph p1 = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    Map<String, Object> config = p1.getConfig();
    config.put("enabled", true);
    p1.setConfig(config);
    p1.setText("checkpoint test text");
    notebookRepo.save(note, null);
    // second checkpoint
    notebookRepo.checkpoint(TEST_NOTE_ID, "second commit", null);
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID, null).size()).isEqualTo(2);
    int paragraphCount_2 = notebookRepo.get(TEST_NOTE_ID, null).getParagraphs().size();
    assertThat(paragraphCount_2).isEqualTo(paragraphCount_1 + 1);
    // get note from revision 1
    Note noteRevision_1 = notebookRepo.get(TEST_NOTE_ID, revision_1.id, null);
    assertThat(noteRevision_1.getParagraphs().size()).isEqualTo(paragraphCount_1);
    // get current note
    note = notebookRepo.get(TEST_NOTE_ID, null);
    assertThat(note.getParagraphs().size()).isEqualTo(paragraphCount_2);
    // add one more paragraph and save
    Paragraph p2 = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    config.put("enabled", false);
    p2.setConfig(config);
    p2.setText("get revision when modified note test text");
    notebookRepo.save(note, null);
    note = notebookRepo.get(TEST_NOTE_ID, null);
    int paragraphCount_3 = note.getParagraphs().size();
    assertThat(paragraphCount_3).isEqualTo(paragraphCount_2 + 1);
    // get revision 1 again
    noteRevision_1 = notebookRepo.get(TEST_NOTE_ID, revision_1.id, null);
    assertThat(noteRevision_1.getParagraphs().size()).isEqualTo(paragraphCount_1);
    // check that note is unchanged
    note = notebookRepo.get(TEST_NOTE_ID, null);
    assertThat(note.getParagraphs().size()).isEqualTo(paragraphCount_3);
}
Also used : Revision(org.apache.zeppelin.notebook.repo.NotebookRepo.Revision) Note(org.apache.zeppelin.notebook.Note) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 62 with Paragraph

use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.

the class GitNotebookRepoTest method showNotebookHistoryMultipleNotesTest.

@Test
public void showNotebookHistoryMultipleNotesTest() throws IOException {
    //initial checks
    notebookRepo = new GitNotebookRepo(conf);
    assertThat(notebookRepo.list(null)).isNotEmpty();
    assertThat(containsNote(notebookRepo.list(null), TEST_NOTE_ID)).isTrue();
    assertThat(containsNote(notebookRepo.list(null), TEST_NOTE_ID2)).isTrue();
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID, null)).isEmpty();
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID2, null)).isEmpty();
    //add commit to both notes
    notebookRepo.checkpoint(TEST_NOTE_ID, "first commit, note1", null);
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID, null).size()).isEqualTo(1);
    notebookRepo.checkpoint(TEST_NOTE_ID2, "first commit, note2", null);
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID2, null).size()).isEqualTo(1);
    //modify, save and checkpoint first note
    Note note = notebookRepo.get(TEST_NOTE_ID, null);
    Paragraph p = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    Map<String, Object> config = p.getConfig();
    config.put("enabled", true);
    p.setConfig(config);
    p.setText("%md note1 test text");
    notebookRepo.save(note, null);
    assertThat(notebookRepo.checkpoint(TEST_NOTE_ID, "second commit, note1", null)).isNotNull();
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID, null).size()).isEqualTo(2);
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID2, null).size()).isEqualTo(1);
    assertThat(notebookRepo.checkpoint(TEST_NOTE_ID2, "first commit, note2", null)).isEqualTo(Revision.EMPTY);
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID2, null).size()).isEqualTo(1);
    //modify, save and checkpoint second note
    note = notebookRepo.get(TEST_NOTE_ID2, null);
    p = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    config = p.getConfig();
    config.put("enabled", false);
    p.setConfig(config);
    p.setText("%md note2 test text");
    notebookRepo.save(note, null);
    assertThat(notebookRepo.checkpoint(TEST_NOTE_ID2, "second commit, note2", null)).isNotNull();
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID, null).size()).isEqualTo(2);
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID2, null).size()).isEqualTo(2);
}
Also used : Note(org.apache.zeppelin.notebook.Note) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 63 with Paragraph

use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.

the class GitNotebookRepoTest method getRevisionFailTest.

@Test
public void getRevisionFailTest() throws IOException {
    // initial checks
    notebookRepo = new GitNotebookRepo(conf);
    assertThat(notebookRepo.list(null)).isNotEmpty();
    assertThat(containsNote(notebookRepo.list(null), TEST_NOTE_ID)).isTrue();
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID, null)).isEmpty();
    // add first checkpoint
    Revision revision_1 = notebookRepo.checkpoint(TEST_NOTE_ID, "first commit", null);
    assertThat(notebookRepo.revisionHistory(TEST_NOTE_ID, null).size()).isEqualTo(1);
    int paragraphCount_1 = notebookRepo.get(TEST_NOTE_ID, null).getParagraphs().size();
    // get current note
    Note note = notebookRepo.get(TEST_NOTE_ID, null);
    assertThat(note.getParagraphs().size()).isEqualTo(paragraphCount_1);
    // add one more paragraph and save
    Paragraph p1 = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    Map<String, Object> config = p1.getConfig();
    config.put("enabled", true);
    p1.setConfig(config);
    p1.setText("get revision when modified note test text");
    notebookRepo.save(note, null);
    int paragraphCount_2 = note.getParagraphs().size();
    // get note from revision 1
    Note noteRevision_1 = notebookRepo.get(TEST_NOTE_ID, revision_1.id, null);
    assertThat(noteRevision_1.getParagraphs().size()).isEqualTo(paragraphCount_1);
    // get current note
    note = notebookRepo.get(TEST_NOTE_ID, null);
    assertThat(note.getParagraphs().size()).isEqualTo(paragraphCount_2);
    // test for absent revision
    Revision absentRevision = new Revision("absentId", StringUtils.EMPTY, 0);
    note = notebookRepo.get(TEST_NOTE_ID, absentRevision.id, null);
    assertThat(note).isNull();
}
Also used : Revision(org.apache.zeppelin.notebook.repo.NotebookRepo.Revision) Note(org.apache.zeppelin.notebook.Note) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 64 with Paragraph

use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.

the class NotebookServer method onRemoteRunParagraph.

@Override
public void onRemoteRunParagraph(String noteId, String paragraphId) throws Exception {
    Notebook notebookIns = notebook();
    try {
        if (notebookIns == null) {
            throw new Exception("onRemoteRunParagraph notebook instance is null");
        }
        Note noteIns = notebookIns.getNote(noteId);
        if (noteIns == null) {
            throw new Exception(String.format("Can't found note id %s", noteId));
        }
        Paragraph paragraph = noteIns.getParagraph(paragraphId);
        if (paragraph == null) {
            throw new Exception(String.format("Can't found paragraph %s %s", noteId, paragraphId));
        }
        Set<String> userAndRoles = Sets.newHashSet();
        userAndRoles.add(SecurityUtils.getPrincipal());
        userAndRoles.addAll(SecurityUtils.getRoles());
        if (!notebookIns.getNotebookAuthorization().hasWriteAuthorization(userAndRoles, noteId)) {
            throw new ForbiddenException(String.format("can't execute note %s", noteId));
        }
        AuthenticationInfo subject = new AuthenticationInfo(SecurityUtils.getPrincipal());
        paragraph.setAuthenticationInfo(subject);
        noteIns.run(paragraphId);
    } catch (Exception e) {
        throw e;
    }
}
Also used : ForbiddenException(org.apache.zeppelin.rest.exception.ForbiddenException) Notebook(org.apache.zeppelin.notebook.Notebook) Note(org.apache.zeppelin.notebook.Note) URISyntaxException(java.net.URISyntaxException) FileSystemException(org.apache.commons.vfs2.FileSystemException) ForbiddenException(org.apache.zeppelin.rest.exception.ForbiddenException) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) Paragraph(org.apache.zeppelin.notebook.Paragraph)

Example 65 with Paragraph

use of org.apache.zeppelin.notebook.Paragraph in project zeppelin by apache.

the class InterpreterRestApiTest method testInterpreterRestart.

@Test
public void testInterpreterRestart() throws IOException, InterruptedException {
    // when: create new note
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    note.addParagraph(AuthenticationInfo.ANONYMOUS);
    Paragraph p = note.getLastParagraph();
    Map config = p.getConfig();
    config.put("enabled", true);
    // when: run markdown paragraph
    p.setConfig(config);
    p.setText("%md markdown");
    p.setAuthenticationInfo(anonymous);
    note.run(p.getId());
    while (p.getStatus() != Status.FINISHED) {
        Thread.sleep(100);
    }
    assertEquals(p.getResult().message().get(0).getData(), getSimulatedMarkdownResult("markdown"));
    // when: restart interpreter
    for (InterpreterSetting setting : ZeppelinServer.notebook.getInterpreterSettingManager().getInterpreterSettings(note.getId())) {
        if (setting.getName().equals("md")) {
            // call restart interpreter API
            PutMethod put = httpPut("/interpreter/setting/restart/" + setting.getId(), "");
            assertThat("test interpreter restart:", put, isAllowed());
            put.releaseConnection();
            break;
        }
    }
    // when: run markdown paragraph, again
    p = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    p.setConfig(config);
    p.setText("%md markdown restarted");
    p.setAuthenticationInfo(anonymous);
    note.run(p.getId());
    while (p.getStatus() != Status.FINISHED) {
        Thread.sleep(100);
    }
    // then
    assertEquals(p.getResult().message().get(0).getData(), getSimulatedMarkdownResult("markdown restarted"));
    ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
Also used : Note(org.apache.zeppelin.notebook.Note) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Aggregations

Paragraph (org.apache.zeppelin.notebook.Paragraph)75 Note (org.apache.zeppelin.notebook.Note)69 Test (org.junit.Test)40 Map (java.util.Map)30 TypeToken (com.google.gson.reflect.TypeToken)12 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)12 Path (javax.ws.rs.Path)11 ZeppelinApi (org.apache.zeppelin.annotation.ZeppelinApi)11 PostMethod (org.apache.commons.httpclient.methods.PostMethod)9 Message (org.apache.zeppelin.notebook.socket.Message)9 Notebook (org.apache.zeppelin.notebook.Notebook)7 GetMethod (org.apache.commons.httpclient.methods.GetMethod)6 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)6 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)5 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 JsonObject (com.google.gson.JsonObject)4 IOException (java.io.IOException)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4