Search in sources :

Example 26 with Notebook

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

the class ZeppelinRestApiTest method testGetNoteInfo.

@Test
public void testGetNoteInfo() throws IOException {
    LOG.info("testGetNoteInfo");
    String noteId = null;
    try {
        // Create note to get info
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
        assertNotNull("can't create new note", noteId);
        // use write lock because name is overwritten
        String paragraphText = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            note.setName("note");
            Paragraph paragraph = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            Map<String, Object> config = paragraph.getConfig();
            config.put("enabled", true);
            paragraph.setConfig(config);
            String paragraphTextTmp = "%md This is my new paragraph in my new note";
            paragraph.setText(paragraphTextTmp);
            TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
            return paragraphTextTmp;
        });
        CloseableHttpResponse get = httpGet("/notebook/" + noteId);
        String getResponse = EntityUtils.toString(get.getEntity(), StandardCharsets.UTF_8);
        LOG.info("testGetNoteInfo \n" + getResponse);
        assertThat("test note get method:", get, isAllowed());
        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");
        List<Map<String, Object>> paragraphs = (List<Map<String, Object>>) body.get("paragraphs");
        assertTrue(paragraphs.size() > 0);
        assertEquals(paragraphText, paragraphs.get(0).get("text"));
        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) 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 27 with Notebook

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

the class ZeppelinRestApiTest method testMoveParagraph.

@Test
public void testMoveParagraph() throws IOException {
    String noteId = null;
    try {
        noteId = TestUtils.getInstance(Notebook.class).createNote("note1_testMoveParagraph", anonymous);
        Paragraph p2 = TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p.setTitle("title1");
            p.setText("text1");
            Paragraph p2tmp = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p2tmp.setTitle("title2");
            p2tmp.setText("text2");
            TestUtils.getInstance(Notebook.class).saveNote(note, anonymous);
            return p2tmp;
        });
        CloseableHttpResponse post = httpPost("/notebook/" + noteId + "/paragraph/" + p2.getId() + "/move/" + 0, "");
        assertThat("Test post method: ", post, isAllowed());
        post.close();
        TestUtils.getInstance(Notebook.class).processNote(noteId, retrNote -> {
            Paragraph paragraphAtIdx0 = retrNote.getParagraphs().get(0);
            assertEquals(p2.getId(), paragraphAtIdx0.getId());
            assertEquals(p2.getTitle(), paragraphAtIdx0.getTitle());
            assertEquals(p2.getText(), paragraphAtIdx0.getText());
            return null;
        });
        CloseableHttpResponse post2 = httpPost("/notebook/" + noteId + "/paragraph/" + p2.getId() + "/move/" + 10, "");
        assertThat("Test post method: ", post2, isBadRequest());
        post.close();
    } 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 28 with Notebook

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

the class ClusterEventTest method init.

@BeforeClass
public static void init() throws Exception {
    ZeppelinConfiguration zconf = genZeppelinConf();
    ZeppelinServerMock.startUp("ClusterEventTest", zconf);
    notebook = TestUtils.getInstance(Notebook.class);
    authorizationService = TestUtils.getInstance(AuthorizationService.class);
    schedulerService = new QuartzSchedulerService(zconf, notebook);
    schedulerService.waitForFinishInit();
    notebookServer = spy(NotebookServer.getInstance());
    notebookService = new NotebookService(notebook, authorizationService, zconf, schedulerService);
    ConfigurationService configurationService = new ConfigurationService(notebook.getConf());
    when(notebookServer.getNotebookService()).thenReturn(notebookService);
    when(notebookServer.getConfigurationService()).thenReturn(configurationService);
    startOtherZeppelinClusterNode(zconf);
    // wait zeppelin cluster startup
    Thread.sleep(10000);
    // mock cluster manager client
    clusterClient = ClusterManagerClient.getInstance(zconf);
    clusterClient.start(metaKey);
    // Waiting for cluster startup
    int wait = 0;
    while (wait++ < 100) {
        if (clusterIsStartup() && clusterClient.raftInitialized()) {
            LOGGER.info("wait {}(ms) found cluster leader", wait * 500);
            break;
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    Thread.sleep(3000);
    assertEquals(true, clusterIsStartup());
    getClusterServerMeta();
}
Also used : Notebook(org.apache.zeppelin.notebook.Notebook) AuthorizationService(org.apache.zeppelin.notebook.AuthorizationService) QuartzSchedulerService(org.apache.zeppelin.notebook.scheduler.QuartzSchedulerService) ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) ConfigurationService(org.apache.zeppelin.service.ConfigurationService) NotebookService(org.apache.zeppelin.service.NotebookService) BeforeClass(org.junit.BeforeClass)

Example 29 with Notebook

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

the class ClusterEventTest method testRenameNoteEvent.

@Test
public void testRenameNoteEvent() throws IOException {
    String noteId = null;
    try {
        String oldName = "old_name";
        noteId = TestUtils.getInstance(Notebook.class).createNote(oldName, anonymous);
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            assertEquals(note.getName(), oldName);
            return null;
        });
        final String newName = "testName";
        String jsonRequest = "{\"name\": " + newName + "}";
        CloseableHttpResponse put = AbstractTestRestApi.httpPut("/notebook/" + noteId + "/rename/", jsonRequest);
        assertThat("test testRenameNote:", put, AbstractTestRestApi.isAllowed());
        put.close();
        TestUtils.getInstance(Notebook.class).processNote(noteId, note -> {
            assertEquals(note.getName(), newName);
            return null;
        });
        // wait cluster sync event
        Thread.sleep(1000);
        checkClusterNoteEventListener();
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    } 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 30 with Notebook

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

