summaryrefslogtreecommitdiffstats
path: root/javatests/com/google/gerrit/server/schema/Schema_150_to_151_Test.java
blob: 4d5db6da3b8956f4a57ece0c945faca1dd2dede5 (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
// Copyright (C) 2017 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 static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;

import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.AccountGroup.Id;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.reviewdb.server.ReviewDbWrapper;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.Sequences;
import com.google.gerrit.server.account.GroupUUID;
import com.google.gerrit.server.util.time.TimeUtil;
import com.google.gerrit.testing.InMemoryTestEnvironment;
import com.google.gerrit.testing.TestUpdateUI;
import com.google.gwtorm.jdbc.JdbcSchema;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import org.eclipse.jgit.lib.PersonIdent;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class Schema_150_to_151_Test {

  @Rule public InMemoryTestEnvironment testEnv = new InMemoryTestEnvironment();

  @Inject private Schema_151 schema151;
  @Inject private ReviewDb db;
  @Inject private IdentifiedUser currentUser;
  @Inject private @GerritPersonIdent Provider<PersonIdent> serverIdent;
  @Inject private Sequences seq;

  private Connection connection;
  private PreparedStatement createdOnRetrieval;
  private PreparedStatement createdOnUpdate;
  private PreparedStatement auditEntryDeletion;
  private JdbcSchema jdbcSchema;

  @Before
  public void unwrapDb() {
    jdbcSchema = ReviewDbWrapper.unwrapJbdcSchema(db);
  }

  @Before
  public void setUp() throws Exception {
    assume().that(db instanceof JdbcSchema).isTrue();

    connection = ((JdbcSchema) db).getConnection();

    try (Statement stmt = connection.createStatement()) {
      stmt.execute(
          "CREATE TABLE account_groups ("
              + " group_uuid varchar(255) DEFAULT '' NOT NULL,"
              + " group_id INTEGER DEFAULT 0 NOT NULL,"
              + " name varchar(255) DEFAULT '' NOT NULL,"
              + " created_on TIMESTAMP,"
              + " description CLOB,"
              + " owner_group_uuid varchar(255) DEFAULT '' NOT NULL,"
              + " visible_to_all CHAR(1) DEFAULT 'N' NOT NULL"
              + ")");

      stmt.execute(
          "CREATE TABLE account_group_members ("
              + " group_id INTEGER DEFAULT 0 NOT NULL,"
              + " account_id INTEGER DEFAULT 0 NOT NULL"
              + ")");

      stmt.execute(
          "CREATE TABLE account_group_members_audit ("
              + " group_id INTEGER DEFAULT 0 NOT NULL,"
              + " account_id INTEGER DEFAULT 0 NOT NULL,"
              + " added_by INTEGER DEFAULT 0 NOT NULL,"
              + " added_on TIMESTAMP,"
              + " removed_by INTEGER,"
              + " removed_on TIMESTAMP"
              + ")");
    }

    createdOnRetrieval =
        connection.prepareStatement("SELECT created_on FROM account_groups WHERE group_id = ?");
    createdOnUpdate =
        connection.prepareStatement("UPDATE account_groups SET created_on = ? WHERE group_id = ?");
    auditEntryDeletion =
        connection.prepareStatement("DELETE FROM account_group_members_audit WHERE group_id = ?");
  }

  @After
  public void tearDown() throws Exception {
    if (auditEntryDeletion != null) {
      auditEntryDeletion.close();
    }
    if (createdOnUpdate != null) {
      createdOnUpdate.close();
    }
    if (createdOnRetrieval != null) {
      createdOnRetrieval.close();
    }
    if (connection != null) {
      connection.close();
    }
  }

  @Test
  public void createdOnIsPopulatedForGroupsCreatedAfterAudit() throws Exception {
    Timestamp testStartTime = TimeUtil.nowTs();
    AccountGroup.Id groupId = createGroupInReviewDb("Group for schema migration");
    setCreatedOnToVeryOldTimestamp(groupId);

    schema151.migrateData(db, new TestUpdateUI());

    Timestamp createdOn = getCreatedOn(groupId);
    assertThat(createdOn).isAtLeast(testStartTime);
  }

  @Test
  public void createdOnIsPopulatedForGroupsCreatedBeforeAudit() throws Exception {
    AccountGroup.Id groupId = createGroupInReviewDb("Ancient group for schema migration");
    setCreatedOnToVeryOldTimestamp(groupId);
    removeAuditEntriesFor(groupId);

    schema151.migrateData(db, new TestUpdateUI());

    Timestamp createdOn = getCreatedOn(groupId);
    assertThat(createdOn).isEqualTo(AccountGroup.auditCreationInstantTs());
  }

  private AccountGroup.Id createGroupInReviewDb(String name) throws Exception {
    AccountGroup group =
        new AccountGroup(
            new AccountGroup.NameKey(name),
            new AccountGroup.Id(seq.nextGroupId()),
            GroupUUID.make(name, serverIdent.get()),
            TimeUtil.nowTs());
    storeInReviewDb(group);
    addMembersInReviewDb(group.getId(), currentUser.getAccountId());
    return group.getId();
  }

  private Timestamp getCreatedOn(Id groupId) throws Exception {
    createdOnRetrieval.setInt(1, groupId.get());
    try (ResultSet results = createdOnRetrieval.executeQuery()) {
      if (results.first()) {
        return results.getTimestamp(1);
      }
    }
    return null;
  }

  private void setCreatedOnToVeryOldTimestamp(Id groupId) throws Exception {
    createdOnUpdate.setInt(1, groupId.get());
    Instant instant = LocalDateTime.of(1800, Month.JANUARY, 1, 0, 0).toInstant(ZoneOffset.UTC);
    createdOnUpdate.setTimestamp(1, Timestamp.from(instant));
    createdOnUpdate.setInt(2, groupId.get());
    createdOnUpdate.executeUpdate();
  }

  private void removeAuditEntriesFor(AccountGroup.Id groupId) throws Exception {
    auditEntryDeletion.setInt(1, groupId.get());
    auditEntryDeletion.executeUpdate();
  }

  private void storeInReviewDb(AccountGroup... groups) throws Exception {
    try (PreparedStatement stmt =
        jdbcSchema
            .getConnection()
            .prepareStatement(
                "INSERT INTO account_groups"
                    + " (group_uuid,"
                    + " group_id,"
                    + " name,"
                    + " description,"
                    + " created_on,"
                    + " owner_group_uuid,"
                    + " visible_to_all) VALUES (?, ?, ?, ?, ?, ?, ?)")) {
      for (AccountGroup group : groups) {
        stmt.setString(1, group.getGroupUUID().get());
        stmt.setInt(2, group.getId().get());
        stmt.setString(3, group.getName());
        stmt.setString(4, group.getDescription());
        stmt.setTimestamp(5, group.getCreatedOn());
        stmt.setString(6, group.getOwnerGroupUUID().get());
        stmt.setString(7, group.isVisibleToAll() ? "Y" : "N");
        stmt.addBatch();
      }
      stmt.executeBatch();
    }
  }

  private void addMembersInReviewDb(AccountGroup.Id groupId, Account.Id... memberIds)
      throws Exception {
    try (PreparedStatement addMemberStmt =
            jdbcSchema
                .getConnection()
                .prepareStatement(
                    "INSERT INTO account_group_members"
                        + " (group_id,"
                        + " account_id) VALUES ("
                        + groupId.get()
                        + ", ?)");
        PreparedStatement addMemberAuditStmt =
            jdbcSchema
                .getConnection()
                .prepareStatement(
                    "INSERT INTO account_group_members_audit"
                        + " (group_id,"
                        + " account_id,"
                        + " added_by,"
                        + " added_on) VALUES ("
                        + groupId.get()
                        + ", ?, "
                        + currentUser.getAccountId().get()
                        + ", ?)")) {
      Timestamp addedOn = TimeUtil.nowTs();
      for (Account.Id memberId : memberIds) {
        addMemberStmt.setInt(1, memberId.get());
        addMemberStmt.addBatch();

        addMemberAuditStmt.setInt(1, memberId.get());
        addMemberAuditStmt.setTimestamp(2, addedOn);
        addMemberAuditStmt.addBatch();
      }
      addMemberStmt.executeBatch();
      addMemberAuditStmt.executeBatch();
    }
  }
}