diff options
author | Léo Geoffroy <leo.geoffroy@sonarsource.com> | 2024-11-21 16:09:04 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-11-26 20:02:50 +0000 |
commit | 3c68707953061224705c3cf915b49b73c970149e (patch) | |
tree | 5a8ec5e64046e954362e18182a8b03e2920b908f /server/sonar-webserver-webapi-v2 | |
parent | 53575cc8e467fe4c4fb2b468f5373f5d5f530f75 (diff) | |
download | sonarqube-3c68707953061224705c3cf915b49b73c970149e.tar.gz sonarqube-3c68707953061224705c3cf915b49b73c970149e.zip |
SONAR-23652 Add modified flag to payload of mode endpoint
Diffstat (limited to 'server/sonar-webserver-webapi-v2')
3 files changed, 40 insertions, 12 deletions
diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/mode/controller/DefaultModeController.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/mode/controller/DefaultModeController.java index ba722bf48ea..2fb0f75f556 100644 --- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/mode/controller/DefaultModeController.java +++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/mode/controller/DefaultModeController.java @@ -33,6 +33,7 @@ import org.sonar.server.v2.api.mode.resources.ModeResource; import static org.sonar.core.config.MQRModeConstants.MULTI_QUALITY_MODE_DEFAULT_VALUE; import static org.sonar.core.config.MQRModeConstants.MULTI_QUALITY_MODE_ENABLED; +import static org.sonar.core.config.MQRModeConstants.MULTI_QUALITY_MODE_MODIFIED; public class DefaultModeController implements ModeController { @@ -56,7 +57,15 @@ public class DefaultModeController implements ModeController { @Override public ModeResource getMode() { ModeEnum mode = configuration.getBoolean(MULTI_QUALITY_MODE_ENABLED).orElse(MULTI_QUALITY_MODE_DEFAULT_VALUE) ? ModeEnum.MQR : ModeEnum.STANDARD_EXPERIENCE; - return new ModeResource(mode); + try (DbSession dbSession = dbClient.openSession(false)) { + return new ModeResource(mode, isModeModified(dbSession)); + } + } + + private boolean isModeModified(DbSession dbSession) { + return dbClient.internalPropertiesDao().selectByKey(dbSession, MULTI_QUALITY_MODE_MODIFIED) + .map(Boolean::parseBoolean) + .orElse(false); } @Override @@ -65,17 +74,20 @@ public class DefaultModeController implements ModeController { Boolean isMQREnabledRequest = modeResource.mode().equals(ModeEnum.MQR); Boolean isMqREnabled = configuration.getBoolean(MULTI_QUALITY_MODE_ENABLED).orElse(MULTI_QUALITY_MODE_DEFAULT_VALUE); - if (!isMQREnabledRequest.equals(isMqREnabled)) { - try (DbSession dbSession = dbClient.openSession(false)) { + try (DbSession dbSession = dbClient.openSession(false)) { + if (!isMQREnabledRequest.equals(isMqREnabled)) { + dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(MULTI_QUALITY_MODE_ENABLED).setValue(isMQREnabledRequest.toString())); + dbClient.internalPropertiesDao().save(dbSession, MULTI_QUALITY_MODE_MODIFIED, "true"); dbSession.commit(); - } - settingsChangeNotifier.onGlobalPropertyChange(MULTI_QUALITY_MODE_ENABLED, isMQREnabledRequest.toString()); - if (qualityGateConditionsValidator.hasConditionsMismatch(isMQREnabledRequest)) { - notificationManager.scheduleForSending(new QualityGateMetricsUpdateNotification(isMQREnabledRequest)); + + settingsChangeNotifier.onGlobalPropertyChange(MULTI_QUALITY_MODE_ENABLED, isMQREnabledRequest.toString()); + if (qualityGateConditionsValidator.hasConditionsMismatch(isMQREnabledRequest)) { + notificationManager.scheduleForSending(new QualityGateMetricsUpdateNotification(isMQREnabledRequest)); + } } + return new ModeResource(Boolean.TRUE.equals(isMQREnabledRequest) ? ModeEnum.MQR : ModeEnum.STANDARD_EXPERIENCE, isModeModified(dbSession)); } - return new ModeResource(Boolean.TRUE.equals(isMQREnabledRequest) ? ModeEnum.MQR : ModeEnum.STANDARD_EXPERIENCE); } } diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/mode/resources/ModeResource.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/mode/resources/ModeResource.java index 6cdf29d5d0c..90c866d150c 100644 --- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/mode/resources/ModeResource.java +++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/mode/resources/ModeResource.java @@ -24,7 +24,6 @@ import javax.validation.constraints.NotNull; import org.sonar.server.v2.api.mode.enums.ModeEnum; public record ModeResource( - @NotNull @Schema(accessMode = Schema.AccessMode.READ_WRITE) ModeEnum mode - -) { + @NotNull @Schema(accessMode = Schema.AccessMode.READ_WRITE) ModeEnum mode, + @Schema(accessMode = Schema.AccessMode.READ_ONLY) boolean modified) { } diff --git a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/mode/controller/DefaultModeControllerTest.java b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/mode/controller/DefaultModeControllerTest.java index d20786d03c4..948c1aca1ec 100644 --- a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/mode/controller/DefaultModeControllerTest.java +++ b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/mode/controller/DefaultModeControllerTest.java @@ -26,6 +26,7 @@ import org.junit.jupiter.api.extension.RegisterExtension; import org.sonar.api.config.Configuration; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.property.InternalPropertiesDao; import org.sonar.db.property.PropertiesDao; import org.sonar.db.property.PropertyDto; import org.sonar.server.issue.notification.QualityGateMetricsUpdateNotification; @@ -42,6 +43,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import static org.sonar.core.config.MQRModeConstants.MULTI_QUALITY_MODE_ENABLED; +import static org.sonar.core.config.MQRModeConstants.MULTI_QUALITY_MODE_MODIFIED; import static org.sonar.server.v2.WebApiEndpoints.JSON_MERGE_PATCH_CONTENT_TYPE; import static org.sonar.server.v2.WebApiEndpoints.MODE_ENDPOINT; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -60,6 +62,7 @@ class DefaultModeControllerTest { private final NotificationManager notificationManager = mock(NotificationManager.class); private final QualityGateConditionsValidator qualityGateConditionsValidator = mock(QualityGateConditionsValidator.class); private final PropertiesDao propertiesDao = mock(PropertiesDao.class); + private final InternalPropertiesDao internalPropertiesDao = mock(InternalPropertiesDao.class); private final MockMvc mockMvc = ControllerTester .getMockMvc(new DefaultModeController(userSession, dbClient, configuration, settingsChangeNotifier, notificationManager, qualityGateConditionsValidator)); private DbSession dbSession; @@ -67,18 +70,31 @@ class DefaultModeControllerTest { @BeforeEach void before() { when(dbClient.propertiesDao()).thenReturn(propertiesDao); + when(dbClient.internalPropertiesDao()).thenReturn(internalPropertiesDao); dbSession = mock(DbSession.class); when(dbClient.openSession(false)).thenReturn(dbSession); } @Test void getMode_whenMQRMode_shouldReturnExpectedMode() throws Exception { + when(internalPropertiesDao.selectByKey(dbSession, MULTI_QUALITY_MODE_MODIFIED)).thenReturn(Optional.empty()); when(configuration.getBoolean(MULTI_QUALITY_MODE_ENABLED)).thenReturn(Optional.of(true)); mockMvc.perform(get(MODE_ENDPOINT)) .andExpectAll( status().isOk(), content().json(""" - {"mode":"MQR"}""")); + {"mode":"MQR", "modified": false}""")); + } + + @Test + void getMode_whenMQRModeAndHasBeenModified_shouldReturnExpectedMode() throws Exception { + when(configuration.getBoolean(MULTI_QUALITY_MODE_ENABLED)).thenReturn(Optional.of(true)); + when(internalPropertiesDao.selectByKey(dbSession, MULTI_QUALITY_MODE_MODIFIED)).thenReturn(Optional.of("true")); + mockMvc.perform(get(MODE_ENDPOINT + "/")) + .andExpectAll( + status().isOk(), + content().json(""" + {"mode":"MQR", "modified": true}""")); } @Test @@ -142,6 +158,7 @@ class DefaultModeControllerTest { {"mode":"MQR"}""")); verify(propertiesDao, times(1)).saveProperty(dbSession, new PropertyDto().setKey(MULTI_QUALITY_MODE_ENABLED).setValue("true")); + verify(internalPropertiesDao, times(1)).save(dbSession, MULTI_QUALITY_MODE_MODIFIED, "true"); verify(settingsChangeNotifier, times(1)).onGlobalPropertyChange(MULTI_QUALITY_MODE_ENABLED, "true"); verifyNoInteractions(notificationManager); } |