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.LoggerFactory;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
@ -35,20 +36,20 @@ public class DateUtils {
private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);
/**
* <code>java.util.Date</code> to <code>java.time.LocalDateTime</code>
* use default zone
* @param date
* @return
* date to local datetime
*
* @param date date
* @return local datetime
*/
private static LocalDateTime date2LocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
/**
* <code>java.time.LocalDateTime</code> to <code>java.util.Date</code>
* use default zone
* @param localDateTime
* @return
* local datetime to date
*
* @param localDateTime local datetime
* @return date
*/
private static Date localDateTime2Date(LocalDateTime localDateTime) {
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() {
return getCurrentTime(Constants.YYYY_MM_DD_HH_MM_SS);
}
/**
* @param format format
* @return get the date string in the specified format of the current time
* 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) {
// return new SimpleDateFormat(format).format(new Date());
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
}
/**
* get the formatted date string
*
* @param date date
* @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) {
// return new SimpleDateFormat(format).format(date);
return format(date2LocalDateTime(date), format);
}
/**
* get the formatted date string
*
* @param localDateTime local data time
* @param format e.g. yyyy-MM-dd HH:mm:ss
* @return get the formatted date string
* @param format yyyy-MM-dd HH:mm:ss
* @return date string
*/
public static String format(LocalDateTime localDateTime, String format) {
return localDateTime.format(DateTimeFormatter.ofPattern(format));
}
/**
* convert time to yyyy-MM-dd HH:mm:ss format
*
* @param date date
* @return convert time to yyyy-MM-dd HH:mm:ss format
* @return date string
*/
public static String dateToString(Date date) {
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 format format
* @return convert string to date and time
* @return date
*/
public static Date parse(String date, String format) {
try {
// return new SimpleDateFormat(format).parse(date);
LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format));
return localDateTime2Date(ldt);
} catch (Exception e) {
@ -115,6 +125,7 @@ public class DateUtils {
return null;
}
/**
* convert date str to yyyy-MM-dd HH:mm:ss format
*
@ -228,8 +239,8 @@ public class DateUtils {
/**
* 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
* @return get monday
*/
@ -246,7 +257,7 @@ public class DateUtils {
/**
* get sunday
* <p>
*
* note: Set the first day of the week to Monday, the default is Sunday
* @param date date
* @return get sunday
@ -263,6 +274,7 @@ public class DateUtils {
/**
* get first day of month
*
* @param date date
* @return first day of month
* */
@ -277,6 +289,7 @@ public class DateUtils {
/**
* get some hour of day
*
* @param date date
* @param hours hours
* @return some hour of day
@ -295,6 +308,7 @@ public class DateUtils {
/**
* get last day of month
*
* @param date date
* @return get last day of month
*/
@ -372,5 +386,12 @@ public class DateUtils {
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;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.dao.entity.AccessToken;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dolphinscheduler.dao.entity.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
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)
@SpringBootTest
@Transactional
@Rollback(true)
public class AccessTokenMapperTest {
@Resource
@Autowired
AccessTokenMapper accessTokenMapper;
@Autowired
UserMapper userMapper;
/**
* insert
* @return AccessToken
* test insert
* @throws Exception
*/
private AccessToken insertOne(){
//insertOne
AccessToken accessToken = new AccessToken();
accessToken.setUserId(4);
accessToken.setToken("hello, access token");
accessToken.setCreateTime(new Date());
accessToken.setUpdateTime(new Date());
accessToken.setExpireTime(new Date());
accessTokenMapper.insert(accessToken);
return accessToken;
@Test
public void testInsert() throws Exception{
Integer userId = 1;
AccessToken accessToken = createAccessToken(userId);
assertNotNull(accessToken.getId());
assertThat(accessToken.getId(), greaterThan(0));
}
/**
* test update
* test select by id
* @throws Exception
*/
@Test
public void testUpdate(){
//insertOne
AccessToken accessToken = insertOne();
//update
accessToken.setToken("hello, token");
int update = accessTokenMapper.updateById(accessToken);
accessTokenMapper.deleteById(accessToken.getId());
Assert.assertEquals(update, 1);
public void testSelectById() throws Exception{
Integer userId = 1;
AccessToken accessToken = createAccessToken(userId);
AccessToken resultAccessToken
= accessTokenMapper.selectById(accessToken.getId());
assertEquals(accessToken, resultAccessToken);
}
/**
* test delete
* test page
*/
@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();
int delete = accessTokenMapper.deleteById(accessToken.getId());
Assert.assertEquals(delete, 1);
assertEquals(Integer.valueOf(accessTokenPage.getRecords().size()),size);
for (AccessToken accessToken : accessTokenPage.getRecords()){
AccessToken resultAccessToken = accessTokenMap.get(accessToken.getId());
assertEquals(accessToken,resultAccessToken);
}
}
/**
* test query
* test update
*/
@Test
public void testQuery(){
public void testUpdate() throws Exception{
Integer userId = 1;
AccessToken accessToken = insertOne();
//query
List<AccessToken> token = accessTokenMapper.selectList(null);
Assert.assertNotEquals(token.size(), 0);
accessTokenMapper.deleteById(accessToken.getId());
AccessToken accessToken = createAccessToken(userId);
//update
accessToken.setToken("56789");
accessToken.setExpireTime(DateUtils.getCurrentDate());
accessToken.setUpdateTime(DateUtils.getCurrentDate());
accessTokenMapper.updateById(accessToken);
AccessToken resultAccessToken = accessTokenMapper.selectById(accessToken.getId());
assertEquals(accessToken, resultAccessToken);
}
/**
* test page
* test delete
*/
@Test
public void testSelectAccessTokenPage() {
AccessToken accessToken = insertOne();
Page page = new Page(1, 3);
String userName = "";
IPage<AccessToken> accessTokenPage = accessTokenMapper.selectAccessTokenPage(page, userName, 4);
Assert.assertNotEquals(accessTokenPage.getTotal(), 0);
public void testDelete() throws Exception{
Integer userId = 1;
AccessToken accessToken = createAccessToken(userId);
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