]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5329 Add medium test on qprofile activity with author name
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 20 Jun 2014 13:18:20 +0000 (15:18 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 20 Jun 2014 13:21:23 +0000 (15:21 +0200)
sonar-core/src/main/java/org/sonar/core/user/UserDao.java
sonar-core/src/main/java/org/sonar/core/user/UserMapper.java
sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml
sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java
sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/insert-result.xml [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/activity/ws/SearchAction.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java

index d022ee0c1350ab705e23efe8994bb854bcf08851..afcda180beae45b6763f8a4a0999b8c16bec5f51 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.core.persistence.DaoComponent;
 import org.sonar.core.persistence.MyBatis;
 
 import javax.annotation.CheckForNull;
+
 import java.util.List;
 
 /**
@@ -101,6 +102,12 @@ public class UserDao implements BatchComponent, ServerComponent, DaoComponent {
     }
   }
 
+  @CheckForNull
+  public UserDto insert(SqlSession session, UserDto dto) {
+    session.getMapper(UserMapper.class).insert(dto);
+    return dto;
+  }
+
   /**
    * Deactivate a user and drops all his preferences.
    * @return false if the user does not exist, true if the existing user has been deactivated
index a2f1dfebe02ab41378db947f4648bd4ab9123376..6e49630a361bcddab46570cde3db25ddde46da5e 100644 (file)
@@ -44,6 +44,8 @@ public interface UserMapper {
   @CheckForNull
   GroupDto selectGroupByName(String name);
 
+  void insert(UserDto userDto);
+
   void removeUserFromGroups(long userId);
 
   void deleteUserActiveDashboards(long userId);
@@ -63,4 +65,5 @@ public interface UserMapper {
   void deleteUserRoles(long userId);
 
   void deactivateUser(long userId);
+
 }
index 3b9c25b8f8db06208244b7165fb1c5cdbf8df6ff..0a272daa876eef2ccfa486926dae36186b542d54 100644 (file)
     UPDATE users SET active=${_false} WHERE id=#{id}
   </update>
 
+  <insert id="insert" parameterType="User" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+    INSERT INTO users (login, name, email, active, created_at, updated_at)
+    VALUES (#{login}, #{name}, #{email}, ${_true}, #{createdAt}, #{updatedAt})
+  </insert>
+
 </mapper>
index 2db206ddbf881a2b1183a8c72bb055273bfa4fee..981695b23c93c26d05694a6842c0f84112a9d5bb 100644 (file)
  */
 package org.sonar.core.user;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.user.UserQuery;
+import org.sonar.api.utils.DateUtils;
 import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.sonar.core.persistence.DbSession;
 
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
 
 import static org.fest.assertions.Assertions.assertThat;
 
 public class UserDaoTest extends AbstractDaoTestCase {
 
-  private UserDao dao;
+  UserDao dao;
+
+  DbSession session;
 
   @Before
   public void setUp() {
+    session = getMyBatis().openSession(false);
     dao = new UserDao(getMyBatis());
   }
 
+  @After
+  public void tearDown() throws Exception {
+    session.close();
+  }
+
   @Test
   public void selectUserByLogin_ignore_inactive() {
     setupData("selectActiveUserByLogin");
@@ -156,6 +164,16 @@ public class UserDaoTest extends AbstractDaoTestCase {
     assertThat(group).isNull();
   }
 
+  @Test
+  public void insert_user() {
+    Date date = DateUtils.parseDate("2014-06-20");
+
+    UserDto userDto = new UserDto().setId(1L).setLogin("john").setName("John").setEmail("jo@hn.com").setCreatedAt(date).setUpdatedAt(date);
+    dao.insert(session, userDto);
+
+    checkTables("insert");
+  }
+
   @Test
   public void deactivate_user() {
     setupData("deactivate_user");
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/insert-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/insert-result.xml
new file mode 100644 (file)
index 0000000..374d956
--- /dev/null
@@ -0,0 +1,3 @@
+<dataset>
+  <users id="1" login="john" name="John" email="jo@hn.com" created_at="2014-06-20" updated_at="2014-06-20" active="[true]"/>
+</dataset>
index 61788652235aef5428321022b77f5948e7316d96..587de65ac8a3b9aa53e9bb3db0fc7fabfe3529c5 100644 (file)
@@ -55,6 +55,7 @@ public class SearchAction implements RequestHandler {
       .createAction(SEARCH_ACTION)
       .setDescription("Search for a logs")
       .setSince("4.4")
+      .setInternal(true)
       .setHandler(this);
 
     // Other parameters
index 64dedfbc402552ba5ce4e42fa3cb8327dc1e934c..91a4e342fbeed4192bcabf0abd733d2c8d16feed 100644 (file)
@@ -47,6 +47,7 @@ import org.sonar.server.user.UserSession;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
+
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
@@ -295,11 +296,7 @@ public class QProfileService implements ServerComponent {
         profileActivity.ruleName(ruleDto != null ? ruleDto.getName() : null);
 
         UserDto user = db.userDao().selectActiveUserByLogin(profileActivity.login(), session);
-        if (user != null) {
-          profileActivity.authorName(user.getName());
-        } else {
-          profileActivity.authorName(null);
-        }
+        profileActivity.authorName(user != null ? user.getName() : null);
         results.add(profileActivity);
       }
       return results;
index b54d251dd0a71a0d54e71e40db05383c0399245c..183e3072a0029fd9efd2144e1407e35d4b5da507 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.qualityprofile.db.ActiveRuleKey;
 import org.sonar.core.rule.RuleDto;
+import org.sonar.core.user.UserDto;
 import org.sonar.server.activity.ActivityService;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
@@ -42,6 +43,7 @@ import org.sonar.server.search.QueryOptions;
 import org.sonar.server.tester.ServerTester;
 import org.sonar.server.user.MockUserSession;
 
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
@@ -134,7 +136,10 @@ public class QProfileServiceMediumTest {
 
   @Test
   public void search_qprofile_activity() throws InterruptedException {
-    MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("me");
+    MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("david");
+
+    UserDto user = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com").setCreatedAt(new Date()).setUpdatedAt(new Date());
+    db.userDao().insert(dbSession, user);
 
     // We need an actual rule in DB to test RuleName in Activity
     RuleDto rule = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1);
@@ -156,8 +161,8 @@ public class QProfileServiceMediumTest {
     assertThat(activity.profileKey()).isEqualTo(XOO_P1_KEY);
     assertThat(activity.severity()).isEqualTo(Severity.MAJOR);
     assertThat(activity.ruleName()).isEqualTo(rule.getName());
-    assertThat(activity.login()).isEqualTo("me");
-    assertThat(activity.authorName()).isNull();
+    assertThat(activity.login()).isEqualTo("david");
+    assertThat(activity.authorName()).isEqualTo("David");
 
     assertThat(activity.parameters()).hasSize(1);
     assertThat(activity.parameters().get("max")).isEqualTo("10");
@@ -184,6 +189,28 @@ public class QProfileServiceMediumTest {
     assertThat(activity.parameters().get("max")).isEqualTo("10");
   }
 
+  @Test
+  public void search_qprofile_activity_with_user_not_found() throws InterruptedException {
+    MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("david");
+
+    // We need an actual rule in DB to test RuleName in Activity
+    db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1);
+
+    tester.get(ActivityService.class).write(dbSession, Activity.Type.QPROFILE,
+      ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1))
+        .setSeverity(Severity.MAJOR)
+        .setParameter("max", "10")
+    );
+    dbSession.commit();
+
+    List<QProfileActivity> activities = service.findActivities(new QProfileActivityQuery(), new QueryOptions());
+    assertThat(activities).hasSize(1);
+
+    QProfileActivity activity = activities.get(0);
+    assertThat(activity.login()).isEqualTo("david");
+    assertThat(activity.authorName()).isNull();
+  }
+
   @Test
   public void search_qprofile_activity_with_rule_not_found() throws InterruptedException {
     MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("me");