From 79a9c949121ca453ea148fc8d5a474918872dcfd Mon Sep 17 00:00:00 2001 From: at055612 <22818309+at055612@users.noreply.github.com> Date: Wed, 20 Dec 2023 13:33:09 +0000 Subject: [PATCH] gh-3947 Fix failing test, add tests --- .../stroom-explorer-impl/build.gradle | 1 + .../explorer/impl/TestDocRefInfoCache.java | 219 ++++++++++++++++++ .../impl/TestDocRefInfoServiceImpl.java | 18 +- .../java/stroom/security/impl/UserCache.java | 2 + 4 files changed, 232 insertions(+), 8 deletions(-) create mode 100644 stroom-explorer/stroom-explorer-impl/src/test/java/stroom/explorer/impl/TestDocRefInfoCache.java diff --git a/stroom-explorer/stroom-explorer-impl/build.gradle b/stroom-explorer/stroom-explorer-impl/build.gradle index 458834ed751..8cba2960d0c 100644 --- a/stroom-explorer/stroom-explorer-impl/build.gradle +++ b/stroom-explorer/stroom-explorer-impl/build.gradle @@ -35,6 +35,7 @@ dependencies { implementation libs.swagger_annotations implementation libs.ws_rs_api + testImplementation project(':stroom-cache:stroom-cache-impl') testImplementation project(':stroom-security:stroom-security-mock') testImplementation project(':stroom-test-common') diff --git a/stroom-explorer/stroom-explorer-impl/src/test/java/stroom/explorer/impl/TestDocRefInfoCache.java b/stroom-explorer/stroom-explorer-impl/src/test/java/stroom/explorer/impl/TestDocRefInfoCache.java new file mode 100644 index 00000000000..1761f557779 --- /dev/null +++ b/stroom-explorer/stroom-explorer-impl/src/test/java/stroom/explorer/impl/TestDocRefInfoCache.java @@ -0,0 +1,219 @@ +package stroom.explorer.impl; + +import stroom.cache.impl.CacheManagerImpl; +import stroom.docref.DocContentMatch; +import stroom.docref.DocRef; +import stroom.docref.DocRefInfo; +import stroom.explorer.api.ExplorerActionHandler; +import stroom.explorer.shared.DocumentType; +import stroom.explorer.shared.DocumentTypeGroup; +import stroom.security.api.SecurityContext; +import stroom.security.mock.MockSecurityContext; +import stroom.svg.shared.SvgImage; +import stroom.util.NullSafe; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +class TestDocRefInfoCache { + + public static final String TYPE_FOO = "foo"; + public static final String TYPE_BAR = "bar"; + public static final DocRef DOC_REF_1 = DocRef.builder() + .randomUuid() + .type(TYPE_FOO) + .build(); + public static final DocRef DOC_REF_2 = DocRef.builder() + .randomUuid() + .type(TYPE_FOO) + .build(); + public static final DocRef DOC_REF_3 = DocRef.builder() + .randomUuid() + .type(TYPE_BAR) + .build(); + public static final DocRef DOC_REF_4 = DocRef.builder() + .randomUuid() + .type(TYPE_BAR) + .build(); + + final CacheManagerImpl cacheManager = new CacheManagerImpl(); + final SecurityContext securityContext = new MockSecurityContext(); + DocRefInfoCache docRefInfoCache; + + @BeforeEach + void setUp() { + final ExplorerActionHandlers explorerActionHandlers = new ExplorerActionHandlers( + Set.of( + new MyExplorerActionHandler(Set.of(DOC_REF_1, DOC_REF_2)), + new MyExplorerActionHandler(Set.of(DOC_REF_3, DOC_REF_4)))); + + docRefInfoCache = new DocRefInfoCache( + cacheManager, + explorerActionHandlers, + ExplorerConfig::new, + securityContext); + } + + @Test + void testGet() { + final DocRef docRef = DOC_REF_1; + final Optional docRefInfo = docRefInfoCache.get(docRef); + + assertThat(docRefInfo) + .isNotEmpty(); + assertThat(docRefInfo.get().getDocRef()) + .isEqualTo(docRef); + assertThat(docRefInfoCache.get(docRef.getUuid())) + .isEqualTo(docRefInfoCache.get(docRef)); + } + + @Test + void testGet_noType() { + DocRef docRef = DOC_REF_1; + Optional docRefInfo = docRefInfoCache.get(stripType(docRef)); + + assertThat(docRefInfo) + .isNotEmpty(); + assertThat(docRefInfo.get().getDocRef()) + .isEqualTo(docRef); + assertThat(docRefInfoCache.get(docRef.getUuid())) + .isEqualTo(docRefInfoCache.get(docRef)); + + docRef = DOC_REF_4; + docRefInfo = docRefInfoCache.get(stripType(docRef)); + + assertThat(docRefInfo) + .isNotEmpty(); + assertThat(docRefInfo.get().getDocRef()) + .isEqualTo(docRef); + assertThat(docRefInfoCache.get(docRef.getUuid())) + .isEqualTo(docRefInfoCache.get(docRef)); + } + + @Test + void testGet_noName() { + DocRef docRef = DOC_REF_1; + final Optional docRefInfo = docRefInfoCache.get(docRef.withoutName()); + + assertThat(docRefInfo) + .isNotEmpty(); + assertThat(docRefInfo.get().getDocRef()) + .isEqualTo(docRef); + } + + private static DocRef stripType(final DocRef docRef) { + return docRef.copy() + .type(null) + .build(); + } + + + // -------------------------------------------------------------------------------- + + + private static class MyExplorerActionHandler implements ExplorerActionHandler { + + private final Map docRefs = new HashMap<>(); + private final String type; + + private MyExplorerActionHandler(final Set docRefs) { + docRefs.forEach(docRef -> this.docRefs.put(docRef.getUuid(), docRef)); + final Set types = docRefs.stream() + .map(DocRef::getType) + .collect(Collectors.toSet()); + if (types.size() != 1) { + throw new IllegalArgumentException("Expecting one type only"); + } + this.type = types.iterator().next(); + } + + @Override + public List findByContent(final String pattern, final boolean regex, final boolean matchCase) { + throw new UnsupportedOperationException(); + } + + @Override + public Set listDocuments() { + throw new UnsupportedOperationException(); + } + + @Override + public List findByNames(final List names, final boolean allowWildCards) { + throw new UnsupportedOperationException(); + } + + @Override + public DocRef createDocument(final String name) { + throw new UnsupportedOperationException(); + } + + @Override + public DocRef copyDocument(final DocRef docRef, + final String name, + final boolean makeNameUnique, + final Set existingNames) { + throw new UnsupportedOperationException(); + } + + @Override + public DocRef moveDocument(final String uuid) { + throw new UnsupportedOperationException(); + } + + @Override + public DocRef renameDocument(final String uuid, final String name) { + throw new UnsupportedOperationException(); + } + + @Override + public void deleteDocument(final String uuid) { + throw new UnsupportedOperationException(); + } + + @Override + public DocRefInfo info(final String uuid) { + return NullSafe.get( + docRefs.get(uuid), + docRef -> new DocRefInfo( + docRef, + System.currentTimeMillis(), + System.currentTimeMillis(), + "user1", + "user1", + null)); + } + + @Override + public DocumentType getDocumentType() { + return new DocumentType( + DocumentTypeGroup.CONFIGURATION, + type, + type, + SvgImage.OK); + } + + @Override + public Map> getDependencies() { + throw new UnsupportedOperationException(); + } + + @Override + public Set getDependencies(final DocRef docRef) { + throw new UnsupportedOperationException(); + } + + @Override + public void remapDependencies(final DocRef docRef, final Map remappings) { + throw new UnsupportedOperationException(); + } + } +} diff --git a/stroom-explorer/stroom-explorer-impl/src/test/java/stroom/explorer/impl/TestDocRefInfoServiceImpl.java b/stroom-explorer/stroom-explorer-impl/src/test/java/stroom/explorer/impl/TestDocRefInfoServiceImpl.java index 62ff206edd0..1ceacfc6339 100644 --- a/stroom-explorer/stroom-explorer-impl/src/test/java/stroom/explorer/impl/TestDocRefInfoServiceImpl.java +++ b/stroom-explorer/stroom-explorer-impl/src/test/java/stroom/explorer/impl/TestDocRefInfoServiceImpl.java @@ -45,7 +45,7 @@ class TestDocRefInfoServiceImpl { DOC_REF2, DOC_REF3); - private static final Map CACHE_DATA = DOC_REFS.stream() + private static final Map CACHE_DATA = DOC_REFS.stream() .map(docRef -> new DocRefInfo( docRef, System.currentTimeMillis(), @@ -53,7 +53,9 @@ class TestDocRefInfoServiceImpl { "user1", "user1", null)) - .collect(Collectors.toMap(DocRefInfo::getDocRef, Function.identity())); + .collect(Collectors.toMap( + docRefInfo -> docRefInfo.getDocRef().getUuid(), + Function.identity())); @Mock private DocRefInfoCache mockDocRefInfoCache; @@ -76,7 +78,7 @@ private void initMockCache() { Mockito .doAnswer(invocation -> { final DocRef docRef = invocation.getArgument(0); - return Optional.ofNullable(CACHE_DATA.get(docRef)); + return Optional.ofNullable(CACHE_DATA.get(docRef.getUuid())); }) .when(mockDocRefInfoCache).get(Mockito.any(DocRef.class)); } @@ -174,11 +176,11 @@ void decorate_list_noType() { DOC_REF3); Assertions - .assertThatThrownBy(() -> { - final List outputDocRefs = docRefInfoService.decorate(inputDocRefs); - }) - .hasMessageContaining("type is not set") - .isInstanceOf(RuntimeException.class); + .assertThat(docRefInfoService.decorate(inputDocRefs)) + .containsExactly( + DOC_REF1, + DOC_REF2, + DOC_REF3); } @Test diff --git a/stroom-security/stroom-security-impl/src/main/java/stroom/security/impl/UserCache.java b/stroom-security/stroom-security-impl/src/main/java/stroom/security/impl/UserCache.java index cef35664225..31b59e1ce21 100644 --- a/stroom-security/stroom-security-impl/src/main/java/stroom/security/impl/UserCache.java +++ b/stroom-security/stroom-security-impl/src/main/java/stroom/security/impl/UserCache.java @@ -62,6 +62,8 @@ class UserCache implements Clearable, EntityEvent.Handler { // TODO: 20/07/2023 We could consider trying to re-use User objects between // all three caches but it presents concurrency issues and the risk of deadlocks, // maybe. + // See https://github.com/ben-manes/caffeine/issues/279 for ideas. + // It is complicated by displayName being mutable and optional. cacheByUuid = cacheManager.createLoadingCache( CACHE_NAME_BY_UUID, () -> authorisationConfigProvider.get().getUserByUuidCache(),