--- /dev/null
+/*
+ * 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<PropertyDto> 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());
+ }
+}
--- /dev/null
+/*
+ * 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;
+
--- /dev/null
+/*
+ * 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();
+ }
+}
+++ /dev/null
-/*
- * 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<PropertyDto> 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());
- }
-}
+++ /dev/null
-/*
- * 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();
- }
-}
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;
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);