|
| 1 | +package de.tum.in.www1.hephaestus.workspace; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.Mockito.when; |
| 5 | + |
| 6 | +import de.tum.in.www1.hephaestus.feature.FeatureFlagService; |
| 7 | +import de.tum.in.www1.hephaestus.gitprovider.common.gitlab.GitLabProperties; |
| 8 | +import de.tum.in.www1.hephaestus.gitprovider.github.GitHubProperties; |
| 9 | +import de.tum.in.www1.hephaestus.gitprovider.user.User; |
| 10 | +import de.tum.in.www1.hephaestus.gitprovider.user.UserRepository; |
| 11 | +import de.tum.in.www1.hephaestus.testconfig.BaseUnitTest; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Optional; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.mockito.Mock; |
| 16 | + |
| 17 | +class WorkspaceQueryServiceTest extends BaseUnitTest { |
| 18 | + |
| 19 | + @Mock |
| 20 | + private WorkspaceRepository workspaceRepository; |
| 21 | + |
| 22 | + @Mock |
| 23 | + private WorkspaceMembershipRepository workspaceMembershipRepository; |
| 24 | + |
| 25 | + @Mock |
| 26 | + private RepositoryToMonitorRepository repositoryToMonitorRepository; |
| 27 | + |
| 28 | + @Mock |
| 29 | + private UserRepository userRepository; |
| 30 | + |
| 31 | + @Mock |
| 32 | + private GitHubProperties gitHubProperties; |
| 33 | + |
| 34 | + @Mock |
| 35 | + private GitLabProperties gitLabProperties; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private FeatureFlagService featureFlagService; |
| 39 | + |
| 40 | + @Test |
| 41 | + void findAccessibleWorkspacesSortsByDisplayNameAndDeduplicatesMemberships() { |
| 42 | + Workspace alphaWorkspace = workspace(1L, "alpha-space", "Alpha Workspace", true); |
| 43 | + Workspace bravoWorkspace = workspace(2L, "bravo-space", "Bravo Workspace", false); |
| 44 | + Workspace zuluWorkspace = workspace(3L, "zulu-space", "Zulu Workspace", true); |
| 45 | + |
| 46 | + User currentUser = new User(); |
| 47 | + currentUser.setId(42L); |
| 48 | + |
| 49 | + WorkspaceMembership bravoMembership = membership(bravoWorkspace); |
| 50 | + WorkspaceMembership alphaMembership = membership(alphaWorkspace); |
| 51 | + |
| 52 | + WorkspaceQueryService service = new WorkspaceQueryService( |
| 53 | + workspaceRepository, |
| 54 | + workspaceMembershipRepository, |
| 55 | + repositoryToMonitorRepository, |
| 56 | + userRepository, |
| 57 | + gitHubProperties, |
| 58 | + gitLabProperties, |
| 59 | + featureFlagService |
| 60 | + ); |
| 61 | + |
| 62 | + when(workspaceRepository.findByStatusAndIsPubliclyViewableTrue(Workspace.WorkspaceStatus.ACTIVE)).thenReturn( |
| 63 | + List.of(zuluWorkspace, alphaWorkspace) |
| 64 | + ); |
| 65 | + when(workspaceMembershipRepository.findByUser_Id(42L)).thenReturn(List.of(bravoMembership, alphaMembership)); |
| 66 | + when(workspaceRepository.findAllById(List.of(2L, 1L))).thenReturn(List.of(bravoWorkspace, alphaWorkspace)); |
| 67 | + |
| 68 | + List<Workspace> workspaces = service.findAccessibleWorkspaces(Optional.of(currentUser)); |
| 69 | + |
| 70 | + assertThat(workspaces) |
| 71 | + .extracting(Workspace::getWorkspaceSlug) |
| 72 | + .containsExactly("alpha-space", "bravo-space", "zulu-space"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void findAccessibleWorkspacesSortsPublicWorkspacesForAnonymousUsers() { |
| 77 | + Workspace zuluWorkspace = workspace(3L, "zulu-space", "Zulu Workspace", true); |
| 78 | + Workspace alphaWorkspace = workspace(1L, "alpha-space", "Alpha Workspace", true); |
| 79 | + |
| 80 | + WorkspaceQueryService service = new WorkspaceQueryService( |
| 81 | + workspaceRepository, |
| 82 | + workspaceMembershipRepository, |
| 83 | + repositoryToMonitorRepository, |
| 84 | + userRepository, |
| 85 | + gitHubProperties, |
| 86 | + gitLabProperties, |
| 87 | + featureFlagService |
| 88 | + ); |
| 89 | + |
| 90 | + when(workspaceRepository.findByStatusAndIsPubliclyViewableTrue(Workspace.WorkspaceStatus.ACTIVE)).thenReturn( |
| 91 | + List.of(zuluWorkspace, alphaWorkspace) |
| 92 | + ); |
| 93 | + |
| 94 | + List<Workspace> workspaces = service.findAccessibleWorkspaces(Optional.empty()); |
| 95 | + |
| 96 | + assertThat(workspaces).extracting(Workspace::getWorkspaceSlug).containsExactly("alpha-space", "zulu-space"); |
| 97 | + } |
| 98 | + |
| 99 | + private Workspace workspace(Long id, String slug, String displayName, boolean publiclyViewable) { |
| 100 | + Workspace workspace = new Workspace(); |
| 101 | + workspace.setId(id); |
| 102 | + workspace.setWorkspaceSlug(slug); |
| 103 | + workspace.setDisplayName(displayName); |
| 104 | + workspace.setIsPubliclyViewable(publiclyViewable); |
| 105 | + workspace.setStatus(Workspace.WorkspaceStatus.ACTIVE); |
| 106 | + return workspace; |
| 107 | + } |
| 108 | + |
| 109 | + private WorkspaceMembership membership(Workspace workspace) { |
| 110 | + WorkspaceMembership membership = new WorkspaceMembership(); |
| 111 | + membership.setWorkspace(workspace); |
| 112 | + membership.setId(new WorkspaceMembership.Id(workspace.getId(), 42L)); |
| 113 | + return membership; |
| 114 | + } |
| 115 | +} |
0 commit comments