Search in sources :

Example 61 with Notebook

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

the class ZeppelinRestApiTest method testUpdateParagraph.

@Test
public void testUpdateParagraph() throws IOException {
    String noteId = null;
    try {
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testUpdateParagraph", anonymous);
        String jsonRequest = "{\"title\": \"title1\", \"text\": \"text1\"}";
        CloseableHttpResponse post = httpPost("/notebook/" + noteId + "/paragraph", jsonRequest);
        Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        post.close();
        String newParagraphId = (String) resp.get("body");
        TestUtils.getInstance(Notebook.class).processNote(noteId, noteP -> {
            Paragraph newParagraph = noteP.getParagraph(newParagraphId);
            assertEquals("title1", newParagraph.getTitle());
            assertEquals("text1", newParagraph.getText());
            return null;
        });
        String updateRequest = "{\"text\": \"updated text\"}";
        CloseableHttpResponse put = httpPut("/notebook/" + noteId + "/paragraph/" + newParagraphId, updateRequest);
        assertThat("Test update method:", put, isAllowed());
        put.close();
        TestUtils.getInstance(Notebook.class).processNote(noteId, noteP -> {
            Paragraph updatedParagraph = noteP.getParagraph(newParagraphId);
            assertEquals("title1", updatedParagraph.getTitle());
            assertEquals("updated text", updatedParagraph.getText());
            return null;
        });
        String updateBothRequest = "{\"title\": \"updated title\", \"text\" : \"updated text 2\" }";
        CloseableHttpResponse updatePut = httpPut("/notebook/" + noteId + "/paragraph/" + newParagraphId, updateBothRequest);
        updatePut.close();
        TestUtils.getInstance(Notebook.class).processNote(noteId, noteP -> {
            Paragraph updatedBothParagraph = noteP.getParagraph(newParagraphId);
            assertEquals("updated title", updatedBothParagraph.getTitle());
            assertEquals("updated text 2", updatedBothParagraph.getText());
            return null;
        });
    } 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) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 62 with Notebook

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

the class ZeppelinRestApiTest method testJobs.

@Test
public void testJobs() throws Exception {
    // create a note and a paragraph
    String noteId = null;
    try {
        System.setProperty(ConfVars.ZEPPELIN_NOTEBOOK_CRON_ENABLE.getVarName(), "true");
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testJobs", 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, false, false, new HashMap<>());
            } catch (Exception e) {
                fail();
            }
            return null;
        });
        String jsonRequest = "{\"cron\":\"* * * * * ?\" }";
        // right cron expression but not exist note.
        CloseableHttpResponse postCron = httpPost("/notebook/cron/notexistnote", jsonRequest);
        assertThat("", postCron, isNotFound());
        postCron.close();
        // right cron expression.
        postCron = httpPost("/notebook/cron/" + noteId, jsonRequest);
        assertThat("", postCron, isAllowed());
        postCron.close();
        Thread.sleep(1000);
        // wrong cron expression.
        jsonRequest = "{\"cron\":\"a * * * * ?\" }";
        postCron = httpPost("/notebook/cron/" + noteId, jsonRequest);
        assertThat("", postCron, isBadRequest());
        postCron.close();
        Thread.sleep(1000);
        // remove cron job.
        CloseableHttpResponse deleteCron = httpDelete("/notebook/cron/" + noteId);
        assertThat("", deleteCron, isAllowed());
        deleteCron.close();
    } 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 63 with Notebook

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

the class ZeppelinRestApiTest method testGetNoteJob.

