Search in sources :

Example 1 with MockNode

use of org.onehippo.repository.mock.MockNode in project hippo by NHS-digital-website.

the class JcrNodeUtilsTest method streamOf_returnsNodesFromNodeIteratorAsStream.

@Test
public void streamOf_returnsNodesFromNodeIteratorAsStream() {
    // given
    final MockNode nodeA = new MockNode("nodeA");
    final MockNode nodeB = new MockNode("nodeB");
    final MockNode nodeC = new MockNode("nodeC");
    final NodeIterator nodeIterator = new MockNodeIterator(asList(nodeA, nodeB, nodeC));
    // when
    final Stream<Node> actualNodeStream = JcrNodeUtils.streamOf(nodeIterator);
    // then
    assertThat("Returned stream contains nodes from the iterator, exactly.", actualNodeStream.collect(toList()), containsInAnyOrder(nodeA, nodeB, nodeC));
}
Also used : MockNodeIterator(org.onehippo.repository.mock.MockNodeIterator) MockNodeIterator(org.onehippo.repository.mock.MockNodeIterator) MockNode(org.onehippo.repository.mock.MockNode) MockNode(org.onehippo.repository.mock.MockNode) Test(org.junit.Test)

Example 2 with MockNode

use of org.onehippo.repository.mock.MockNode in project hippo by NHS-digital-website.

the class AttachmentFieldValidatorTest method createNodeConfiguration.

private Node createNodeConfiguration(boolean duplicateDisplayName) throws RepositoryException {
    String displayName1 = "displayname1";
    String displayName2 = "displayname2";
    if (duplicateDisplayName) {
        displayName1 = displayName2;
    }
    Node node = new MockNode("document", "doc");
    node.addNode("attachment", HIPPO_ATTACHMENT_PRIMARY_NODE_TYPE).setProperty(HIPPO_FILENAME_PROPERTY_NAME, "duplicateFileName");
    node.addNode("attachment2", HIPPO_ATTACHMENT_PRIMARY_NODE_TYPE).setProperty(HIPPO_FILENAME_PROPERTY_NAME, "duplicateFileName");
    node.addNode("attachment/resource", DEFAULT_ATTACHMENT_NAME_WHEN_NO_FILE_UPLOADED).setProperty(HIPPO_FILENAME_PROPERTY_NAME, "another_filename");
    node.getNode("attachment").setProperty(HIPPO_ATTACHMENT_DISPLAY_NAME_PROPERTY_NAME, displayName1);
    node.getNode("attachment2").setProperty(HIPPO_ATTACHMENT_DISPLAY_NAME_PROPERTY_NAME, displayName2);
    return node;
}
Also used : MockNode(org.onehippo.repository.mock.MockNode) Node(javax.jcr.Node) MockNode(org.onehippo.repository.mock.MockNode)

Example 3 with MockNode

use of org.onehippo.repository.mock.MockNode in project hippo by NHS-digital-website.

the class AttachmentFieldValidatorTest method reportsValidationViloation_forBlankUploadField.

@Test
public void reportsValidationViloation_forBlankUploadField() throws Exception {
    Node node = new MockNode("document", "doc");
    node.addNode("attachment", HIPPO_ATTACHMENT_PRIMARY_NODE_TYPE);
    node.addNode("attachment/resource", DEFAULT_ATTACHMENT_NAME_WHEN_NO_FILE_UPLOADED).setProperty(HIPPO_FILENAME_PROPERTY_NAME, DEFAULT_ATTACHMENT_NAME_WHEN_NO_FILE_UPLOADED);
    given(validationContextMock.getDocumentNode()).willReturn(node);
    given(validationContextMock.createViolation("blank-attachment")).willReturn(violationMock);
    final Optional<Violation> violation = attachmentFieldValidator.validate(validationContextMock, nodeDecoratorMock);
    assertThat("Violation has been reported, blank upload", violation.isPresent());
    verify(validationContextMock).createViolation("blank-attachment");
}
Also used : Violation(org.onehippo.cms.services.validation.api.Violation) MockNode(org.onehippo.repository.mock.MockNode) Node(javax.jcr.Node) MockNode(org.onehippo.repository.mock.MockNode) Test(org.junit.Test)

Example 4 with MockNode

use of org.onehippo.repository.mock.MockNode in project hippo by NHS-digital-website.

the class JcrTestUtilsHst method mockJcrRepo.

