Search in sources :

Example 11 with Paragraph

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

the class ZeppelinRestApiTest method testCloneNote.

@Test
public void testCloneNote() throws IOException, CloneNotSupportedException, IllegalArgumentException {
    LOG.info("testCloneNote");
    // Create note to clone
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    assertNotNull("can't create new note", note);
    note.setName("source note for clone");
    Paragraph paragraph = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    Map config = paragraph.getConfig();
    config.put("enabled", true);
    paragraph.setConfig(config);
    paragraph.setText("%md This is my new paragraph in my new note");
    note.persist(anonymous);
    String sourceNoteId = note.getId();
    String noteName = "clone Note Name";
    // Call Clone Note REST API
    String jsonRequest = "{\"name\":\"" + noteName + "\"}";
    PostMethod post = httpPost("/notebook/" + sourceNoteId, jsonRequest);
    LOG.info("testNoteClone \n" + post.getResponseBodyAsString());
    assertThat("test note clone method:", post, isAllowed());
    Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    String newNoteId = (String) resp.get("body");
    LOG.info("newNoteId:=" + newNoteId);
    Note newNote = ZeppelinServer.notebook.getNote(newNoteId);
    assertNotNull("Can not find new note by id", newNote);
    assertEquals("Compare note names", noteName, newNote.getName());
    assertEquals("Compare paragraphs count", note.getParagraphs().size(), newNote.getParagraphs().size());
    //cleanup
    ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
    ZeppelinServer.notebook.removeNote(newNote.getId(), anonymous);
    post.releaseConnection();
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) TypeToken(com.google.gson.reflect.TypeToken) Note(org.apache.zeppelin.notebook.Note) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 12 with Paragraph

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

the class ZeppelinRestApiTest method testNoteCreateWithParagraphs.

@Test
public void testNoteCreateWithParagraphs() throws IOException {
    // Call Create Note REST API
    String noteName = "test";
    String jsonRequest = "{\"name\":\"" + noteName + "\", \"paragraphs\": [" + "{\"title\": \"title1\", \"text\": \"text1\"}," + "{\"title\": \"title2\", \"text\": \"text2\"}" + "]}";
    PostMethod post = httpPost("/notebook/", jsonRequest);
    LOG.info("testNoteCreate \n" + post.getResponseBodyAsString());
    assertThat("test note create method:", post, isAllowed());
    Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    String newNoteId = (String) resp.get("body");
    LOG.info("newNoteId:=" + newNoteId);
    Note newNote = ZeppelinServer.notebook.getNote(newNoteId);
    assertNotNull("Can not find new note by id", newNote);
    // This is partial test as newNote is in memory but is not persistent
    String newNoteName = newNote.getName();
    LOG.info("new note name is: " + newNoteName);
    String expectedNoteName = noteName;
    if (noteName.isEmpty()) {
        expectedNoteName = "Note " + newNoteId;
    }
    assertEquals("compare note name", expectedNoteName, newNoteName);
    assertEquals("initial paragraph check failed", 3, newNote.getParagraphs().size());
    for (Paragraph p : newNote.getParagraphs()) {
        if (StringUtils.isEmpty(p.getText())) {
            continue;
        }
        assertTrue("paragraph title check failed", p.getTitle().startsWith("title"));
        assertTrue("paragraph text check failed", p.getText().startsWith("text"));
    }
    // cleanup
    ZeppelinServer.notebook.removeNote(newNoteId, anonymous);
    post.releaseConnection();
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) TypeToken(com.google.gson.reflect.TypeToken) Note(org.apache.zeppelin.notebook.Note) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 13 with Paragraph

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

the class InterpreterRestApiTest method testRestartInterpreterPerNote.

@Test
public void testRestartInterpreterPerNote() 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: get md interpreter
    InterpreterSetting mdIntpSetting = null;
    for (InterpreterSetting setting : ZeppelinServer.notebook.getInterpreterSettingManager().getInterpreterSettings(note.getId())) {
        if (setting.getName().equals("md")) {
            mdIntpSetting = setting;
            break;
        }
    }
    String jsonRequest = "{\"noteId\":\"" + note.getId() + "\"}";
    // Restart isolated mode of Interpreter for note.
    mdIntpSetting.getOption().setPerNote(InterpreterOption.ISOLATED);
    PutMethod put = httpPut("/interpreter/setting/restart/" + mdIntpSetting.getId(), jsonRequest);
    assertThat("isolated interpreter restart:", put, isAllowed());
    put.releaseConnection();
    // Restart scoped mode of Interpreter for note.
    mdIntpSetting.getOption().setPerNote(InterpreterOption.SCOPED);
    put = httpPut("/interpreter/setting/restart/" + mdIntpSetting.getId(), jsonRequest);
    assertThat("scoped interpreter restart:", put, isAllowed());
    put.releaseConnection();
    // Restart shared mode of Interpreter for note.
    mdIntpSetting.getOption().setPerNote(InterpreterOption.SHARED);
    put = httpPut("/interpreter/setting/restart/" + mdIntpSetting.getId(), jsonRequest);
    assertThat("shared interpreter restart:", put, isAllowed());
    put.releaseConnection();
    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)

Example 14 with Paragraph

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

the class NotebookRestApiTest method testUpdateParagraphConfig.

@Test
public void testUpdateParagraphConfig() throws IOException {
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    String noteId = note.getId();
    Paragraph p = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    assertNull(p.getConfig().get("colWidth"));
    String paragraphId = p.getId();
    String jsonRequest = "{\"colWidth\": 6.0}";
    PutMethod put = httpPut("/notebook/" + noteId + "/paragraph/" + paragraphId + "/config", jsonRequest);
    assertThat("test testUpdateParagraphConfig:", put, isAllowed());
    Map<String, Object> resp = gson.fromJson(put.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    Map<String, Object> respBody = (Map<String, Object>) resp.get("body");
    Map<String, Object> config = (Map<String, Object>) respBody.get("config");
    put.releaseConnection();
    assertEquals(config.get("colWidth"), 6.0);
    note = ZeppelinServer.notebook.getNote(noteId);
    assertEquals(note.getParagraph(paragraphId).getConfig().get("colWidth"), 6.0);
    //cleanup
    ZeppelinServer.notebook.removeNote(noteId, anonymous);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) Note(org.apache.zeppelin.notebook.Note) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 15 with Paragraph

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

the class NotebookRestApiTest method testClearAllParagraphOutput.

@Test
public void testClearAllParagraphOutput() throws IOException {
    // Create note and set result explicitly
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    Paragraph p1 = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    InterpreterResult result = new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TEXT, "result");
    p1.setResult(result);
    Paragraph p2 = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    p2.setReturn(result, new Throwable());
    // clear paragraph result
    PutMethod put = httpPut("/notebook/" + note.getId() + "/clear", "");
    LOG.info("test clear paragraph output response\n" + put.getResponseBodyAsString());
    assertThat(put, isAllowed());
    put.releaseConnection();
    // check if paragraph results are cleared
    GetMethod get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p1.getId());
    assertThat(get, isAllowed());
    Map<String, Object> resp1 = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    Map<String, Object> resp1Body = (Map<String, Object>) resp1.get("body");
    assertNull(resp1Body.get("result"));
    get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p2.getId());
    assertThat(get, isAllowed());
    Map<String, Object> resp2 = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    Map<String, Object> resp2Body = (Map<String, Object>) resp2.get("body");
    assertNull(resp2Body.get("result"));
    get.releaseConnection();
    //cleanup
    ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) Note(org.apache.zeppelin.notebook.Note) GetMethod(org.apache.commons.httpclient.methods.GetMethod) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) 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