Search in sources :

Example 1 with NhsdMockQueryManager

use of org.apache.sling.testing.mock.jcr.NhsdMockQueryManager 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

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 Node (javax.jcr.Node)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 ResolvedMount (org.hippoecm.hst.core.request.ResolvedMount)1 MockHstRequestContext (org.hippoecm.hst.mock.core.request.MockHstRequestContext)1