Search in sources :

Example 6 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project zeppelin by apache.

the class ZeppelinRestApiTest method testRunParagraphWithParams.

@Test
public void testRunParagraphWithParams() throws IOException, InterruptedException {
    LOG.info("testRunParagraphWithParams");
    // 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("%spark\nval param = z.input(\"param\").toString\nprintln(param)");
    note.persist(anonymous);
    String noteId = note.getId();
    note.runAll();
    // wait until job is finished or timeout.
    int timeout = 1;
    while (!paragraph.isTerminated()) {
        Thread.sleep(1000);
        if (timeout++ > 120) {
            LOG.info("testRunParagraphWithParams timeout job.");
            break;
        }
    }
    // Call Run paragraph REST API
    PostMethod postParagraph = httpPost("/notebook/job/" + noteId + "/" + paragraph.getId(), "{\"params\": {\"param\": \"hello\", \"param2\": \"world\"}}");
    assertThat("test paragraph run:", postParagraph, isAllowed());
    postParagraph.releaseConnection();
    Thread.sleep(1000);
    Note retrNote = ZeppelinServer.notebook.getNote(noteId);
    Paragraph retrParagraph = retrNote.getParagraph(paragraph.getId());
    Map<String, Object> params = retrParagraph.settings.getParams();
    assertEquals("hello", params.get("param"));
    assertEquals("world", params.get("param2"));
    //cleanup
    ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
Also used : 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 7 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod 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 8 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod 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 9 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project zeppelin by apache.

the class ZeppelinRestApiTest method testNoteCreate.

private void testNoteCreate(String noteName) throws IOException {
    // Call Create Note REST API
    String jsonRequest = "{\"name\":\"" + noteName + "\"}";
    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);
    // 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)

Example 10 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project zeppelin by apache.

the class NotebookRestApiTest method testCloneNote.

@Test
public void testCloneNote() throws IOException {
    Note note1 = ZeppelinServer.notebook.createNote(anonymous);
    PostMethod post = httpPost("/notebook/" + note1.getId(), "");
    LOG.info("testCloneNote response\n" + post.getResponseBodyAsString());
    assertThat(post, isAllowed());
    Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    String clonedNoteId = (String) resp.get("body");
    post.releaseConnection();
    GetMethod get = httpGet("/notebook/" + clonedNoteId);
    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");
    assertEquals((String) resp2Body.get("name"), "Note " + clonedNoteId);
    get.releaseConnection();
    //cleanup
    ZeppelinServer.notebook.removeNote(note1.getId(), anonymous);
    ZeppelinServer.notebook.removeNote(clonedNoteId, anonymous);
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) TypeToken(com.google.gson.reflect.TypeToken) Note(org.apache.zeppelin.notebook.Note) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Map(java.util.Map) Test(org.junit.Test)

Aggregations

PostMethod (org.apache.commons.httpclient.methods.PostMethod)203 HttpClient (org.apache.commons.httpclient.HttpClient)114 Test (org.junit.Test)55 IOException (java.io.IOException)32 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)28 Part (org.apache.commons.httpclient.methods.multipart.Part)25 NameValuePair (org.apache.commons.httpclient.NameValuePair)24 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)23 Map (java.util.Map)21 GetMethod (org.apache.commons.httpclient.methods.GetMethod)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)21 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)21 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 Header (org.apache.commons.httpclient.Header)16 Note (org.apache.zeppelin.notebook.Note)13 HttpException (org.apache.commons.httpclient.HttpException)12 File (java.io.File)11 InputStream (java.io.InputStream)11 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)11 JSONParser (org.json.simple.parser.JSONParser)11