@Test
public void testGetNoteJob() throws Exception {
    LOG.info("testGetNoteJob");
    String noteId = null;
    try {
        // Create note to run test.
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testGetNoteJob", anonymous);
        // use write lock because name is overwritten
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            assertNotNull("can't create new note", note);
            note.setName("note for run test");
            Paragraph paragraph = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Map<String, Object> config = paragraph.getConfig();
            config.put("enabled", true);
            paragraph.setConfig(config);
            paragraph.setText("%sh sleep 1");
            paragraph.setAuthenticationInfo(anonymous);
            TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
            return null;
        });
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            try {
                note.runAll(anonymous, true, false, new HashMap<>());
            } catch (Exception e) {
                fail();
            }
            return null;
        });
        // assume that status of the paragraph is running
        CloseableHttpResponse get = httpGet("/notebook/job/" + noteId);
        assertThat("test get note job: ", get, isAllowed());
        String responseBody = EntityUtils.toString(get.getEntity(), StandardCharsets.UTF_8);
        get.close();
        LOG.info("test get note job: \n" + responseBody);
        Map<String, Object> resp = gson.fromJson(responseBody, new TypeToken<Map<String, Object>>() {
        }.getType());
        NoteJobStatus noteJobStatus = NoteJobStatus.fromJson(gson.toJson(resp.get("body")));
        assertEquals(1, noteJobStatus.getParagraphJobStatusList().size());
        int progress = Integer.parseInt(noteJobStatus.getParagraphJobStatusList().get(0).getProgress());
        assertTrue(progress >= 0 && progress <= 100);
        // wait until job is finished or timeout.
        int timeout = 1;
        boolean terminated = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> note.getParagraph(0).isTerminated());
        while (!terminated) {
            Thread.sleep(100);
            terminated = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> note.getParagraph(0).isTerminated());
            if (timeout++ > 10) {
                LOG.info("testGetNoteJob timeout job.");
                break;
            }
        }
    } finally {
        // cleanup
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        }
    }
}
Also used : MethodSorters(org.junit.runners.MethodSorters) TestUtils(org.apache.zeppelin.utils.TestUtils) Arrays(java.util.Arrays) TypeToken(com.google.gson.reflect.TypeToken) BeforeClass(org.junit.BeforeClass) NoteJobStatus(org.apache.zeppelin.rest.message.NoteJobStatus) HashMap(java.util.HashMap) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) StringUtils(org.apache.commons.lang3.StringUtils) EntityUtils(org.apache.http.util.EntityUtils) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) HashSet(java.util.HashSet) Gson(com.google.gson.Gson) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Map(java.util.Map) ConfVars(org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars) AuthorizationService(org.apache.zeppelin.notebook.AuthorizationService) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) Paragraph(org.apache.zeppelin.notebook.Paragraph) AfterClass(org.junit.AfterClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) Note(org.apache.zeppelin.notebook.Note) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Notebook(org.apache.zeppelin.notebook.Notebook) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) FixMethodOrder(org.junit.FixMethodOrder) Assert.assertEquals(org.junit.Assert.assertEquals) Notebook(org.apache.zeppelin.notebook.Notebook) IOException(java.io.IOException) Paragraph(org.apache.zeppelin.notebook.Paragraph) NoteJobStatus(org.apache.zeppelin.rest.message.NoteJobStatus) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 64 with Notebook

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

the class ZeppelinRestApiTest method testExportNote.

@Test
public void testExportNote() throws IOException {
    LOG.info("testExportNote");
    String noteId = null;
    try {
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testExportNote", anonymous);
        // use write lock because name is overwritten
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            assertNotNull("can't create new note", note);
            note.setName("source note for export");
            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 null;
        });
        // Call export Note REST API
        CloseableHttpResponse get = httpGet("/notebook/export/" + noteId);
        String getResponse = EntityUtils.toString(get.getEntity(), StandardCharsets.UTF_8);
        LOG.info("testNoteExport \n" + getResponse);
        assertThat("test note export method:", get, isAllowed());
        Map<String, Object> resp = gson.fromJson(getResponse, new TypeToken<Map<String, Object>>() {
        }.getType());
        String exportJSON = (String) resp.get("body");
        assertNotNull("Can not find new notejson", exportJSON);
        LOG.info("export JSON:=" + exportJSON);
        get.close();
    } finally {
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, 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)

Example 65 with Notebook

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

the class NotebookSecurityRestApiTest method deleteNoteForUser.

private void deleteNoteForUser(String noteId, String user, String pwd) throws IOException {
    CloseableHttpResponse delete = httpDelete(("/notebook/" + noteId), user, pwd);
    assertThat("Test delete method:", delete, isAllowed());
    delete.close();
    // make sure note is deleted
    if (!noteId.isEmpty()) {
        TestUtils.getInstance(Notebook.class).processNote(noteId, deletedNote -> {
            assertNull("Deleted note should be null", deletedNote);
            return null;
        });
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

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