]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7299 Remove UserSession dependency from FavoriteUpdater
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 23 Jan 2017 13:04:30 +0000 (14:04 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 24 Jan 2017 17:36:49 +0000 (18:36 +0100)
Having UserSession as a dependency of FavoriteUpdater forces any classes that is using it to also have a UserSession dependency

12 files changed:
server/sonar-server/src/main/java/org/sonar/server/component/DefaultRubyComponentService.java
server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java
server/sonar-server/src/main/java/org/sonar/server/favorite/FavoriteUpdater.java
server/sonar-server/src/main/java/org/sonar/server/favorite/ws/AddAction.java
server/sonar-server/src/main/java/org/sonar/server/favorite/ws/RemoveAction.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/CreateAction.java
server/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/queue/ReportSubmitterTest.java
server/sonar-server/src/test/java/org/sonar/server/favorite/FavoriteUpdaterTest.java
server/sonar-server/src/test/java/org/sonar/server/favorite/ws/AddActionTest.java
server/sonar-server/src/test/java/org/sonar/server/favorite/ws/RemoveActionTest.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/CreateActionTest.java

index 69c9b1425ad70c97f190f8df67be70a3aeef0b71..f1cbd928f1b62c5a6dd68ecda1502793a17eb0fa 100644 (file)
@@ -74,7 +74,8 @@ public class DefaultRubyComponentService {
     permissionTemplateService.applyDefaultPermissionTemplate(dbSession, organizationUuid, provisionedComponent.getKey());
     if (Qualifiers.PROJECT.equals(provisionedComponent.qualifier())
       && permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, provisionedComponent)) {
-      favoriteUpdater.add(dbSession, provisionedComponent);
+      // TODO Set userId as null as this method will be removed in a next commit
+      favoriteUpdater.add(dbSession, provisionedComponent, null);
       dbSession.commit();
     }
 
index e5660d58d88b3036446964f7e4cb8cfcbbc91878..42186f3f891d43a1bf36fe8c0d10ba1a1ecb946b 100644 (file)
@@ -117,7 +117,7 @@ public class ReportSubmitter {
       .build();
     ComponentDto project = componentService.create(dbSession, newProject);
     if (permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, project)) {
-      favoriteUpdater.add(dbSession, project);
+      favoriteUpdater.add(dbSession, project, projectCreatorUserId);
       dbSession.commit();
     }
 
index 6851a9aeb8b5672ee9202e13df86c4aea5108b28..8596c9688dfeec8d9463b77a799e89d32119cf15 100644 (file)
 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 org.sonar.server.user.UserSession;
 
 import static org.sonar.server.ws.WsUtils.checkRequest;
 
@@ -35,46 +35,44 @@ public class FavoriteUpdater {
   static final String PROP_FAVORITE_KEY = "favourite";
 
   private final DbClient dbClient;
-  private final UserSession userSession;
 
-  public FavoriteUpdater(DbClient dbClient, UserSession userSession) {
+  public FavoriteUpdater(DbClient dbClient) {
     this.dbClient = dbClient;
-    this.userSession = userSession;
   }
 
   /**
-   * Set favorite to the logged in user. If no user is logged, no action is done
+   * Set favorite to the logged in user. If no user, no action is done
    */
-  public void add(DbSession dbSession, ComponentDto componentDto) {
-    if (!userSession.isLoggedIn()) {
+  public void add(DbSession dbSession, ComponentDto componentDto, @Nullable Long userId) {
+    if (userId == null) {
       return;
     }
 
     List<PropertyDto> existingFavoriteOnComponent = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
       .setKey(PROP_FAVORITE_KEY)
-      .setUserId(userSession.getUserId())
+      .setUserId(userId.intValue())
       .setComponentId(componentDto.getId())
       .build(), dbSession);
     checkRequest(existingFavoriteOnComponent.isEmpty(), "Component '%s' is already a favorite", componentDto.getKey());
     dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
       .setKey(PROP_FAVORITE_KEY)
       .setResourceId(componentDto.getId())
-      .setUserId(Long.valueOf(userSession.getUserId())));
+      .setUserId(userId));
   }
 
   /**
-   * Remove a favorite to the logged in user.
+   * Remove a favorite to the user.
    * @throws BadRequestException if the component is not a favorite
    */
-  public void remove(DbSession dbSession, ComponentDto component) {
-    if (!userSession.isLoggedIn()) {
+  public void remove(DbSession dbSession, ComponentDto component, @Nullable Long userId) {
+    if (userId == null) {
       return;
     }
 
     int result = dbClient.propertiesDao().delete(dbSession, new PropertyDto()
       .setKey(PROP_FAVORITE_KEY)
       .setResourceId(component.getId())
-      .setUserId(Long.valueOf(userSession.getUserId())));
+      .setUserId(userId));
     checkRequest(result == 1, "Component '%s' is not a favorite", component.key());
   }
 }
index 271df58b3fa7850ee9e8772d66349015725c3f16..94919aa1cff6d139732c5aee55110f29119edfb6 100644 (file)
@@ -76,7 +76,7 @@ public class AddAction implements FavoritesWsAction {
         userSession
           .checkLoggedIn()
           .checkComponentUuidPermission(UserRole.USER, componentDto.uuid());
-        favoriteUpdater.add(dbSession, componentDto);
+        favoriteUpdater.add(dbSession, componentDto, userSession.isLoggedIn() ? userSession.getUserId().longValue() : null);
         dbSession.commit();
       }
     };
index 32591f84b9e65f2e9798273572a78c095727ea6b..f4f8045e84af9a43d559387129a300d0bb6d547f 100644 (file)
@@ -74,7 +74,7 @@ public class RemoveAction implements FavoritesWsAction {
         ComponentDto component = componentFinder.getByKey(dbSession, request.mandatoryParam(PARAM_COMPONENT));
         userSession
           .checkLoggedIn();
-        favoriteUpdater.remove(dbSession, component);
+        favoriteUpdater.remove(dbSession, component, userSession.isLoggedIn() ? userSession.getUserId().longValue() : null);
         dbSession.commit();
       }
     };