the class NotebookServerTest method testAngularObjectSaveToNote.

@Test
public void testAngularObjectSaveToNote() throws IOException, InterruptedException {
    // create a notebook
    String note1Id = null;
    try {
        note1Id = notebook.createNote("note1", "angular", anonymous);
        // get reference to interpreterGroup
        InterpreterGroup interpreterGroup = null;
        List<InterpreterSetting> settings = notebook.processNote(note1Id, note1 -> note1.getBindedInterpreterSettings(new ArrayList<>()));
        for (InterpreterSetting setting : settings) {
            if (setting.getName().equals("angular")) {
                interpreterGroup = setting.getOrCreateInterpreterGroup("anonymous", note1Id);
                break;
            }
        }
        String p1Id = notebook.processNote(note1Id, note1 -> {
            // start interpreter process
            Paragraph p1 = note1.addNewParagraph(AuthenticationInfo.ANONYMOUS);
            p1.setText("%angular <h2>Bind here : {{COMMAND_TYPE}}</h2>");
            p1.setAuthenticationInfo(anonymous);
            note1.run(p1.getId());
            return p1.getId();
        });
        // wait for paragraph finished
        Status status = notebook.processNote(note1Id, note1 -> note1.getParagraph(p1Id).getStatus());
        while (true) {
            if (status == Job.Status.FINISHED) {
                break;
            }
            Thread.sleep(100);
            status = notebook.processNote(note1Id, note1 -> note1.getParagraph(p1Id).getStatus());
        }
        // sleep for 1 second to make sure job running thread finish to fire event. See ZEPPELIN-3277
        Thread.sleep(1000);
        // create two sockets and open it
        NotebookSocket sock1 = createWebSocket();
        notebookServer.onOpen(sock1);
        // getNote, getAngularObject
        verify(sock1, times(0)).send(anyString());
        // open the same notebook from sockets
        notebookServer.onMessage(sock1, new Message(OP.GET_NOTE).put("id", note1Id).toJson());
        reset(sock1);
        // bind object from sock1
        notebookServer.onMessage(sock1, new Message(OP.ANGULAR_OBJECT_CLIENT_BIND).put("noteId", note1Id).put("paragraphId", p1Id).put("name", "COMMAND_TYPE").put("value", "COMMAND_TYPE_VALUE").put("interpreterGroupId", interpreterGroup.getId()).toJson());
        List<AngularObject> list = notebook.processNote(note1Id, note1 -> note1.getAngularObjects("angular-shared_process"));
        assertEquals(1, list.size());
        assertEquals(note1Id, list.get(0).getNoteId());
        assertEquals(p1Id, list.get(0).getParagraphId());
        assertEquals("COMMAND_TYPE", list.get(0).getName());
        assertEquals("COMMAND_TYPE_VALUE", list.get(0).get());
        // Check if the interpreterGroup AngularObjectRegistry is updated
        Map<String, Map<String, AngularObject>> mapRegistry = interpreterGroup.getAngularObjectRegistry().getRegistry();
        AngularObject ao = mapRegistry.get(note1Id + "_" + p1Id).get("COMMAND_TYPE");
        assertEquals("COMMAND_TYPE", ao.getName());
        assertEquals("COMMAND_TYPE_VALUE", ao.get());
        // update bind object from sock1
        notebookServer.onMessage(sock1, new Message(OP.ANGULAR_OBJECT_UPDATED).put("noteId", note1Id).put("paragraphId", p1Id).put("name", "COMMAND_TYPE").put("value", "COMMAND_TYPE_VALUE_UPDATE").put("interpreterGroupId", interpreterGroup.getId()).toJson());
        list = notebook.processNote(note1Id, note1 -> note1.getAngularObjects("angular-shared_process"));
        assertEquals(1, list.size());
        assertEquals(note1Id, list.get(0).getNoteId());
        assertEquals(p1Id, list.get(0).getParagraphId());
        assertEquals("COMMAND_TYPE", list.get(0).getName());
        assertEquals("COMMAND_TYPE_VALUE_UPDATE", list.get(0).get());
        // Check if the interpreterGroup AngularObjectRegistry is updated
        mapRegistry = interpreterGroup.getAngularObjectRegistry().getRegistry();
        AngularObject ao1 = mapRegistry.get(note1Id + "_" + p1Id).get("COMMAND_TYPE");
        assertEquals("COMMAND_TYPE", ao1.getName());
        assertEquals("COMMAND_TYPE_VALUE_UPDATE", ao1.get());
        // unbind object from sock1
        notebookServer.onMessage(sock1, new Message(OP.ANGULAR_OBJECT_CLIENT_UNBIND).put("noteId", note1Id).put("paragraphId", p1Id).put("name", "COMMAND_TYPE").put("value", "COMMAND_TYPE_VALUE").put("interpreterGroupId", interpreterGroup.getId()).toJson());
        list = notebook.processNote(note1Id, note1 -> note1.getAngularObjects("angular-shared_process"));
        assertEquals(0, list.size());
        // Check if the interpreterGroup AngularObjectRegistry is delete
        mapRegistry = interpreterGroup.getAngularObjectRegistry().getRegistry();
        AngularObject ao2 = mapRegistry.get(note1Id + "_" + p1Id).get("COMMAND_TYPE");
        assertNull(ao2);
    } finally {
        if (note1Id != null) {
            notebook.removeNote(note1Id, anonymous);
        }
    }
}
Also used : Status(org.apache.zeppelin.scheduler.Job.Status) ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) TestUtils(org.apache.zeppelin.utils.TestUtils) Arrays(java.util.Arrays) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) InetAddress(java.net.InetAddress) ServiceContext(org.apache.zeppelin.service.ServiceContext) ParagraphInfo(org.apache.zeppelin.interpreter.thrift.ParagraphInfo) Arrays.asList(java.util.Arrays.asList) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Job(org.apache.zeppelin.scheduler.Job) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) AuthorizationService(org.apache.zeppelin.notebook.AuthorizationService) NotebookRepoWithVersionControl(org.apache.zeppelin.notebook.repo.NotebookRepoWithVersionControl) AngularObjectBuilder(org.apache.zeppelin.display.AngularObjectBuilder) AngularObject(org.apache.zeppelin.display.AngularObject) AfterClass(org.junit.AfterClass) NoteProcessor(org.apache.zeppelin.notebook.Notebook.NoteProcessor) NoteInfo(org.apache.zeppelin.notebook.NoteInfo) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) RemoteAngularObjectRegistry(org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Status(org.apache.zeppelin.scheduler.Job.Status) Mockito.mock(org.mockito.Mockito.mock) BeforeClass(org.junit.BeforeClass) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HttpServletRequest(javax.servlet.http.HttpServletRequest) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) Message(org.apache.zeppelin.common.Message) Mockito.anyString(org.mockito.Mockito.anyString) OP(org.apache.zeppelin.common.Message.OP) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) Before(org.junit.Before) Paragraph(org.apache.zeppelin.notebook.Paragraph) NotebookService(org.apache.zeppelin.service.NotebookService) Assert.assertNotNull(org.junit.Assert.assertNotNull) Note(org.apache.zeppelin.notebook.Note) Assert.assertTrue(org.junit.Assert.assertTrue) TException(org.apache.thrift.TException) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UnknownHostException(java.net.UnknownHostException) Notebook(org.apache.zeppelin.notebook.Notebook) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ServiceException(org.apache.zeppelin.interpreter.thrift.ServiceException) AbstractTestRestApi(org.apache.zeppelin.rest.AbstractTestRestApi) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) Mockito.reset(org.mockito.Mockito.reset) Assert.assertEquals(org.junit.Assert.assertEquals) Message(org.apache.zeppelin.common.Message) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) ArrayList(java.util.ArrayList) AngularObject(org.apache.zeppelin.display.AngularObject) Mockito.anyString(org.mockito.Mockito.anyString) Paragraph(org.apache.zeppelin.notebook.Paragraph) InterpreterGroup(org.apache.zeppelin.interpreter.InterpreterGroup) 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