public static MockSession mockJcrRepo(final String repositoryYaml, final Map<String, Class<? extends HippoBean>> jcrPrimaryNodeTypeBeanPairs, final MockHstRequestContext hstRequestContext, final String expectedQuery, final String... expectedNodesAbsolutePaths) throws IOException, RepositoryException {
    final NhsdMockQueryManager jcrQueryManager = new NhsdMockQueryManager();
    final MockNode root = MockNode.root(jcrQueryManager);
    MockNodeFactory.importYaml(repositoryYaml, root);
    final HstQueryManagerImpl hstQueryManager = new HstQueryManagerImpl(null, new ObjectConverterImpl(Optional.ofNullable(jcrPrimaryNodeTypeBeanPairs).orElse(emptyMap()), new String[0]), DateTools.Resolution.MILLISECOND);
    final List<Node> expectedNodes = Optional.ofNullable(expectedNodesAbsolutePaths).map(Arrays::stream).orElse(Stream.empty()).map(path -> wrapCheckedException(() -> root.getNode(path))).collect(toList());
    jcrQueryManager.registerResultHandler(query -> {
        if (query.getStatement().equals(expectedQuery)) {
            return new MockQueryResult(expectedNodes);
        }
        return new MockQueryResult(emptyList());
    });
    final MockSession session = root.getSession();
    ReflectionTestUtils.setField(hstQueryManager, "session", session);
    final Mount mount = mock(Mount.class);
    final ResolvedMount resolvedMount = mock(ResolvedMount.class);
    given(resolvedMount.getMount()).willReturn(mount);
    given(mount.isPreview()).willReturn(false);
    MockHstRequestContext requestContext = hstRequestContext;
    if (requestContext == null) {
        requestContext = new MockHstRequestContext();
        ModifiableRequestContextProvider.set(requestContext);
    }
    requestContext.setDefaultHstQueryManager(hstQueryManager);
    requestContext.setResolvedMount(resolvedMount);
    requestContext.setSession(session);
    return session;
}
Also used : Arrays(java.util.Arrays) MockHstRequestContext(org.hippoecm.hst.mock.core.request.MockHstRequestContext) MockNodeFactory(org.onehippo.repository.mock.MockNodeFactory) ResolvedMount(org.hippoecm.hst.core.request.ResolvedMount) RepositoryException(javax.jcr.RepositoryException) BDDMockito.given(org.mockito.BDDMockito.given) Node(javax.jcr.Node) MockQueryResult(org.apache.sling.testing.mock.jcr.MockQueryResult) Map(java.util.Map) ObjectConverterImpl(org.hippoecm.hst.content.beans.manager.ObjectConverterImpl) Mount(org.hippoecm.hst.configuration.hosting.Mount) Collections.emptyMap(java.util.Collections.emptyMap) MockNode(org.onehippo.repository.mock.MockNode) ModifiableRequestContextProvider(org.hippoecm.hst.container.ModifiableRequestContextProvider) MockSession(org.onehippo.repository.mock.MockSession) Collections.emptyList(java.util.Collections.emptyList) IOException(java.io.IOException) HstQueryManagerImpl(org.hippoecm.hst.content.beans.query.HstQueryManagerImpl) HippoBean(org.hippoecm.hst.content.beans.standard.HippoBean) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) NhsdMockQueryManager(org.apache.sling.testing.mock.jcr.NhsdMockQueryManager) Stream(java.util.stream.Stream) DateTools(org.hippoecm.repository.util.DateTools) Optional(java.util.Optional) ExceptionTestUtils.wrapCheckedException(uk.nhs.digital.test.util.ExceptionTestUtils.wrapCheckedException) Mockito.mock(org.mockito.Mockito.mock) MockHstRequestContext(org.hippoecm.hst.mock.core.request.MockHstRequestContext) Node(javax.jcr.Node) MockNode(org.onehippo.repository.mock.MockNode) ResolvedMount(org.hippoecm.hst.core.request.ResolvedMount) Mount(org.hippoecm.hst.configuration.hosting.Mount) NhsdMockQueryManager(org.apache.sling.testing.mock.jcr.NhsdMockQueryManager) HstQueryManagerImpl(org.hippoecm.hst.content.beans.query.HstQueryManagerImpl) MockNode(org.onehippo.repository.mock.MockNode) MockSession(org.onehippo.repository.mock.MockSession) ObjectConverterImpl(org.hippoecm.hst.content.beans.manager.ObjectConverterImpl) ResolvedMount(org.hippoecm.hst.core.request.ResolvedMount) MockQueryResult(org.apache.sling.testing.mock.jcr.MockQueryResult)

Aggregations

MockNode (org.onehippo.repository.mock.MockNode)4 Node (javax.jcr.Node)3 Test (org.junit.Test)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 RepositoryException (javax.jcr.RepositoryException)1 MockQueryResult (org.apache.sling.testing.mock.jcr.MockQueryResult)1 NhsdMockQueryManager (org.apache.sling.testing.mock.jcr.NhsdMockQueryManager)1 Mount (org.hippoecm.hst.configuration.hosting.Mount)1 ModifiableRequestContextProvider (org.hippoecm.hst.container.ModifiableRequestContextProvider)1 ObjectConverterImpl (org.hippoecm.hst.content.beans.manager.ObjectConverterImpl)1 HstQueryManagerImpl (org.hippoecm.hst.content.beans.query.HstQueryManagerImpl)1 HippoBean (org.hippoecm.hst.content.beans.standard.HippoBean)1