Search in sources :

Example 21 with Notebook

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

the class ZeppelinRestApiTest method testInsertParagraph.

@Test
public void testInsertParagraph() throws IOException {
    String noteId = null;
    try {
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testInsertParagraph", anonymous);
        String jsonRequest = "{\"title\": \"title1\", \"text\": \"text1\"}";
        CloseableHttpResponse post = httpPost("/notebook/" + noteId + "/paragraph", jsonRequest);
        String postResponse = EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8);
        LOG.info("testInsertParagraph response\n" + postResponse);
        assertThat("Test insert method:", post, isAllowed());
        post.close();
        Map<String, Object> resp = gson.fromJson(postResponse, new TypeToken<Map<String, Object>>() {
        }.getType());
        String newParagraphId = (String) resp.get("body");
        LOG.info("newParagraphId:=" + newParagraphId);
        Paragraph lastParagraph = TestUtils.getInstance(Notebook.class).processNote(noteId, Note::getLastParagraph);
        TestUtils.getInstance(Notebook.class).processNote(noteId, retrNote -> {
            Paragraph newParagraph = retrNote.getParagraph(newParagraphId);
            assertNotNull("Can not find new paragraph by id", newParagraph);
            assertEquals("title1", newParagraph.getTitle());
            assertEquals("text1", newParagraph.getText());
            assertEquals(newParagraph.getId(), lastParagraph.getId());
            return null;
        });
        // insert to index 0
        String jsonRequest2 = "{\"index\": 0, \"title\": \"title2\", \"text\": \"text2\"}";
        CloseableHttpResponse post2 = httpPost("/notebook/" + noteId + "/paragraph", jsonRequest2);
        LOG.info("testInsertParagraph response2\n" + EntityUtils.toString(post2.getEntity(), StandardCharsets.UTF_8));
        assertThat("Test insert method:", post2, isAllowed());
        post2.close();
        Paragraph paragraphAtIdx0 = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> note.getParagraphs().get(0));
        assertEquals("title2", paragraphAtIdx0.getTitle());
        assertEquals("text2", paragraphAtIdx0.getText());
        // append paragraph providing graph
        String jsonRequest3 = "{\"title\": \"title3\", \"text\": \"text3\", " + "\"config\": {\"colWidth\": 9.0, \"title\": true, " + "\"results\": [{\"graph\": {\"mode\": \"pieChart\"}}]}}";
        CloseableHttpResponse post3 = httpPost("/notebook/" + noteId + "/paragraph", jsonRequest3);
        LOG.info("testInsertParagraph response4\n" + EntityUtils.toString(post3.getEntity(), StandardCharsets.UTF_8));
        assertThat("Test insert method:", post3, isAllowed());
        post3.close();
        Paragraph p = TestUtils.getInstance(Notebook.class).processNote(noteId, Note::getLastParagraph);
        assertEquals("title3", p.getTitle());
        assertEquals("text3", p.getText());
        Map result = ((List<Map>) p.getConfig().get("results")).get(0);
        String mode = ((Map) result.get("graph")).get("mode").toString();
        assertEquals("pieChart", mode);
        assertEquals(9.0, p.getConfig().get("colWidth"));
        assertTrue(((boolean) p.getConfig().get("title")));
    } finally {
        // cleanup
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) Note(org.apache.zeppelin.notebook.Note) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 22 with Notebook

use of org.apache.zeppelin.notebook.Notebook 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 + "\"}";
    CloseableHttpResponse post = httpPost("/notebook/", jsonRequest);
    String postResponse = EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8);
    LOG.info("testNoteCreate \n" + postResponse);
    assertThat("test note create method:", post, isAllowed());
    Map<String, Object> resp = gson.fromJson(postResponse, new TypeToken<Map<String, Object>>() {
    }.getType());
    String newNoteId = (String) resp.get("body");
    LOG.info("newNoteId:=" + newNoteId);
    TestUtils.getInstance(Notebook.class).processNote(newNoteId, newNote -> {
        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 noteNameTmp = noteName;
        if (StringUtils.isBlank(noteNameTmp)) {
            noteNameTmp = "Untitled Note";
        }
        assertEquals("compare note name", noteNameTmp, newNoteName);
        return null;
    });
    // cleanup
    TestUtils.getInstance(Notebook.class).removeNote(newNoteId, anonymous);
    post.close();
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 23 with Notebook

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

the class ZeppelinRestApiTest method testCronDisable.

