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;
34 import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.Assertions.assertThatThrownBy;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.spy;
39 import static org.mockito.Mockito.when;
40 import static org.sonar.core.platform.ServerId.DATABASE_ID_LENGTH;
41 import static org.sonar.core.platform.ServerId.NOT_UUID_DATASET_ID_LENGTH;
42 import static org.sonar.core.platform.ServerId.UUID_DATASET_ID_LENGTH;
43 import static org.sonar.process.ProcessProperties.Property.JDBC_URL;
44 import static org.sonar.server.platform.serverid.ServerIdFactoryImpl.crc32Hex;
46 @RunWith(DataProviderRunner.class)
47 public class ServerIdFactoryImplTest {
48 private static final ServerId A_SERVERID = ServerId.of(randomAlphabetic(DATABASE_ID_LENGTH), randomAlphabetic(UUID_DATASET_ID_LENGTH));
50 private MapSettings settings = new MapSettings();
51 private Configuration config = settings.asConfig();
52 private ServerIdGenerator serverIdGenerator = spy(new ServerIdGenerator());
53 private JdbcUrlSanitizer jdbcUrlSanitizer = mock(JdbcUrlSanitizer.class);
54 private ServerIdFactoryImpl underTest = new ServerIdFactoryImpl(config, serverIdGenerator, jdbcUrlSanitizer);
57 public void create_from_scratch_fails_with_ISE_if_JDBC_property_not_set() {
58 expectMissingJdbcUrlISE(() -> underTest.create());
62 public void create_from_scratch_creates_ServerId_from_JDBC_URL_and_new_uuid() {
63 String jdbcUrl = "jdbc";
64 String sanitizedJdbcUrl = "sanitized_jdbc";
66 String uuid = serverIdGenerator.generate();
67 when(serverIdGenerator.generate()).thenReturn(uuid);
69 settings.setProperty(JDBC_URL.getKey(), jdbcUrl);
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(serverIdGenerator.generate()).thenThrow(new IllegalStateException("generate 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");