index 6ec82adcc0f59f3b62383a5ee253826c4469e2f0..b661f8d11f874c35972e0c82ce83af4f20508bca 100644 (file)
@@ -114,9 +114,10 @@ public class CreateAction implements ProjectsWsAction {
   }
 
   private void handlePermissionTemplate(DbSession dbSession, ComponentDto componentDto, String organizationUuid) {
-    permissionTemplateService.applyDefault(dbSession, organizationUuid, componentDto, userSession.isLoggedIn() ? userSession.getUserId().longValue() : null);
+    Long userId = userSession.isLoggedIn() ? userSession.getUserId().longValue() : null;
+    permissionTemplateService.applyDefault(dbSession, organizationUuid, componentDto, userId);
     if (permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, componentDto)) {
-      favoriteUpdater.add(dbSession, componentDto);
+      favoriteUpdater.add(dbSession, componentDto, userId);
       dbSession.commit();
     }
   }
index c85c582c25f55efc503d5c81a91a19e8020d9c07..9b2db1113f35091c19767ddfd1ab2027afe5a450 100644 (file)
@@ -99,7 +99,7 @@ public class DefaultRubyComponentServiceTest {
     assertThat(project.qualifier()).isEqualTo(qualifier);
     assertThat(project.getId()).isEqualTo(result);
     verify(permissionTemplateService).applyDefaultPermissionTemplate(any(DbSession.class), eq(defaultOrganizationUuid), eq(componentKey));
-    verify(favoriteUpdater).add(any(DbSession.class), eq(project));
+    verify(favoriteUpdater).add(any(DbSession.class), eq(project), eq(null));
   }
 
   @Test(expected = BadRequestException.class)
index e3c8b3d4e61f53bac09aa85fde9365689e865b06..99b49a96a8935f0fd75ea80dbefa9abeaef441ca 100644 (file)
@@ -38,10 +38,10 @@ import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.server.component.ComponentService;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.favorite.FavoriteUpdater;
 import org.sonar.server.component.NewComponent;
 import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.favorite.FavoriteUpdater;
 import org.sonar.server.permission.PermissionTemplateService;
 import org.sonar.server.tester.UserSessionRule;
 
@@ -92,7 +92,7 @@ public class ReportSubmitterTest {
     thrown.expect(NotFoundException.class);
     thrown.expectMessage("Organization with key 'fop' does not exist");
 
-    underTest.submit("fop", PROJECT_KEY, null, null, null /*method will fail before parameter is used*/);
+    underTest.submit("fop", PROJECT_KEY, null, null, null /* method will fail before parameter is used */);
   }
 
   @Test
