]> source.dussan.org Git - sonarqube.git/blob
a8d73b2ce8fca741afe849e6abe90ab45588934d
[sonarqube.git] /
1 package org.sonar.server.platform.db.migration.version.v66;/*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 import java.sql.SQLException;
22 import java.util.List;
23 import java.util.Map;
24 import javax.annotation.Nullable;
25 import org.apache.commons.lang.math.RandomUtils;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.db.CoreDbTester;
30
31 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.api.Assertions.entry;
34
35 public class PopulateAnalysisUuidColumnOnWebhookDeliveriesTest {
36   private static final String TABLE_WEBHOOK_DELIVERY = "webhook_deliveries";
37   private static final String TABLE_CE_ACTIVITIY = "ce_activity";
38
39   @Rule
40   public final CoreDbTester db = CoreDbTester.createForSchema(PopulateAnalysisUuidColumnOnWebhookDeliveriesTest.class, "initial.sql");
41   @Rule
42   public ExpectedException expectedException = ExpectedException.none();
43
44   private PopulateAnalysisUuidColumnOnWebhookDeliveries underTest = new PopulateAnalysisUuidColumnOnWebhookDeliveries(db.database());
45
46   @Test
47   public void migration_must_set_analysis_uuid() throws SQLException {
48     String ceTaskUuid = randomAlphanumeric(40);
49     String analysisUuid = randomAlphanumeric(40);
50     String webhookDeliveryUuid = randomAlphanumeric(40);
51     insertWebhookDelivery(webhookDeliveryUuid,null, ceTaskUuid);
52     insertCeActivity(ceTaskUuid, analysisUuid);
53
54     assertThat(db.countRowsOfTable(TABLE_WEBHOOK_DELIVERY)).isEqualTo(1);
55     assertThat(db.countRowsOfTable(TABLE_CE_ACTIVITIY)).isEqualTo(1);
56     underTest.execute();
57
58     List<Map<String, Object>> maps = selectAllWebhookDeliveries();
59     assertThat(maps).hasSize(1);
60     assertThat(maps.get(0)).containsExactly(
61       entry("ANALYSIS_UUID", analysisUuid), entry("UUID", webhookDeliveryUuid), entry("CE_TASK_UUID", ceTaskUuid));
62   }
63
64   @Test
65   public void migration_should_be_reentrant() throws SQLException {
66     for (int i = 0; i < 10; i++) {
67       insertWebhookDelivery(randomAlphanumeric(40),null, randomAlphanumeric(40));
68       insertCeActivity(randomAlphanumeric(40), randomAlphanumeric(40));
69     }
70
71     underTest.execute();
72     List<Map<String, Object>> firstExecutionResult = selectAllWebhookDeliveries();
73     underTest.execute();
74     assertThat(selectAllWebhookDeliveries()).isEqualTo(firstExecutionResult);
75   }
76
77   private List<Map<String, Object>> selectAllWebhookDeliveries() {
78     return db.select("select uuid, ce_task_uuid, analysis_uuid from webhook_deliveries");
79   }
80
81   private void insertCeActivity(String uuid, String analysisUuid) {
82     db.executeInsert(TABLE_CE_ACTIVITIY,
83       "UUID", uuid,
84       "TASK_TYPE", randomAlphanumeric(5),
85       "ANALYSIS_UUID", analysisUuid,
86       "STATUS", randomAlphanumeric(5),
87       "IS_LAST", RandomUtils.nextBoolean(),
88       "IS_LAST_KEY", randomAlphanumeric(50),
89       "EXECUTION_COUNT", RandomUtils.nextInt(10),
90       "SUBMITTED_AT", RandomUtils.nextInt(),
91       "CREATED_AT", RandomUtils.nextInt(),
92       "UPDATED_AT", RandomUtils.nextInt()
93     );
94   }
95
96   private void insertWebhookDelivery(String uuid, @Nullable String analysisUuid, String ceTaskUuid) {
97     db.executeInsert(TABLE_WEBHOOK_DELIVERY,
98       "UUID", uuid,
99       "COMPONENT_UUID", randomAlphanumeric(30),
100       "ANALYSIS_UUID", analysisUuid,
101       "CE_TASK_UUID", ceTaskUuid,
102       "NAME", randomAlphanumeric(15),
103       "URL", randomAlphanumeric(15),
104       "SUCCESS", RandomUtils.nextBoolean(),
105       "PAYLOAD", randomAlphanumeric(200),
106       "CREATED_AT", RandomUtils.nextInt()
107     );
108   }
109 }