Search in sources :

Example 1 with Page

use of org.knime.core.webui.page.Page in project knime-core by knime.

the class NodeViewEntTest method testNodeViewEnt.

/**
 * Tests {@link NodeViewEnt#NodeViewEnt(NativeNodeContainer)}.
 *
 * @throws IOException
 */
@Test
public void testNodeViewEnt() throws IOException {
    var wfm = WorkflowManagerUtil.createEmptyWorkflow();
    NativeNodeContainer nncWithoutNodeView = WorkflowManagerUtil.createAndAddNode(wfm, new VirtualSubNodeInputNodeFactory(null, new PortType[0]));
    Assert.assertThrows(IllegalArgumentException.class, () -> new NodeViewEnt(nncWithoutNodeView));
    Function<NodeViewNodeModel, NodeView> nodeViewCreator = m -> {
        Page p = Page.builder(() -> "blub", "index.html").build();
        return NodeViewTest.createNodeView(p, new TextInitialDataService() {

            @Override
            public String getInitialData() {
                return "dummy initial data";
            }
        }, null, null);
    };
    NativeNodeContainer nnc = WorkflowManagerUtil.createAndAddNode(wfm, new NodeViewNodeFactory(nodeViewCreator));
    wfm.executeAllAndWaitUntilDone();
    nnc.setNodeMessage(NodeMessage.newWarning("node message"));
    nnc.getNodeAnnotation().getData().setText("node annotation");
    var ent = new NodeViewEnt(nnc, null);
    assertThat(ent.getProjectId(), startsWith("workflow"));
    assertThat(ent.getWorkflowId(), is("root"));
    assertThat(ent.getNodeId(), is("root:2"));
    assertThat(ent.getInitialData(), is("dummy initial data"));
    assertThat(ent.getInitialSelection(), is(nullValue()));
    var resourceInfo = ent.getResourceInfo();
    assertThat(resourceInfo.getUrl(), endsWith("index.html"));
    assertThat(resourceInfo.getPath(), is(nullValue()));
    assertThat(resourceInfo.getType(), is(Resource.Type.HTML.toString()));
    assertThat(resourceInfo.getId(), is(PageUtil.getPageId(nnc, false, false)));
    var nodeInfo = ent.getNodeInfo();
    assertThat(nodeInfo.getNodeName(), is("NodeView"));
    assertThat(nodeInfo.getNodeAnnotation(), is("node annotation"));
    assertThat(nodeInfo.getNodeState(), is("executed"));
    assertThat(nodeInfo.getNodeWarnMessage(), is("node message"));
    assertThat(nodeInfo.getNodeErrorMessage(), is(nullValue()));
    // a node view as a 'component' without initial data
    nodeViewCreator = m -> {
        Page p = Page.builder(PageTest.BUNDLE_ID, "files", "component.umd.min.js").build();
        return NodeViewTest.createNodeView(p);
    };
    nnc = WorkflowManagerUtil.createAndAddNode(wfm, new NodeViewNodeFactory(nodeViewCreator));
    wfm.executeAllAndWaitUntilDone();
    ent = new NodeViewEnt(nnc, null);
    resourceInfo = ent.getResourceInfo();
    assertThat(ent.getInitialData(), is(nullValue()));
    assertThat(resourceInfo.getType(), is(Resource.Type.VUE_COMPONENT_LIB.toString()));
    assertThat(resourceInfo.getUrl(), endsWith("component.umd.min.js"));
    assertThat(resourceInfo.getPath(), is(nullValue()));
    // test to create a node view entity while running headless (e.g. on the executor)
    NativeNodeContainer nnc2 = nnc;
    runOnExecutor(() -> {
        var ent2 = new NodeViewEnt(nnc2, null);
        assertThat(ent2.getResourceInfo().getPath(), endsWith("component.umd.min.js"));
        assertThat(ent2.getResourceInfo().getUrl(), is(nullValue()));
    });
    WorkflowManagerUtil.disposeWorkflow(wfm);
}
Also used : NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) NodeMessage(org.knime.core.node.workflow.NodeMessage) VirtualSubNodeInputNodeFactory(org.knime.core.node.workflow.virtual.subnode.VirtualSubNodeInputNodeFactory) NodeView(org.knime.core.webui.node.view.NodeView) SelectionEventMode(org.knime.gateway.impl.service.events.SelectionEventSource.SelectionEventMode) RowKey(org.knime.core.data.RowKey) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Resource(org.knime.core.webui.page.Resource) SelectionEventSource(org.knime.gateway.impl.service.events.SelectionEventSource) Page(org.knime.core.webui.page.Page) Function(java.util.function.Function) PageUtil(org.knime.core.webui.page.PageUtil) SelectionEvent(org.knime.gateway.impl.service.events.SelectionEvent) WorkflowManagerUtil(org.knime.testing.util.WorkflowManagerUtil) Matchers.nullValue(org.hamcrest.Matchers.nullValue) BiConsumer(java.util.function.BiConsumer) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ONE_HUNDRED_MILLISECONDS(org.awaitility.Duration.ONE_HUNDRED_MILLISECONDS) PortType(org.knime.core.node.port.PortType) NodeViewNodeFactory(org.knime.testing.node.view.NodeViewNodeFactory) Awaitility.await(org.awaitility.Awaitility.await) PageTest(org.knime.core.webui.page.PageTest) NodeViewTest(org.knime.core.webui.node.view.NodeViewTest) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) NodeViewManagerTest.runOnExecutor(org.knime.core.webui.node.view.NodeViewManagerTest.runOnExecutor) Matchers.startsWith(org.hamcrest.Matchers.startsWith) Mockito.verify(org.mockito.Mockito.verify) List(java.util.List) NodeViewNodeModel(org.knime.testing.node.view.NodeViewNodeModel) TextInitialDataService(org.knime.core.webui.data.text.TextInitialDataService) Matchers.is(org.hamcrest.Matchers.is) Assert(org.junit.Assert) Matchers.endsWith(org.hamcrest.Matchers.endsWith) Collections(java.util.Collections) FIVE_SECONDS(org.awaitility.Duration.FIVE_SECONDS) Mockito.mock(org.mockito.Mockito.mock) SelectionEventSourceTest(org.knime.gateway.impl.service.events.SelectionEventSourceTest) TextInitialDataService(org.knime.core.webui.data.text.TextInitialDataService) NodeViewNodeFactory(org.knime.testing.node.view.NodeViewNodeFactory) VirtualSubNodeInputNodeFactory(org.knime.core.node.workflow.virtual.subnode.VirtualSubNodeInputNodeFactory) Page(org.knime.core.webui.page.Page) NodeViewNodeModel(org.knime.testing.node.view.NodeViewNodeModel) NodeView(org.knime.core.webui.node.view.NodeView) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) PortType(org.knime.core.node.port.PortType) PageTest(org.knime.core.webui.page.PageTest) NodeViewTest(org.knime.core.webui.node.view.NodeViewTest) Test(org.junit.Test) SelectionEventSourceTest(org.knime.gateway.impl.service.events.SelectionEventSourceTest)

