summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/test/java/com/google/gerrit/server/schema/SchemaCreatorTest.java
blob: 41a34af514f7475595b97629a5ccb6c60897b7b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.schema;

import com.google.gerrit.reviewdb.AccountGroup;
import com.google.gerrit.reviewdb.ApprovalCategory;
import com.google.gerrit.reviewdb.ApprovalCategoryValue;
import com.google.gerrit.reviewdb.Project;
import com.google.gerrit.reviewdb.RefRight;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.reviewdb.SystemConfig;
import com.google.gerrit.server.workflow.NoOpFunction;
import com.google.gerrit.server.workflow.SubmitFunction;
import com.google.gerrit.testutil.TestDatabase;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.jdbc.JdbcSchema;

import junit.framework.TestCase;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;

public class SchemaCreatorTest extends TestCase {
  private ApprovalCategory.Id codeReview = new ApprovalCategory.Id("CRVW");
  private TestDatabase db;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    db = new TestDatabase();
  }

  @Override
  protected void tearDown() throws Exception {
    TestDatabase.drop(db);
    super.tearDown();
  }

  public void testGetCauses_CreateSchema() throws OrmException, SQLException {
    // Initially the schema should be empty.
    //
    {
      final JdbcSchema d = (JdbcSchema) db.open();
      try {
        final String[] types = {"TABLE", "VIEW"};
        final ResultSet rs =
            d.getConnection().getMetaData().getTables(null, null, null, types);
        try {
          assertFalse(rs.next());
        } finally {
          rs.close();
        }
      } finally {
        d.close();
      }
    }

    // Create the schema using the current schema version.
    //
    db.create();
    db.assertSchemaVersion();
    final SystemConfig config = db.getSystemConfig();
    assertNotNull(config);
    assertNotNull(config.adminGroupId);
    assertNotNull(config.anonymousGroupId);
    assertNotNull(config.registeredGroupId);

    // By default sitePath is set to the current working directory.
    //
    File sitePath = new File(".").getAbsoluteFile();
    if (sitePath.getName().equals(".")) {
      sitePath = sitePath.getParentFile();
    }
    assertEquals(sitePath.getAbsolutePath(), config.sitePath);

    // This is randomly generated and should be at least 20 bytes long.
    //
    assertNotNull(config.registerEmailPrivateKey);
    assertTrue(20 < config.registerEmailPrivateKey.length());
  }

  public void testSubsequentGetReads() throws OrmException {
    db.create();
    final SystemConfig exp = db.getSystemConfig();
    final SystemConfig act = db.getSystemConfig();

    assertNotSame(exp, act);
    assertEquals(exp.adminGroupId, act.adminGroupId);
    assertEquals(exp.anonymousGroupId, act.anonymousGroupId);
    assertEquals(exp.registeredGroupId, act.registeredGroupId);
    assertEquals(exp.sitePath, act.sitePath);
    assertEquals(exp.registerEmailPrivateKey, act.registerEmailPrivateKey);
  }

  public void testCreateSchema_Group_Administrators() throws OrmException {
    db.create();
    final SystemConfig config = db.getSystemConfig();
    final ReviewDb c = db.open();
    try {
      final AccountGroup admin = c.accountGroups().get(config.adminGroupId);
      assertNotNull(admin);
      assertEquals(config.adminGroupId, admin.getId());
      assertEquals("Administrators", admin.getName());
      assertSame(AccountGroup.Type.INTERNAL, admin.getType());
    } finally {
      c.close();
    }
  }

  public void testCreateSchema_Group_AnonymousUsers() throws OrmException {
    db.create();
    final SystemConfig config = db.getSystemConfig();
    final ReviewDb c = db.open();
    try {
      final AccountGroup anon = c.accountGroups().get(config.anonymousGroupId);
      assertNotNull(anon);
      assertEquals(config.anonymousGroupId, anon.getId());
      assertEquals("Anonymous Users", anon.getName());
      assertSame(AccountGroup.Type.SYSTEM, anon.getType());
    } finally {
      c.close();
    }
  }

  public void testCreateSchema_Group_RegisteredUsers() throws OrmException {
    db.create();
    final SystemConfig config = db.getSystemConfig();
    final ReviewDb c = db.open();
    try {
      final AccountGroup reg = c.accountGroups().get(config.registeredGroupId);
      assertNotNull(reg);
      assertEquals(config.registeredGroupId, reg.getId());
      assertEquals("Registered Users", reg.getName());
      assertSame(AccountGroup.Type.SYSTEM, reg.getType());
    } finally {
      c.close();
    }
  }

  public void testCreateSchema_WildCardProject() throws OrmException {
    final ReviewDb c = db.create().open();
    try {
      final SystemConfig cfg;
      final Project all;

      cfg = c.systemConfig().get(new SystemConfig.Key());
      all = c.projects().get(cfg.wildProjectName);
      assertNotNull(all);
      assertEquals("-- All Projects --", all.getName());
      assertFalse(all.isUseContributorAgreements());
      assertFalse(all.isUseSignedOffBy());
    } finally {
      c.close();
    }
  }

  public void testCreateSchema_ApprovalCategory_CodeReview()
      throws OrmException {
    final ReviewDb c = db.create().open();
    try {
      final ApprovalCategory cat;

      cat = c.approvalCategories().get(codeReview);
      assertNotNull(cat);
      assertEquals(codeReview, cat.getId());
      assertEquals("Code Review", cat.getName());
      assertEquals("R", cat.getAbbreviatedName());
      assertEquals("MaxWithBlock", cat.getFunctionName());
      assertTrue(cat.isCopyMinScore());
      assertFalse(cat.isAction());
      assertTrue(0 <= cat.getPosition());
    } finally {
      c.close();
    }
    assertValueRange(codeReview, -2, -1, 0, 1, 2);
  }

  public void testCreateSchema_ApprovalCategory_Read() throws OrmException {
    final ReviewDb c = db.create().open();
    try {
      final ApprovalCategory cat;

      cat = c.approvalCategories().get(ApprovalCategory.READ);
      assertNotNull(cat);
      assertEquals(ApprovalCategory.READ, cat.getId());
      assertEquals("Read Access", cat.getName());
      assertNull(cat.getAbbreviatedName());
      assertEquals(NoOpFunction.NAME, cat.getFunctionName());
      assertTrue(cat.isAction());
    } finally {
      c.close();
    }
    assertValueRange(ApprovalCategory.READ, -1, 1, 2);
  }

  public void testCreateSchema_ApprovalCategory_Submit() throws OrmException {
    final ReviewDb c = db.create().open();
    try {
      final ApprovalCategory cat;

      cat = c.approvalCategories().get(ApprovalCategory.SUBMIT);
      assertNotNull(cat);
      assertEquals(ApprovalCategory.SUBMIT, cat.getId());
      assertEquals("Submit", cat.getName());
      assertNull(cat.getAbbreviatedName());
      assertEquals(SubmitFunction.NAME, cat.getFunctionName());
      assertTrue(cat.isAction());
    } finally {
      c.close();
    }
    assertValueRange(ApprovalCategory.SUBMIT, 1);
  }

  public void testCreateSchema_ApprovalCategory_PushTag() throws OrmException {
    final ReviewDb c = db.create().open();
    try {
      final ApprovalCategory cat;

      cat = c.approvalCategories().get(ApprovalCategory.PUSH_TAG);
      assertNotNull(cat);
      assertEquals(ApprovalCategory.PUSH_TAG, cat.getId());
      assertEquals("Push Tag", cat.getName());
      assertNull(cat.getAbbreviatedName());
      assertEquals(NoOpFunction.NAME, cat.getFunctionName());
      assertTrue(cat.isAction());
    } finally {
      c.close();
    }
    assertValueRange(ApprovalCategory.PUSH_TAG, //
        ApprovalCategory.PUSH_TAG_SIGNED, //
        ApprovalCategory.PUSH_TAG_ANNOTATED);
  }

  public void testCreateSchema_ApprovalCategory_PushHead() throws OrmException {
    final ReviewDb c = db.create().open();
    try {
      final ApprovalCategory cat;

      cat = c.approvalCategories().get(ApprovalCategory.PUSH_HEAD);
      assertNotNull(cat);
      assertEquals(ApprovalCategory.PUSH_HEAD, cat.getId());
      assertEquals("Push Branch", cat.getName());
      assertNull(cat.getAbbreviatedName());
      assertEquals(NoOpFunction.NAME, cat.getFunctionName());
      assertTrue(cat.isAction());
    } finally {
      c.close();
    }
    assertValueRange(ApprovalCategory.PUSH_HEAD, //
        ApprovalCategory.PUSH_HEAD_UPDATE, //
        ApprovalCategory.PUSH_HEAD_CREATE, //
        ApprovalCategory.PUSH_HEAD_REPLACE);
  }

  public void testCreateSchema_ApprovalCategory_Owner() throws OrmException {
    final ReviewDb c = db.create().open();
    try {
      final ApprovalCategory cat;

      cat = c.approvalCategories().get(ApprovalCategory.OWN);
      assertNotNull(cat);
      assertEquals(ApprovalCategory.OWN, cat.getId());
      assertEquals("Owner", cat.getName());
      assertNull(cat.getAbbreviatedName());
      assertEquals(NoOpFunction.NAME, cat.getFunctionName());
      assertTrue(cat.isAction());
    } finally {
      c.close();
    }
    assertValueRange(ApprovalCategory.OWN, 1);
  }

  private void assertValueRange(ApprovalCategory.Id cat, int... range)
      throws OrmException {
    final HashSet<ApprovalCategoryValue.Id> act =
        new HashSet<ApprovalCategoryValue.Id>();
    final ReviewDb c = db.open();
    try {
      for (ApprovalCategoryValue v : c.approvalCategoryValues().byCategory(cat)) {
        assertNotNull(v.getId());
        assertNotNull(v.getName());
        assertEquals(cat, v.getCategoryId());
        assertFalse(v.getName().isEmpty());

        act.add(v.getId());
      }
    } finally {
      c.close();
    }

    for (int value : range) {
      final ApprovalCategoryValue.Id exp =
          new ApprovalCategoryValue.Id(cat, (short) value);
      if (!act.remove(exp)) {
        fail("Category " + cat + " lacks value " + value);
      }
    }
    if (!act.isEmpty()) {
      fail("Category " + cat + " has additional values: " + act);
    }
  }

  public void testCreateSchema_DefaultAccess_AnonymousUsers()
      throws OrmException {
    db.create();
    final SystemConfig config = db.getSystemConfig();
    assertDefaultRight("refs/*", config.anonymousGroupId,
        ApprovalCategory.READ, 1, 1);
  }

  public void testCreateSchema_DefaultAccess_RegisteredUsers()
      throws OrmException {
    db.create();
    final SystemConfig config = db.getSystemConfig();
    assertDefaultRight("refs/*", config.registeredGroupId,
        ApprovalCategory.READ, 1, 2);
    assertDefaultRight("refs/heads/*", config.registeredGroupId, codeReview,
        -1, 1);
  }

  public void testCreateSchema_DefaultAccess_Administrators()
      throws OrmException {
    db.create();
    final SystemConfig config = db.getSystemConfig();
    assertDefaultRight("refs/*", config.adminGroupId, ApprovalCategory.READ, 1,
        1);
  }

  private void assertDefaultRight(final String pattern,
      final AccountGroup.Id group, final ApprovalCategory.Id category, int min,
      int max) throws OrmException {
    final ReviewDb c = db.open();
    try {
      final SystemConfig cfg;
      final Project all;
      final RefRight right;

      cfg = c.systemConfig().get(new SystemConfig.Key());
      all = c.projects().get(cfg.wildProjectName);
      right =
          c.refRights().get(
              new RefRight.Key(all.getNameKey(), new RefRight.RefPattern(
                  pattern), category, group));

      assertNotNull(right);
      assertEquals(all.getNameKey(), right.getProjectNameKey());
      assertEquals(group, right.getAccountGroupId());
      assertEquals(category, right.getApprovalCategoryId());
      assertEquals(min, right.getMinValue());
      assertEquals(max, right.getMaxValue());
    } finally {
      c.close();
    }
  }
}