Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<maven.plugin.jxr.version>3.4.0</maven.plugin.jxr.version>
<jackson.version>2.15.2</jackson.version>
<fop.version>2.8</fop.version>
<argLine></argLine>
</properties>

<developers>
Expand Down Expand Up @@ -145,6 +146,27 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.onehippo.cms7</groupId>
<artifactId>hippo-cms-test</artifactId>
Expand Down Expand Up @@ -219,6 +241,27 @@
</modules>

<profiles>
<profile>
<id>coverage</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${maven.plugin.jacoco.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

<profile>
<!-- For generating/publishing GitHub Site Pages into /docs/ folder on master branch only -->
<id>github.pages</id>
Expand Down
45 changes: 41 additions & 4 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,20 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -219,7 +231,6 @@
<groupId>org.bloomreach.forge.exportjson</groupId>
<artifactId>exportjson-repository</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
Expand All @@ -234,7 +245,7 @@
<configuration>
<reuseForks>false</reuseForks>
<workingDirectory>${project.build.directory}</workingDirectory>
<!--<systemProperties>--><!--<property>--><!--<name>testrepopath</name>--><!--<value>${project.build.directory}/testrepository</value>--><!--</property>--><!--</systemProperties>-->
<argLine>@{argLine}</argLine>
</configuration>
</plugin>
<plugin>
Expand All @@ -247,4 +258,30 @@
</plugins>
</build>

<profiles>
<profile>
<id>coverage</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<includeCurrentProject>true</includeCurrentProject>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/*
* Copyright 2024 Bloomreach B.V. (http://www.bloomreach.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.forge.exportjson.repository.workflow;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.lang.reflect.Field;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;

import org.hippoecm.repository.api.WorkflowContext;
import org.hippoecm.repository.ext.WorkflowImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class ExportHtmlWorkflowImplTest {

@Mock private WorkflowContext mockContext;
@Mock private Session mockSession;
@Mock private Node mockDocument;
@Mock private NodeType mockNodeType;
@Mock private PropertyIterator mockPropertyIterator;

private ExportHtmlWorkflowImpl workflow;

private static final String NODE_ID = "test-id";

@BeforeEach
void setUp() throws Exception {
workflow = new ExportHtmlWorkflowImpl();
injectContext(workflow, mockContext);
}

@Test
void exportHtmlDocument_withStyles_containsCssBlock() throws Exception {
stubEmptyDocument(false);

String html = workflow.exportHtmlDocument(NODE_ID, false, true);

assertTrue(html.contains("<style>"));
assertTrue(html.contains("font-family: Arial"));
}

@Test
void exportHtmlDocument_withoutStyles_omitsCssBlock() throws Exception {
stubEmptyDocument(false);

String html = workflow.exportHtmlDocument(NODE_ID, false, false);

assertFalse(html.contains("<style>"));
}

@Test
void exportHtmlDocument_withMetadataAllPresent_includesAllMetadataValues() throws Exception {
Property created = mock(Property.class);
Property modified = mock(Property.class);
Property author = mock(Property.class);
Value createdVal = mock(Value.class);
Value modifiedVal = mock(Value.class);
Value authorVal = mock(Value.class);

stubEmptyDocument(true);
when(mockDocument.hasProperty("jcr:created")).thenReturn(true);
when(mockDocument.getProperty("jcr:created")).thenReturn(created);
when(created.getValue()).thenReturn(createdVal);
when(createdVal.getString()).thenReturn("2024-01-01");
when(mockDocument.hasProperty("jcr:lastModified")).thenReturn(true);
when(mockDocument.getProperty("jcr:lastModified")).thenReturn(modified);
when(modified.getValue()).thenReturn(modifiedVal);
when(modifiedVal.getString()).thenReturn("2024-06-01");
when(mockDocument.hasProperty("hippo:author")).thenReturn(true);
when(mockDocument.getProperty("hippo:author")).thenReturn(author);
when(author.getValue()).thenReturn(authorVal);
when(authorVal.getString()).thenReturn("admin");

String html = workflow.exportHtmlDocument(NODE_ID, true, false);

assertTrue(html.contains("2024-01-01"));
assertTrue(html.contains("2024-06-01"));
assertTrue(html.contains("admin"));
assertTrue(html.contains("metadata"));
}

@Test
void exportHtmlDocument_withMetadataAllAbsent_metadataSectionIsEmpty() throws Exception {
stubEmptyDocument(true);
when(mockDocument.hasProperty("jcr:created")).thenReturn(false);
when(mockDocument.hasProperty("jcr:lastModified")).thenReturn(false);
when(mockDocument.hasProperty("hippo:author")).thenReturn(false);

String html = workflow.exportHtmlDocument(NODE_ID, true, false);

assertTrue(html.contains("metadata"));
assertFalse(html.contains("Created:"));
assertFalse(html.contains("Last Modified:"));
}

@Test
void exportHtmlDocument_withoutMetadata_omitsMetadataSection() throws Exception {
stubEmptyDocument(false);

String html = workflow.exportHtmlDocument(NODE_ID, false, false);

assertFalse(html.contains("class=\"metadata\""));
}

@Test
void exportHtmlDocument_singleValuedProperty_renderedInPropertyRow() throws Exception {
Property prop = mock(Property.class);
Value val = mock(Value.class);
stubDocumentWithProperty(prop, false);
when(prop.getName()).thenReturn("content:title");
when(prop.isMultiple()).thenReturn(false);
when(prop.getValue()).thenReturn(val);
when(val.getString()).thenReturn("My Document");

String html = workflow.exportHtmlDocument(NODE_ID, false, false);

assertTrue(html.contains("content:title"));
assertTrue(html.contains("My Document"));
}

@Test
void exportHtmlDocument_multiValuedProperty_valuesJoinedWithBreak() throws Exception {
Property prop = mock(Property.class);
Value v1 = mock(Value.class);
Value v2 = mock(Value.class);
stubDocumentWithProperty(prop, false);
when(prop.getName()).thenReturn("content:tags");
when(prop.isMultiple()).thenReturn(true);
when(prop.getValues()).thenReturn(new Value[]{v1, v2});
when(v1.getString()).thenReturn("alpha");
when(v2.getString()).thenReturn("beta");

String html = workflow.exportHtmlDocument(NODE_ID, false, false);

assertTrue(html.contains("alpha"));
assertTrue(html.contains("beta"));
assertTrue(html.contains("<br>"));
}

@Test
void exportHtmlDocument_unserializableProperty_showsErrorText() throws Exception {
Property prop = mock(Property.class);
stubDocumentWithProperty(prop, false);
when(prop.getName()).thenReturn("bad:prop");
when(prop.isMultiple()).thenReturn(false);
when(prop.getValue()).thenThrow(new RepositoryException("Cannot read"));

String html = workflow.exportHtmlDocument(NODE_ID, false, false);

assertTrue(html.contains("[Unable to serialize]"));
}

@Test
void exportHtmlDocument_specialCharsInNodeName_escapedInOutput() throws Exception {
when(mockContext.getInternalWorkflowSession()).thenReturn(mockSession);
when(mockSession.getNodeByIdentifier(NODE_ID)).thenReturn(mockDocument);
when(mockDocument.getName()).thenReturn("<script>alert('xss')</script>");
when(mockDocument.getPath()).thenReturn("/content/test");
when(mockDocument.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNodeType.getName()).thenReturn("content:document");
when(mockDocument.getProperties()).thenReturn(mockPropertyIterator);
when(mockPropertyIterator.hasNext()).thenReturn(false);

String html = workflow.exportHtmlDocument(NODE_ID, false, false);

assertFalse(html.contains("<script>"));
assertTrue(html.contains("&lt;script&gt;"));
}

@Test
void invokeWorkflow_isNoOp() throws Exception {
assertDoesNotThrow(() -> workflow.invokeWorkflow());
}

// --- helpers ---

private void stubEmptyDocument(boolean stubMetadataCalls) throws RepositoryException {
when(mockContext.getInternalWorkflowSession()).thenReturn(mockSession);
when(mockSession.getNodeByIdentifier(NODE_ID)).thenReturn(mockDocument);
when(mockDocument.getName()).thenReturn("test-doc");
when(mockDocument.getPath()).thenReturn("/content/documents/test-doc");
when(mockDocument.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNodeType.getName()).thenReturn("content:document");
when(mockDocument.getProperties()).thenReturn(mockPropertyIterator);
when(mockPropertyIterator.hasNext()).thenReturn(false);
}

private void stubDocumentWithProperty(Property prop, boolean stubMetadataCalls) throws RepositoryException {
when(mockContext.getInternalWorkflowSession()).thenReturn(mockSession);
when(mockSession.getNodeByIdentifier(NODE_ID)).thenReturn(mockDocument);
when(mockDocument.getName()).thenReturn("test-doc");
when(mockDocument.getPath()).thenReturn("/content/documents/test-doc");
when(mockDocument.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNodeType.getName()).thenReturn("content:document");
when(mockDocument.getProperties()).thenReturn(mockPropertyIterator);
when(mockPropertyIterator.hasNext()).thenReturn(true, false);
when(mockPropertyIterator.nextProperty()).thenReturn(prop);
}

private static void injectContext(WorkflowImpl target, WorkflowContext context) throws Exception {
Field field = WorkflowImpl.class.getDeclaredField("context");
field.setAccessible(true);
field.set(target, context);
}
}
Loading