3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info 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.platform.serverid;
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.sonar.api.config.Configuration;
31 import org.sonar.api.config.internal.MapSettings;
32 import org.sonar.core.platform.ServerId;
33 import org.sonar.core.util.UuidFactory;
34 import org.sonar.core.util.Uuids;
36 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.api.Assertions.assertThatThrownBy;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.core.platform.ServerId.DATABASE_ID_LENGTH;
42 import static org.sonar.core.platform.ServerId.NOT_UUID_DATASET_ID_LENGTH;
43 import static org.sonar.core.platform.ServerId.UUID_DATASET_ID_LENGTH;
44 import static org.sonar.process.ProcessProperties.Property.JDBC_URL;
45 import static org.sonar.server.platform.serverid.ServerIdFactoryImpl.crc32Hex;
47 @RunWith(DataProviderRunner.class)
48 public class ServerIdFactoryImplTest {
49 private static final ServerId A_SERVERID = ServerId.of(randomAlphabetic(DATABASE_ID_LENGTH), randomAlphabetic(UUID_DATASET_ID_LENGTH));
52 private MapSettings settings = new MapSettings();
53 private Configuration config = settings.asConfig();
54 private UuidFactory uuidFactory = mock(UuidFactory.class);
55 private JdbcUrlSanitizer jdbcUrlSanitizer = mock(JdbcUrlSanitizer.class);
56 private ServerIdFactoryImpl underTest = new ServerIdFactoryImpl(config, uuidFactory, jdbcUrlSanitizer);
59 public void create_from_scratch_fails_with_ISE_if_JDBC_property_not_set() {
60 expectMissingJdbcUrlISE(() -> underTest.create());
64 public void create_from_scratch_creates_ServerId_from_JDBC_URL_and_new_uuid() {
65 String jdbcUrl = "jdbc";
66 String uuid = Uuids.create();
67 String sanitizedJdbcUrl = "sanitized_jdbc";
68 settings.setProperty(JDBC_URL.getKey(), jdbcUrl);
69 when(uuidFactory.create()).thenReturn(uuid);
70 when(jdbcUrlSanitizer.sanitize(jdbcUrl)).thenReturn(sanitizedJdbcUrl);
72 ServerId serverId = underTest.create();
74 assertThat(serverId.getDatabaseId()).contains(crc32Hex(sanitizedJdbcUrl));
75 assertThat(serverId.getDatasetId()).isEqualTo(uuid);
79 public void create_from_ServerId_fails_with_ISE_if_JDBC_property_not_set() {
80 expectMissingJdbcUrlISE(() -> underTest.create(A_SERVERID));
84 @UseDataProvider("anyFormatServerId")
85 public void create_from_ServerId_creates_ServerId_from_JDBC_URL_and_serverId_datasetId(ServerId currentServerId) {
86 String jdbcUrl = "jdbc";
87 String sanitizedJdbcUrl = "sanitized_jdbc";
88 settings.setProperty(JDBC_URL.getKey(), jdbcUrl);
89 when(uuidFactory.create()).thenThrow(new IllegalStateException("UuidFactory.create() should not be called"));
90 when(jdbcUrlSanitizer.sanitize(jdbcUrl)).thenReturn(sanitizedJdbcUrl);
92 ServerId serverId = underTest.create(currentServerId);
94 assertThat(serverId.getDatabaseId()).contains(crc32Hex(sanitizedJdbcUrl));
95 assertThat(serverId.getDatasetId()).isEqualTo(currentServerId.getDatasetId());
99 public static Object[][] anyFormatServerId() {
100 return new Object[][] {
101 {ServerId.parse(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))},
102 {ServerId.parse(randomAlphabetic(NOT_UUID_DATASET_ID_LENGTH))},
103 {ServerId.parse(randomAlphabetic(UUID_DATASET_ID_LENGTH))},
104 {ServerId.of(randomAlphabetic(DATABASE_ID_LENGTH), randomAlphabetic(NOT_UUID_DATASET_ID_LENGTH))},
105 {ServerId.of(randomAlphabetic(DATABASE_ID_LENGTH), randomAlphabetic(UUID_DATASET_ID_LENGTH))}
109 private void expectMissingJdbcUrlISE(ThrowingCallable callback) {
110 assertThatThrownBy(callback)
111 .isInstanceOf(IllegalStateException.class)
112 .hasMessage("Missing JDBC URL");