Browse Source

1,modify DateUtils,add getCurrentDate method 2,AccessTokenMapperTest UT modify (#1378)

* 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
pull/2/head
qiaozhanwei 5 years ago committed by lgcareer
parent
commit
5947fd526e
  1. 67
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java
  2. 204
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AccessTokenMapperTest.java

67
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java

@ -20,6 +20,7 @@ import org.apache.dolphinscheduler.common.Constants;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
@ -35,20 +36,20 @@ public class DateUtils {
private static final Logger logger = LoggerFactory.getLogger(DateUtils.class); private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);
/** /**
* <code>java.util.Date</code> to <code>java.time.LocalDateTime</code> * date to local datetime
* use default zone *
* @param date * @param date date
* @return * @return local datetime
*/ */
private static LocalDateTime date2LocalDateTime(Date date) { private static LocalDateTime date2LocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
} }
/** /**
* <code>java.time.LocalDateTime</code> to <code>java.util.Date</code> * local datetime to date
* use default zone *
* @param localDateTime * @param localDateTime local datetime
* @return * @return date
*/ */
private static Date localDateTime2Date(LocalDateTime localDateTime) { private static Date localDateTime2Date(LocalDateTime localDateTime) {
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant(); Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
@ -56,43 +57,51 @@ public class DateUtils {
} }
/** /**
* @return get the formatted date string for the current time * get current date str
*
* @return date string
*/ */
public static String getCurrentTime() { public static String getCurrentTime() {
return getCurrentTime(Constants.YYYY_MM_DD_HH_MM_SS); return getCurrentTime(Constants.YYYY_MM_DD_HH_MM_SS);
} }
/** /**
* @param format format * get the date string in the specified format of the current time
* @return get the date string in the specified format of the current time *
* @param format date format
* @return date string
*/ */
public static String getCurrentTime(String format) { public static String getCurrentTime(String format) {
// return new SimpleDateFormat(format).format(new Date());
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format)); return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
} }
/** /**
* get the formatted date string
*
* @param date date * @param date date
* @param format e.g. yyyy-MM-dd HH:mm:ss * @param format e.g. yyyy-MM-dd HH:mm:ss
* @return get the formatted date string * @return date string
*/ */
public static String format(Date date, String format) { public static String format(Date date, String format) {
// return new SimpleDateFormat(format).format(date);
return format(date2LocalDateTime(date), format); return format(date2LocalDateTime(date), format);
} }
/** /**
* get the formatted date string
*
* @param localDateTime local data time * @param localDateTime local data time
* @param format e.g. yyyy-MM-dd HH:mm:ss * @param format yyyy-MM-dd HH:mm:ss
* @return get the formatted date string * @return date string
*/ */
public static String format(LocalDateTime localDateTime, String format) { public static String format(LocalDateTime localDateTime, String format) {
return localDateTime.format(DateTimeFormatter.ofPattern(format)); return localDateTime.format(DateTimeFormatter.ofPattern(format));
} }
/** /**
* convert time to yyyy-MM-dd HH:mm:ss format
*
* @param date date * @param date date
* @return convert time to yyyy-MM-dd HH:mm:ss format * @return date string
*/ */
public static String dateToString(Date date) { public static String dateToString(Date date) {
return format(date, Constants.YYYY_MM_DD_HH_MM_SS); return format(date, Constants.YYYY_MM_DD_HH_MM_SS);
@ -100,13 +109,14 @@ public class DateUtils {
/** /**
* convert string to date and time
*
* @param date date * @param date date
* @param format format * @param format format
* @return convert string to date and time * @return date
*/ */
public static Date parse(String date, String format) { public static Date parse(String date, String format) {
try { try {
// return new SimpleDateFormat(format).parse(date);
LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format)); LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format));
return localDateTime2Date(ldt); return localDateTime2Date(ldt);
} catch (Exception e) { } catch (Exception e) {
@ -115,6 +125,7 @@ public class DateUtils {
return null; return null;
} }
/** /**
* convert date str to yyyy-MM-dd HH:mm:ss format * convert date str to yyyy-MM-dd HH:mm:ss format
* *
@ -228,8 +239,8 @@ public class DateUtils {
/** /**
* get monday * get monday
* <p> *
* note: Set the first day of the week to Monday, the default is Sunday</p> * note: Set the first day of the week to Monday, the default is Sunday
* @param date date * @param date date
* @return get monday * @return get monday
*/ */
@ -246,7 +257,7 @@ public class DateUtils {
/** /**
* get sunday * get sunday
* <p> *
* note: Set the first day of the week to Monday, the default is Sunday * note: Set the first day of the week to Monday, the default is Sunday
* @param date date * @param date date
* @return get sunday * @return get sunday
@ -263,6 +274,7 @@ public class DateUtils {
/** /**
* get first day of month * get first day of month
*
* @param date date * @param date date
* @return first day of month * @return first day of month
* */ * */
@ -277,6 +289,7 @@ public class DateUtils {
/** /**
* get some hour of day * get some hour of day
*
* @param date date * @param date date
* @param hours hours * @param hours hours
* @return some hour of day * @return some hour of day
@ -295,6 +308,7 @@ public class DateUtils {
/** /**
* get last day of month * get last day of month
*
* @param date date * @param date date
* @return get last day of month * @return get last day of month
*/ */
@ -372,5 +386,12 @@ public class DateUtils {
return cal.getTime(); return cal.getTime();
} }
/**
* get current date
* @return current date
*/
public static Date getCurrentDate() {
return DateUtils.parse(DateUtils.getCurrentTime(),
Constants.YYYY_MM_DD_HH_MM_SS);
}
} }

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

@ -16,95 +16,211 @@
*/ */
package org.apache.dolphinscheduler.dao.mapper; package org.apache.dolphinscheduler.dao.mapper;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.dao.entity.AccessToken; import org.apache.dolphinscheduler.dao.entity.AccessToken;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dolphinscheduler.dao.entity.User;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
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 org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Date; import java.text.SimpleDateFormat;
import java.util.List; import java.util.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* AccessToken mapper test
*/
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@Transactional
@Rollback(true)
public class AccessTokenMapperTest { public class AccessTokenMapperTest {
@Autowired
@Resource
AccessTokenMapper accessTokenMapper; AccessTokenMapper accessTokenMapper;
@Autowired
UserMapper userMapper;
/** /**
* insert * test insert
* @return AccessToken * @throws Exception
*/ */
private AccessToken insertOne(){ @Test
//insertOne public void testInsert() throws Exception{
AccessToken accessToken = new AccessToken(); Integer userId = 1;
accessToken.setUserId(4);
accessToken.setToken("hello, access token"); AccessToken accessToken = createAccessToken(userId);
accessToken.setCreateTime(new Date()); assertNotNull(accessToken.getId());
accessToken.setUpdateTime(new Date()); assertThat(accessToken.getId(), greaterThan(0));
accessToken.setExpireTime(new Date());
accessTokenMapper.insert(accessToken);
return accessToken;
} }
/** /**
* test update * test select by id
* @throws Exception
*/ */
@Test @Test
public void testUpdate(){ public void testSelectById() throws Exception{
//insertOne Integer userId = 1;
AccessToken accessToken = insertOne();
//update AccessToken accessToken = createAccessToken(userId);
accessToken.setToken("hello, token");
int update = accessTokenMapper.updateById(accessToken); AccessToken resultAccessToken
accessTokenMapper.deleteById(accessToken.getId()); = accessTokenMapper.selectById(accessToken.getId());
Assert.assertEquals(update, 1);
assertEquals(accessToken, resultAccessToken);
} }
/** /**
* test delete * test page
*/ */
@Test @Test
public void testDelete(){ public void testSelectAccessTokenPage() throws Exception{
Integer count = 4;
String userName = "zhangsan";
Integer offset = 2;
Integer size = 2;
Map<Integer, AccessToken> accessTokenMap = createAccessTokens(count, userName);
Page page = new Page(offset, size);
IPage<AccessToken> accessTokenPage = accessTokenMapper.selectAccessTokenPage(page, userName, 0);
AccessToken accessToken = insertOne(); assertEquals(Integer.valueOf(accessTokenPage.getRecords().size()),size);
int delete = accessTokenMapper.deleteById(accessToken.getId());
Assert.assertEquals(delete, 1);
for (AccessToken accessToken : accessTokenPage.getRecords()){
AccessToken resultAccessToken = accessTokenMap.get(accessToken.getId());
assertEquals(accessToken,resultAccessToken);
}
} }
/** /**
* test query * test update
*/ */
@Test @Test
public void testQuery(){ public void testUpdate() throws Exception{
Integer userId = 1;
AccessToken accessToken = insertOne(); AccessToken accessToken = createAccessToken(userId);
//query //update
List<AccessToken> token = accessTokenMapper.selectList(null); accessToken.setToken("56789");
Assert.assertNotEquals(token.size(), 0); accessToken.setExpireTime(DateUtils.getCurrentDate());
accessTokenMapper.deleteById(accessToken.getId()); accessToken.setUpdateTime(DateUtils.getCurrentDate());
accessTokenMapper.updateById(accessToken);
AccessToken resultAccessToken = accessTokenMapper.selectById(accessToken.getId());
assertEquals(accessToken, resultAccessToken);
} }
/** /**
* test page * test delete
*/ */
@Test @Test
public void testSelectAccessTokenPage() { public void testDelete() throws Exception{
AccessToken accessToken = insertOne(); Integer userId = 1;
Page page = new Page(1, 3);
String userName = ""; AccessToken accessToken = createAccessToken(userId);
IPage<AccessToken> accessTokenPage = accessTokenMapper.selectAccessTokenPage(page, userName, 4);
Assert.assertNotEquals(accessTokenPage.getTotal(), 0);
accessTokenMapper.deleteById(accessToken.getId()); accessTokenMapper.deleteById(accessToken.getId());
AccessToken resultAccessToken =
accessTokenMapper.selectById(accessToken.getId());
assertNull(resultAccessToken);
}
/**
* create accessTokens
* @param count
* @param userName
* @return
* @throws Exception
*/
private Map<Integer,AccessToken> createAccessTokens(
Integer count,String userName) throws Exception{
User user = createUser(userName);
Map<Integer,AccessToken> accessTokenMap = new HashMap<>();
for (int i = 1 ; i<= count ; i++){
AccessToken accessToken = createAccessToken(user.getId(),userName);
accessTokenMap.put(accessToken.getId(),accessToken);
}
return accessTokenMap;
}
/**
* create user
* @param userName
* @return
* @throws Exception
*/
private User createUser(String userName) throws Exception{
User user = new User();
user.setUserName(userName);
user.setUserPassword("123");
user.setUserType(UserType.GENERAL_USER);
user.setEmail("test@qq.com");
user.setPhone("13102557272");
user.setTenantId(1);
user.setCreateTime(DateUtils.getCurrentDate());
user.setUpdateTime(DateUtils.getCurrentDate());
user.setQueue("default");
userMapper.insert(user);
return user;
} }
/**
* create access token
*
* @return AccessToken
*/
private AccessToken createAccessToken(Integer userId,String userName)throws Exception{
Random random = new Random();
//insertOne
AccessToken accessToken = new AccessToken();
accessToken.setUserName(userName);
accessToken.setUserId(userId);
accessToken.setToken(String.valueOf(random.nextLong()));
accessToken.setCreateTime(DateUtils.getCurrentDate());
accessToken.setUpdateTime(DateUtils.getCurrentDate());
accessToken.setExpireTime(DateUtils.getCurrentDate());
accessTokenMapper.insert(accessToken);
return accessToken;
}
/**
* create access token
*
* @return AccessToken
*/
private AccessToken createAccessToken(Integer userId)throws Exception{
return createAccessToken(userId,null);
}
} }
Loading…
Cancel
Save