@@ -139,15 +139,16 @@ public class ReportSubmitterTest {
     mockSuccessfulPrepareSubmitCall();
     ComponentDto createdProject = new ComponentDto().setId(23L).setUuid(PROJECT_UUID).setKey(PROJECT_KEY);
     when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(createdProject);
-    when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(organization.getUuid()), anyLong(), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
-      .thenReturn(true);
+    when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(organization.getUuid()), anyLong(), eq(SCAN_EXECUTION), anyString(),
+      eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
+        .thenReturn(true);
     when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(any(DbSession.class), eq(organization.getUuid()), any(ComponentDto.class))).thenReturn(true);
 
     underTest.submit(organization.getKey(), PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
 
     verifyReportIsPersisted(TASK_UUID);
     verify(permissionTemplateService).applyDefault(any(DbSession.class), eq(organization.getUuid()), eq(createdProject), anyLong());
-    verify(favoriteUpdater).add(any(DbSession.class), eq(createdProject));
+    verify(favoriteUpdater).add(any(DbSession.class), eq(createdProject), eq(null));
     verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
       @Override
       protected boolean matchesSafely(CeTaskSubmit submit) {
@@ -169,8 +170,9 @@ public class ReportSubmitterTest {
     mockSuccessfulPrepareSubmitCall();
     ComponentDto createdProject = new ComponentDto().setId(23L).setUuid(PROJECT_UUID).setKey(PROJECT_KEY);
     when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(createdProject);
-    when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(defaultOrganizationUuid), anyLong(), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
-      .thenReturn(true);
+    when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(defaultOrganizationUuid), anyLong(), eq(SCAN_EXECUTION), anyString(),
+      eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
+        .thenReturn(true);
     when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(any(DbSession.class), eq(defaultOrganizationUuid), any(ComponentDto.class))).thenReturn(false);
 
     underTest.submit(defaultOrganizationKey, PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
@@ -184,8 +186,9 @@ public class ReportSubmitterTest {
 
     mockSuccessfulPrepareSubmitCall();
     when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(new ComponentDto().setId(23L).setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
-    when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(defaultOrganizationUuid), anyLong(), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
-      .thenReturn(true);
+    when(permissionTemplateService.wouldUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(defaultOrganizationUuid), anyLong(), eq(SCAN_EXECUTION), anyString(),
+      eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
+        .thenReturn(true);
 
     underTest.submit(defaultOrganizationKey, PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
 
index 5a856f4239a4f52b15d5db8e2ccd8f40a1a9f06f..39c22d5a2e29c59fac7350e1b2d993abe000f4e5 100644 (file)
@@ -31,7 +31,6 @@ import org.sonar.db.component.ComponentDto;
 import org.sonar.db.organization.OrganizationTesting;
 import org.sonar.db.property.PropertyQuery;
 import org.sonar.server.exceptions.BadRequestException;
-import org.sonar.server.tester.UserSessionRule;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
@@ -44,8 +43,6 @@ public class FavoriteUpdaterTest {
     .setKey(COMPONENT_KEY);
   private static final long USER_ID = 42L;
 
-  @Rule
-  public UserSessionRule userSession = UserSessionRule.standalone().login().setUserId((int) USER_ID);
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
@@ -54,35 +51,33 @@ public class FavoriteUpdaterTest {
   private DbClient dbClient = db.getDbClient();
   private DbSession dbSession = db.getSession();
 
-  private FavoriteUpdater underTest = new FavoriteUpdater(dbClient, userSession);
+  private FavoriteUpdater underTest = new FavoriteUpdater(dbClient);
 
   @Test
   public void put_favorite() {
     assertNoFavorite();
 
-    underTest.add(dbSession, COMPONENT);
+    underTest.add(dbSession, COMPONENT, USER_ID);
 
     assertFavorite();
   }
 
   @Test
-  public void do_nothing_when_not_logged_in() {
-    userSession.anonymous();
-
-    underTest.add(dbSession, COMPONENT);
+  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);
+    underTest.add(dbSession, COMPONENT, USER_ID);
     assertFavorite();
 
     expectedException.expect(BadRequestException.class);
     expectedException.expectMessage("Component 'K1' is already a favorite");
 
-    underTest.add(dbSession, COMPONENT);
+    underTest.add(dbSession, COMPONENT, USER_ID);
   }
 
   private void assertFavorite() {
index 05433565978db058cdb4705209bef7d41b9f01aa..654e62a23607552c99cd7c45eee586a36b852564 100644 (file)
@@ -34,10 +34,10 @@ import org.sonar.db.component.ComponentDto;
 import org.sonar.db.property.PropertyDto;
 import org.sonar.db.property.PropertyQuery;
 import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.favorite.FavoriteUpdater;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.exceptions.UnauthorizedException;
+import org.sonar.server.favorite.FavoriteUpdater;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.TestResponse;
@@ -64,7 +64,7 @@ public class AddActionTest {
   private DbClient dbClient = db.getDbClient();
   private DbSession dbSession = db.getSession();
 
-  private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient, userSession);
+  private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient);
   private WsActionTester ws = new WsActionTester(new AddAction(userSession, dbClient, favoriteUpdater, new ComponentFinder(dbClient)));
 
   @Test
index e21850492c65539ed9c806095fa7c158bec386be..60b0910e897abaa49e7e6cd263a88c924b8d99b9 100644 (file)
@@ -58,7 +58,7 @@ public class RemoveActionTest {
   public DbTester db = DbTester.create();
   private DbClient dbClient = db.getDbClient();
 
-  private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient, userSession);
+  private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient);
   private WsActionTester ws = new WsActionTester(new RemoveAction(userSession, dbClient, favoriteUpdater, new ComponentFinder(dbClient)));
 
   @Test
index 541ad2afdb35ab9232e97e49aff3baeea93ea4db..8610fc8f404e4dc8c1fc4125203a0e16c1aaa6fa 100644 (file)
@@ -105,7 +105,7 @@ public class CreateActionTest {
         new ProjectMeasuresIndexer(system2, db.getDbClient(), es.client()),
         new ComponentIndexer(db.getDbClient(), es.client())),
       new PermissionTemplateService(db.getDbClient(), settings, new PermissionIndexer(db.getDbClient(), es.client()), userSession),
-      new FavoriteUpdater(db.getDbClient(), userSession),
+      new FavoriteUpdater(db.getDbClient()),
       defaultOrganizationProvider));
 
   @Before