3 * Copyright (C) 2009-2024 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.db.permission.template;
22 import java.security.SecureRandom;
23 import java.util.Date;
24 import java.util.Random;
25 import java.util.function.Consumer;
26 import org.sonar.core.util.Uuids;
27 import org.sonar.db.permission.PermissionsTestHelper;
29 import static java.util.Arrays.stream;
30 import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
31 import static org.apache.commons.lang3.RandomStringUtils.randomAscii;
33 public class PermissionTemplateTesting {
35 private static final Random RANDOM = new SecureRandom();
38 public static PermissionTemplateDto newPermissionTemplateDto(Consumer<PermissionTemplateDto>... populators) {
39 PermissionTemplateDto dto = new PermissionTemplateDto()
40 .setName(randomAlphanumeric(60))
41 .setDescription(randomAscii(500))
42 .setUuid(Uuids.create())
43 .setCreatedAt(new Date())
44 .setUpdatedAt(new Date());
45 stream(populators).forEach(p -> p.accept(dto));
49 public static PermissionTemplateUserDto newPermissionTemplateUserDto() {
50 return new PermissionTemplateUserDto()
51 .setPermission(PermissionsTestHelper.ALL_PERMISSIONS.toArray(new String[0])[RANDOM.nextInt(PermissionsTestHelper.ALL_PERMISSIONS.size())])
52 .setCreatedAt(new Date())
53 .setUpdatedAt(new Date());
56 public static PermissionTemplateGroupDto newPermissionTemplateGroupDto() {
57 return new PermissionTemplateGroupDto()
58 .setUuid(Uuids.createFast())
59 .setPermission(PermissionsTestHelper.ALL_PERMISSIONS.toArray(new String[0])[RANDOM.nextInt(PermissionsTestHelper.ALL_PERMISSIONS.size())])
60 .setCreatedAt(new Date())
61 .setUpdatedAt(new Date());
64 public static PermissionTemplateCharacteristicDto newPermissionTemplateCharacteristicDto() {
65 return new PermissionTemplateCharacteristicDto()
66 .setUuid(Uuids.createFast())
67 .setPermission(PermissionsTestHelper.ALL_PERMISSIONS.toArray(new String[0])[RANDOM.nextInt(PermissionsTestHelper.ALL_PERMISSIONS.size())])
68 .setWithProjectCreator(RANDOM.nextBoolean())
69 .setCreatedAt(System.currentTimeMillis())
70 .setUpdatedAt(System.currentTimeMillis());