3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.permission.index;
22 import java.util.List;
23 import java.util.stream.Stream;
24 import org.sonar.db.component.ComponentDto;
25 import org.sonar.db.entity.EntityDto;
26 import org.sonar.db.user.GroupDto;
27 import org.sonar.db.user.UserDto;
28 import org.sonar.server.es.EsTester;
30 import static java.util.Arrays.stream;
32 public class PermissionIndexerTester {
34 private final PermissionIndexer permissionIndexer;
36 public PermissionIndexerTester(EsTester esTester, NeedAuthorizationIndexer indexer, NeedAuthorizationIndexer... others) {
37 NeedAuthorizationIndexer[] indexers = Stream.concat(Stream.of(indexer), stream(others)).toArray(NeedAuthorizationIndexer[]::new);
38 this.permissionIndexer = new PermissionIndexer(null, esTester.client(), indexers);
41 public PermissionIndexerTester allowOnlyAnyone(ComponentDto... projects) {
42 return allow(stream(projects).map(project -> new IndexPermissions(project.uuid(), project.qualifier()).allowAnyone()).toList());
45 public PermissionIndexerTester allowOnlyAnyone(EntityDto... entities) {
46 return allow(stream(entities).map(entity -> new IndexPermissions(entity.getUuid(), entity.getQualifier()).allowAnyone()).toList());
49 public PermissionIndexerTester allowOnlyUser(ComponentDto project, UserDto user) {
50 IndexPermissions dto = new IndexPermissions(project.uuid(), project.qualifier())
51 .addUserUuid(user.getUuid());
55 public PermissionIndexerTester allowOnlyUser(EntityDto entityDto, UserDto user) {
56 IndexPermissions dto = new IndexPermissions(entityDto.getUuid(), entityDto.getQualifier())
57 .addUserUuid(user.getUuid());
61 public PermissionIndexerTester allowOnlyGroup(ComponentDto project, GroupDto group) {
62 IndexPermissions dto = new IndexPermissions(project.uuid(), project.qualifier())
63 .addGroupUuid(group.getUuid());
67 public PermissionIndexerTester allowOnlyGroup(EntityDto entityDto, GroupDto group) {
68 IndexPermissions dto = new IndexPermissions(entityDto.getUuid(), entityDto.getQualifier())
69 .addGroupUuid(group.getUuid());
73 public PermissionIndexerTester allow(IndexPermissions... indexPermissions) {
74 return allow(stream(indexPermissions).toList());
77 public PermissionIndexerTester allow(List<IndexPermissions> indexPermissions) {
78 permissionIndexer.index(indexPermissions);