Example 2 with Page

use of org.knime.core.webui.page.Page in project knime-core by knime.

the class NodeViewManagerTest method testLoadViewSettingsOnViewCreation.

/**
 * Makes sure that view settings are loaded from the node into the node view when created for the first time.
 *
 * @throws InvalidSettingsException
 */
@Test
public void testLoadViewSettingsOnViewCreation() throws InvalidSettingsException {
    var page = Page.builder(() -> "test page content", "index.html").build();
    AtomicReference<NodeSettingsRO> loadedNodeSettings = new AtomicReference<>();
    NativeNodeContainer nc = createNodeWithNodeView(m_wfm, m -> new // NOSONAR
    NodeView() {

        @Override
        public Optional<InitialDataService> createInitialDataService() {
            return Optional.empty();
        }

        @Override
        public Optional<DataService> createDataService() {
            return Optional.empty();
        }

        @Override
        public Optional<ApplyDataService> createApplyDataService() {
            return Optional.empty();
        }

        @Override
        public Page getPage() {
            return page;
        }

        @Override
        public void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException {
        // 
        }

        @Override
        public void loadValidatedSettingsFrom(final NodeSettingsRO settings) {
            loadedNodeSettings.set(settings);
        }
    });
    // prepare view settings
    var settings = new NodeSettings("node_settings");
    m_wfm.saveNodeSettings(nc.getID(), settings);
    var viewSettings = new NodeSettings("view");
    viewSettings.addString("view setting key", "view setting value");
    settings.addNodeSettings(viewSettings);
    settings.addNodeSettings(new NodeSettings("model"));
    m_wfm.loadNodeSettings(nc.getID(), settings);
    // test
    NodeViewManager.getInstance().getNodeView(nc);
    assertTrue(loadedNodeSettings.get().containsKey("view setting key"));
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) Optional(java.util.Optional) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) AtomicReference(java.util.concurrent.atomic.AtomicReference) Page(org.knime.core.webui.page.Page) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) Test(org.junit.Test)

Example 3 with Page

use of org.knime.core.webui.page.Page in project knime-core by knime.

the class NodeViewManagerTest method testGetNodeViewPageUrl.

/**
 * Tests {@link NodeViewManager#getNodeViewPageUrl(NativeNodeContainer)}.
 *
 * @throws URISyntaxException
 * @throws IOException
 */
