Browse Source

AlertGroupMapperTest UT modify (#1385)

* ConnectionFactory add paging intercepter

* remove spotbugs-annotations.jar LGPL protocol

* test

* catch exception transaction method does not take effect to modify

* .evn rollback

* DataSourceService rollback

* LogViewServiceGrpc rollback

* dockerfile add application-api.properties and change application.properties to application-dao.properties

* server startup error modify

* data type convert error ,email send error bug fix

* Merge remote-tracking branch 'remotes/upstream/dev-db' into dev-db

# Conflicts:
#	dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/CombinedApplicationServer.java

* revert sql.vue

* revert CommandType

* revert CommandType

* revert

* 1,modify DateUtils,add getCurrentDate method
2,AccessTokenMapperTest UT modify

* AlertGroupMapperTest UT modify
pull/2/head
qiaozhanwei 5 years ago committed by lgcareer
parent
commit
bf1fec74de
  1. 38
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/AlertGroup.java
  2. 26
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AccessTokenMapperTest.java
  3. 316
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapperTest.java

38
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/AlertGroup.java

@ -107,4 +107,42 @@ public class AlertGroup {
public void setDescription(String description) { public void setDescription(String description) {
this.description = description; this.description = description;
} }
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AlertGroup that = (AlertGroup) o;
if (id != that.id) {
return false;
}
if (groupName != null ? !groupName.equals(that.groupName) : that.groupName != null) {
return false;
}
if (groupType != that.groupType) {
return false;
}
if (description != null ? !description.equals(that.description) : that.description != null) {
return false;
}
return !(createTime != null ? !createTime.equals(that.createTime) : that.createTime != null) && !(updateTime != null ? !updateTime.equals(that.updateTime) : that.updateTime != null);
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (groupName != null ? groupName.hashCode() : 0);
result = 31 * result + (groupType != null ? groupType.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (createTime != null ? createTime.hashCode() : 0);
result = 31 * result + (updateTime != null ? updateTime.hashCode() : 0);
return result;
}
} }

26
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AccessTokenMapperTest.java

