Search in sources :

Example 56 with Notebook

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

the class ZeppelinRestApiTest method testDeleteParagraph.

@Test
public void testDeleteParagraph() throws IOException {
    String noteId = null;
    try {
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testDeleteParagraph", anonymous);
        Paragraph p = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            Paragraph ptmp = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            ptmp.setTitle("title1");
            ptmp.setText("text1");
            TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
            return ptmp;
        });
        CloseableHttpResponse delete = httpDelete("/notebook/" + noteId + "/paragraph/" + p.getId());
        assertThat("Test delete method: ", delete, isAllowed());
        delete.close();
        TestUtils.getInstance(Notebook.class).processNote(noteId, retrNote -> {
            Paragraph retrParagrah = retrNote.getParagraph(p.getId());
            assertNull("paragraph should be deleted", retrParagrah);
            return null;
        });
    } 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) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 57 with Notebook

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

the class ZeppelinRestApiTest method testGetParagraph.

@Test
public void testGetParagraph() throws IOException {
    String noteId = null;
    try {
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testGetParagraph", anonymous);
        String pId = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p.setTitle("hello");
            p.setText("world");
            TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
            return p.getId();
        });
        CloseableHttpResponse get = httpGet("/notebook/" + noteId + "/paragraph/" + pId);
        String getResponse = EntityUtils.toString(get.getEntity(), StandardCharsets.UTF_8);
        LOG.info("testGetParagraph response\n" + getResponse);
        assertThat("Test get method: ", get, isAllowed());
        get.close();
        Map<String, Object> resp = gson.fromJson(getResponse, new TypeToken<Map<String, Object>>() {
        }.getType());
        assertNotNull(resp);
        assertEquals("OK", resp.get("status"));
        Map<String, Object> body = (Map<String, Object>) resp.get("body");
        assertEquals(pId, body.get("id"));
        assertEquals("hello", body.get("title"));
        assertEquals("world", body.get("text"));
    } 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) HashMap(java.util.HashMap) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 58 with Notebook

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

the class ZeppelinRestApiTest method testNoteJobs.

@Test
public void testNoteJobs() throws Exception {
    LOG.info("testNoteJobs");
    String noteId = null;
    try {
        // Create note to run test.
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testNoteJobs", anonymous);
        // use write lock because name is overwritten
        String paragraphId = 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("%md This is test paragraph.");
            TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
            return paragraph.getId();
        });
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            try {
                note.runAll(anonymous, true, false, new HashMap<>());
            } catch (Exception e) {
                fail();
            }
            return null;
        });
        // 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(1000);
            terminated = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> note.getParagraph(0).isTerminated());
            if (timeout++ > 10) {
                LOG.info("testNoteJobs timeout job.");
                break;
            }
        }
        // Call Run note jobs REST API
        CloseableHttpResponse postNoteJobs = httpPost("/notebook/job/" + noteId + "?blocking=true", "");
        assertThat("test note jobs run:", postNoteJobs, isAllowed());
        postNoteJobs.close();
        // Call Stop note jobs REST API
        CloseableHttpResponse deleteNoteJobs = httpDelete("/notebook/job/" + noteId);
        assertThat("test note stop:", deleteNoteJobs, isAllowed());
        deleteNoteJobs.close();
        Thread.sleep(1000);
        // Call Run paragraph REST API
        CloseableHttpResponse postParagraph = httpPost("/notebook/job/" + noteId + "/" + paragraphId, "");
        assertThat("test paragraph run:", postParagraph, isAllowed());
        postParagraph.close();
        Thread.sleep(1000);
        // Call Stop paragraph REST API
        CloseableHttpResponse deleteParagraph = httpDelete("/notebook/job/" + noteId + "/" + paragraphId);
        assertThat("test paragraph stop:", deleteParagraph, isAllowed());
        deleteParagraph.close();
        Thread.sleep(1000);
    } 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) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 59 with Notebook

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

the class ZeppelinRestApiTest method testImportNotebook.

@Test
public void testImportNotebook() throws IOException {
    String noteId = null;
    Map<String, Object> resp;
    String oldJson;
    String noteName;
    String importId = null;
    try {
        noteName = "source note for import";
        LOG.info("testImportNote");
        // create test note
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testImportNotebook", anonymous);
        // use write lock because name is overwritten
        int paragraphSize = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            assertNotNull("can't create new note", note);
            note.setName(noteName);
            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();
        });
        // get note content as JSON
        oldJson = getNoteContent(noteId);
        // delete it first then import it
        TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        // call note post
        CloseableHttpResponse importPost = httpPost("/notebook/import/", oldJson);
        assertThat(importPost, isAllowed());
        resp = gson.fromJson(EntityUtils.toString(importPost.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        importId = (String) resp.get("body");
        assertNotNull("Did not get back a note id in body", importId);
        TestUtils.getInstance(Notebook.class).processNote(importId, newNote -> {
            assertEquals("Compare note names", noteName, newNote.getName());
            assertEquals("Compare paragraphs count", paragraphSize, newNote.getParagraphs().size());
            return null;
        });
        importPost.close();
    } finally {
        if (null != noteId) {
            TestUtils.getInstance(Notebook.class).removeNote(noteId, anonymous);
        }
        TestUtils.getInstance(Notebook.class).removeNote(importId, anonymous);
    }
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HashMap(java.util.HashMap) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 60 with Notebook

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

the class ZeppelinRestApiTest method testTitleSearch.

@Test
public void testTitleSearch() throws IOException, InterruptedException {
    String noteId = null;
    try {
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testTitleSearch", anonymous);
        String jsonRequest = "{\"title\": \"testTitleSearchOfParagraph\", " + "\"text\": \"ThisIsToTestSearchMethodWithTitle \"}";
        CloseableHttpResponse postNoteText = httpPost("/notebook/" + noteId + "/paragraph", jsonRequest);
        postNoteText.close();
        Thread.sleep(3000);
        CloseableHttpResponse searchNote = httpGet("/notebook/search?q='testTitleSearchOfParagraph'");
        Map<String, Object> respSearchResult = gson.fromJson(EntityUtils.toString(searchNote.getEntity(), StandardCharsets.UTF_8), new TypeToken<Map<String, Object>>() {
        }.getType());
        ArrayList searchBody = (ArrayList) respSearchResult.get("body");
        int numberOfTitleHits = 0;
        for (int i = 0; i < searchBody.size(); i++) {
            Map<String, String> searchResult = (Map<String, String>) searchBody.get(i);
            if (searchResult.get("header").contains("testTitleSearchOfParagraph")) {
                numberOfTitleHits++;
            }
        }
        assertEquals("Paragraph title hits must be at-least one", true, numberOfTitleHits >= 1);
        searchNote.close();
    } 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) ArrayList(java.util.ArrayList) 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