Skip to content

Commit

Permalink
gh-3947 Fix failing test, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
at055612 committed Dec 20, 2023
1 parent 652665c commit 79a9c94
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 8 deletions.
1 change: 1 addition & 0 deletions stroom-explorer/stroom-explorer-impl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Original file line number Diff line number Diff line change
@@ -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> 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> 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> 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<String, DocRef> docRefs = new HashMap<>();
private final String type;

private MyExplorerActionHandler(final Set<DocRef> docRefs) {
docRefs.forEach(docRef -> this.docRefs.put(docRef.getUuid(), docRef));
final Set<String> 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<DocContentMatch> findByContent(final String pattern, final boolean regex, final boolean matchCase) {
throw new UnsupportedOperationException();
}

@Override
public Set<DocRef> listDocuments() {
throw new UnsupportedOperationException();
}

@Override
public List<DocRef> findByNames(final List<String> 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<String> 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<DocRef, Set<DocRef>> getDependencies() {
throw new UnsupportedOperationException();
}

@Override
public Set<DocRef> getDependencies(final DocRef docRef) {
throw new UnsupportedOperationException();
}

@Override
public void remapDependencies(final DocRef docRef, final Map<DocRef, DocRef> remappings) {
throw new UnsupportedOperationException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ class TestDocRefInfoServiceImpl {
DOC_REF2,
DOC_REF3);

private static final Map<DocRef, DocRefInfo> CACHE_DATA = DOC_REFS.stream()
private static final Map<String, DocRefInfo> CACHE_DATA = DOC_REFS.stream()
.map(docRef -> new DocRefInfo(
docRef,
System.currentTimeMillis(),
System.currentTimeMillis(),
"user1",
"user1",
null))
.collect(Collectors.toMap(DocRefInfo::getDocRef, Function.identity()));
.collect(Collectors.toMap(
docRefInfo -> docRefInfo.getDocRef().getUuid(),
Function.identity()));

@Mock
private DocRefInfoCache mockDocRefInfoCache;
Expand All @@ -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));
}
Expand Down Expand Up @@ -174,11 +176,11 @@ void decorate_list_noType() {
DOC_REF3);

Assertions
.assertThatThrownBy(() -> {
final List<DocRef> outputDocRefs = docRefInfoService.decorate(inputDocRefs);
})
.hasMessageContaining("type is not set")
.isInstanceOf(RuntimeException.class);
.assertThat(docRefInfoService.decorate(inputDocRefs))
.containsExactly(
DOC_REF1,
DOC_REF2,
DOC_REF3);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 79a9c94

Please sign in to comment.