From: Sébastien Lesaint Date: Fri, 22 Jun 2018 15:29:51 +0000 (+0200) Subject: move FavoriteUpdater to server-common X-Git-Tag: 7.5~921 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6cb785ff6aec0a217de43de3474c57a5f59c3481;p=sonarqube.git move FavoriteUpdater to server-common --- diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/favorite/FavoriteUpdater.java b/server/sonar-server-common/src/main/java/org/sonar/server/favorite/FavoriteUpdater.java new file mode 100644 index 00000000000..cc58bc0e882 --- /dev/null +++ b/server/sonar-server-common/src/main/java/org/sonar/server/favorite/FavoriteUpdater.java @@ -0,0 +1,76 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.favorite; + +import java.util.List; +import javax.annotation.Nullable; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.property.PropertyDto; +import org.sonar.db.property.PropertyQuery; + +import static com.google.common.base.Preconditions.checkArgument; + +public class FavoriteUpdater { + static final String PROP_FAVORITE_KEY = "favourite"; + + private final DbClient dbClient; + + public FavoriteUpdater(DbClient dbClient) { + this.dbClient = dbClient; + } + + /** + * Set favorite to the logged in user. If no user, no action is done + */ + public void add(DbSession dbSession, ComponentDto componentDto, @Nullable Integer userId) { + if (userId == null) { + return; + } + + List existingFavoriteOnComponent = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() + .setKey(PROP_FAVORITE_KEY) + .setUserId(userId) + .setComponentId(componentDto.getId()) + .build(), dbSession); + checkArgument(existingFavoriteOnComponent.isEmpty(), "Component '%s' is already a favorite", componentDto.getDbKey()); + dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() + .setKey(PROP_FAVORITE_KEY) + .setResourceId(componentDto.getId()) + .setUserId(userId)); + } + + /** + * Remove a favorite to the user. + * @throws IllegalArgumentException if the component is not a favorite + */ + public void remove(DbSession dbSession, ComponentDto component, @Nullable Integer userId) { + if (userId == null) { + return; + } + + int result = dbClient.propertiesDao().delete(dbSession, new PropertyDto() + .setKey(PROP_FAVORITE_KEY) + .setResourceId(component.getId()) + .setUserId(userId)); + checkArgument(result == 1, "Component '%s' is not a favorite", component.getDbKey()); + } +} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/favorite/package-info.java b/server/sonar-server-common/src/main/java/org/sonar/server/favorite/package-info.java new file mode 100644 index 00000000000..c97371f3501 --- /dev/null +++ b/server/sonar-server-common/src/main/java/org/sonar/server/favorite/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.server.favorite; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/favorite/FavoriteUpdaterTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/favorite/FavoriteUpdaterTest.java new file mode 100644 index 00000000000..836be016cac --- /dev/null +++ b/server/sonar-server-common/src/test/java/org/sonar/server/favorite/FavoriteUpdaterTest.java @@ -0,0 +1,94 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.favorite; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentTesting; +import org.sonar.db.organization.OrganizationTesting; +import org.sonar.db.property.PropertyQuery; + +import static org.assertj.core.api.Assertions.assertThat; + +public class FavoriteUpdaterTest { + private static final long COMPONENT_ID = 23L; + private static final String COMPONENT_KEY = "K1"; + private static final ComponentDto COMPONENT = ComponentTesting.newPrivateProjectDto(OrganizationTesting.newOrganizationDto()) + .setId(COMPONENT_ID) + .setDbKey(COMPONENT_KEY); + private static final int USER_ID = 42; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + + private DbClient dbClient = db.getDbClient(); + private DbSession dbSession = db.getSession(); + + private FavoriteUpdater underTest = new FavoriteUpdater(dbClient); + + @Test + public void put_favorite() { + assertNoFavorite(); + + underTest.add(dbSession, COMPONENT, USER_ID); + + assertFavorite(); + } + + @Test + public void do_nothing_when_no_user() { + underTest.add(dbSession, COMPONENT, null); + + assertNoFavorite(); + } + + @Test + public void fail_when_adding_existing_favorite() { + underTest.add(dbSession, COMPONENT, USER_ID); + assertFavorite(); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Component 'K1' is already a favorite"); + + underTest.add(dbSession, COMPONENT, USER_ID); + } + + private void assertFavorite() { + assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() + .setUserId(USER_ID) + .setComponentId(COMPONENT_ID) + .build(), dbSession)).hasSize(1); + } + + private void assertNoFavorite() { + assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() + .setUserId(USER_ID) + .setComponentId(COMPONENT_ID) + .build(), dbSession)).isEmpty(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/favorite/FavoriteUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/favorite/FavoriteUpdater.java deleted file mode 100644 index a9f3d195214..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/favorite/FavoriteUpdater.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2018 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.favorite; - -import java.util.List; -import javax.annotation.Nullable; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.property.PropertyDto; -import org.sonar.db.property.PropertyQuery; -import org.sonar.server.exceptions.BadRequestException; - -import static org.sonar.server.ws.WsUtils.checkRequest; - -public class FavoriteUpdater { - static final String PROP_FAVORITE_KEY = "favourite"; - - private final DbClient dbClient; - - public FavoriteUpdater(DbClient dbClient) { - this.dbClient = dbClient; - } - - /** - * Set favorite to the logged in user. If no user, no action is done - */ - public void add(DbSession dbSession, ComponentDto componentDto, @Nullable Integer userId) { - if (userId == null) { - return; - } - - List existingFavoriteOnComponent = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() - .setKey(PROP_FAVORITE_KEY) - .setUserId(userId) - .setComponentId(componentDto.getId()) - .build(), dbSession); - checkRequest(existingFavoriteOnComponent.isEmpty(), "Component '%s' is already a favorite", componentDto.getDbKey()); - dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() - .setKey(PROP_FAVORITE_KEY) - .setResourceId(componentDto.getId()) - .setUserId(userId)); - } - - /** - * Remove a favorite to the user. - * @throws BadRequestException if the component is not a favorite - */ - public void remove(DbSession dbSession, ComponentDto component, @Nullable Integer userId) { - if (userId == null) { - return; - } - - int result = dbClient.propertiesDao().delete(dbSession, new PropertyDto() - .setKey(PROP_FAVORITE_KEY) - .setResourceId(component.getId()) - .setUserId(userId)); - checkRequest(result == 1, "Component '%s' is not a favorite", component.getDbKey()); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/favorite/FavoriteUpdaterTest.java b/server/sonar-server/src/test/java/org/sonar/server/favorite/FavoriteUpdaterTest.java deleted file mode 100644 index d931f3373ea..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/favorite/FavoriteUpdaterTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2018 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.favorite; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.utils.System2; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.DbTester; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ComponentTesting; -import org.sonar.db.organization.OrganizationTesting; -import org.sonar.db.property.PropertyQuery; -import org.sonar.server.exceptions.BadRequestException; - -import static org.assertj.core.api.Assertions.assertThat; - -public class FavoriteUpdaterTest { - private static final long COMPONENT_ID = 23L; - private static final String COMPONENT_KEY = "K1"; - private static final ComponentDto COMPONENT = ComponentTesting.newPrivateProjectDto(OrganizationTesting.newOrganizationDto()) - .setId(COMPONENT_ID) - .setDbKey(COMPONENT_KEY); - private static final int USER_ID = 42; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); - private DbClient dbClient = db.getDbClient(); - private DbSession dbSession = db.getSession(); - - private FavoriteUpdater underTest = new FavoriteUpdater(dbClient); - - @Test - public void put_favorite() { - assertNoFavorite(); - - underTest.add(dbSession, COMPONENT, USER_ID); - - assertFavorite(); - } - - @Test - public void do_nothing_when_no_user() { - underTest.add(dbSession, COMPONENT, null); - - assertNoFavorite(); - } - - @Test - public void fail_when_adding_existing_favorite() { - underTest.add(dbSession, COMPONENT, USER_ID); - assertFavorite(); - - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Component 'K1' is already a favorite"); - - underTest.add(dbSession, COMPONENT, USER_ID); - } - - private void assertFavorite() { - assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() - .setUserId(USER_ID) - .setComponentId(COMPONENT_ID) - .build(), dbSession)).hasSize(1); - } - - private void assertNoFavorite() { - assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() - .setUserId(USER_ID) - .setComponentId(COMPONENT_ID) - .build(), dbSession)).isEmpty(); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/favorite/ws/RemoveActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/favorite/ws/RemoveActionTest.java index 280f190bbaa..6cc32477483 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/favorite/ws/RemoveActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/favorite/ws/RemoveActionTest.java @@ -30,7 +30,6 @@ import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.organization.OrganizationDto; import org.sonar.server.component.TestComponentFinder; -import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.favorite.FavoriteUpdater; @@ -82,7 +81,7 @@ public class RemoveActionTest { public void fail_if_not_already_a_favorite() { insertProjectAndPermissions(); - expectedException.expect(BadRequestException.class); + expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Component '" + PROJECT_KEY + "' is not a favorite"); call(PROJECT_KEY);