Search in sources :

Example 66 with Notebook

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

the class NotebookSecurityRestApiTest method testThatWriterCannotRemoveNote.

@Test
public void testThatWriterCannotRemoveNote() throws IOException {
    String noteId = createNoteForUser("test_4", "admin", "password1");
    // set permission
    String payload = "{ \"owners\": [\"admin\", \"user1\"], \"readers\": [\"user2\"], " + "\"runners\": [\"user2\"], \"writers\": [\"user2\"] }";
    CloseableHttpResponse put = httpPut("/notebook/" + noteId + "/permissions", payload, "admin", "password1");
    assertThat("test set note permission method:", put, isAllowed());
    put.close();
    userTryRemoveNote(noteId, "user2", "password3", isForbidden());
    userTryRemoveNote(noteId, "user1", "password2", isAllowed());
    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) Test(org.junit.Test)

Example 67 with Notebook

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

the class NotebookSecurityRestApiTest method createNoteForUser.

private String createNoteForUser(String noteName, String user, String pwd) throws IOException {
    String jsonRequest = "{\"name\":\"" + noteName + "\"}";
    CloseableHttpResponse post = httpPost("/notebook/", jsonRequest, user, pwd);
    assertThat("test note create method:", post, isAllowed());
    Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
    }.getType());
    post.close();
    String newNoteId = (String) resp.get("body");
    Notebook notebook = TestUtils.getInstance(Notebook.class);
    notebook.processNote(newNoteId, newNote -> {
        assertNotNull("Can not find new note by id", newNote);
        return null;
    });
    return newNoteId;
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 68 with Notebook

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

the class NotebookRestApiTest method testRunNoteNonBlocking_Isolated.

@Test
public void testRunNoteNonBlocking_Isolated() throws IOException, InterruptedException {
    LOG.info("Running testRunNoteNonBlocking_Isolated");
    String note1Id = null;
    try {
        InterpreterSettingManager interpreterSettingManager = TestUtils.getInstance(InterpreterSettingManager.class);
        InterpreterSetting interpreterSetting = interpreterSettingManager.getInterpreterSettingByName("python");
        int pythonProcessNum = interpreterSetting.getAllInterpreterGroups().size();
        note1Id = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
        // 2 paragraphs
        // P1:
        // %python
        // from __future__ import print_function
        // import time
        // time.sleep(1)
        // user='abc'
        // P2:
        // %python
        // print(user)
        // 
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Paragraph p2 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p1.setText("%python from __future__ import print_function\nimport time\ntime.sleep(1)\nuser='abc'");
            p2.setText("%python print(user)");
            return null;
        });
        CloseableHttpResponse post = httpPost("/notebook/job/" + note1Id + "?blocking=false&isolated=true", "");
        assertThat(post, isAllowed());
        Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        assertEquals("OK", resp.get("status"));
        post.close();
        // wait for all the paragraphs are done
        boolean isRunning = TestUtils.getInstance(Notebook.class).processNote(note1Id, Note::isRunning);
        while (isRunning) {
            Thread.sleep(1000);
            isRunning = TestUtils.getInstance(Notebook.class).processNote(note1Id, Note::isRunning);
        }
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            Paragraph p1 = note1.getParagraph(0);
            Paragraph p2 = note1.getParagraph(1);
            assertEquals(Job.Status.FINISHED, p1.getStatus());
            assertEquals(Job.Status.FINISHED, p2.getStatus());
            assertEquals("abc\n", p2.getReturn().message().get(0).getData());
            return null;
        });
        // no new python process is created because it is isolated mode.
        assertEquals(pythonProcessNum, interpreterSetting.getAllInterpreterGroups().size());
    } finally {
        // cleanup
        if (null != note1Id) {
            TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) Note(org.apache.zeppelin.notebook.Note) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InterpreterSettingManager(org.apache.zeppelin.interpreter.InterpreterSettingManager) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 69 with Notebook

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

the class NotebookRestApiTest method testRenameNote.

@Test
public void testRenameNote() throws IOException {
    LOG.info("Running testRenameNote");
    String noteId = null;
    try {
        String oldName = "old_name";
        noteId = TestUtils.getInstance(Notebook.class).createNote(oldName, anonymous);
        assertEquals(oldName, TestUtils.getInstance(Notebook.class).processNote(noteId, Note::getName));
        final String newName = "testName";
        String jsonRequest = "{\"name\": " + newName + "}";
        CloseableHttpResponse put = httpPut("/notebook/" + noteId + "/rename/", jsonRequest);
        assertThat("test testRenameNote:", put, isAllowed());
        put.close();
        assertEquals(newName, TestUtils.getInstance(Notebook.class).processNote(noteId, Note::getName));
    } finally {
        // cleanup
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 70 with Notebook

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

the class NotebookRestApiTest method testGetNoteByPath.

@Test
public void testGetNoteByPath() throws IOException {
    LOG.info("Running testGetNoteByPath");
    String note1Id = null;
    try {
        String notePath = "dir1/note1";
        note1Id = TestUtils.getInstance(Notebook.class).createNote(notePath, anonymous);
        TestUtils.getInstance(Notebook.class).processNote(note1Id, note1 -> {
            note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            TestUtils.getInstance(Notebook.class).saveNote(note1, anonymous);
            return null;
        });
        CloseableHttpResponse post = httpPost("/notebook/getByPath", "{\"notePath\":\"" + notePath + "\"}");
        assertThat(post, isAllowed());
        Map<String, Object> resp = gson.fromJson(EntityUtils.toString(post.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        Map<String, Object> noteObject = (Map<String, Object>) resp.get("body");
        assertEquals(notePath, ((String) noteObject.get("path")).substring(1));
        post.close();
    } finally {
        // cleanup
        if (null != note1Id) {
            TestUtils.getInstance(Notebook.class).removeNote(note1Id, anonymous);
        }
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HashMap(java.util.HashMap) Map(java.util.Map) 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