@Test
public void testGetNodeViewPageUrl() throws URISyntaxException, IOException {
    var staticPage = Page.builder(BUNDLE_ID, "files", "page.html").addResourceFile("resource.html").build();
    var dynamicPage = Page.builder(() -> "page content", "page.html").addResourceFromString(() -> "resource content", "resource.html").build();
    NativeNodeContainer nnc = createNodeWithNodeView(m_wfm, m -> createNodeView(staticPage));
    NativeNodeContainer nnc2 = createNodeWithNodeView(m_wfm, m -> createNodeView(staticPage));
    NativeNodeContainer nnc3 = createNodeWithNodeView(m_wfm, m -> createNodeView(dynamicPage));
    var nodeViewManager = NodeViewManager.getInstance();
    String url = nodeViewManager.getNodeViewPageUrl(nnc).orElse("");
    String url2 = nodeViewManager.getNodeViewPageUrl(nnc2).orElse(null);
    String url3 = nodeViewManager.getNodeViewPageUrl(nnc3).orElse(null);
    String url4 = nodeViewManager.getNodeViewPageUrl(nnc3).orElse(null);
    assertThat("file url of static pages not expected to change", url, is(url2));
    assertThat("file url of dynamic pages expected to change between node instances", url, is(not(url3)));
    assertThat("file url of dynamic pages not expected for same node instance (without node state change)", url3, is(url4));
    assertThat("resource files are expected to be written, too", new File(new URI(url.replace("page.html", "resource.html"))).exists(), is(true));
    assertThat(new File(new URI(url)).exists(), is(true));
    assertThat(new File(new URI(url3)).exists(), is(true));
    String pageContent = Files.readLines(new File(new URI(url3)), StandardCharsets.UTF_8).get(0);
    assertThat(pageContent, is("page content"));
    // impose node state changes
    m_wfm.executeAllAndWaitUntilDone();
    var dynamicPage2 = Page.builder(() -> "new page content", "page.html").addResourceFromString(() -> "resource content", "resource.html").build();
    nnc = createNodeWithNodeView(m_wfm, m -> createNodeView(dynamicPage2));
    String url5 = nodeViewManager.getNodeViewPageUrl(nnc).orElse(null);
    pageContent = Files.readLines(new File(new URI(url5)), StandardCharsets.UTF_8).get(0);
    assertThat(pageContent, is("new page content"));
    runOnExecutor(() -> assertThat(nodeViewManager.getNodeViewPageUrl(nnc2).isEmpty(), is(true)));
}
Also used : NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) VirtualSubNodeInputNodeFactory(org.knime.core.node.workflow.virtual.subnode.VirtualSubNodeInputNodeFactory) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Assert.assertThrows(org.junit.Assert.assertThrows) URISyntaxException(java.net.URISyntaxException) Matchers.not(org.hamcrest.Matchers.not) WorkflowManagerUtil.createAndAddNode(org.knime.testing.util.WorkflowManagerUtil.createAndAddNode) NodeSettings(org.knime.core.node.NodeSettings) ApplyDataService(org.knime.core.webui.data.ApplyDataService) Page(org.knime.core.webui.page.Page) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) InitialDataService(org.knime.core.webui.data.InitialDataService) BUNDLE_ID(org.knime.core.webui.page.PageTest.BUNDLE_ID) NodeContainer(org.knime.core.node.workflow.NodeContainer) Files(com.google.common.io.Files) WorkflowManagerUtil(org.knime.testing.util.WorkflowManagerUtil) After(org.junit.After) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NodeViewTest.createNodeView(org.knime.core.webui.node.view.NodeViewTest.createNodeView) URI(java.net.URI) TextReExecuteDataService(org.knime.core.webui.data.text.TextReExecuteDataService) Before(org.junit.Before) PortType(org.knime.core.node.port.PortType) NodeViewNodeFactory(org.knime.testing.node.view.NodeViewNodeFactory) WorkflowManager(org.knime.core.node.workflow.WorkflowManager) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) DataService(org.knime.core.webui.data.DataService) NodeViewNodeModel(org.knime.testing.node.view.NodeViewNodeModel) Optional(java.util.Optional) TextInitialDataService(org.knime.core.webui.data.text.TextInitialDataService) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) Awaitility(org.awaitility.Awaitility) TextDataService(org.knime.core.webui.data.text.TextDataService) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) URI(java.net.URI) NativeNodeContainer(org.knime.core.node.workflow.NativeNodeContainer) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 NativeNodeContainer (org.knime.core.node.workflow.NativeNodeContainer)3 Page (org.knime.core.webui.page.Page)3 IOException (java.io.IOException)2 Optional (java.util.Optional)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Function (java.util.function.Function)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Matchers.is (org.hamcrest.Matchers.is)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 NodeSettings (org.knime.core.node.NodeSettings)2 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)2 PortType (org.knime.core.node.port.PortType)2 VirtualSubNodeInputNodeFactory (org.knime.core.node.workflow.virtual.subnode.VirtualSubNodeInputNodeFactory)2 TextInitialDataService (org.knime.core.webui.data.text.TextInitialDataService)2 NodeViewNodeFactory (org.knime.testing.node.view.NodeViewNodeFactory)2 NodeViewNodeModel (org.knime.testing.node.view.NodeViewNodeModel)2 WorkflowManagerUtil (org.knime.testing.util.WorkflowManagerUtil)2 Files (com.google.common.io.Files)1 File (java.io.File)1