3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.ws.template;
22 import java.util.List;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.config.Settings;
27 import org.sonar.api.resources.Qualifiers;
28 import org.sonar.api.resources.ResourceType;
29 import org.sonar.api.resources.ResourceTypes;
30 import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder.TemplateUuidQualifier;
32 import static java.util.Arrays.asList;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.groups.Tuple.tuple;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37 import static org.sonar.server.permission.DefaultPermissionTemplates.DEFAULT_TEMPLATE_PROPERTY;
38 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
40 public class DefaultPermissionTemplateFinderTest {
42 ResourceTypes resourceTypes = mock(ResourceTypes.class);
43 Settings settings = new Settings();
45 DefaultPermissionTemplateFinder underTest;
49 underTest = new DefaultPermissionTemplateFinder(settings, resourceTypes);
51 .setProperty(DEFAULT_TEMPLATE_PROPERTY, "default-template-uuid")
52 .setProperty(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT), "default-project-template-uuid")
53 .setProperty(defaultRootQualifierTemplateProperty("DEV"), "default-dev-template-uuid")
54 .setProperty(defaultRootQualifierTemplateProperty(Qualifiers.VIEW), "default-view-template-uuid");
55 when(resourceTypes.getRoots()).thenReturn(rootResourceTypes());
59 public void get_default_template_uuids_in_settings() {
60 Set<String> result = underTest.getDefaultTemplateUuids();
62 assertThat(result).containsOnly("default-project-template-uuid", "default-view-template-uuid", "default-dev-template-uuid");
66 public void get_default_template_uuid_if_no_property() {
69 .setProperty(DEFAULT_TEMPLATE_PROPERTY, "default-template-uuid");
70 underTest = new DefaultPermissionTemplateFinder(settings, resourceTypes);
72 Set<String> result = underTest.getDefaultTemplateUuids();
74 assertThat(result).containsOnly("default-template-uuid");
78 public void get_default_project_template_uuid_if_no_property_for_views() {
81 .setProperty(DEFAULT_TEMPLATE_PROPERTY, "default-template-uuid")
82 .setProperty(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT), "default-project-template-uuid")
83 .setProperty(defaultRootQualifierTemplateProperty("DEV"), "default-dev-template-uuid");
85 List<TemplateUuidQualifier> result = underTest.getDefaultTemplatesByQualifier();
87 assertThat(result).extracting(TemplateUuidQualifier::getQualifier, TemplateUuidQualifier::getTemplateUuid)
89 tuple(Qualifiers.PROJECT, "default-project-template-uuid"),
90 tuple(Qualifiers.VIEW, "default-project-template-uuid"),
91 tuple("DEV", "default-dev-template-uuid"));
94 private static List<ResourceType> rootResourceTypes() {
95 ResourceType project = ResourceType.builder(Qualifiers.PROJECT).build();
96 ResourceType view = ResourceType.builder(Qualifiers.VIEW).build();
97 ResourceType dev = ResourceType.builder("DEV").build();
99 return asList(project, view, dev);