@Test
public void testCronDisable() throws Exception {
    String noteId = null;
    try {
        // create a note and a paragraph
        System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_CRON_ENABLE.getVarName(), "false");
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testCronDisable", anonymous);
        // use write lock because Name is overwritten
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            note.setName("note for run test");
            Paragraph paragraph = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            paragraph.setText("%md This is test paragraph.");
            Map<String, Object> config = paragraph.getConfig();
            config.put("enabled", true);
            paragraph.setConfig(config);
            return null;
        });
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            try {
                note.runAll(AuthenticationInfo.ANONYMOUS, true, true, new HashMap<>());
            } catch (Exception e) {
                fail();
            }
            return null;
        });
        String jsonRequest = "{\"cron\":\"* * * * * ?\" }";
        // right cron expression.
        CloseableHttpResponse postCron = httpPost("/notebook/cron/" + noteId, jsonRequest);
        assertThat("", postCron, isForbidden());
        postCron.close();
        System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_CRON_ENABLE.getVarName(), "true");
        System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_CRON_FOLDERS.getVarName(), "/System");
        // use write lock, because Name is overwritten
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            note.setName("System/test2");
            return null;
        });
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            try {
                note.runAll(AuthenticationInfo.ANONYMOUS, true, true, new HashMap<>());
            } catch (Exception e) {
                fail();
            }
            return null;
        });
        postCron = httpPost("/notebook/cron/" + noteId, jsonRequest);
        assertThat("", postCron, isAllowed());
        postCron.close();
        Thread.sleep(1000);
        // remove cron job.
        CloseableHttpResponse deleteCron = httpDelete("/notebook/cron/" + noteId);
        assertThat("", deleteCron, isAllowed());
        deleteCron.close();
        Thread.sleep(1000);
        System.clearProperty(ConfVars.ZEPPELIN_NOTEBOOK_CRON_FOLDERS.getVarName());
    } finally {
        // cleanup
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        }
        System.clearProperty(ConfVars.ZEPPELIN_NOTEBOOK_CRON_ENABLE.getVarName());
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 24 with Notebook

use of org.apache.zeppelin.notebook.Notebook 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\"}," + "{\"title\": \"titleConfig\", \"text\": \"text3\", " + "\"config\": {\"colWidth\": 9.0, \"title\": true, " + "\"results\": [{\"graph\": {\"mode\": \"pieChart\"}}] " + "}}]} ";
    CloseableHttpResponse post = httpPost("/notebook/", jsonRequest);
    String postResponse = EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8);
    LOG.info("testNoteCreate \n" + postResponse);
    assertThat("test note create method:", post, isAllowed());
    Map<String, Object> resp = gson.fromJson(postResponse, new TypeToken<Map<String, Object>>() {
    }.getType());
    String newNoteId = (String) resp.get("body");
    LOG.info("newNoteId:=" + newNoteId);
    TestUtils.getInstance(Notebook.class).processNote(newNoteId, newNote -> {
        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"));
            if (p.getTitle().equals("titleConfig")) {
                assertEquals("paragraph col width check failed", 9.0, p.getConfig().get("colWidth"));
                assertTrue("paragraph show title check failed", ((boolean) p.getConfig().get("title")));
                Map graph = ((List<Map>) p.getConfig().get("results")).get(0);
                String mode = ((Map) graph.get("graph")).get("mode").toString();
                assertEquals("paragraph graph mode check failed", "pieChart", mode);
            }
        }
        return null;
    });
    // cleanup
    TestUtils.getInstance(Notebook.class).removeNote(newNoteId, anonymous);
    post.close();
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 25 with Notebook

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

the class ZeppelinRestApiTest method testCloneNote.

@Test
public void testCloneNote() throws IOException, IllegalArgumentException {
    LOG.info("testCloneNote");
    String noteId = null;
    String newNoteId = null;
    try {
        // Create note to clone
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testCloneNote", anonymous);
        int paragraphSize = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            assertNotNull("can't create new note", note);
            note.setName("source note for clone");
            Paragraph paragraph = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Map<String, Object> config = paragraph.getConfig();
            config.put("enabled", true);
            paragraph.setConfig(config);
            paragraph.setText("%md This is my new paragraph in my new note");
            TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
            return note.getParagraphs().size();
        });
        String noteName = "clone Note Name";
        // Call Clone Note REST API
        String jsonRequest = "{\"name\":\"" + noteName + "\"}";
        CloseableHttpResponse post = httpPost("/notebook/" + noteId, jsonRequest);
        String postResponse = EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8);
        LOG.info("testNoteClone \n" + postResponse);
        assertThat("test note clone method:", post, isAllowed());
        Map<String, Object> resp = gson.fromJson(postResponse, new TypeToken<Map<String, Object>>() {
        }.getType());
        newNoteId = (String) resp.get("body");
        LOG.info("newNoteId:=" + newNoteId);
        TestUtils.getInstance(Notebook.class).processNote(newNoteId, newNote -> {
            assertNotNull("Can not find new note by id", newNote);
            assertEquals("Compare note names", noteName, newNote.getName());
            assertEquals("Compare paragraphs count", paragraphSize, newNote.getParagraphs().size());
            return null;
        });
        post.close();
    } finally {
        // cleanup
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        }
        TestUtils.getInstance(Notebook.class).removeNote(newNoteId, anonymous);
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Aggregations

Notebook (org.apache.zeppelin.notebook.Notebook)79 Test (org.junit.Test)53 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)48 Paragraph (org.apache.zeppelin.notebook.Paragraph)43 TypeToken (com.google.gson.reflect.TypeToken)33 Map (java.util.Map)25 HashMap (java.util.HashMap)24 Note (org.apache.zeppelin.notebook.Note)20 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)12 AuthorizationService (org.apache.zeppelin.notebook.AuthorizationService)12 Before (org.junit.Before)12 ZeppelinConfiguration (org.apache.zeppelin.conf.ZeppelinConfiguration)11 List (java.util.List)10 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)10 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)9 BeforeClass (org.junit.BeforeClass)9 InterpreterSettingManager (org.apache.zeppelin.interpreter.InterpreterSettingManager)8 InterpreterGroup (org.apache.zeppelin.interpreter.InterpreterGroup)7 RemoteAngularObjectRegistry (org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry)7