From 9366cb85997d872d4656eac38bb9ef835789432d Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 20 Jun 2014 15:18:20 +0200 Subject: [PATCH] SONAR-5329 Add medium test on qprofile activity with author name --- .../java/org/sonar/core/user/UserDao.java | 7 ++++ .../java/org/sonar/core/user/UserMapper.java | 3 ++ .../org/sonar/core/user/UserMapper.xml | 5 +++ .../java/org/sonar/core/user/UserDaoTest.java | 28 +++++++++++++--- .../core/user/UserDaoTest/insert-result.xml | 3 ++ .../server/activity/ws/SearchAction.java | 1 + .../qualityprofile/QProfileService.java | 7 ++-- .../QProfileServiceMediumTest.java | 33 +++++++++++++++++-- 8 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/insert-result.xml diff --git a/sonar-core/src/main/java/org/sonar/core/user/UserDao.java b/sonar-core/src/main/java/org/sonar/core/user/UserDao.java index d022ee0c135..afcda180bea 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/UserDao.java +++ b/sonar-core/src/main/java/org/sonar/core/user/UserDao.java @@ -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 diff --git a/sonar-core/src/main/java/org/sonar/core/user/UserMapper.java b/sonar-core/src/main/java/org/sonar/core/user/UserMapper.java index a2f1dfebe02..6e49630a361 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/UserMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/user/UserMapper.java @@ -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); + } diff --git a/sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml b/sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml index 3b9c25b8f8d..0a272daa876 100644 --- a/sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml @@ -101,4 +101,9 @@ UPDATE users SET active=${_false} WHERE id=#{id} + + INSERT INTO users (login, name, email, active, created_at, updated_at) + VALUES (#{login}, #{name}, #{email}, ${_true}, #{createdAt}, #{updatedAt}) + + diff --git a/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java index 2db206ddbf8..981695b23c9 100644 --- a/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/user/UserDaoTest.java @@ -19,27 +19,35 @@ */ 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 index 00000000000..374d956745d --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/user/UserDaoTest/insert-result.xml @@ -0,0 +1,3 @@ + + + diff --git a/sonar-server/src/main/java/org/sonar/server/activity/ws/SearchAction.java b/sonar-server/src/main/java/org/sonar/server/activity/ws/SearchAction.java index 61788652235..587de65ac8a 100644 --- a/sonar-server/src/main/java/org/sonar/server/activity/ws/SearchAction.java +++ b/sonar-server/src/main/java/org/sonar/server/activity/ws/SearchAction.java @@ -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 diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java index 64dedfbc402..91a4e342fbe 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileService.java @@ -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; diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java index b54d251dd0a..183e3072a00 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileServiceMediumTest.java @@ -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 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"); -- 2.39.5