From 4edadae62e0137c8e7d9cf08868d6b55dfbd620f Mon Sep 17 00:00:00 2001 From: vran Date: Sat, 26 Mar 2022 19:26:50 +0800 Subject: [PATCH] feat: add integration test (#65) * feat: add integration test * feat: update action config * fix: action run failed * feat: add test report step in action --- .github/workflows/auto-check.yml | 12 +- .../com/databasir/api/GroupController.java | 6 +- build.gradle | 10 ++ .../domain/group/service/GroupService.java | 13 +- .../project/data/DataSourcePropertyValue.java | 4 + .../project/service/ProjectService.java | 3 +- .../core/domain/user/service/UserService.java | 12 +- .../constant/RoleConstants.java | 10 ++ .../SystemStartedEventSubscriber.java | 4 +- .../service/DatabaseTypeServiceTest.java | 92 +++++++++++++ .../DocumentDescriptionServiceTest.java | 48 +++++++ .../DocumentDiscussionServiceTest.java | 80 +++++++++++ .../group/service/GroupServiceTest.java | 130 +++++++++++++++++- .../project/service/ProjectServiceTest.java | 125 +++++++++++++++++ .../user/service/UserProjectServiceTest.java | 54 ++++++++ .../domain/user/service/UserServiceTest.java | 55 +++++++- .../DocumentEventSubscriberTest.java | 4 +- .../subscriber/UserEventSubscriberTest.java | 4 +- .../sql/domain/database/CreateDuplicate.sql | 4 + .../sql/domain/database/DeleteById.sql | 4 + .../resources/sql/domain/database/Update.sql | 4 + .../sql/domain/discussion/Create.sql | 6 + .../sql/domain/discussion/DeleteById.sql | 8 ++ .../sql/domain/group/GroupDelete.sql | 22 +++ .../sql/domain/group/GroupUpdate.sql | 10 ++ .../resources/sql/domain/group/MemberAdd.sql | 7 + .../sql/domain/group/MemberRemove.sql | 9 ++ .../sql/domain/group/MemberRoleChange.sql | 5 + .../resources/sql/domain/project/Delete.sql | 10 ++ .../resources/sql/domain/project/Update.sql | 15 ++ .../sql/domain/user/AddFavorites.sql | 2 + .../sql/domain/user/AddSysOwnerTo.sql | 8 +- .../sql/domain/user/RemoveFavorites.sql | 4 + .../sql/domain/user/RemoveSysOwnerFrom.sql | 6 + .../sql/domain/user/RenewPassword.sql | 4 +- .../sql/domain/user/SwitchEnableStatus.sql | 5 +- .../sql/domain/user/UpdateNickname.sql | 7 + .../sql/domain/user/UpdatePassword.sql | 7 + .../DocumentEventSubscriberTest.sql | 38 ++--- .../subscriber/UserEventSubscriberTest.sql | 8 +- .../java/com/databasir/dao/impl/BaseDao.java | 6 + 41 files changed, 806 insertions(+), 59 deletions(-) create mode 100644 core/src/main/java/com/databasir/core/infrastructure/constant/RoleConstants.java create mode 100644 core/src/test/java/com/databasir/core/domain/database/service/DatabaseTypeServiceTest.java create mode 100644 core/src/test/java/com/databasir/core/domain/description/service/DocumentDescriptionServiceTest.java create mode 100644 core/src/test/java/com/databasir/core/domain/discussion/service/DocumentDiscussionServiceTest.java create mode 100644 core/src/test/java/com/databasir/core/domain/project/service/ProjectServiceTest.java create mode 100644 core/src/test/java/com/databasir/core/domain/user/service/UserProjectServiceTest.java create mode 100644 core/src/test/resources/sql/domain/database/CreateDuplicate.sql create mode 100644 core/src/test/resources/sql/domain/database/DeleteById.sql create mode 100644 core/src/test/resources/sql/domain/database/Update.sql create mode 100644 core/src/test/resources/sql/domain/discussion/Create.sql create mode 100644 core/src/test/resources/sql/domain/discussion/DeleteById.sql create mode 100644 core/src/test/resources/sql/domain/group/GroupDelete.sql create mode 100644 core/src/test/resources/sql/domain/group/GroupUpdate.sql create mode 100644 core/src/test/resources/sql/domain/group/MemberAdd.sql create mode 100644 core/src/test/resources/sql/domain/group/MemberRemove.sql create mode 100644 core/src/test/resources/sql/domain/group/MemberRoleChange.sql create mode 100644 core/src/test/resources/sql/domain/project/Delete.sql create mode 100644 core/src/test/resources/sql/domain/project/Update.sql create mode 100644 core/src/test/resources/sql/domain/user/AddFavorites.sql create mode 100644 core/src/test/resources/sql/domain/user/RemoveFavorites.sql create mode 100644 core/src/test/resources/sql/domain/user/RemoveSysOwnerFrom.sql create mode 100644 core/src/test/resources/sql/domain/user/UpdateNickname.sql create mode 100644 core/src/test/resources/sql/domain/user/UpdatePassword.sql diff --git a/.github/workflows/auto-check.yml b/.github/workflows/auto-check.yml index 428f934..759a07b 100644 --- a/.github/workflows/auto-check.yml +++ b/.github/workflows/auto-check.yml @@ -39,14 +39,20 @@ jobs: steps: - uses: actions/checkout@v2 - name: Set up JDK 11 - uses: actions/setup-java@v1 + uses: actions/setup-java@v2 with: + distribution: 'temurin' java-version: '11' - server-id: github # Value of the distributionManagement/repository/id field of the pom.xml - settings-path: ${{ github.workspace }} # location for the settings.xml file + cache: 'gradle' - name: Gradle Permission Grant run: chmod +x ./gradlew - name: Gradle Test run: ./gradlew api:check core:check + + - name: Publish Test Report + uses: mikepenz/action-junit-report@v3 + if: always() + with: + report_paths: '**/build/test-results/test/TEST-*.xml' diff --git a/api/src/main/java/com/databasir/api/GroupController.java b/api/src/main/java/com/databasir/api/GroupController.java index 1860da3..40a7dd4 100644 --- a/api/src/main/java/com/databasir/api/GroupController.java +++ b/api/src/main/java/com/databasir/api/GroupController.java @@ -17,6 +17,8 @@ import javax.validation.Valid; import java.util.Arrays; import java.util.List; +import static com.databasir.core.infrastructure.constant.RoleConstants.GROUP_MEMBER; +import static com.databasir.core.infrastructure.constant.RoleConstants.GROUP_OWNER; import static org.springframework.data.domain.Sort.Direction.DESC; @RestController @@ -86,7 +88,7 @@ public class GroupController { public JsonData addGroupMember(@PathVariable Integer groupId, @RequestBody @Valid GroupMemberCreateRequest request) { userOperationValidator.forbiddenIfUpdateSelfRole(request.getUserId()); - List groupRoles = Arrays.asList("GROUP_OWNER", "GROUP_MEMBER"); + List groupRoles = Arrays.asList(GROUP_OWNER, GROUP_MEMBER); if (!groupRoles.contains(request.getRole())) { throw new IllegalArgumentException("role should be GROUP_OWNER or GROUP_MEMBER"); } @@ -117,7 +119,7 @@ public class GroupController { @PathVariable Integer userId, @RequestBody GroupMemberRoleUpdateRequest request) { userOperationValidator.forbiddenIfUpdateSelfRole(userId); - List groupRoles = Arrays.asList("GROUP_OWNER", "GROUP_MEMBER"); + List groupRoles = Arrays.asList(GROUP_OWNER, GROUP_MEMBER); if (!groupRoles.contains(request.getRole())) { throw new IllegalArgumentException("role should be GROUP_OWNER or GROUP_MEMBER"); } diff --git a/build.gradle b/build.gradle index ddb8c5d..b1b8e4e 100644 --- a/build.gradle +++ b/build.gradle @@ -20,6 +20,7 @@ subprojects { apply plugin: 'org.springframework.boot' apply plugin: 'nu.studer.jooq' apply plugin: 'checkstyle' + apply plugin: 'jacoco' group 'com.databasir' version '1.0.0' @@ -67,6 +68,15 @@ subprojects { ignoreFailures = false maxWarnings = 0 } + + jacocoTestReport { + dependsOn test + reports { + xml.required = false + csv.required = false + html.outputLocation = layout.buildDirectory.dir('reports/jacoco/html') + } + } } diff --git a/core/src/main/java/com/databasir/core/domain/group/service/GroupService.java b/core/src/main/java/com/databasir/core/domain/group/service/GroupService.java index 91e3515..7d99092 100644 --- a/core/src/main/java/com/databasir/core/domain/group/service/GroupService.java +++ b/core/src/main/java/com/databasir/core/domain/group/service/GroupService.java @@ -20,6 +20,8 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; +import static com.databasir.core.infrastructure.constant.RoleConstants.GROUP_OWNER; + @Service @RequiredArgsConstructor public class GroupService { @@ -39,7 +41,7 @@ public class GroupService { private final GroupResponseConverter groupResponseConverter; @Transactional - public void create(GroupCreateRequest request) { + public Integer create(GroupCreateRequest request) { GroupPojo groupPojo = groupPojoConverter.of(request); Integer groupId = groupDao.insertAndReturnId(groupPojo); List roles = request.getGroupOwnerUserIds() @@ -47,25 +49,26 @@ public class GroupService { .map(userId -> { UserRolePojo role = new UserRolePojo(); role.setUserId(userId); - role.setRole("GROUP_OWNER"); + role.setRole(GROUP_OWNER); role.setGroupId(groupId); return role; }) .collect(Collectors.toList()); userRoleDao.batchInsert(roles); + return groupId; } @Transactional public void update(GroupUpdateRequest request) { GroupPojo groupPojo = groupPojoConverter.of(request); groupDao.updateById(groupPojo); - userRoleDao.deleteByRoleAndGroupId("GROUP_OWNER", groupPojo.getId()); + userRoleDao.deleteByRoleAndGroupId(GROUP_OWNER, groupPojo.getId()); List roles = request.getGroupOwnerUserIds() .stream() .map(userId -> { UserRolePojo role = new UserRolePojo(); role.setUserId(userId); - role.setRole("GROUP_OWNER"); + role.setRole(GROUP_OWNER); role.setGroupId(groupPojo.getId()); return role; }) @@ -114,7 +117,7 @@ public class GroupService { public GroupResponse get(Integer groupId) { GroupPojo groupPojo = groupDao.selectById(groupId); - List users = userDao.selectLimitUsersByRoleAndGroup(groupId, "GROUP_OWNER", 50); + List users = userDao.selectLimitUsersByRoleAndGroup(groupId, GROUP_OWNER, 50); return groupResponseConverter.toResponse(groupPojo, users); } diff --git a/core/src/main/java/com/databasir/core/domain/project/data/DataSourcePropertyValue.java b/core/src/main/java/com/databasir/core/domain/project/data/DataSourcePropertyValue.java index 990eb82..d44220f 100644 --- a/core/src/main/java/com/databasir/core/domain/project/data/DataSourcePropertyValue.java +++ b/core/src/main/java/com/databasir/core/domain/project/data/DataSourcePropertyValue.java @@ -1,10 +1,14 @@ package com.databasir.core.domain.project.data; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; import javax.validation.constraints.NotBlank; @Data +@NoArgsConstructor +@AllArgsConstructor public class DataSourcePropertyValue { @NotBlank diff --git a/core/src/main/java/com/databasir/core/domain/project/service/ProjectService.java b/core/src/main/java/com/databasir/core/domain/project/service/ProjectService.java index 7e85452..db2b50d 100644 --- a/core/src/main/java/com/databasir/core/domain/project/service/ProjectService.java +++ b/core/src/main/java/com/databasir/core/domain/project/service/ProjectService.java @@ -59,7 +59,7 @@ public class ProjectService { } @Transactional - public void create(ProjectCreateRequest request) { + public Integer create(ProjectCreateRequest request) { ProjectPojo project = projectPojoConverter.of(request); Integer projectId = null; try { @@ -78,6 +78,7 @@ public class ProjectService { ProjectSyncRulePojo syncRule = projectPojoConverter.of(request.getProjectSyncRule(), projectId); projectSyncRuleDao.insertAndReturnId(syncRule); + return projectId; } @Transactional diff --git a/core/src/main/java/com/databasir/core/domain/user/service/UserService.java b/core/src/main/java/com/databasir/core/domain/user/service/UserService.java index a4f05e6..6c89828 100644 --- a/core/src/main/java/com/databasir/core/domain/user/service/UserService.java +++ b/core/src/main/java/com/databasir/core/domain/user/service/UserService.java @@ -26,6 +26,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.*; +import static com.databasir.core.infrastructure.constant.RoleConstants.SYS_OWNER; import static java.util.stream.Collectors.*; @Service @@ -63,7 +64,7 @@ public class UserService { .filter(ur -> ur.getGroupId() != null) .collect(groupingBy(UserRolePojo::getUserId, mapping(UserRolePojo::getGroupId, toList()))); Map> sysOwnerGroupByUserId = userRoles.stream() - .filter(ur -> ur.getRole().equals("SYS_OWNER")) + .filter(ur -> ur.getRole().equals(SYS_OWNER)) .collect(groupingBy(UserRolePojo::getUserId)); return users.map(user -> userResponseConverter.pageResponse(user, sysOwnerGroupByUserId.containsKey(user.getId()), @@ -143,16 +144,16 @@ public class UserService { } public void removeSysOwnerFrom(Integer userId) { - if (userRoleDao.hasRole(userId, "SYS_OWNER")) { - userRoleDao.deleteRole(userId, "SYS_OWNER"); + if (userRoleDao.hasRole(userId, SYS_OWNER)) { + userRoleDao.deleteRole(userId, SYS_OWNER); } } public void addSysOwnerTo(Integer userId) { - if (!userRoleDao.hasRole(userId, "SYS_OWNER")) { + if (!userRoleDao.hasRole(userId, SYS_OWNER)) { UserRolePojo role = new UserRolePojo(); role.setUserId(userId); - role.setRole("SYS_OWNER"); + role.setRole(SYS_OWNER); userRoleDao.insertAndReturnId(role); } } @@ -167,6 +168,7 @@ public class UserService { } String newHashedPassword = bCryptPasswordEncoder.encode(request.getNewPassword()); userDao.updatePassword(userId, newHashedPassword); + loginDao.deleteByUserId(userId); } public void updateNickname(Integer userId, UserNicknameUpdateRequest request) { diff --git a/core/src/main/java/com/databasir/core/infrastructure/constant/RoleConstants.java b/core/src/main/java/com/databasir/core/infrastructure/constant/RoleConstants.java new file mode 100644 index 0000000..fadf7e6 --- /dev/null +++ b/core/src/main/java/com/databasir/core/infrastructure/constant/RoleConstants.java @@ -0,0 +1,10 @@ +package com.databasir.core.infrastructure.constant; + +public interface RoleConstants { + + String SYS_OWNER = "SYS_OWNER"; + + String GROUP_OWNER = "GROUP_OWNER"; + + String GROUP_MEMBER = "GROUP_MEMBER"; +} diff --git a/core/src/main/java/com/databasir/core/infrastructure/event/subscriber/SystemStartedEventSubscriber.java b/core/src/main/java/com/databasir/core/infrastructure/event/subscriber/SystemStartedEventSubscriber.java index 02693e2..a72a8a3 100644 --- a/core/src/main/java/com/databasir/core/infrastructure/event/subscriber/SystemStartedEventSubscriber.java +++ b/core/src/main/java/com/databasir/core/infrastructure/event/subscriber/SystemStartedEventSubscriber.java @@ -27,6 +27,8 @@ import java.util.Optional; import java.util.function.BiFunction; import java.util.stream.Collectors; +import static com.databasir.core.infrastructure.constant.RoleConstants.SYS_OWNER; + @Component @Slf4j @RequiredArgsConstructor @@ -129,7 +131,7 @@ public class SystemStartedEventSubscriber { Integer userId = userDao.insertAndReturnId(admin); UserRolePojo role = new UserRolePojo(); role.setUserId(userId); - role.setRole("SYS_OWNER"); + role.setRole(SYS_OWNER); userRoleDao.insertAndReturnId(role); } } diff --git a/core/src/test/java/com/databasir/core/domain/database/service/DatabaseTypeServiceTest.java b/core/src/test/java/com/databasir/core/domain/database/service/DatabaseTypeServiceTest.java new file mode 100644 index 0000000..ed2d134 --- /dev/null +++ b/core/src/test/java/com/databasir/core/domain/database/service/DatabaseTypeServiceTest.java @@ -0,0 +1,92 @@ +package com.databasir.core.domain.database.service; + +import com.databasir.common.DatabasirException; +import com.databasir.core.BaseTest; +import com.databasir.core.domain.DomainErrors; +import com.databasir.core.domain.database.data.DatabaseTypeCreateRequest; +import com.databasir.core.domain.database.data.DatabaseTypeUpdateRequest; +import com.databasir.dao.impl.DatabaseTypeDao; +import com.databasir.dao.tables.pojos.DatabaseTypePojo; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.transaction.annotation.Transactional; + +@Transactional +class DatabaseTypeServiceTest extends BaseTest { + + @Autowired + private DatabaseTypeService databaseTypeService; + + @Autowired + private DatabaseTypeDao databaseTypeDao; + + @Test + void create() { + DatabaseTypeCreateRequest request = new DatabaseTypeCreateRequest(); + request.setDatabaseType("ut-mysql"); + request.setIcon(""); + request.setDescription("integration test"); + request.setJdbcDriverFileUrl("some url"); + request.setJdbcDriverClassName("com.mysql.jdbc.Driver"); + request.setJdbcProtocol("jdbc:mysql"); + request.setUrlPattern("{{jdbc.protocol}}//{{db.url}}/{{db.schema}}"); + Integer id = databaseTypeService.create(request); + Assertions.assertNotNull(id); + } + + @Test + @Sql("classpath:sql/domain/database/CreateDuplicate.sql") + void createWhenDatabaseTypeDuplicate() { + DatabaseTypeCreateRequest request = new DatabaseTypeCreateRequest(); + request.setDatabaseType("ut-mysql"); + request.setIcon(""); + request.setDescription("integration test"); + request.setJdbcDriverFileUrl("some url"); + request.setJdbcDriverClassName("com.mysql.jdbc.Driver"); + request.setJdbcProtocol("jdbc:mysql"); + request.setUrlPattern("{{jdbc.protocol}}//{{db.url}}/{{db.schema}}"); + + DatabasirException err = Assertions.assertThrows(DatabasirException.class, + () -> databaseTypeService.create(request)); + Assertions.assertEquals(DomainErrors.DATABASE_TYPE_NAME_DUPLICATE.getErrCode(), err.getErrCode()); + } + + @Test + @Sql("classpath:sql/domain/database/Update.sql") + void update() { + DatabaseTypeUpdateRequest request = new DatabaseTypeUpdateRequest(); + request.setId(-1000); + request.setIcon(""); + request.setDatabaseType("new-type"); + request.setDescription("integration test"); + request.setJdbcDriverFileUrl("some url"); + request.setJdbcDriverClassName("com.mysql.jdbc.Driver"); + request.setJdbcProtocol("jdbc:postgresql"); + request.setUrlPattern("{{jdbc.protocol}}//{{db.url}}/{{db.schema}}"); + databaseTypeService.update(request); + + DatabaseTypePojo pojo = databaseTypeDao.selectByDatabaseType("new-type"); + Assertions.assertNotNull(pojo); + Assertions.assertEquals("integration test", pojo.getDescription()); + Assertions.assertEquals("jdbc:postgresql", pojo.getJdbcProtocol()); + Assertions.assertEquals("{{jdbc.protocol}}//{{db.url}}/{{db.schema}}", pojo.getUrlPattern()); + } + + @Test + @Sql("classpath:sql/domain/database/DeleteById.sql") + void deleteById() { + int id = -1000; + databaseTypeService.deleteById(id); + Assertions.assertFalse(databaseTypeDao.existsById(id)); + } + + @Test + void deleteByIdWhenNotExists() { + int id = -1000; + databaseTypeService.deleteById(id); + Assertions.assertFalse(databaseTypeDao.existsById(id)); + } + +} \ No newline at end of file diff --git a/core/src/test/java/com/databasir/core/domain/description/service/DocumentDescriptionServiceTest.java b/core/src/test/java/com/databasir/core/domain/description/service/DocumentDescriptionServiceTest.java new file mode 100644 index 0000000..bf25675 --- /dev/null +++ b/core/src/test/java/com/databasir/core/domain/description/service/DocumentDescriptionServiceTest.java @@ -0,0 +1,48 @@ +package com.databasir.core.domain.description.service; + +import com.databasir.core.BaseTest; +import com.databasir.core.domain.description.data.DocumentDescriptionSaveRequest; +import com.databasir.dao.impl.DocumentDescriptionDao; +import com.databasir.dao.tables.pojos.DocumentDescriptionPojo; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Transactional +class DocumentDescriptionServiceTest extends BaseTest { + + @Autowired + private DocumentDescriptionService documentDescriptionService; + + @Autowired + private DocumentDescriptionDao documentDescriptionDao; + + @Test + void save() { + DocumentDescriptionSaveRequest saveRequest = new DocumentDescriptionSaveRequest(); + saveRequest.setTableName("ut"); + saveRequest.setColumnName("ut"); + saveRequest.setContent("hello world"); + int groupId = -1; + int projectId = -1; + int userId = -1; + documentDescriptionService.save(groupId, projectId, userId, saveRequest); + + boolean isExists = documentDescriptionDao.exists(projectId, + saveRequest.getTableName(), saveRequest.getColumnName()); + Assertions.assertTrue(isExists); + + DocumentDescriptionSaveRequest updateRequest = new DocumentDescriptionSaveRequest(); + updateRequest.setTableName("ut"); + updateRequest.setColumnName("ut"); + updateRequest.setContent("update content"); + documentDescriptionService.save(groupId, projectId, userId, updateRequest); + var tableData = documentDescriptionDao.selectTableDescriptionByProjectId(projectId); + Assertions.assertEquals(0, tableData.size()); + List descriptionData = documentDescriptionDao.selectByProjectId(projectId); + Assertions.assertEquals(1, descriptionData.size()); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/databasir/core/domain/discussion/service/DocumentDiscussionServiceTest.java b/core/src/test/java/com/databasir/core/domain/discussion/service/DocumentDiscussionServiceTest.java new file mode 100644 index 0000000..521acd7 --- /dev/null +++ b/core/src/test/java/com/databasir/core/domain/discussion/service/DocumentDiscussionServiceTest.java @@ -0,0 +1,80 @@ +package com.databasir.core.domain.discussion.service; + +import com.databasir.common.exception.Forbidden; +import com.databasir.core.BaseTest; +import com.databasir.core.domain.discussion.data.DiscussionCreateRequest; +import com.databasir.core.infrastructure.event.subscriber.DiscussionEventSubscriber; +import com.databasir.dao.Tables; +import com.databasir.dao.impl.DocumentDiscussionDao; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.transaction.annotation.Transactional; + +import static org.mockito.Mockito.*; + +@Transactional +class DocumentDiscussionServiceTest extends BaseTest { + + @Autowired + private DocumentDiscussionService documentDiscussionService; + + @Autowired + private DocumentDiscussionDao documentDiscussionDao; + + @MockBean + private DiscussionEventSubscriber discussionEventSubscriber; + + @Test + @Sql("classpath:sql/domain/discussion/DeleteById.sql") + void deleteById() { + int groupId = -999; + int projectId = -999; + int discussionId = -999; + documentDiscussionService.deleteById(groupId, projectId, discussionId); + + Assertions.assertFalse(documentDiscussionDao.existsById(discussionId)); + } + + @Test + void deleteByIdWhenProjectNotExists() { + int groupId = -1; + int projectId = -2; + Assertions.assertThrows(Forbidden.class, + () -> documentDiscussionService.deleteById(groupId, projectId, -1)); + } + + @Test + @Sql("classpath:sql/domain/discussion/Create.sql") + void create() { + doNothing().when(discussionEventSubscriber).onDiscussionCreated(any()); + + DiscussionCreateRequest request = new DiscussionCreateRequest(); + request.setContent("test"); + request.setColumnName("test"); + request.setTableName("test"); + int groupId = -999; + int projectId = -999; + int userId = -999; + documentDiscussionService.create(groupId, projectId, userId, request); + + var data = documentDiscussionDao.selectList(Tables.DOCUMENT_DISCUSSION.PROJECT_ID.eq(projectId)); + Assertions.assertEquals(1, data.size()); + verify(discussionEventSubscriber, times(1)).onDiscussionCreated(any()); + } + + @Test + void createWhenProjectNotExists() { + DiscussionCreateRequest request = new DiscussionCreateRequest(); + request.setContent("test"); + request.setColumnName("test"); + request.setTableName("test"); + int groupId = -1; + int projectId = -1; + int userId = -1; + Assertions.assertThrows(Forbidden.class, + () -> documentDiscussionService.create(groupId, projectId, userId, request)); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/databasir/core/domain/group/service/GroupServiceTest.java b/core/src/test/java/com/databasir/core/domain/group/service/GroupServiceTest.java index a4ee563..6adb66c 100644 --- a/core/src/test/java/com/databasir/core/domain/group/service/GroupServiceTest.java +++ b/core/src/test/java/com/databasir/core/domain/group/service/GroupServiceTest.java @@ -1,32 +1,160 @@ package com.databasir.core.domain.group.service; +import com.databasir.common.DatabasirException; +import com.databasir.core.BaseTest; +import com.databasir.core.domain.DomainErrors; +import com.databasir.core.domain.group.data.GroupCreateRequest; +import com.databasir.core.domain.group.data.GroupMemberCreateRequest; +import com.databasir.core.domain.group.data.GroupUpdateRequest; +import com.databasir.core.infrastructure.constant.RoleConstants; +import com.databasir.dao.Tables; +import com.databasir.dao.impl.GroupDao; +import com.databasir.dao.impl.ProjectDao; +import com.databasir.dao.impl.ProjectSyncRuleDao; +import com.databasir.dao.impl.UserRoleDao; +import com.databasir.dao.tables.pojos.ProjectSyncRulePojo; +import com.databasir.dao.tables.pojos.UserRolePojo; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.*; -class GroupServiceTest { +@Transactional +class GroupServiceTest extends BaseTest { + + @Autowired + private GroupService groupService; + + @Autowired + private UserRoleDao userRoleDao; + + @Autowired + private GroupDao groupDao; + + @Autowired + private ProjectDao projectDao; + + @Autowired + private ProjectSyncRuleDao projectSyncRuleDao; @Test void create() { + GroupCreateRequest request = new GroupCreateRequest(); + request.setName(UUID.randomUUID().toString()); + request.setDescription("group service test"); + request.setGroupOwnerUserIds(List.of(1, 2, 3)); + Integer groupId = groupService.create(request); + assertNotNull(groupId); + + List roles = userRoleDao.selectByUserIds(List.of(1, 2, 3)) + .stream() + .filter(r -> Objects.equals(r.getGroupId(), groupId) && r.getRole().equals("GROUP_OWNER")) + .collect(Collectors.toList()); + assertEquals(3, roles.size()); } @Test + @Sql("classpath:sql/domain/group/GroupUpdate.sql") void update() { + int groupId = -999; + GroupUpdateRequest request = new GroupUpdateRequest(); + request.setId(groupId); + request.setName(UUID.randomUUID().toString()); + request.setDescription(UUID.randomUUID().toString()); + request.setGroupOwnerUserIds(List.of(1000, 1001)); + groupService.update(request); + + List members = userRoleDao.selectList(Tables.USER_ROLE.GROUP_ID.eq(groupId)); + assertEquals(3, members.size()); + List ownerUserIds = members.stream() + .filter(r -> r.getRole().equals("GROUP_OWNER")) + .map(UserRolePojo::getUserId) + .collect(Collectors.toList()); + assertEquals(2, ownerUserIds.size()); + assertTrue(ownerUserIds.contains(1000)); + assertTrue(ownerUserIds.contains(1001)); } @Test + @Sql("classpath:sql/domain/group/GroupDelete.sql") + @SuppressWarnings("checkstyle:VariableDeclarationUsageDistance") void delete() { + int groupId = -999; + List undeleteProjectIds = projectDao.selectProjectIdsByGroupId(groupId); + groupService.delete(groupId); + + // should clear all projects + List projectIds = projectDao.selectProjectIdsByGroupId(groupId); + assertEquals(0, projectIds.size()); + + // should clear group members + List members = userRoleDao.selectList(Tables.USER_ROLE.GROUP_ID.eq(groupId)); + assertEquals(0, members.size()); + + // should clear project sync schedule + List syncRule = projectSyncRuleDao.selectInProjectIds(undeleteProjectIds); + assertTrue(syncRule.stream().allMatch(r -> !r.getIsAutoSync())); } @Test + @Sql("classpath:sql/domain/group/MemberRemove.sql") void removeMember() { + int groupId = -999; + // remove group member + assertTrue(userRoleDao.hasRole(-1, groupId)); + groupService.removeMember(groupId, -1); + assertFalse(userRoleDao.hasRole(-1, groupId)); + + // remove group owner + assertTrue(userRoleDao.hasRole(-2, groupId)); + groupService.removeMember(groupId, -2); + assertFalse(userRoleDao.hasRole(-2, groupId)); + assertTrue(userRoleDao.hasRole(-2, -1000)); } @Test + @Sql("classpath:sql/domain/group/MemberAdd.sql") void addMember() { + int groupId = -999; + assertFalse(userRoleDao.hasRole(-1, groupId, RoleConstants.GROUP_MEMBER)); + + GroupMemberCreateRequest request = new GroupMemberCreateRequest(); + request.setRole(RoleConstants.GROUP_MEMBER); + request.setUserId(-1); + groupService.addMember(groupId, request); + + assertTrue(userRoleDao.hasRole(-1, groupId, RoleConstants.GROUP_MEMBER)); } @Test + @Sql("classpath:sql/domain/group/MemberAdd.sql") + void addMemberWhenWasGroupMember() { + int groupId = -1000; + GroupMemberCreateRequest request = new GroupMemberCreateRequest(); + request.setRole(RoleConstants.GROUP_MEMBER); + request.setUserId(-2); + var err = assertThrows(DatabasirException.class, () -> groupService.addMember(groupId, request)); + assertEquals(DomainErrors.USER_ROLE_DUPLICATE.getErrCode(), err.getErrCode()); + } + + @Test + @Sql("classpath:sql/domain/group/MemberRoleChange.sql") void changeMemberRole() { + int groupId = -999; + int userId = -1; + groupService.changeMemberRole(groupId, userId, RoleConstants.GROUP_MEMBER); + assertTrue(userRoleDao.hasRole(userId, groupId, RoleConstants.GROUP_MEMBER)); + + groupService.changeMemberRole(groupId, userId, RoleConstants.GROUP_OWNER); + assertTrue(userRoleDao.hasRole(userId, groupId, RoleConstants.GROUP_OWNER)); + List roles = userRoleDao.selectByUserIds(List.of(userId)); + assertEquals(1, roles.size()); } } \ No newline at end of file diff --git a/core/src/test/java/com/databasir/core/domain/project/service/ProjectServiceTest.java b/core/src/test/java/com/databasir/core/domain/project/service/ProjectServiceTest.java new file mode 100644 index 0000000..8b197f5 --- /dev/null +++ b/core/src/test/java/com/databasir/core/domain/project/service/ProjectServiceTest.java @@ -0,0 +1,125 @@ +package com.databasir.core.domain.project.service; + +import com.databasir.core.BaseTest; +import com.databasir.core.domain.project.data.DataSourcePropertyValue; +import com.databasir.core.domain.project.data.ProjectCreateRequest; +import com.databasir.core.domain.project.data.ProjectCreateRequest.DataSourceCreateRequest; +import com.databasir.core.domain.project.data.ProjectCreateRequest.ProjectSyncRuleCreateRequest; +import com.databasir.core.domain.project.data.ProjectTestConnectionRequest; +import com.databasir.core.domain.project.data.ProjectUpdateRequest; +import com.databasir.core.domain.project.data.ProjectUpdateRequest.ProjectSyncRuleUpdateRequest; +import com.databasir.core.infrastructure.connection.DatabaseTypes; +import com.databasir.dao.impl.ProjectDao; +import com.databasir.dao.impl.ProjectSyncRuleDao; +import com.databasir.dao.tables.pojos.ProjectSyncRulePojo; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Transactional +class ProjectServiceTest extends BaseTest { + + @Autowired + private ProjectService projectService; + + @Autowired + private ProjectDao projectDao; + + @Autowired + private ProjectSyncRuleDao projectSyncRuleDao; + + @Value("${databasir.db.username}") + private String dbUsername; + + @Value("${databasir.db.password}") + private String dbPassword; + + @Value("${databasir.db.url}") + private String dbUrl; + + @Test + void create() { + ProjectCreateRequest request = new ProjectCreateRequest(); + request.setName("ut"); + request.setDescription("integration test"); + request.setGroupId(-1000); + + DataSourceCreateRequest dataSource = new DataSourceCreateRequest(); + dataSource.setUsername("root"); + dataSource.setPassword("123456"); + dataSource.setUrl("localhost:3306"); + dataSource.setDatabaseName("databasir"); + dataSource.setSchemaName("databasir"); + dataSource.setDatabaseType(DatabaseTypes.MYSQL); + dataSource.setProperties(List.of(new DataSourcePropertyValue("useSSL", "false"))); + request.setDataSource(dataSource); + + ProjectSyncRuleCreateRequest syncRule = new ProjectSyncRuleCreateRequest(); + syncRule.setIgnoreTableNameRegexes(List.of("flywway.*", "demo.*")); + syncRule.setIgnoreColumnNameRegexes(List.of("id.*", "demo.*")); + syncRule.setIsAutoSync(true); + syncRule.setAutoSyncCron("0 0 0-23 0 0 ? *"); + request.setProjectSyncRule(syncRule); + + Integer id = projectService.create(request); + Assertions.assertNotNull(id); + } + + @Test + @Sql("classpath:sql/domain/project/Update.sql") + void update() { + ProjectUpdateRequest request = new ProjectUpdateRequest(); + request.setId(-1000); + request.setName("ut"); + request.setDescription("integration test"); + + var dataSource = new ProjectUpdateRequest.DataSourceUpdateRequest(); + dataSource.setUsername("root"); + dataSource.setPassword("123456"); + dataSource.setUrl("localhost:3306"); + dataSource.setDatabaseName("databasir"); + dataSource.setSchemaName("databasir"); + dataSource.setDatabaseType(DatabaseTypes.MYSQL); + dataSource.setProperties(List.of(new DataSourcePropertyValue("useSSL", "false"))); + request.setDataSource(dataSource); + + var syncRule = new ProjectSyncRuleUpdateRequest(); + syncRule.setIgnoreTableNameRegexes(List.of("flywway.*", "demo.*")); + syncRule.setIgnoreColumnNameRegexes(List.of("id.*", "demo.*")); + syncRule.setIsAutoSync(true); + syncRule.setAutoSyncCron("0 0 0-23 0 0 ? *"); + request.setProjectSyncRule(syncRule); + + projectService.update(-999, request); + } + + @Test + @Sql("classpath:sql/domain/project/Delete.sql") + void delete() { + int projectId = -1000; + projectService.delete(projectId); + Assertions.assertFalse(projectDao.existsById(projectId)); + ProjectSyncRulePojo syncRule = projectSyncRuleDao.selectByProjectId(projectId); + Assertions.assertNotNull(syncRule); + Assertions.assertNotNull(syncRule.getAutoSyncCron()); + Assertions.assertFalse(syncRule.getIsAutoSync()); + } + + @Test + void testConnection() { + ProjectTestConnectionRequest request = new ProjectTestConnectionRequest(); + request.setUsername(dbUsername); + request.setPassword(dbPassword); + request.setUrl(dbUrl); + request.setDatabaseName("databasir"); + request.setSchemaName("databasir"); + request.setDatabaseType(DatabaseTypes.MYSQL); + request.setProperties(List.of(new DataSourcePropertyValue("useSSL", "false"))); + projectService.testConnection(request); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/databasir/core/domain/user/service/UserProjectServiceTest.java b/core/src/test/java/com/databasir/core/domain/user/service/UserProjectServiceTest.java new file mode 100644 index 0000000..0b6c188 --- /dev/null +++ b/core/src/test/java/com/databasir/core/domain/user/service/UserProjectServiceTest.java @@ -0,0 +1,54 @@ +package com.databasir.core.domain.user.service; + +import com.databasir.common.DatabasirException; +import com.databasir.core.BaseTest; +import com.databasir.core.domain.DomainErrors; +import com.databasir.dao.impl.UserFavoriteProjectDao; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Transactional +class UserProjectServiceTest extends BaseTest { + + @Autowired + private UserProjectService userProjectService; + + @Autowired + private UserFavoriteProjectDao userFavoriteProjectDao; + + @Test + @Sql("classpath:sql/domain/user/AddFavorites.sql") + void addFavorites() { + int userId = 1; + int projectId = 999; + userProjectService.addFavorites(projectId, userId); + var data = userFavoriteProjectDao.selectByUserIdAndProjectIds(userId, List.of(projectId)); + Assertions.assertEquals(1, data.size()); + } + + @Test + void addFavoritesWhenProjectNotExists() { + DatabasirException ex = Assertions.assertThrows(DatabasirException.class, + () -> userProjectService.addFavorites(-999, 1)); + Assertions.assertEquals(DomainErrors.PROJECT_NOT_FOUND.getErrCode(), ex.getErrCode()); + } + + @Test + @Sql("classpath:sql/domain/user/RemoveFavorites.sql") + void removeFavorites() { + int projectId = 999; + int userId = 999; + // remove exists data + userProjectService.removeFavorites(projectId, userId); + var data = userFavoriteProjectDao.selectByUserIdAndProjectIds(userId, List.of(projectId)); + Assertions.assertEquals(0, data.size()); + + // remove unknown data + userProjectService.removeFavorites(-999, -999); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/databasir/core/domain/user/service/UserServiceTest.java b/core/src/test/java/com/databasir/core/domain/user/service/UserServiceTest.java index 15b9464..987d02a 100644 --- a/core/src/test/java/com/databasir/core/domain/user/service/UserServiceTest.java +++ b/core/src/test/java/com/databasir/core/domain/user/service/UserServiceTest.java @@ -2,21 +2,29 @@ package com.databasir.core.domain.user.service; import com.databasir.core.BaseTest; import com.databasir.core.domain.user.data.UserCreateRequest; +import com.databasir.core.domain.user.data.UserNicknameUpdateRequest; +import com.databasir.core.domain.user.data.UserPasswordUpdateRequest; import com.databasir.core.domain.user.data.UserSource; import com.databasir.core.infrastructure.event.subscriber.UserEventSubscriber; import com.databasir.dao.impl.LoginDao; import com.databasir.dao.impl.UserDao; +import com.databasir.dao.impl.UserRoleDao; import com.databasir.dao.tables.pojos.LoginPojo; import com.databasir.dao.tables.pojos.UserPojo; +import com.databasir.dao.tables.pojos.UserRolePojo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.jdbc.Sql; import org.springframework.transaction.annotation.Transactional; +import java.util.Collections; +import java.util.List; import java.util.Optional; import java.util.UUID; +import java.util.stream.Collectors; +import static com.databasir.core.infrastructure.constant.RoleConstants.SYS_OWNER; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -35,6 +43,9 @@ class UserServiceTest extends BaseTest { @Autowired private LoginDao loginDao; + @Autowired + private UserRoleDao userRoleDao; + @Test void create() { doNothing().when(userEventSubscriber).onUserCreated(any()); @@ -56,7 +67,7 @@ class UserServiceTest extends BaseTest { @Sql("classpath:sql/domain/user/RenewPassword.sql") void renewPassword() { doNothing().when(userEventSubscriber).onPasswordRenewed(any()); - String newPassword = userService.renewPassword(1, 2); + String newPassword = userService.renewPassword(-1, -2); assertNotNull(newPassword); assertEquals(8, newPassword.length()); verify(userEventSubscriber, times(1)).onPasswordRenewed(any()); @@ -65,7 +76,7 @@ class UserServiceTest extends BaseTest { @Test @Sql("classpath:sql/domain/user/SwitchEnableStatus.sql") void switchEnableStatusToFalse() { - Integer userId = 1; + Integer userId = -999; userService.switchEnableStatus(userId, false); UserPojo user = userDao.selectById(userId); assertNotNull(user); @@ -76,22 +87,54 @@ class UserServiceTest extends BaseTest { } @Test + @Sql("classpath:sql/domain/user/RemoveSysOwnerFrom.sql") void removeSysOwnerFrom() { - // TODO + Integer userId = -998; + userService.removeSysOwnerFrom(userId); + List roles = userRoleDao.selectByUserIds(Collections.singletonList(userId)) + .stream().filter(role -> role.getRole().equals(SYS_OWNER)) + .collect(Collectors.toList()); + assertEquals(0, roles.size()); } @Test + @Sql("classpath:sql/domain/user/AddSysOwnerTo.sql") void addSysOwnerTo() { - // TODO + Integer userId = -999; + userService.addSysOwnerTo(userId); + List roles = userRoleDao.selectByUserIds(Collections.singletonList(userId)) + .stream().filter(role -> role.getRole().equals(SYS_OWNER)) + .collect(Collectors.toList()); + + assertEquals(1, roles.size()); + assertEquals(SYS_OWNER, roles.iterator().next().getRole()); } @Test + @Sql("classpath:sql/domain/user/UpdatePassword.sql") void updatePassword() { - // TODO + UserPasswordUpdateRequest request = new UserPasswordUpdateRequest(); + request.setNewPassword("123456"); + request.setConfirmNewPassword("123456"); + request.setOriginPassword("123123"); + Integer userId = -999; + userService.updatePassword(userId, request); + // should delete login info + Optional loginPojoOpt = loginDao.selectByUserId(userId); + assertTrue(loginPojoOpt.isEmpty()); } @Test + @Sql("classpath:sql/domain/user/UpdateNickname.sql") void updateNickname() { - // TODO + Integer userId = -999; + UserNicknameUpdateRequest request = new UserNicknameUpdateRequest(); + String nickname = UUID.randomUUID().toString(); + request.setNickname(nickname); + userService.updateNickname(userId, request); + + UserPojo user = userDao.selectById(userId); + assertNotNull(user); + assertEquals(nickname, user.getNickname()); } } \ No newline at end of file diff --git a/core/src/test/java/com/databasir/core/infrastructure/event/subscriber/DocumentEventSubscriberTest.java b/core/src/test/java/com/databasir/core/infrastructure/event/subscriber/DocumentEventSubscriberTest.java index d0b085d..51ab611 100644 --- a/core/src/test/java/com/databasir/core/infrastructure/event/subscriber/DocumentEventSubscriberTest.java +++ b/core/src/test/java/com/databasir/core/infrastructure/event/subscriber/DocumentEventSubscriberTest.java @@ -11,12 +11,14 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.jdbc.Sql; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; +@Transactional class DocumentEventSubscriberTest extends BaseTest { @Autowired @@ -52,7 +54,7 @@ class DocumentEventSubscriberTest extends BaseTest { event.setDiff(diff); event.setNewVersion(2L); event.setOldVersion(1L); - event.setProjectId(1); + event.setProjectId(-1); documentEventSubscriber.onDocumentUpdated(event); verify(mailSender, times(1)).batchSendHtml(any(), any(), any(), any()); } diff --git a/core/src/test/java/com/databasir/core/infrastructure/event/subscriber/UserEventSubscriberTest.java b/core/src/test/java/com/databasir/core/infrastructure/event/subscriber/UserEventSubscriberTest.java index 08961dd..264af64 100644 --- a/core/src/test/java/com/databasir/core/infrastructure/event/subscriber/UserEventSubscriberTest.java +++ b/core/src/test/java/com/databasir/core/infrastructure/event/subscriber/UserEventSubscriberTest.java @@ -10,11 +10,13 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.jdbc.Sql; +import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import static org.mockito.Mockito.*; +@Transactional class UserEventSubscriberTest extends BaseTest { @Autowired @@ -39,7 +41,7 @@ class UserEventSubscriberTest extends BaseTest { .newPassword("123456") .nickname("demo") .renewTime(LocalDateTime.now()) - .renewByUserId(1) + .renewByUserId(-999) .build(); userEventSubscriber.onPasswordRenewed(event); verify(mailSender, times(1)).sendHtml(any(), any(), any(), any()); diff --git a/core/src/test/resources/sql/domain/database/CreateDuplicate.sql b/core/src/test/resources/sql/domain/database/CreateDuplicate.sql new file mode 100644 index 0000000..41fee4f --- /dev/null +++ b/core/src/test/resources/sql/domain/database/CreateDuplicate.sql @@ -0,0 +1,4 @@ +INSERT INTO databasir.database_type (id, database_type, icon, description, jdbc_driver_file_url, jdbc_driver_class_name, + jdbc_protocol, url_pattern) +VALUES (-1000, 'ut-mysql', '', 'system default mysql', 'N/A', 'com.mysql.cj.jdbc.Driver', 'jdbc:mysql', + '{{jdbc.protocol}}://{{db.url}}/{{db.name}}'); diff --git a/core/src/test/resources/sql/domain/database/DeleteById.sql b/core/src/test/resources/sql/domain/database/DeleteById.sql new file mode 100644 index 0000000..41fee4f --- /dev/null +++ b/core/src/test/resources/sql/domain/database/DeleteById.sql @@ -0,0 +1,4 @@ +INSERT INTO databasir.database_type (id, database_type, icon, description, jdbc_driver_file_url, jdbc_driver_class_name, + jdbc_protocol, url_pattern) +VALUES (-1000, 'ut-mysql', '', 'system default mysql', 'N/A', 'com.mysql.cj.jdbc.Driver', 'jdbc:mysql', + '{{jdbc.protocol}}://{{db.url}}/{{db.name}}'); diff --git a/core/src/test/resources/sql/domain/database/Update.sql b/core/src/test/resources/sql/domain/database/Update.sql new file mode 100644 index 0000000..41fee4f --- /dev/null +++ b/core/src/test/resources/sql/domain/database/Update.sql @@ -0,0 +1,4 @@ +INSERT INTO databasir.database_type (id, database_type, icon, description, jdbc_driver_file_url, jdbc_driver_class_name, + jdbc_protocol, url_pattern) +VALUES (-1000, 'ut-mysql', '', 'system default mysql', 'N/A', 'com.mysql.cj.jdbc.Driver', 'jdbc:mysql', + '{{jdbc.protocol}}://{{db.url}}/{{db.name}}'); diff --git a/core/src/test/resources/sql/domain/discussion/Create.sql b/core/src/test/resources/sql/domain/discussion/Create.sql new file mode 100644 index 0000000..66f68ce --- /dev/null +++ b/core/src/test/resources/sql/domain/discussion/Create.sql @@ -0,0 +1,6 @@ +INSERT INTO databasir.`group` (id, name, description, deleted, update_at, create_at) +VALUES (-999, '会员平台 ut', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'); + +INSERT INTO databasir.project (id, name, description, group_id, deleted, deleted_token, create_at) +VALUES (-999, 'demo test', 'demo', -999, 0, 1, '2022-03-06 02:11:02'); + diff --git a/core/src/test/resources/sql/domain/discussion/DeleteById.sql b/core/src/test/resources/sql/domain/discussion/DeleteById.sql new file mode 100644 index 0000000..46a9b20 --- /dev/null +++ b/core/src/test/resources/sql/domain/discussion/DeleteById.sql @@ -0,0 +1,8 @@ +INSERT INTO databasir.`group` (id, name, description, deleted, update_at, create_at) +VALUES (-999, '会员平台 ut', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'); + +INSERT INTO databasir.project (id, name, description, group_id, deleted, deleted_token, create_at) +VALUES (-999, 'demo test', 'demo', -999, 0, 1, '2022-03-06 02:11:02'); + +INSERT INTO databasir.document_discussion(id, content, user_id, project_id, table_name, column_name) +VALUES (-999, 'demo test', -1, -999, 'ut', 'ut'); diff --git a/core/src/test/resources/sql/domain/group/GroupDelete.sql b/core/src/test/resources/sql/domain/group/GroupDelete.sql new file mode 100644 index 0000000..873537e --- /dev/null +++ b/core/src/test/resources/sql/domain/group/GroupDelete.sql @@ -0,0 +1,22 @@ +INSERT INTO databasir.`group` (id, name, description, deleted, update_at, create_at) +VALUES (-999, '会员平台 ut', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'); + +INSERT INTO databasir.user_role (user_id, role, group_id) +VALUES (1000, 'GROUP_OWNER', -999), + (1001, 'GROUP_OWNER', -999), + (1002, 'GROUP_OWNER', -999), + (1003, 'GROUP_OWNER', -999), + (1004, 'GROUP_MEMBER', -999); + +INSERT INTO databasir.project (id, name, description, group_id, deleted, deleted_token, create_at) +VALUES (-999, 'demo test', 'demo', -999, 0, 1, '2022-03-06 02:11:02'), + (-1000, 'demo test3', 'no member', -999, 0, 1, '2022-03-25 09:00:16'), + (-1001, 'demo test2', 'demo', -999, 0, 0, '2022-03-06 02:11:35'); + + +INSERT INTO databasir.project_sync_rule (project_id, ignore_table_name_regex_array, ignore_column_name_regex_array, + is_auto_sync, auto_sync_cron) +VALUES (-999, '[]', '[]', 1, '0 0/20 * * * ? '), + (-1000, '[]', '[]', 1, '0 0/20 * * * ? '), + (-1001, '[]', '[]', 1, '0 0/20 * * * ? '); + diff --git a/core/src/test/resources/sql/domain/group/GroupUpdate.sql b/core/src/test/resources/sql/domain/group/GroupUpdate.sql new file mode 100644 index 0000000..15f2d1e --- /dev/null +++ b/core/src/test/resources/sql/domain/group/GroupUpdate.sql @@ -0,0 +1,10 @@ +INSERT INTO databasir.`group` (id, name, description, deleted, update_at, create_at) +VALUES (-999, '会员平台 ut', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'), + (-1000, '商品中心 ut', '商品中心', 0, '2022-03-06 02:11:53', '2022-03-06 02:11:53'); + +INSERT INTO databasir.user_role (user_id, role, group_id) +VALUES (1000, 'GROUP_OWNER', -999), + (1001, 'GROUP_OWNER', -999), + (1002, 'GROUP_OWNER', -999), + (1003, 'GROUP_OWNER', -999), + (1004, 'GROUP_MEMBER', -999); diff --git a/core/src/test/resources/sql/domain/group/MemberAdd.sql b/core/src/test/resources/sql/domain/group/MemberAdd.sql new file mode 100644 index 0000000..28b0511 --- /dev/null +++ b/core/src/test/resources/sql/domain/group/MemberAdd.sql @@ -0,0 +1,7 @@ +INSERT INTO databasir.`group` (id, name, description, deleted, update_at, create_at) +VALUES (-999, '会员平台 ut', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'), + (-1000, '会员平台 ut2', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'); + +INSERT INTO databasir.user_role (user_id, role, group_id) +VALUES (-2, 'GROUP_MEMBER', -999), + (-2, 'GROUP_OWNER', -1000); diff --git a/core/src/test/resources/sql/domain/group/MemberRemove.sql b/core/src/test/resources/sql/domain/group/MemberRemove.sql new file mode 100644 index 0000000..cfacfb5 --- /dev/null +++ b/core/src/test/resources/sql/domain/group/MemberRemove.sql @@ -0,0 +1,9 @@ +INSERT INTO databasir.`group` (id, name, description, deleted, update_at, create_at) +VALUES (-999, '会员平台 ut', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'), + (-1000, '会员平台 ut2', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'); + +INSERT INTO databasir.user_role (user_id, role, group_id) +VALUES (-1, 'SYS_OWNER', NULL), + (-1, 'GROUP_OWNER', -999), + (-2, 'GROUP_MEMBER', -999), + (-2, 'GROUP_OWNER', -1000); diff --git a/core/src/test/resources/sql/domain/group/MemberRoleChange.sql b/core/src/test/resources/sql/domain/group/MemberRoleChange.sql new file mode 100644 index 0000000..79602ab --- /dev/null +++ b/core/src/test/resources/sql/domain/group/MemberRoleChange.sql @@ -0,0 +1,5 @@ +INSERT INTO databasir.`group` (id, name, description, deleted, update_at, create_at) +VALUES (-999, '会员平台 ut', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'); + +INSERT INTO databasir.user_role (user_id, role, group_id) +VALUES (-1, 'GROUP_OWNER', -999); diff --git a/core/src/test/resources/sql/domain/project/Delete.sql b/core/src/test/resources/sql/domain/project/Delete.sql new file mode 100644 index 0000000..6f3772f --- /dev/null +++ b/core/src/test/resources/sql/domain/project/Delete.sql @@ -0,0 +1,10 @@ +INSERT INTO databasir.`group` (id, name, description, deleted, update_at, create_at) +VALUES (-999, '会员平台 ut', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'); + +INSERT INTO databasir.project (id, name, description, group_id, deleted, deleted_token, create_at) +VALUES (-1000, 'demo test2', 'demo', -999, 0, 0, '2022-03-06 02:11:35'); + +INSERT INTO databasir.project_sync_rule (project_id, ignore_table_name_regex_array, ignore_column_name_regex_array, + is_auto_sync, auto_sync_cron) +VALUES (-1000, '[]', '[]', 1, '0 0/20 * * * ? '); + diff --git a/core/src/test/resources/sql/domain/project/Update.sql b/core/src/test/resources/sql/domain/project/Update.sql new file mode 100644 index 0000000..60307fd --- /dev/null +++ b/core/src/test/resources/sql/domain/project/Update.sql @@ -0,0 +1,15 @@ +INSERT INTO databasir.`group` (id, name, description, deleted, update_at, create_at) +VALUES (-999, '会员平台 ut', '会员平台', 0, '2022-03-06 02:10:40', '2022-03-06 02:10:40'); + +INSERT INTO databasir.project (id, name, description, group_id, deleted, deleted_token, create_at) +VALUES (-1000, 'demo test2', 'demo', -999, 0, 0, '2022-03-06 02:11:35'); + +INSERT INTO databasir.project_sync_rule (project_id, ignore_table_name_regex_array, ignore_column_name_regex_array, + is_auto_sync, auto_sync_cron) +VALUES (-1000, '[]', '[]', 0, '0 0/20 * * * ? '); + +INSERT INTO databasir.data_source(id, project_id, database_name, schema_name, database_type, url, username, password) +VALUES (-1000, -1000, 'databasir', 'databasir', 'mysql', 'localhost:3306', 'root', '123456'); + +INSERT INTO databasir.data_source_property(data_source_id, `key`, value) +VALUES (-1000, 'useSSL', 'true') \ No newline at end of file diff --git a/core/src/test/resources/sql/domain/user/AddFavorites.sql b/core/src/test/resources/sql/domain/user/AddFavorites.sql new file mode 100644 index 0000000..d970429 --- /dev/null +++ b/core/src/test/resources/sql/domain/user/AddFavorites.sql @@ -0,0 +1,2 @@ +INSERT IGNORE INTO databasir.project (id, NAME, DESCRIPTION, group_id, deleted, deleted_token, create_at) +VALUES (999, 'addFavoritesTest', 'demo', 1, 0, 1, '2022-03-06 02:11:02'); diff --git a/core/src/test/resources/sql/domain/user/AddSysOwnerTo.sql b/core/src/test/resources/sql/domain/user/AddSysOwnerTo.sql index a1bdd23..38fe5a2 100644 --- a/core/src/test/resources/sql/domain/user/AddSysOwnerTo.sql +++ b/core/src/test/resources/sql/domain/user/AddSysOwnerTo.sql @@ -1,4 +1,6 @@ INSERT IGNORE INTO databasir.user (id, email, username, password, nickname, avatar, enabled) -VALUES (1, 'N/A', 'databasir', '$2a$10$8Wjfdbbk76jDNbQ0zKq', 'Databasir Admin', NULL, 1), - (2, 'sysOwner@databasir.com', 'sysOwner', '$2a$10$wXPDzPceCXeU1PLXGKRAEW', 'a', NULL, 1), - (3, 'notSysOwner@databasir.com', 'notSysOwner', '$2a$10$wXPDzPceU1PLXGKRAEW', 'b', NULL, 1); +VALUES (-998, 'sysOwner@databasir.com', 'sysOwner', '$2a$10$wXPDzPceCXeU1PLXGKRAEW', 'a', NULL, 1), + (-999, 'notSysOwner@databasir.com', 'notSysOwner', '$2a$10$wXPDzPceU1PLXGKRAEW', 'b', NULL, 1); + +INSERT IGNORE INTO databasir.user_role (user_id, role, group_id, create_at) +VALUES (-998, 'SYS_OWNER', NULL, '2022-03-06 02:05:30'); diff --git a/core/src/test/resources/sql/domain/user/RemoveFavorites.sql b/core/src/test/resources/sql/domain/user/RemoveFavorites.sql new file mode 100644 index 0000000..3f3a86a --- /dev/null +++ b/core/src/test/resources/sql/domain/user/RemoveFavorites.sql @@ -0,0 +1,4 @@ +INSERT IGNORE INTO databasir.user_favorite_project (user_id, project_id, create_at) +VALUES (999, 999, '2022-03-06 10:25:47'), + (999, 1000, '2022-03-10 09:13:30'); + diff --git a/core/src/test/resources/sql/domain/user/RemoveSysOwnerFrom.sql b/core/src/test/resources/sql/domain/user/RemoveSysOwnerFrom.sql new file mode 100644 index 0000000..38fe5a2 --- /dev/null +++ b/core/src/test/resources/sql/domain/user/RemoveSysOwnerFrom.sql @@ -0,0 +1,6 @@ +INSERT IGNORE INTO databasir.user (id, email, username, password, nickname, avatar, enabled) +VALUES (-998, 'sysOwner@databasir.com', 'sysOwner', '$2a$10$wXPDzPceCXeU1PLXGKRAEW', 'a', NULL, 1), + (-999, 'notSysOwner@databasir.com', 'notSysOwner', '$2a$10$wXPDzPceU1PLXGKRAEW', 'b', NULL, 1); + +INSERT IGNORE INTO databasir.user_role (user_id, role, group_id, create_at) +VALUES (-998, 'SYS_OWNER', NULL, '2022-03-06 02:05:30'); diff --git a/core/src/test/resources/sql/domain/user/RenewPassword.sql b/core/src/test/resources/sql/domain/user/RenewPassword.sql index 6f57ef2..d03c43a 100644 --- a/core/src/test/resources/sql/domain/user/RenewPassword.sql +++ b/core/src/test/resources/sql/domain/user/RenewPassword.sql @@ -1,3 +1,3 @@ INSERT IGNORE INTO databasir.user (id, email, username, password, nickname, avatar, enabled) -VALUES (1, 'N/A', 'databasir', '$2a$10$8WjfdbbDpzvkz5Rc.8TBk76jDNbQ0zKq', 'Databasir Admin', NULL, 1), - (2, 'vran@databasir.com', 'vran', '$2a$10$wXPDzPceCpqYErlZ3DRh.gOpgXXeU1PLXGKRAEW', 'vranssss', NULL, 1); \ No newline at end of file +VALUES (-1, 'N/A', 'databasir', '$2a$10$8WjfdbbDpzvkz5Rc.8TBk76jDNbQ0zKq', 'Databasir Admin', NULL, 1), + (-2, 'vran@databasir.com', 'vran', '$2a$10$wXPDzPceCpqYErlZ3DRh.gOpgXXeU1PLXGKRAEW', 'vranssss', NULL, 1); \ No newline at end of file diff --git a/core/src/test/resources/sql/domain/user/SwitchEnableStatus.sql b/core/src/test/resources/sql/domain/user/SwitchEnableStatus.sql index 8819e64..f62eda4 100644 --- a/core/src/test/resources/sql/domain/user/SwitchEnableStatus.sql +++ b/core/src/test/resources/sql/domain/user/SwitchEnableStatus.sql @@ -1,7 +1,6 @@ INSERT IGNORE INTO databasir.user (id, email, username, password, nickname, avatar, enabled) -VALUES (1, 'N/A', 'databasir', '$2a$10$8WjfdbbDpzvkz5Rc.8TBk76jDNbQ0zKq', 'Databasir Admin', NULL, 1), - (2, 'vran@databasir.com', 'vran', '$2a$10$wXPDzPceCpqYErlZ3DRh.gOpgXXeU1PLXGKRAEW', 'vranssss', NULL, 1); +VALUES (-999, 'vran@databasir.com', 'vran', '$2a$10$wXPDzPceCpqYErlZ3DRh.gOpgXXeU1PLXGKRAEW', 'vranssss', NULL, 1); INSERT IGNORE INTO databasir.login (user_id, access_token, refresh_token, access_token_expire_at, refresh_token_expire_at) -VALUES (2, 'eyJ0eXAiOiJKV1QM', '1a884c14ef6542ce0261', '2022-03-12 20:24:28', '2022-03-27 20:09:29'); +VALUES (-999, 'eyJ0eXAiOiJKV1QM', '1a884c14ef6542ce0261', '2022-03-12 20:24:28', '2022-03-27 20:09:29'); diff --git a/core/src/test/resources/sql/domain/user/UpdateNickname.sql b/core/src/test/resources/sql/domain/user/UpdateNickname.sql new file mode 100644 index 0000000..76b45fd --- /dev/null +++ b/core/src/test/resources/sql/domain/user/UpdateNickname.sql @@ -0,0 +1,7 @@ +INSERT IGNORE INTO databasir.login (user_id, access_token, refresh_token, access_token_expire_at, + refresh_token_expire_at) +VALUES (-999, 'eyJ0eXAiOiJKV1QM', '1a884c14ef6542ce0261', '2022-03-12 20:24:28', '2022-03-27 20:09:29'); + +INSERT IGNORE INTO databasir.user (id, email, username, password, nickname, avatar, enabled) +VALUES (-999, 'demo2@databasir.com', 'demo2', '$2a$10$KTi5HBIBURn/5h9d7sp2DeIxK4T03vlh34dSwyJphjeNBRGWTzEDK', 'demo2', + NULL, 1); diff --git a/core/src/test/resources/sql/domain/user/UpdatePassword.sql b/core/src/test/resources/sql/domain/user/UpdatePassword.sql new file mode 100644 index 0000000..392b129 --- /dev/null +++ b/core/src/test/resources/sql/domain/user/UpdatePassword.sql @@ -0,0 +1,7 @@ +INSERT INTO databasir.login (user_id, access_token, refresh_token, access_token_expire_at, + refresh_token_expire_at) +VALUES (-999, 'eyJ0eXAiOiJKV1QM', '1a884c14ef6542ce0261', '2022-03-12 20:24:28', '2022-03-27 20:09:29'); + +INSERT INTO databasir.user (id, email, username, password, nickname, avatar, enabled, update_at, create_at) +VALUES (-999, 'demo2@databasir.com', 'demo2', '$2a$10$KTi5HBIBURn/5h9d7sp2DeIxK4T03vlh34dSwyJphjeNBRGWTzEDK', 'demo2', + NULL, 1, '2022-03-25 15:02:49', '2022-03-25 15:02:49'); diff --git a/core/src/test/resources/sql/event/subscriber/DocumentEventSubscriberTest.sql b/core/src/test/resources/sql/event/subscriber/DocumentEventSubscriberTest.sql index 4aa49f3..5ea45b9 100644 --- a/core/src/test/resources/sql/event/subscriber/DocumentEventSubscriberTest.sql +++ b/core/src/test/resources/sql/event/subscriber/DocumentEventSubscriberTest.sql @@ -1,22 +1,22 @@ -INSERT IGNORE INTO databasir.user (id, email, username, password, nickname, avatar, enabled) -VALUES (1, 'N/A', 'databasir', '$2a$10$8WjfdbbDpzvkz5Rc.8TBk76jDNbQ0zKq', 'Databasir Admin', NULL, 1), - (2, 'vran@databasir.com', 'vran', '$2a$10$wXPDzPceCpqYErlZ3DRh.gOpgXXeU1PLXGKRAEW', 'vranssss', NULL, 1), - (3, 'test@databasir.com', 'test', '$2a$10$XbnZlQB26cKKGB4WLlgZxxwbUr9tD1Amn/3Tf5H0i', 'test', NULL, 1), - (4, 'sample@databasir.com', 'sample', '$2a$10$Ua0zrJ7.HDb6ZIRNWb7fCJiG2OZRTN1.', 'sample', NULL, 1), - (5, 'demo@databasir.com', 'demo', '$2a$10$J4JT19JBO3LpWAvRARDxieFtm/pFQtna.dDq', 'demo', NULL, 0); +INSERT INTO databasir.user (id, email, username, password, nickname, avatar, enabled) +VALUES (-1, 'jack@databasir.com', 'jack', '$2a$10$8WjfdbbDpzvkz5Rc.8TBk76jDNbQ0zKq', 'Databasir Admin', NULL, 1), + (-2, 'vran@databasir.com', 'vran', '$2a$10$wXPDzPceCpqYErlZ3DRh.gOpgXXeU1PLXGKRAEW', 'vranssss', NULL, 1), + (-3, 'test@databasir.com', 'test', '$2a$10$XbnZlQB26cKKGB4WLlgZxxwbUr9tD1Amn/3Tf5H0i', 'test', NULL, 1), + (-4, 'sample@databasir.com', 'sample', '$2a$10$Ua0zrJ7.HDb6ZIRNWb7fCJiG2OZRTN1.', 'sample', NULL, 1), + (-5, 'demo@databasir.com', 'demo', '$2a$10$J4JT19JBO3LpWAvRARDxieFtm/pFQtna.dDq', 'demo', NULL, 0); -INSERT IGNORE INTO databasir.project (id, name, description, group_id, deleted, deleted_token) -VALUES (1, 'demo', 'demo', 1, 0, 1), - (2, 'no member', 'no member', 1, 0, 1); +INSERT INTO databasir.project (id, name, description, group_id, deleted, deleted_token) +VALUES (-1, 'demo', 'demo', 1, 0, 1), + (-2, 'no member', 'no member', 1, 0, 1); -INSERT IGNORE INTO databasir.`group` (id, name, description, deleted) -VALUES (1, '会员平台', '会员平台', 0), - (2, '成员为空', '会员平台', 0); +INSERT INTO databasir.`group` (id, name, description, deleted) +VALUES (-1, '会员平台', '会员平台', 0), + (-2, '成员为空', '会员平台', 0); -INSERT IGNORE INTO databasir.user_role (user_id, role, group_id) -VALUES (1, 'SYS_OWNER', NULL), - (1, 'GROUP_OWNER', 1), - (2, 'GROUP_OWNER', 1), - (3, 'GROUP_MEMBER', 1), - (4, 'GROUP_OWNER', 1), - (5, 'GROUP_OWNER', 1); +INSERT INTO databasir.user_role (user_id, role, group_id) +VALUES (-1, 'SYS_OWNER', NULL), + (-1, 'GROUP_OWNER', 1), + (-2, 'GROUP_OWNER', 1), + (-3, 'GROUP_MEMBER', 1), + (-4, 'GROUP_OWNER', 1), + (-5, 'GROUP_OWNER', 1); diff --git a/core/src/test/resources/sql/event/subscriber/UserEventSubscriberTest.sql b/core/src/test/resources/sql/event/subscriber/UserEventSubscriberTest.sql index d052db5..427a929 100644 --- a/core/src/test/resources/sql/event/subscriber/UserEventSubscriberTest.sql +++ b/core/src/test/resources/sql/event/subscriber/UserEventSubscriberTest.sql @@ -1,7 +1,3 @@ -INSERT IGNORE INTO databasir.user (id, email, username, password, nickname, avatar, enabled) -VALUES (1, 'N/A', 'databasir', '$2a$10$8WjfdbbDpzvkz5Rc.8TBk76jDNbQ0zKq', 'Databasir Admin', NULL, 1), - (2, 'vran@databasir.com', 'vran', '$2a$10$wXPDzPceCpqYErlZ3DRh.gOpgXXeU1PLXGKRAEW', 'vranssss', NULL, 1), - (3, 'test@databasir.com', 'test', '$2a$10$XbnZlQB26cKKGB4WLlgZxxwbUr9tD1Amn/3Tf5H0i', 'test', NULL, 1), - (4, 'sample@databasir.com', 'sample', '$2a$10$Ua0zrJ7.HDb6ZIRNWb7fCJiG2OZRTN1.', 'sample', NULL, 1), - (5, 'demo@databasir.com', 'demo', '$2a$10$J4JT19JBO3LpWAvRARDxieFtm/pFQtna.dDq', 'demo', NULL, 0); +INSERT INTO databasir.user (id, email, username, password, nickname, avatar, enabled) +VALUES (-999, 'vran@databasir.com', 'vran', '$2a$10$wXPDzPceCpqYErlZ3DRh.gOpgXXeU1PLXGKRAEW', 'vranssss', NULL, 1); diff --git a/dao/src/main/java/com/databasir/dao/impl/BaseDao.java b/dao/src/main/java/com/databasir/dao/impl/BaseDao.java index dbcb8fe..1780078 100644 --- a/dao/src/main/java/com/databasir/dao/impl/BaseDao.java +++ b/dao/src/main/java/com/databasir/dao/impl/BaseDao.java @@ -86,6 +86,12 @@ public abstract class BaseDao { .fetchOptionalInto(pojoType); } + public List selectList(Condition condition) { + return getDslContext() + .select(table.fields()).from(table).where(condition) + .fetchInto(pojoType); + } + public R selectOne(Condition condition) { return selectOptionalOne(condition) .orElseThrow(() -> new DataNotExistsException("data not exists in "