From a396c6dcefeaf6aca14ba957197f7e45e91ecb5b Mon Sep 17 00:00:00 2001 From: Dejan Milisavljevic Date: Thu, 8 Aug 2024 15:27:05 +0200 Subject: [PATCH] SONAR-22731 Define flag to be able to use legacy ratings --- .../usertoken/UserTokenAuthenticationIT.java | 6 +- .../core/config/CorePropertyDefinitions.java | 1 + .../core/config/LegacyRatingConstants.java | 31 +++++++++++ .../core/config/LegacyRatingProperties.java | 55 +++++++++++++++++++ .../config/LegacyRatingPropertiesTest.java | 31 +++++++++++ 5 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 sonar-core/src/main/java/org/sonar/core/config/LegacyRatingConstants.java create mode 100644 sonar-core/src/main/java/org/sonar/core/config/LegacyRatingProperties.java create mode 100644 sonar-core/src/test/java/org/sonar/core/config/LegacyRatingPropertiesTest.java diff --git a/server/sonar-webserver-auth/src/it/java/org/sonar/server/usertoken/UserTokenAuthenticationIT.java b/server/sonar-webserver-auth/src/it/java/org/sonar/server/usertoken/UserTokenAuthenticationIT.java index a4546a82ce5..bda4835a225 100644 --- a/server/sonar-webserver-auth/src/it/java/org/sonar/server/usertoken/UserTokenAuthenticationIT.java +++ b/server/sonar-webserver-auth/src/it/java/org/sonar/server/usertoken/UserTokenAuthenticationIT.java @@ -150,7 +150,7 @@ public class UserTokenAuthenticationIT { String tokenHash = "123456789"; when(tokenGenerator.hash(token)).thenReturn(tokenHash); UserDto user1 = db.users().insertUser(); - UserTokenDto userTokenDto = db.users().insertToken(user1, t -> t.setTokenHash(tokenHash)); + db.users().insertToken(user1, t -> t.setTokenHash(tokenHash)); UserDto user2 = db.users().insertUser(); db.users().insertToken(user2, t -> t.setTokenHash("another-token-hash")); return token; @@ -206,7 +206,7 @@ public class UserTokenAuthenticationIT { public void throw_authentication_exception_if_token_is_expired() { String token = "known-token"; String tokenHash = "123456789"; - long expirationTimestamp = System.currentTimeMillis(); + long expirationTimestamp = System.currentTimeMillis() - 1; when(request.getHeader(AUTHORIZATION_HEADER)).thenReturn("Basic " + toBase64(token + ":")); when(tokenGenerator.hash(token)).thenReturn(tokenHash); UserDto user1 = db.users().insertUser(); @@ -270,7 +270,7 @@ public class UserTokenAuthenticationIT { @Test public void does_not_authenticate_from_user_token_when_token_does_not_match_active_user() { UserDto user = db.users().insertDisabledUser(); - String tokenName = db.users().insertToken(user, t -> t.setTokenHash(NEW_USER_TOKEN_HASH).setType(USER_TOKEN.name())).getName(); + db.users().insertToken(user, t -> t.setTokenHash(NEW_USER_TOKEN_HASH).setType(USER_TOKEN.name())).getName(); when(request.getHeader(AUTHORIZATION_HEADER)).thenReturn("Basic " + toBase64(EXAMPLE_NEW_USER_TOKEN + ":")); diff --git a/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java b/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java index 758efac19cd..86f2f2ea797 100644 --- a/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java +++ b/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java @@ -68,6 +68,7 @@ public class CorePropertyDefinitions { defs.addAll(PurgeProperties.all()); defs.addAll(EmailSettings.definitions()); defs.addAll(ScannerProperties.all()); + defs.addAll(LegacyRatingProperties.all()); defs.addAll(asList( PropertyDefinition.builder(CoreProperties.MODULE_LEVEL_ARCHIVED_SETTINGS) diff --git a/sonar-core/src/main/java/org/sonar/core/config/LegacyRatingConstants.java b/sonar-core/src/main/java/org/sonar/core/config/LegacyRatingConstants.java new file mode 100644 index 00000000000..847de4cd930 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/config/LegacyRatingConstants.java @@ -0,0 +1,31 @@ +/* + * 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.core.config; + +public class LegacyRatingConstants { + + public static final String LEGACY_RATING_CATEGORY = "Legacy rating mode"; + public static final String LEGACY_RATING_SUB_CATEGORY = "Legacy rating mode"; + public static final String LEGACY_RATING_MODE_ENABLED = "sonar.legacy.ratings.mode.enabled"; + + private LegacyRatingConstants() { + //class cannot be instantiated + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/config/LegacyRatingProperties.java b/sonar-core/src/main/java/org/sonar/core/config/LegacyRatingProperties.java new file mode 100644 index 00000000000..9e56594cf82 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/config/LegacyRatingProperties.java @@ -0,0 +1,55 @@ +/* + * 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.core.config; + +import java.util.Collections; +import java.util.List; +import org.sonar.api.PropertyType; +import org.sonar.api.config.PropertyDefinition; + +import static org.sonar.core.config.LegacyRatingConstants.LEGACY_RATING_CATEGORY; +import static org.sonar.core.config.LegacyRatingConstants.LEGACY_RATING_MODE_ENABLED; +import static org.sonar.core.config.LegacyRatingConstants.LEGACY_RATING_SUB_CATEGORY; + +public final class LegacyRatingProperties { + + private LegacyRatingProperties() { + } + + public static List all() { + return Collections.singletonList( + PropertyDefinition.builder(LEGACY_RATING_MODE_ENABLED) + .defaultValue(Boolean.FALSE.toString()) + .name("Enable legacy mode") + //TODO Need to add the correct link to the documentation once it is available + .description("Ratings have updated logic and have grades ranging from A to D, while the old scale ranges from A to E " + + "(read more about why)." + + "

" + + "If you choose legacy mode, ratings and other counts will be calculated using the former logic. The old ratings scale (A-E) is " + + "deprecated and is scheduled for replacement by the new scale (A-D) in the next LTA.") + .type(PropertyType.BOOLEAN) + .category(LEGACY_RATING_CATEGORY) + .subCategory(LEGACY_RATING_SUB_CATEGORY) + .index(1) + .build() + ); + + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/config/LegacyRatingPropertiesTest.java b/sonar-core/src/test/java/org/sonar/core/config/LegacyRatingPropertiesTest.java new file mode 100644 index 00000000000..4c23430a9ba --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/config/LegacyRatingPropertiesTest.java @@ -0,0 +1,31 @@ +/* + * 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.core.config; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class LegacyRatingPropertiesTest { + @Test + void all_shouldGetProperties() { + assertThat(LegacyRatingProperties.all()).hasSize(1); + } +} -- 2.39.5