diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2024-12-11 17:09:56 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-12-12 20:03:05 +0000 |
commit | f9acf6b6cf38ef07a600eb61ba85a23435b1b56a (patch) | |
tree | ef53fe1f3e02029bec72ea8e6bddb4ede4758c9b | |
parent | 2abcd2d2b6ee5ac0a0d0860f64eaa2ffe4416ce4 (diff) | |
download | sonarqube-branch-10.8.tar.gz sonarqube-branch-10.8.zip |
SONAR-23954 Fix calculation of the field installedVersionActive in the response of api/system/upgradesbranch-10.8
2 files changed, 59 insertions, 10 deletions
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/ActiveVersionEvaluator.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/ActiveVersionEvaluator.java index fae0633163c..3499b284fed 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/ActiveVersionEvaluator.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/platform/ws/ActiveVersionEvaluator.java @@ -86,11 +86,21 @@ public class ActiveVersionEvaluator { } private static Release findPreviousReleaseIgnoringPatch(SortedSet<Release> releases) { - Release refRelease = releases.last(); - List<Release> sublist = Lists.reverse(releases.stream().toList()); - for (Release release : sublist) { - if (compareWithoutPatchVersion(release.getVersion(), refRelease.getVersion()) < 0) { - return release; + if (!releases.isEmpty()) { + Release refRelease = releases.last(); + int patchesOfRefRelease = 0; + List<Release> sublist = Lists.reverse(releases.stream().toList()); + for (Release release : sublist) { + int versionComparison = compareWithoutPatchVersion(release.getVersion(), refRelease.getVersion()); + if (versionComparison < 0) { + return release; + } else if (versionComparison == 0) { + patchesOfRefRelease++; + } + } + // if all releases have the same version, return the last one + if (patchesOfRefRelease == releases.size()) { + return refRelease; } } throw new IllegalStateException("Unable to find previous release in releases"); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/ActiveVersionEvaluatorTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/ActiveVersionEvaluatorTest.java index d6d507e6b28..394e5a35874 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/ActiveVersionEvaluatorTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/ActiveVersionEvaluatorTest.java @@ -20,6 +20,7 @@ package org.sonar.server.platform.ws; import java.util.Calendar; +import java.util.Collections; import java.util.SortedSet; import java.util.TreeSet; import org.junit.jupiter.api.BeforeEach; @@ -106,12 +107,11 @@ class ActiveVersionEvaluatorTest { } @Test - void evaluateIfActiveVersion_whenNoPreviousReleasesFound_shouldThrowIllegalStateException() { + void evaluateIfActiveVersion_whenNoReleasesFound_shouldThrowIllegalStateException() { - when(sonarQubeVersion.get()).thenReturn(parse("10.4.1")); - TreeSet<Release> releases = new TreeSet<>(); - releases.add(new Release(sonar, Version.create("10.4.1"))); - when(sonar.getAllReleases(any())).thenReturn(releases); + when(sonarQubeVersion.get()).thenReturn(parse("10.8.0")); + + when(sonar.getAllReleases(any())).thenReturn(Collections.emptySortedSet()); assertThatThrownBy(() -> underTest.evaluateIfActiveVersion(updateCenter)) .isInstanceOf(IllegalStateException.class) @@ -134,6 +134,45 @@ class ActiveVersionEvaluatorTest { assertThat(underTest.evaluateIfActiveVersion(updateCenter)).isTrue(); } + @Test + void evaluateIfActiveVersion_whenInstalledVersionIsTheOnlyAvailableVersion_shouldReturnVersionIsActive() { + TreeSet<Release> releases = new TreeSet<>(); + releases.add(new Release(sonar, Version.create("10.8.0.12345"))); + + when(sonarQubeVersion.get()).thenReturn(parse("10.8.0.12345")); + when(updateCenter.getSonar().getAllReleases(any())).thenReturn(releases); + + assertThat(underTest.evaluateIfActiveVersion(updateCenter)).isTrue(); + } + + @Test + void evaluateIfActiveVersion_whenAvailableVersionsAreAllPatchesOfInstalledVersion_shouldReturnVersionIsActive() { + TreeSet<Release> releases = new TreeSet<>(); + releases.add(new Release(sonar, Version.create("10.8.0.12345"))); + releases.add(new Release(sonar, Version.create("10.8.1.12346"))); + when(sonar.getAllReleases(any())).thenReturn(releases); + + when(sonarQubeVersion.get()).thenReturn(parse("10.8.0.12345")); + when(updateCenter.getSonar().getAllReleases(any())).thenReturn(releases); + + assertThat(underTest.evaluateIfActiveVersion(updateCenter)).isTrue(); + } + + @Test + void evaluateIfActiveVersion_whenAvailableVersionsHaveDifferentNamingScheme_shouldReturnVersionIsActive() { + TreeSet<Release> releases = new TreeSet<>(); + releases.add(new Release(sonar, Version.create("10.8.0.12345"))); + releases.add(new Release(sonar, Version.create("10.8.1.12346"))); + releases.add(new Release(sonar, Version.create("2025.1.0.12347"))); + when(sonar.getAllReleases(any())).thenReturn(releases); + + when(sonarQubeVersion.get()).thenReturn(parse("10.8.0.12345")); + when(updateCenter.getSonar().getAllReleases(any())).thenReturn(releases); + + assertThat(underTest.evaluateIfActiveVersion(updateCenter)).isTrue(); + } + + public static SortedSet<Release> getReleases() { TreeSet<Release> releases = new TreeSet<>(); releases.add(new Release(sonar, Version.create("9.9"))); |