aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-core/src
diff options
context:
space:
mode:
authorAurelien Poscia <aurelien.poscia@sonarsource.com>2024-01-19 10:08:55 +0100
committersonartech <sonartech@sonarsource.com>2024-01-25 20:02:56 +0000
commit858b1023bba599c5aa44119469d8451301434abb (patch)
treee0a8621447c7b5b8c7ea447a57deb9feab3f5b31 /server/sonar-webserver-core/src
parentc7d0bb440b8e6bf4c625463b05ec390f1a7a27ab (diff)
downloadsonarqube-858b1023bba599c5aa44119469d8451301434abb.tar.gz
sonarqube-858b1023bba599c5aa44119469d8451301434abb.zip
SONAR-21290 Use UUIDs v4 for all database identifiers
Diffstat (limited to 'server/sonar-webserver-core/src')
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdFactoryImpl.java9
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdModule.java4
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/HttpRequestIdModule.java4
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdConfiguration.java35
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorBase.java29
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorBaseImpl.java31
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorImpl.java69
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdFactoryImplTest.java16
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdModuleTest.java2
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestIdModuleTest.java2
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/RequestIdConfigurationTest.java33
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorImplTest.java62
12 files changed, 25 insertions, 271 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdFactoryImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdFactoryImpl.java
index 4ced9a60b8f..2951a33ee2c 100644
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdFactoryImpl.java
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdFactoryImpl.java
@@ -26,25 +26,24 @@ import java.util.zip.CRC32;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.config.Configuration;
import org.sonar.core.platform.ServerId;
-import org.sonar.core.util.UuidFactory;
import static org.sonar.process.ProcessProperties.Property.JDBC_URL;
public class ServerIdFactoryImpl implements ServerIdFactory {
private final Configuration config;
- private final UuidFactory uuidFactory;
+ private final ServerIdGenerator serverIdGenerator;
private final JdbcUrlSanitizer jdbcUrlSanitizer;
- public ServerIdFactoryImpl(Configuration config, UuidFactory uuidFactory, JdbcUrlSanitizer jdbcUrlSanitizer) {
+ public ServerIdFactoryImpl(Configuration config, ServerIdGenerator serverIdGenerator, JdbcUrlSanitizer jdbcUrlSanitizer) {
this.config = config;
- this.uuidFactory = uuidFactory;
+ this.serverIdGenerator = serverIdGenerator;
this.jdbcUrlSanitizer = jdbcUrlSanitizer;
}
@Override
public ServerId create() {
- return ServerId.of(computeDatabaseId(), uuidFactory.create());
+ return ServerId.of(computeDatabaseId(), serverIdGenerator.generate());
}
@Override
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdModule.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdModule.java
index 1df5c1c7968..b1b76e0ad7f 100644
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdModule.java
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/serverid/ServerIdModule.java
@@ -28,8 +28,8 @@ public class ServerIdModule extends Module {
ServerIdFactoryImpl.class,
JdbcUrlSanitizer.class,
ServerIdChecksum.class,
- ServerIdManager.class
-
+ ServerIdManager.class,
+ ServerIdGenerator.class
);
}
}
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/HttpRequestIdModule.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/HttpRequestIdModule.java
index 57973cdedda..b52fd08ea3a 100644
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/HttpRequestIdModule.java
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/HttpRequestIdModule.java
@@ -24,8 +24,6 @@ import org.sonar.core.platform.Module;
public class HttpRequestIdModule extends Module {
@Override
protected void configureModule() {
- add(new RequestIdConfiguration(RequestIdGeneratorImpl.UUID_GENERATOR_RENEWAL_COUNT),
- RequestIdGeneratorBaseImpl.class,
- RequestIdGeneratorImpl.class);
+ add(RequestIdGeneratorImpl.class);
}
}
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdConfiguration.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdConfiguration.java
deleted file mode 100644
index 5a204b5a082..00000000000
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdConfiguration.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2024 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.platform.web.requestid;
-
-public class RequestIdConfiguration {
- /**
- * @see RequestIdGeneratorImpl#mustRenewUuidGenerator(long)
- */
- private final long uuidGeneratorRenewalCount;
-
- public RequestIdConfiguration(long uuidGeneratorRenewalCount) {
- this.uuidGeneratorRenewalCount = uuidGeneratorRenewalCount;
- }
-
- public long getUidGeneratorRenewalCount() {
- return uuidGeneratorRenewalCount;
- }
-}
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorBase.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorBase.java
deleted file mode 100644
index c1277cad542..00000000000
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorBase.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2024 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.platform.web.requestid;
-
-import org.sonar.core.util.UuidGenerator;
-
-public interface RequestIdGeneratorBase {
- /**
- * Provides a new instance of {@link UuidGenerator.WithFixedBase} to be used by {@link RequestIdGeneratorImpl}.
- */
- UuidGenerator.WithFixedBase createNew();
-}
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorBaseImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorBaseImpl.java
deleted file mode 100644
index a0284897edd..00000000000
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorBaseImpl.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2024 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.platform.web.requestid;
-
-import org.sonar.core.util.UuidGenerator;
-import org.sonar.core.util.UuidGeneratorImpl;
-
-public class RequestIdGeneratorBaseImpl implements RequestIdGeneratorBase {
-
- @Override
- public UuidGenerator.WithFixedBase createNew() {
- return new UuidGeneratorImpl().withFixedBase();
- }
-}
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorImpl.java
index 198f3ae8373..9dbb734a027 100644
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorImpl.java
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorImpl.java
@@ -19,78 +19,13 @@
*/
package org.sonar.server.platform.web.requestid;
-import java.util.Base64;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.concurrent.atomic.AtomicReference;
-import org.sonar.core.util.UuidGenerator;
+import java.util.UUID;
-/**
- * This implementation of {@link RequestIdGenerator} creates unique identifiers for HTTP requests leveraging
- * {@link UuidGenerator.WithFixedBase#generate(int)} and a counter of HTTP requests.
- * <p>
- * To work around the limit of unique values produced by {@link UuidGenerator.WithFixedBase#generate(int)}, the
- * {@link UuidGenerator.WithFixedBase} instance will be renewed every
- * {@link RequestIdConfiguration#getUidGeneratorRenewalCount() RequestIdConfiguration#uidGeneratorRenewalCount}
- * HTTP requests.
- * </p>
- * <p>
- * This implementation is Thread safe.
- * </p>
- */
public class RequestIdGeneratorImpl implements RequestIdGenerator {
- /**
- * The value to which the HTTP request count will be compared to (using a modulo operator,
- * see {@link #mustRenewUuidGenerator(long)}).
- *
- * <p>
- * This value can't be the last value before {@link UuidGenerator.WithFixedBase#generate(int)} returns a non unique
- * value, ie. 2^23-1 because there is no guarantee the renewal will happen before any other thread calls
- * {@link UuidGenerator.WithFixedBase#generate(int)} method of the deplated {@link UuidGenerator.WithFixedBase} instance.
- * </p>
- *
- * <p>
- * To keep a comfortable margin of error, 2^22 will be used.
- * </p>
- */
- public static final long UUID_GENERATOR_RENEWAL_COUNT = 4_194_304;
-
- private final AtomicLong counter = new AtomicLong();
- private final RequestIdGeneratorBase requestIdGeneratorBase;
- private final RequestIdConfiguration requestIdConfiguration;
- private final AtomicReference<UuidGenerator.WithFixedBase> uuidGenerator;
-
- public RequestIdGeneratorImpl(RequestIdGeneratorBase requestIdGeneratorBase, RequestIdConfiguration requestIdConfiguration) {
- this.requestIdGeneratorBase = requestIdGeneratorBase;
- this.uuidGenerator = new AtomicReference<>(requestIdGeneratorBase.createNew());
- this.requestIdConfiguration = requestIdConfiguration;
- }
@Override
public String generate() {
- UuidGenerator.WithFixedBase currentUuidGenerator = this.uuidGenerator.get();
- long counterValue = counter.getAndIncrement();
- if (counterValue != 0 && mustRenewUuidGenerator(counterValue)) {
- UuidGenerator.WithFixedBase newUuidGenerator = requestIdGeneratorBase.createNew();
- uuidGenerator.set(newUuidGenerator);
- return generate(newUuidGenerator, counterValue);
- }
- return generate(currentUuidGenerator, counterValue);
- }
-
- /**
- * Since renewal of {@link UuidGenerator.WithFixedBase} instance is based on the HTTP request counter, only a single
- * thread can get the right value which will make this method return true. So, this is thread-safe by design, therefor
- * this method doesn't need external synchronization.
- * <p>
- * The value to which the counter is compared should however be chosen with caution: see {@link #UUID_GENERATOR_RENEWAL_COUNT}.
- * </p>
- */
- private boolean mustRenewUuidGenerator(long counter) {
- return counter % requestIdConfiguration.getUidGeneratorRenewalCount() == 0;
- }
-
- private static String generate(UuidGenerator.WithFixedBase uuidGenerator, long increment) {
- return Base64.getEncoder().encodeToString(uuidGenerator.generate((int) increment));
+ return UUID.randomUUID().toString();
}
}
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdFactoryImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdFactoryImplTest.java
index ee392696090..2e1c3f5cf4b 100644
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdFactoryImplTest.java
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdFactoryImplTest.java
@@ -30,13 +30,12 @@ import org.junit.runner.RunWith;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.core.platform.ServerId;
-import org.sonar.core.util.UuidFactory;
-import org.sonar.core.util.Uuids;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static org.sonar.core.platform.ServerId.DATABASE_ID_LENGTH;
import static org.sonar.core.platform.ServerId.NOT_UUID_DATASET_ID_LENGTH;
@@ -48,12 +47,11 @@ import static org.sonar.server.platform.serverid.ServerIdFactoryImpl.crc32Hex;
public class ServerIdFactoryImplTest {
private static final ServerId A_SERVERID = ServerId.of(randomAlphabetic(DATABASE_ID_LENGTH), randomAlphabetic(UUID_DATASET_ID_LENGTH));
-
private MapSettings settings = new MapSettings();
private Configuration config = settings.asConfig();
- private UuidFactory uuidFactory = mock(UuidFactory.class);
+ private ServerIdGenerator serverIdGenerator = spy(new ServerIdGenerator());
private JdbcUrlSanitizer jdbcUrlSanitizer = mock(JdbcUrlSanitizer.class);
- private ServerIdFactoryImpl underTest = new ServerIdFactoryImpl(config, uuidFactory, jdbcUrlSanitizer);
+ private ServerIdFactoryImpl underTest = new ServerIdFactoryImpl(config, serverIdGenerator, jdbcUrlSanitizer);
@Test
public void create_from_scratch_fails_with_ISE_if_JDBC_property_not_set() {
@@ -63,10 +61,12 @@ public class ServerIdFactoryImplTest {
@Test
public void create_from_scratch_creates_ServerId_from_JDBC_URL_and_new_uuid() {
String jdbcUrl = "jdbc";
- String uuid = Uuids.create();
String sanitizedJdbcUrl = "sanitized_jdbc";
+
+ String uuid = serverIdGenerator.generate();
+ when(serverIdGenerator.generate()).thenReturn(uuid);
+
settings.setProperty(JDBC_URL.getKey(), jdbcUrl);
- when(uuidFactory.create()).thenReturn(uuid);
when(jdbcUrlSanitizer.sanitize(jdbcUrl)).thenReturn(sanitizedJdbcUrl);
ServerId serverId = underTest.create();
@@ -86,7 +86,7 @@ public class ServerIdFactoryImplTest {
String jdbcUrl = "jdbc";
String sanitizedJdbcUrl = "sanitized_jdbc";
settings.setProperty(JDBC_URL.getKey(), jdbcUrl);
- when(uuidFactory.create()).thenThrow(new IllegalStateException("UuidFactory.create() should not be called"));
+ when(serverIdGenerator.generate()).thenThrow(new IllegalStateException("generate should not be called"));
when(jdbcUrlSanitizer.sanitize(jdbcUrl)).thenReturn(sanitizedJdbcUrl);
ServerId serverId = underTest.create(currentServerId);
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdModuleTest.java
index e1e7723b05b..8660e094b10 100644
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdModuleTest.java
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/serverid/ServerIdModuleTest.java
@@ -31,6 +31,6 @@ public class ServerIdModuleTest {
public void verify_count_of_added_components() {
ListContainer container = new ListContainer();
underTest.configure(container);
- assertThat(container.getAddedObjects()).hasSize(4);
+ assertThat(container.getAddedObjects()).hasSize(5);
}
}
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestIdModuleTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestIdModuleTest.java
index d9c4674c4b8..9530702d414 100644
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestIdModuleTest.java
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/HttpRequestIdModuleTest.java
@@ -31,6 +31,6 @@ public class HttpRequestIdModuleTest {
public void count_components_in_module() {
ListContainer container = new ListContainer();
underTest.configure(container);
- assertThat(container.getAddedObjects()).hasSize(3);
+ assertThat(container.getAddedObjects()).hasSize(1);
}
}
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/RequestIdConfigurationTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/RequestIdConfigurationTest.java
deleted file mode 100644
index 98d1aaf845a..00000000000
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/RequestIdConfigurationTest.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2024 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.platform.web.requestid;
-
-import org.junit.Test;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class RequestIdConfigurationTest {
- private RequestIdConfiguration underTest = new RequestIdConfiguration(50);
-
- @Test
- public void getUidGeneratorRenewalCount_returns_value_provided_from_constructor() {
- assertThat(underTest.getUidGeneratorRenewalCount()).isEqualTo(50);
- }
-}
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorImplTest.java
index 42a869ae1c9..99eca9c0256 100644
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorImplTest.java
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/web/requestid/RequestIdGeneratorImplTest.java
@@ -20,69 +20,19 @@
package org.sonar.server.platform.web.requestid;
import org.junit.Test;
-import org.sonar.core.util.UuidGenerator;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class RequestIdGeneratorImplTest {
- private UuidGenerator.WithFixedBase generator1 = increment -> new byte[] {124, 22, 66, 96, 55, 88, 2, 9};
- private UuidGenerator.WithFixedBase generator2 = increment -> new byte[] {0, 5, 88, 81, 8, 6, 44, 19};
- private UuidGenerator.WithFixedBase generator3 = increment -> new byte[] {126, 9, 35, 76, 2, 1, 2};
- private RequestIdGeneratorBase uidGeneratorBase = mock(RequestIdGeneratorBase.class);
- private IllegalStateException expected = new IllegalStateException("Unexpected third call to createNew");
+ RequestIdGeneratorImpl requestIdGenerator = new RequestIdGeneratorImpl();
@Test
- public void generate_renews_inner_UuidGenerator_instance_every_number_of_calls_to_generate_specified_in_RequestIdConfiguration_supports_2() {
- when(uidGeneratorBase.createNew())
- .thenReturn(generator1)
- .thenReturn(generator2)
- .thenReturn(generator3)
- .thenThrow(expected);
+ public void generate_shouldGenerateUniqueIds() {
+ String requestId1 = requestIdGenerator.generate();
+ String requestId2 = requestIdGenerator.generate();
+ String requestId3 = requestIdGenerator.generate();
- RequestIdGeneratorImpl underTest = new RequestIdGeneratorImpl(uidGeneratorBase, new RequestIdConfiguration(2));
-
- assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); // using generator1
- assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); // still using generator1
- assertThat(underTest.generate()).isEqualTo("AAVYUQgGLBM="); // renewing generator and using generator2
- assertThat(underTest.generate()).isEqualTo("AAVYUQgGLBM="); // still using generator2
- assertThat(underTest.generate()).isEqualTo("fgkjTAIBAg=="); // renewing generator and using generator3
- assertThat(underTest.generate()).isEqualTo("fgkjTAIBAg=="); // using generator3
-
- assertThatThrownBy(() -> {
- underTest.generate(); // renewing generator and failing
- })
- .isInstanceOf(IllegalStateException.class)
- .hasMessage(expected.getMessage());
- }
-
- @Test
- public void generate_renews_inner_UuidGenerator_instance_every_number_of_calls_to_generate_specified_in_RequestIdConfiguration_supports_3() {
- when(uidGeneratorBase.createNew())
- .thenReturn(generator1)
- .thenReturn(generator2)
- .thenReturn(generator3)
- .thenThrow(expected);
-
- RequestIdGeneratorImpl underTest = new RequestIdGeneratorImpl(uidGeneratorBase, new RequestIdConfiguration(3));
-
- assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); // using generator1
- assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); // still using generator1
- assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); // still using generator1
- assertThat(underTest.generate()).isEqualTo("AAVYUQgGLBM="); // renewing generator and using it
- assertThat(underTest.generate()).isEqualTo("AAVYUQgGLBM="); // still using generator2
- assertThat(underTest.generate()).isEqualTo("AAVYUQgGLBM="); // still using generator2
- assertThat(underTest.generate()).isEqualTo("fgkjTAIBAg=="); // renewing generator and using it
- assertThat(underTest.generate()).isEqualTo("fgkjTAIBAg=="); // using generator3
- assertThat(underTest.generate()).isEqualTo("fgkjTAIBAg=="); // using generator3
-
- assertThatThrownBy(() -> {
- underTest.generate(); // renewing generator and failing
- })
- .isInstanceOf(IllegalStateException.class)
- .hasMessage(expected.getMessage());
+ assertThat(requestId1).isNotEqualTo(requestId2).isNotEqualTo(requestId3);
}
}