Search in sources :

Example 66 with Paragraph

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

the class NotebookRestApiTest method testRunParagraphJob.

@Test
public void testRunParagraphJob() throws IOException {
    Note note1 = ZeppelinServer.notebook.createNote(anonymous);
    note1.addParagraph(AuthenticationInfo.ANONYMOUS);
    Paragraph p = note1.addParagraph(AuthenticationInfo.ANONYMOUS);
    // run blank paragraph
    PostMethod post = httpPost("/notebook/job/" + note1.getId() + "/" + p.getId(), "");
    assertThat(post, isAllowed());
    Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    assertEquals(resp.get("status"), "OK");
    post.releaseConnection();
    assertEquals(p.getStatus(), Job.Status.FINISHED);
    // run non-blank paragraph
    p.setText("test");
    post = httpPost("/notebook/job/" + note1.getId() + "/" + p.getId(), "");
    assertThat(post, isAllowed());
    resp = gson.fromJson(post.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    assertEquals(resp.get("status"), "OK");
    post.releaseConnection();
    assertNotEquals(p.getStatus(), Job.Status.READY);
    //cleanup
    ZeppelinServer.notebook.removeNote(note1.getId(), anonymous);
}
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 67 with Paragraph

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

the class ZeppelinRestApiTest method testJobs.

@Test
public void testJobs() throws InterruptedException, IOException {
    // create a note and a paragraph
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    note.setName("note for run test");
    Paragraph paragraph = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    paragraph.setText("%md This is test paragraph.");
    Map config = paragraph.getConfig();
    config.put("enabled", true);
    paragraph.setConfig(config);
    note.runAll();
    // wait until job is finished or timeout.
    int timeout = 1;
    while (!paragraph.isTerminated()) {
        Thread.sleep(1000);
        if (timeout++ > 10) {
            LOG.info("testNoteJobs timeout job.");
            break;
        }
    }
    String jsonRequest = "{\"cron\":\"* * * * * ?\" }";
    // right cron expression but not exist note.
    PostMethod postCron = httpPost("/notebook/cron/notexistnote", jsonRequest);
    assertThat("", postCron, isNotFound());
    postCron.releaseConnection();
    // right cron expression.
    postCron = httpPost("/notebook/cron/" + note.getId(), jsonRequest);
    assertThat("", postCron, isAllowed());
    postCron.releaseConnection();
    Thread.sleep(1000);
    // wrong cron expression.
    jsonRequest = "{\"cron\":\"a * * * * ?\" }";
    postCron = httpPost("/notebook/cron/" + note.getId(), jsonRequest);
    assertThat("", postCron, isBadRequest());
    postCron.releaseConnection();
    Thread.sleep(1000);
    // remove cron job.
    DeleteMethod deleteCron = httpDelete("/notebook/cron/" + note.getId());
    assertThat("", deleteCron, isAllowed());
    deleteCron.releaseConnection();
    ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) PostMethod(org.apache.commons.httpclient.methods.PostMethod) Note(org.apache.zeppelin.notebook.Note) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 68 with Paragraph

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

the class ZeppelinRestApiTest method testDeleteParagraph.

@Test
public void testDeleteParagraph() throws IOException {
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    Paragraph p = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    p.setTitle("title1");
    p.setText("text1");
    note.persist(anonymous);
    DeleteMethod delete = httpDelete("/notebook/" + note.getId() + "/paragraph/" + p.getId());
    assertThat("Test delete method: ", delete, isAllowed());
    delete.releaseConnection();
    Note retrNote = ZeppelinServer.notebook.getNote(note.getId());
    Paragraph retrParagrah = retrNote.getParagraph(p.getId());
    assertNull("paragraph should be deleted", retrParagrah);
    ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) Note(org.apache.zeppelin.notebook.Note) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 69 with Paragraph

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

the class ZeppelinRestApiTest method testGetNoteJob.

@Test
public void testGetNoteJob() throws IOException, InterruptedException {
    LOG.info("testGetNoteJob");
    // Create note to run test.
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    assertNotNull("can't create new note", note);
    note.setName("note for run test");
    Paragraph paragraph = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    Map config = paragraph.getConfig();
    config.put("enabled", true);
    paragraph.setConfig(config);
    paragraph.setText("%sh sleep 1");
    paragraph.setAuthenticationInfo(anonymous);
    note.persist(anonymous);
    String noteId = note.getId();
    note.runAll();
    // wait until paragraph gets started
    while (!paragraph.getStatus().isRunning()) {
        Thread.sleep(100);
    }
    // assume that status of the paragraph is running
    GetMethod get = httpGet("/notebook/job/" + noteId);
    assertThat("test get note job: ", get, isAllowed());
    String responseBody = get.getResponseBodyAsString();
    get.releaseConnection();
    LOG.info("test get note job: \n" + responseBody);
    Map<String, Object> resp = gson.fromJson(responseBody, new TypeToken<Map<String, Object>>() {
    }.getType());
    List<Map<String, Object>> paragraphs = (List<Map<String, Object>>) resp.get("body");
    assertEquals(1, paragraphs.size());
    assertTrue(paragraphs.get(0).containsKey("progress"));
    int progress = Integer.parseInt((String) paragraphs.get(0).get("progress"));
    assertTrue(progress >= 0 && progress <= 100);
    // wait until job is finished or timeout.
    int timeout = 1;
    while (!paragraph.isTerminated()) {
        Thread.sleep(100);
        if (timeout++ > 10) {
            LOG.info("testGetNoteJob timeout job.");
            break;
        }
    }
    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) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 70 with Paragraph

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

the class ZeppelinRestApiTest method testGetParagraph.

@Test
public void testGetParagraph() throws IOException {
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    Paragraph p = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    p.setTitle("hello");
    p.setText("world");
    note.persist(anonymous);
    GetMethod get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p.getId());
    LOG.info("testGetParagraph response\n" + get.getResponseBodyAsString());
    assertThat("Test get method: ", get, isAllowed());
    get.releaseConnection();
    Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    assertNotNull(resp);
    assertEquals("OK", resp.get("status"));
    Map<String, Object> body = (Map<String, Object>) resp.get("body");
    assertEquals(p.getId(), body.get("id"));
    assertEquals("hello", body.get("title"));
    assertEquals("world", body.get("text"));
    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) 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