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;
}
Aggregations