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.web.requestid;
22 import org.junit.Test;
23 import org.sonar.core.util.UuidGenerator;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 public class RequestIdGeneratorImplTest {
32 private UuidGenerator.WithFixedBase generator1 = increment -> new byte[] {124, 22, 66, 96, 55, 88, 2, 9};
33 private UuidGenerator.WithFixedBase generator2 = increment -> new byte[] {0, 5, 88, 81, 8, 6, 44, 19};
34 private UuidGenerator.WithFixedBase generator3 = increment -> new byte[] {126, 9, 35, 76, 2, 1, 2};
35 private RequestIdGeneratorBase uidGeneratorBase = mock(RequestIdGeneratorBase.class);
36 private IllegalStateException expected = new IllegalStateException("Unexpected third call to createNew");
39 public void generate_renews_inner_UuidGenerator_instance_every_number_of_calls_to_generate_specified_in_RequestIdConfiguration_supports_2() {
40 when(uidGeneratorBase.createNew())
41 .thenReturn(generator1)
42 .thenReturn(generator2)
43 .thenReturn(generator3)
46 RequestIdGeneratorImpl underTest = new RequestIdGeneratorImpl(uidGeneratorBase, new RequestIdConfiguration(2));
48 assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); // using generator1
49 assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); // still using generator1
50 assertThat(underTest.generate()).isEqualTo("AAVYUQgGLBM="); // renewing generator and using generator2
51 assertThat(underTest.generate()).isEqualTo("AAVYUQgGLBM="); // still using generator2
52 assertThat(underTest.generate()).isEqualTo("fgkjTAIBAg=="); // renewing generator and using generator3
53 assertThat(underTest.generate()).isEqualTo("fgkjTAIBAg=="); // using generator3
55 assertThatThrownBy(() -> {
56 underTest.generate(); // renewing generator and failing
58 .isInstanceOf(IllegalStateException.class)
59 .hasMessage(expected.getMessage());
63 public void generate_renews_inner_UuidGenerator_instance_every_number_of_calls_to_generate_specified_in_RequestIdConfiguration_supports_3() {
64 when(uidGeneratorBase.createNew())
65 .thenReturn(generator1)
66 .thenReturn(generator2)
67 .thenReturn(generator3)
70 RequestIdGeneratorImpl underTest = new RequestIdGeneratorImpl(uidGeneratorBase, new RequestIdConfiguration(3));
72 assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); // using generator1
73 assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); // still using generator1
74 assertThat(underTest.generate()).isEqualTo("fBZCYDdYAgk="); // still using generator1
75 assertThat(underTest.generate()).isEqualTo("AAVYUQgGLBM="); // renewing generator and using it
76 assertThat(underTest.generate()).isEqualTo("AAVYUQgGLBM="); // still using generator2
77 assertThat(underTest.generate()).isEqualTo("AAVYUQgGLBM="); // still using generator2
78 assertThat(underTest.generate()).isEqualTo("fgkjTAIBAg=="); // renewing generator and using it
79 assertThat(underTest.generate()).isEqualTo("fgkjTAIBAg=="); // using generator3
80 assertThat(underTest.generate()).isEqualTo("fgkjTAIBAg=="); // using generator3
82 assertThatThrownBy(() -> {
83 underTest.generate(); // renewing generator and failing
85 .isInstanceOf(IllegalStateException.class)
86 .hasMessage(expected.getMessage());