|
|
@ -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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |