import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ResourceDao;
import org.sonar.db.component.ResourceDto;
-import org.sonar.server.favorite.FavoriteService;
+import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.permission.PermissionTemplateService;
import org.sonar.server.util.RubyUtils;
private final ResourceDao resourceDao;
private final ComponentService componentService;
private final PermissionTemplateService permissionTemplateService;
- private final FavoriteService favoriteService;
+ private final FavoriteUpdater favoriteUpdater;
public DefaultRubyComponentService(DbClient dbClient, ResourceDao resourceDao, ComponentService componentService, PermissionTemplateService permissionTemplateService,
- FavoriteService favoriteService) {
+ FavoriteUpdater favoriteUpdater) {
this.dbClient = dbClient;
this.resourceDao = resourceDao;
this.componentService = componentService;
this.permissionTemplateService = permissionTemplateService;
- this.favoriteService = favoriteService;
+ this.favoriteUpdater = favoriteUpdater;
}
@Override
permissionTemplateService.applyDefaultPermissionTemplate(dbSession, provisionedComponent.getKey());
if (Qualifiers.PROJECT.equals(provisionedComponent.qualifier())
&& permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, provisionedComponent)) {
- favoriteService.put(dbSession, provisionedComponent.getId());
+ favoriteUpdater.put(dbSession, provisionedComponent.getId());
dbSession.commit();
}
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.ComponentService;
-import org.sonar.server.favorite.FavoriteService;
+import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.component.NewComponent;
import org.sonar.server.permission.PermissionTemplateService;
import org.sonar.server.user.UserSession;
private final ComponentService componentService;
private final PermissionTemplateService permissionTemplateService;
private final DbClient dbClient;
- private final FavoriteService favoriteService;
+ private final FavoriteUpdater favoriteUpdater;
public ReportSubmitter(CeQueue queue, UserSession userSession,
- ComponentService componentService, PermissionTemplateService permissionTemplateService, DbClient dbClient, FavoriteService favoriteService) {
+ ComponentService componentService, PermissionTemplateService permissionTemplateService, DbClient dbClient, FavoriteUpdater favoriteUpdater) {
this.queue = queue;
this.userSession = userSession;
this.componentService = componentService;
this.permissionTemplateService = permissionTemplateService;
this.dbClient = dbClient;
- this.favoriteService = favoriteService;
+ this.favoriteUpdater = favoriteUpdater;
}
public CeTask submit(String projectKey, @Nullable String projectBranch, @Nullable String projectName, InputStream reportInput) {
// "provisioning" permission is check in ComponentService
ComponentDto project = componentService.create(dbSession, newProject);
if (permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, project)) {
- favoriteService.put(dbSession, project.getId());
+ favoriteUpdater.put(dbSession, project.getId());
dbSession.commit();
}
@Override
protected void configureModule() {
add(
- FavoriteService.class,
+ FavoriteUpdater.class,
FavoritesWs.class,
AddAction.class);
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.property.PropertyDto;
-import org.sonar.server.user.UserSession;
-
-public class FavoriteService {
- private static final String PROP_FAVORITE_KEY = "favourite";
-
- private final DbClient dbClient;
- private final UserSession userSession;
-
- public FavoriteService(DbClient dbClient, UserSession userSession) {
- this.dbClient = dbClient;
- this.userSession = userSession;
- }
-
- /**
- * Set favorite to the logged in user. If no user is logged, no action is done
- */
- public void put(DbSession dbSession, long componentDbId) {
- if (!userSession.isLoggedIn()) {
- return;
- }
-
- dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
- .setKey(PROP_FAVORITE_KEY)
- .setResourceId(componentDbId)
- .setUserId(Long.valueOf(userSession.getUserId())));
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.property.PropertyDto;
+import org.sonar.server.user.UserSession;
+
+public class FavoriteUpdater {
+ private static final String PROP_FAVORITE_KEY = "favourite";
+
+ private final DbClient dbClient;
+ private final UserSession userSession;
+
+ public FavoriteUpdater(DbClient dbClient, UserSession userSession) {
+ this.dbClient = dbClient;
+ this.userSession = userSession;
+ }
+
+ /**
+ * Set favorite to the logged in user. If no user is logged, no action is done
+ */
+ public void put(DbSession dbSession, long componentDbId) {
+ if (!userSession.isLoggedIn()) {
+ return;
+ }
+
+ dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
+ .setKey(PROP_FAVORITE_KEY)
+ .setResourceId(componentDbId)
+ .setUserId(Long.valueOf(userSession.getUserId())));
+ }
+}
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.favorite.FavoriteService;
+import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.user.UserSession;
import org.sonar.server.ws.KeyExamples;
public class AddAction implements FavoritesWsAction {
private final UserSession userSession;
private final DbClient dbClient;
- private final FavoriteService favoriteService;
+ private final FavoriteUpdater favoriteUpdater;
private final ComponentFinder componentFinder;
- public AddAction(UserSession userSession, DbClient dbClient, FavoriteService favoriteService, ComponentFinder componentFinder) {
+ public AddAction(UserSession userSession, DbClient dbClient, FavoriteUpdater favoriteUpdater, ComponentFinder componentFinder) {
this.userSession = userSession;
this.dbClient = dbClient;
- this.favoriteService = favoriteService;
+ this.favoriteUpdater = favoriteUpdater;
this.componentFinder = componentFinder;
}
userSession
.checkLoggedIn()
.checkComponentUuidPermission(UserRole.USER, componentDto.uuid());
- favoriteService.put(dbSession, componentDto.getId());
+ favoriteUpdater.put(dbSession, componentDto.getId());
dbSession.commit();
}
};
import org.sonar.server.component.es.ProjectMeasuresIndexer;
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.favorite.FavoriteService;
+import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.i18n.I18nRule;
import org.sonar.server.permission.PermissionTemplateService;
import org.sonar.server.tester.UserSessionRule;
private ComponentService componentService = new ComponentService(dbClient, i18n, userSession, system2, new ComponentFinder(dbClient),
new ProjectMeasuresIndexer(system2, dbClient, es.client()));
private PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class);
- private FavoriteService favoriteService = mock(FavoriteService.class);
+ private FavoriteUpdater favoriteUpdater = mock(FavoriteUpdater.class);
ComponentDbTester componentDb = new ComponentDbTester(db);
- DefaultRubyComponentService underTest = new DefaultRubyComponentService(dbClient, resourceDao, componentService, permissionTemplateService, favoriteService);
+ DefaultRubyComponentService underTest = new DefaultRubyComponentService(dbClient, resourceDao, componentService, permissionTemplateService, favoriteUpdater);
@Test
public void find_by_key() {
assertThat(project.qualifier()).isEqualTo(qualifier);
assertThat(project.getId()).isEqualTo(result);
verify(permissionTemplateService).applyDefaultPermissionTemplate(any(DbSession.class), eq(componentKey));
- verify(favoriteService).put(any(DbSession.class), eq(project.getId()));
+ verify(favoriteUpdater).put(any(DbSession.class), eq(project.getId()));
}
@Test(expected = BadRequestException.class)
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.ComponentService;
-import org.sonar.server.favorite.FavoriteService;
+import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.component.NewComponent;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.permission.PermissionTemplateService;
private CeQueue queue = mock(CeQueueImpl.class);
private ComponentService componentService = mock(ComponentService.class);
private PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class);
- private FavoriteService favoriteService = mock(FavoriteService.class);
+ private FavoriteUpdater favoriteUpdater = mock(FavoriteUpdater.class);
- private ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentService, permissionTemplateService, db.getDbClient(), favoriteService);
+ private ReportSubmitter underTest = new ReportSubmitter(queue, userSession, componentService, permissionTemplateService, db.getDbClient(), favoriteUpdater);
@Test
public void submit_a_report_on_existing_project() {
verifyReportIsPersisted(TASK_UUID);
verifyZeroInteractions(permissionTemplateService);
- verifyZeroInteractions(favoriteService);
+ verifyZeroInteractions(favoriteUpdater);
verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
@Override
protected boolean matchesSafely(CeTaskSubmit submit) {
verifyReportIsPersisted(TASK_UUID);
verify(permissionTemplateService).applyDefault(any(DbSession.class), eq(createdProject), anyLong());
- verify(favoriteService).put(any(DbSession.class), eq(createdProject.getId()));
+ verify(favoriteUpdater).put(any(DbSession.class), eq(createdProject.getId()));
verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
@Override
protected boolean matchesSafely(CeTaskSubmit submit) {
underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
- verifyZeroInteractions(favoriteService);
+ verifyZeroInteractions(favoriteUpdater);
}
@Test
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.sonar.api.utils.System2;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.property.PropertyQuery;
-import org.sonar.server.tester.UserSessionRule;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class FavoriteServiceTest {
- private static final long COMPONENT_ID = 23L;
- private static final long USER_ID = 42L;
-
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone().login().setUserId((int) USER_ID);
-
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
-
- private FavoriteService underTest = new FavoriteService(dbClient, userSession);
-
- @Test
- public void put_favorite() {
- assertNoFavorite();
-
- underTest.put(dbSession, COMPONENT_ID);
-
- assertFavorite();
- }
-
- @Test
- public void do_nothing_when_not_logged_in() {
- userSession.anonymous();
-
- underTest.put(dbSession, COMPONENT_ID);
-
- assertNoFavorite();
- }
-
- @Test
- public void put_existing_favorite() {
- underTest.put(dbSession, COMPONENT_ID);
- assertFavorite();
-
- underTest.put(dbSession, COMPONENT_ID);
-
- assertFavorite();
- }
-
- private void assertFavorite() {
- assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
- .setUserId((int) USER_ID)
- .setComponentId(COMPONENT_ID)
- .build(), dbSession)).hasSize(1);
- }
-
- private void assertNoFavorite() {
- assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
- .setUserId((int) USER_ID)
- .setComponentId(COMPONENT_ID)
- .build(), dbSession)).isEmpty();
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.db.property.PropertyQuery;
+import org.sonar.server.tester.UserSessionRule;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class FavoriteUpdaterTest {
+ private static final long COMPONENT_ID = 23L;
+ private static final long USER_ID = 42L;
+
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone().login().setUserId((int) USER_ID);
+
+ @Rule
+ public DbTester db = DbTester.create(System2.INSTANCE);
+ private DbClient dbClient = db.getDbClient();
+ private DbSession dbSession = db.getSession();
+
+ private FavoriteUpdater underTest = new FavoriteUpdater(dbClient, userSession);
+
+ @Test
+ public void put_favorite() {
+ assertNoFavorite();
+
+ underTest.put(dbSession, COMPONENT_ID);
+
+ assertFavorite();
+ }
+
+ @Test
+ public void do_nothing_when_not_logged_in() {
+ userSession.anonymous();
+
+ underTest.put(dbSession, COMPONENT_ID);
+
+ assertNoFavorite();
+ }
+
+ @Test
+ public void put_existing_favorite() {
+ underTest.put(dbSession, COMPONENT_ID);
+ assertFavorite();
+
+ underTest.put(dbSession, COMPONENT_ID);
+
+ assertFavorite();
+ }
+
+ private void assertFavorite() {
+ assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
+ .setUserId((int) USER_ID)
+ .setComponentId(COMPONENT_ID)
+ .build(), dbSession)).hasSize(1);
+ }
+
+ private void assertNoFavorite() {
+ assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
+ .setUserId((int) USER_ID)
+ .setComponentId(COMPONENT_ID)
+ .build(), dbSession)).isEmpty();
+ }
+}
import org.sonar.db.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.favorite.FavoriteService;
+import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
private DbClient dbClient = db.getDbClient();
private DbSession dbSession = db.getSession();
- private FavoriteService favoriteService = new FavoriteService(dbClient, userSession);
- private WsActionTester ws = new WsActionTester(new AddAction(userSession, dbClient, favoriteService, new ComponentFinder(dbClient)));
+ private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient, userSession);
+ private WsActionTester ws = new WsActionTester(new AddAction(userSession, dbClient, favoriteUpdater, new ComponentFinder(dbClient)));
@Test
public void add_a_project() {
import org.sonar.api.server.ws.WebService.Controller;
import org.sonar.db.DbClient;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.favorite.FavoriteService;
+import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.user.UserSession;
import org.sonar.server.ws.WsTester;
public class FavoritesWsTest {
- private final FavoritesWsAction[] actions = {new AddAction(mock(UserSession.class), mock(DbClient.class), mock(FavoriteService.class), mock(ComponentFinder.class))};
+ private final FavoritesWsAction[] actions = {new AddAction(mock(UserSession.class), mock(DbClient.class), mock(FavoriteUpdater.class), mock(ComponentFinder.class))};
private WsTester ws = new WsTester(new FavoritesWs(actions));
private Controller underTest = ws.controller("api/favorites");