@ -151,9 +151,9 @@ public class AccessTokenMapperTest {
/** /**
* create accessTokens * create accessTokens
* @param count * @param count create accessToken count
* @param userName * @param userName username
* @return * @return accessToken map
* @throws Exception * @throws Exception
*/ */
private Map<Integer,AccessToken> createAccessTokens( private Map<Integer,AccessToken> createAccessTokens(
@ -167,13 +167,14 @@ public class AccessTokenMapperTest {
accessTokenMap.put(accessToken.getId(),accessToken); accessTokenMap.put(accessToken.getId(),accessToken);
} }
return accessTokenMap; return accessTokenMap;
} }
/** /**
* create user * create user
* @param userName * @param userName userName
* @return * @return user
* @throws Exception * @throws Exception
*/ */
private User createUser(String userName) throws Exception{ private User createUser(String userName) throws Exception{
@ -194,9 +195,11 @@ public class AccessTokenMapperTest {
} }
/** /**
* create access token * create access token
* * @param userId userId
* @return AccessToken * @param userName userName
* @return accessToken
* @throws Exception
*/ */
private AccessToken createAccessToken(Integer userId,String userName)throws Exception{ private AccessToken createAccessToken(Integer userId,String userName)throws Exception{
Random random = new Random(); Random random = new Random();
@ -215,9 +218,10 @@ public class AccessTokenMapperTest {
} }
/** /**
* create access token * create access token
* * @param userId userId
* @return AccessToken * @return accessToken
* @throws Exception
*/ */
private AccessToken createAccessToken(Integer userId)throws Exception{ private AccessToken createAccessToken(Integer userId)throws Exception{
return createAccessToken(userId,null); return createAccessToken(userId,null);

316
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapperTest.java

@ -18,6 +18,8 @@ package org.apache.dolphinscheduler.dao.mapper;
import org.apache.dolphinscheduler.common.enums.AlertType; import org.apache.dolphinscheduler.common.enums.AlertType;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.dao.entity.AccessToken;
import org.apache.dolphinscheduler.dao.entity.AlertGroup; import org.apache.dolphinscheduler.dao.entity.AlertGroup;
import org.apache.dolphinscheduler.dao.entity.UserAlertGroup; import org.apache.dolphinscheduler.dao.entity.UserAlertGroup;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
@ -27,13 +29,28 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.springframework.transaction.annotation.Transactional;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* AlertGroup mapper test
*/
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@Transactional
@Rollback(true)
public class AlertGroupMapperTest { public class AlertGroupMapperTest {
@ -43,84 +60,117 @@ public class AlertGroupMapperTest {
@Autowired @Autowired
UserAlertGroupMapper userAlertGroupMapper; UserAlertGroupMapper userAlertGroupMapper;
/** /**
* insert * test insert
* @return AlertGroup
*/ */
private AlertGroup insertOne(){ @Test
//insertOne public void testInsert(){
AlertGroup alertGroup = new AlertGroup(); AlertGroup alertGroup = createAlertGroup();
alertGroup.setGroupName("alert group 1"); assertNotNull(alertGroup);
alertGroup.setDescription("alert test1"); assertThat(alertGroup.getId(),greaterThan(0));
alertGroup.setGroupType(AlertType.EMAIL);
alertGroup.setCreateTime(new Date());
alertGroup.setUpdateTime(new Date());
alertGroupMapper.insert(alertGroup);
return alertGroup;
} }
/** /**
* test update * test selectById
*/ */
@Test @Test
public void testUpdate(){ public void testSelectById() {
//insertOne AlertGroup alertGroup = createAlertGroup();
AlertGroup alertGroup = insertOne(); //query
//update AlertGroup targetAlert = alertGroupMapper.selectById(alertGroup.getId());
alertGroup.setDescription("hello, ag");
int update = alertGroupMapper.updateById(alertGroup); assertEquals(alertGroup, targetAlert);
Assert.assertEquals(update, 1);
alertGroupMapper.deleteById(alertGroup.getId());
} }
/** /**
* test delete * test page
*/ */
@Test @Test
public void testDelete(){ public void testQueryAlertGroupPage() {
String groupName = "testGroup";
Integer count = 4;
Integer offset = 2;
Integer size = 2;
Map<Integer, AlertGroup> alertGroupMap = createAlertGroups(count,groupName);
Page page = new Page(offset, size);
IPage<AlertGroup> alertGroupIPage = alertGroupMapper.queryAlertGroupPage(page, groupName);
List<AlertGroup> alertGroupList = alertGroupIPage.getRecords();
assertEquals(alertGroupList.size(), size.intValue());
for (AlertGroup alertGroup : alertGroupList){
AlertGroup resultAlertGroup = alertGroupMap.get(alertGroup.getId());
if (resultAlertGroup != null){
assertEquals(alertGroup,resultAlertGroup);
}
}
AlertGroup alertGroup = insertOne();
int delete = alertGroupMapper.deleteById(alertGroup.getId());
Assert.assertEquals(delete, 1);
} }
/** /**
* test query * test update
*/ */
@Test @Test
public void testQuery() { public void testUpdate(){
AlertGroup alertGroup = insertOne();
//query AlertGroup alertGroup = createAlertGroup();
List<AlertGroup> alertGroups = alertGroupMapper.selectList(null); alertGroup.setGroupName("modify GroupName");
Assert.assertNotEquals(alertGroups.size(), 0); alertGroup.setGroupType(AlertType.SMS);
alertGroupMapper.deleteById(alertGroup.getId()); alertGroup.setDescription("modify GroupName");
alertGroup.setUpdateTime(DateUtils.getCurrentDate());
alertGroupMapper.updateById(alertGroup);
AlertGroup resultAlertGroup = alertGroupMapper.selectById(alertGroup.getId());
assertEquals(alertGroup,resultAlertGroup);
} }
/** /**
* test page * test delete
*/ */
@Test @Test
public void testQueryAlertGroupPage() { public void testDelete(){
AlertGroup alertGroup = insertOne();
Page page = new Page(1, 3); AlertGroup alertGroup = createAlertGroup();
IPage<AlertGroup> accessTokenPage = alertGroupMapper.queryAlertGroupPage(page,
"alert" ); alertGroupMapper.deleteById(alertGroup.getId());
Assert.assertNotEquals(accessTokenPage.getTotal(), 0);
alertGroupMapper.deleteById(alertGroup.getId()); AlertGroup resultAlertGroup = alertGroupMapper.selectById(alertGroup.getId());
assertNull(resultAlertGroup);
} }
/** /**
* test query by groupname * test query by groupname
*/ */
@Test @Test
public void testQueryByGroupName() { public void testQueryByGroupName() {
Integer count = 4;
String groupName = "testGroup";
AlertGroup alertGroup = insertOne(); Map<Integer, AlertGroup> alertGroupMap = createAlertGroups(count, groupName);
List<AlertGroup> alertGroups = alertGroupMapper.queryByGroupName("alert group 1");
Assert.assertNotEquals(alertGroups.size(), 0); List<AlertGroup> alertGroupList = alertGroupMapper.queryByGroupName("testGroup");
alertGroupMapper.deleteById(alertGroup.getId());
compareAlertGroups(alertGroupMap, alertGroupList);
} }
/** /**
@ -128,15 +178,17 @@ public class AlertGroupMapperTest {
*/ */
@Test @Test
public void testQueryByUserId() { public void testQueryByUserId() {
AlertGroup alertGroup = insertOne(); Integer count = 4;
UserAlertGroup userAlertGroup = new UserAlertGroup(); Integer userId = 1;
userAlertGroup.setAlertgroupId(alertGroup.getId());
userAlertGroup.setUserId(4); Map<Integer, AlertGroup> alertGroupMap =
userAlertGroupMapper.insert(userAlertGroup); createAlertGroups(count, userId);
List<AlertGroup> alertGroups = alertGroupMapper.queryByUserId(4);
Assert.assertNotEquals(alertGroups.size(), 0); List<AlertGroup> alertGroupList =
alertGroupMapper.deleteById(alertGroup.getId()); alertGroupMapper.queryByUserId(userId);
userAlertGroupMapper.deleteById(userAlertGroup.getId());
compareAlertGroups(alertGroupMap,alertGroupList);
} }
/** /**
@ -144,10 +196,13 @@ public class AlertGroupMapperTest {
*/ */
@Test @Test
public void testQueryByAlertType() { public void testQueryByAlertType() {
AlertGroup alertGroup = insertOne(); Integer count = 4;
List<AlertGroup> alertGroups = alertGroupMapper.queryByAlertType(AlertType.EMAIL);
Assert.assertNotEquals(alertGroups.size(), 0); Map<Integer, AlertGroup> alertGroupMap = createAlertGroups(count);
alertGroupMapper.deleteById(alertGroup.getId()); List<AlertGroup> alertGroupList = alertGroupMapper.queryByAlertType(AlertType.EMAIL);
compareAlertGroups(alertGroupMap,alertGroupList);
} }
/** /**
@ -155,9 +210,146 @@ public class AlertGroupMapperTest {
*/ */
@Test @Test
public void testQueryAllGroupList() { public void testQueryAllGroupList() {
AlertGroup alertGroup = insertOne(); Integer count = 4;
List<AlertGroup> alertGroups = alertGroupMapper.queryAllGroupList(); Map<Integer, AlertGroup> alertGroupMap = createAlertGroups(count);
Assert.assertNotEquals(alertGroups.size(), 0);
alertGroupMapper.deleteById(alertGroup.getId()); List<AlertGroup> alertGroupList = alertGroupMapper.queryAllGroupList();
compareAlertGroups(alertGroupMap,alertGroupList);
}
/**
* compare AlertGruops
* @param alertGroupMap alertGroupMap
* @param alertGroupList alertGroupList
*/
private void compareAlertGroups(Map<Integer,AlertGroup> alertGroupMap,
List<AlertGroup> alertGroupList){
// greaterThanOrEqualTo,beacuse maybe db have already exists
assertThat(alertGroupList.size(),greaterThanOrEqualTo(alertGroupMap.size()));
for (AlertGroup alertGroup : alertGroupList){
AlertGroup resultAlertGroup = alertGroupMap.get(alertGroup.getId());
if (resultAlertGroup != null){
assertEquals(alertGroup,resultAlertGroup);
}
}
}
/**
* insert
* @return AlertGroup
*/
private AlertGroup createAlertGroup(String groupName){
AlertGroup alertGroup = new AlertGroup();
alertGroup.setGroupName(groupName);
alertGroup.setDescription("alert group 1");
alertGroup.setGroupType(AlertType.EMAIL);
alertGroup.setCreateTime(DateUtils.getCurrentDate());
alertGroup.setUpdateTime(DateUtils.getCurrentDate());
alertGroupMapper.insert(alertGroup);
return alertGroup;
} }
/**
* insert
* @return AlertGroup
*/
private AlertGroup createAlertGroup(){
AlertGroup alertGroup = new AlertGroup();
alertGroup.setGroupName("testGroup");
alertGroup.setDescription("testGroup");
alertGroup.setGroupType(AlertType.EMAIL);
alertGroup.setCreateTime(DateUtils.getCurrentDate());
alertGroup.setUpdateTime(DateUtils.getCurrentDate());
alertGroupMapper.insert(alertGroup);
return alertGroup;
}
/**
* create AlertGroups
* @param count create AlertGroup count
* @param groupName groupName
* @return AlertGroup map
*/
private Map<Integer,AlertGroup> createAlertGroups(
Integer count,String groupName){
Map<Integer,AlertGroup> alertGroupMap = new HashMap<>();
AlertGroup alertGroup = null;
for (int i = 0 ; i < count; i++){
alertGroup = createAlertGroup(groupName);
alertGroupMap.put(alertGroup.getId(),alertGroup);
}
return alertGroupMap;
}
/**
* create AlertGroups
* @param count create AlertGroup count
* @return AlertGroup map
*/
private Map<Integer,AlertGroup> createAlertGroups(
Integer count){
Map<Integer,AlertGroup> alertGroupMap = new HashMap<>();
AlertGroup alertGroup = null;
for (int i = 0 ; i < count; i++){
alertGroup = createAlertGroup();
alertGroupMap.put(alertGroup.getId(),alertGroup);
}
return alertGroupMap;
}
/**
* create AlertGroups
* @param count create AlertGroup count
* @return AlertGroup map
*/
private Map<Integer,AlertGroup> createAlertGroups(
Integer count,Integer userId){
Map<Integer,AlertGroup> alertGroupMap = new HashMap<>();
AlertGroup alertGroup = null;
for (int i = 0 ; i < count; i++){
alertGroup = createAlertGroup();
createUserAlertGroup(userId,alertGroup.getId());
alertGroupMap.put(alertGroup.getId(),alertGroup);
}
return alertGroupMap;
}
/**
* create AlertGroup
* @param userId userId
* @param alertgroupId alertgroupId
* @return UserAlertGroup
*/
private UserAlertGroup createUserAlertGroup(
Integer userId,Integer alertgroupId){
UserAlertGroup userAlertGroup = new UserAlertGroup();
userAlertGroup.setAlertgroupId(alertgroupId);
userAlertGroup.setUserId(userId);
userAlertGroup.setCreateTime(DateUtils.getCurrentDate());
userAlertGroup.setUpdateTime(DateUtils.getCurrentDate());
userAlertGroupMapper.insert(userAlertGroup);
return userAlertGroup;
}
} }
Loading…
Cancel
Save