Line data Source code
1 : // Copyright (C) 2019 The Android Open Source Project
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : package com.google.gerrit.server.schema.testing;
16 :
17 : import static com.google.common.truth.Truth.assertThat;
18 : import static com.google.common.truth.Truth.assertWithMessage;
19 :
20 : import com.google.common.collect.ImmutableList;
21 : import com.google.common.collect.Iterables;
22 : import com.google.common.collect.Streams;
23 : import com.google.gerrit.entities.RefNames;
24 : import com.google.gerrit.server.config.AllProjectsName;
25 : import com.google.gerrit.server.git.GitRepositoryManager;
26 : import java.io.IOException;
27 : import java.util.Set;
28 : import java.util.stream.Collectors;
29 : import org.eclipse.jgit.errors.ConfigInvalidException;
30 : import org.eclipse.jgit.lib.BlobBasedConfig;
31 : import org.eclipse.jgit.lib.Config;
32 : import org.eclipse.jgit.lib.Ref;
33 : import org.eclipse.jgit.lib.Repository;
34 :
35 : public class AllProjectsCreatorTestUtil {
36 1 : private static final ImmutableList<String> DEFAULT_ALL_PROJECTS_PROJECT_SECTION =
37 1 : ImmutableList.of("[project]", " description = Access inherited by all other projects.");
38 1 : private static final ImmutableList<String> DEFAULT_ALL_PROJECTS_RECEIVE_SECTION =
39 1 : ImmutableList.of(
40 : "[receive]",
41 : " requireContributorAgreement = false",
42 : " requireSignedOffBy = false",
43 : " requireChangeId = true",
44 : " enableSignedPush = false");
45 1 : private static final ImmutableList<String> DEFAULT_ALL_PROJECTS_SUBMIT_SECTION =
46 1 : ImmutableList.of("[submit]", " mergeContent = true");
47 1 : private static final ImmutableList<String> DEFAULT_ALL_PROJECTS_CAPABILITY_SECTION =
48 1 : ImmutableList.of(
49 : "[capability]",
50 : " administrateServer = group Administrators",
51 : " priority = batch group Service Users",
52 : " streamEvents = group Service Users");
53 1 : private static final ImmutableList<String> DEFAULT_ALL_PROJECTS_ACCESS_SECTION =
54 1 : ImmutableList.of(
55 : "[access \"refs/*\"]",
56 : " read = group Administrators",
57 : "[access \"refs/for/*\"]",
58 : " addPatchSet = group Registered Users",
59 : "[access \"refs/for/refs/*\"]",
60 : " push = group Registered Users",
61 : " pushMerge = group Registered Users",
62 : "[access \"refs/heads/*\"]",
63 : " read = group Anonymous Users",
64 : " revert = group Registered Users",
65 : " create = group Administrators",
66 : " create = group Project Owners",
67 : " editTopicName = +force group Administrators",
68 : " editTopicName = +force group Project Owners",
69 : " forgeAuthor = group Registered Users",
70 : " forgeCommitter = group Administrators",
71 : " forgeCommitter = group Project Owners",
72 : " label-Code-Review = -2..+2 group Administrators",
73 : " label-Code-Review = -2..+2 group Project Owners",
74 : " label-Code-Review = -1..+1 group Registered Users",
75 : " push = group Administrators",
76 : " push = group Project Owners",
77 : " submit = group Administrators",
78 : " submit = group Project Owners",
79 : "[access \"refs/meta/config\"]",
80 : " exclusiveGroupPermissions = read",
81 : " create = group Administrators",
82 : " create = group Project Owners",
83 : " label-Code-Review = -2..+2 group Administrators",
84 : " label-Code-Review = -2..+2 group Project Owners",
85 : " push = group Administrators",
86 : " push = group Project Owners",
87 : " read = group Administrators",
88 : " read = group Project Owners",
89 : " submit = group Administrators",
90 : " submit = group Project Owners",
91 : "[access \"refs/meta/version\"]",
92 : " read = group Anonymous Users",
93 : "[access \"refs/tags/*\"]",
94 : " create = group Administrators",
95 : " create = group Project Owners",
96 : " createSignedTag = group Administrators",
97 : " createSignedTag = group Project Owners",
98 : " createTag = group Administrators",
99 : " createTag = group Project Owners");
100 1 : private static final ImmutableList<String> DEFAULT_ALL_PROJECTS_LABEL_SECTION =
101 1 : ImmutableList.of(
102 : "[label \"Code-Review\"]",
103 : " function = MaxWithBlock",
104 : " defaultValue = 0",
105 : " copyCondition = changekind:NO_CHANGE OR changekind:TRIVIAL_REBASE OR is:MIN",
106 : " value = -2 This shall not be submitted",
107 : " value = -1 I would prefer this is not submitted as is",
108 : " value = 0 No score",
109 : " value = +1 Looks good to me, but someone else must approve",
110 : " value = +2 Looks good to me, approved");
111 :
112 : public static String getDefaultAllProjectsWithAllDefaultSections() {
113 1 : return Streams.stream(
114 1 : Iterables.concat(
115 : DEFAULT_ALL_PROJECTS_PROJECT_SECTION,
116 : DEFAULT_ALL_PROJECTS_RECEIVE_SECTION,
117 : DEFAULT_ALL_PROJECTS_SUBMIT_SECTION,
118 : DEFAULT_ALL_PROJECTS_CAPABILITY_SECTION,
119 : DEFAULT_ALL_PROJECTS_ACCESS_SECTION,
120 : DEFAULT_ALL_PROJECTS_LABEL_SECTION))
121 1 : .collect(Collectors.joining("\n"));
122 : }
123 :
124 : public static String getAllProjectsWithoutDefaultAcls() {
125 1 : return Streams.stream(
126 1 : Iterables.concat(
127 : DEFAULT_ALL_PROJECTS_PROJECT_SECTION,
128 : DEFAULT_ALL_PROJECTS_RECEIVE_SECTION,
129 : DEFAULT_ALL_PROJECTS_SUBMIT_SECTION,
130 : DEFAULT_ALL_PROJECTS_LABEL_SECTION))
131 1 : .collect(Collectors.joining("\n"));
132 : }
133 :
134 : // Loads the "project.config" from the All-Projects repo.
135 : public static Config readAllProjectsConfig(
136 : GitRepositoryManager repoManager, AllProjectsName allProjectsName)
137 : throws IOException, ConfigInvalidException {
138 1 : try (Repository repo = repoManager.openRepository(allProjectsName)) {
139 1 : Ref configRef = repo.exactRef(RefNames.REFS_CONFIG);
140 1 : return new BlobBasedConfig(null, repo, configRef.getObjectId(), "project.config");
141 : }
142 : }
143 :
144 : public static void assertTwoConfigsEquivalent(Config config1, Config config2) {
145 1 : Set<String> sections1 = config1.getSections();
146 1 : Set<String> sections2 = config2.getSections();
147 1 : assertThat(sections1).containsExactlyElementsIn(sections2);
148 :
149 1 : sections1.forEach(s -> assertSectionEquivalent(config1, config2, s));
150 1 : }
151 :
152 : public static void assertSectionEquivalent(Config config1, Config config2, String section) {
153 1 : assertSubsectionEquivalent(config1, config2, section, null);
154 :
155 1 : Set<String> subsections1 = config1.getSubsections(section);
156 1 : Set<String> subsections2 = config2.getSubsections(section);
157 1 : assertWithMessage("section \"%s\"", section)
158 1 : .that(subsections1)
159 1 : .containsExactlyElementsIn(subsections2);
160 :
161 1 : subsections1.forEach(s -> assertSubsectionEquivalent(config1, config2, section, s));
162 1 : }
163 :
164 : private static void assertSubsectionEquivalent(
165 : Config config1, Config config2, String section, String subsection) {
166 1 : Set<String> subsectionNames1 = config1.getNames(section, subsection);
167 1 : Set<String> subsectionNames2 = config2.getNames(section, subsection);
168 1 : String name = String.format("subsection \"%s\" of section \"%s\"", subsection, section);
169 1 : assertWithMessage(name).that(subsectionNames1).containsExactlyElementsIn(subsectionNames2);
170 :
171 1 : subsectionNames1.forEach(
172 : n ->
173 1 : assertWithMessage(name)
174 1 : .that(config1.getStringList(section, subsection, n))
175 1 : .asList()
176 1 : .containsExactlyElementsIn(config2.getStringList(section, subsection, n)));
177 1 : }
178 :
179 : private AllProjectsCreatorTestUtil() {}
180 : }
|