dailidong
5 years ago
70 changed files with 3272 additions and 1303 deletions
@ -1,51 +0,0 @@ |
|||||||
# |
|
||||||
# Licensed to the Apache Software Foundation (ASF) under one or more |
|
||||||
# contributor license agreements. See the NOTICE file distributed with |
|
||||||
# this work for additional information regarding copyright ownership. |
|
||||||
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
||||||
# (the "License"); you may not use this file except in compliance with |
|
||||||
# the License. You may obtain a copy of the License at |
|
||||||
# |
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0 |
|
||||||
# |
|
||||||
# Unless required by applicable law or agreed to in writing, software |
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
# See the License for the specific language governing permissions and |
|
||||||
# limitations under the License. |
|
||||||
# |
|
||||||
|
|
||||||
name: SonarCloud |
|
||||||
on: [push, pull_request] |
|
||||||
jobs: |
|
||||||
sonarCloudTrigger: |
|
||||||
name: SonarCloud Trigger |
|
||||||
runs-on: ubuntu-latest |
|
||||||
steps: |
|
||||||
- uses: actions/checkout@v1 |
|
||||||
- uses: actions/setup-java@v1 |
|
||||||
with: |
|
||||||
java-version: 8 |
|
||||||
- uses: actions/cache@v1 |
|
||||||
with: |
|
||||||
path: ~/.m2/repository |
|
||||||
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}-sonarqube |
|
||||||
restore-keys: | |
|
||||||
${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}-sonarqube |
|
||||||
${{ runner.os }}-maven- |
|
||||||
- name: Maven clean |
|
||||||
run: mvn clean |
|
||||||
- name: Run SonarCloud analyse |
|
||||||
run: > |
|
||||||
mvn clean --batch-mode |
|
||||||
org.jacoco:jacoco-maven-plugin:prepare-agent |
|
||||||
verify |
|
||||||
org.sonarsource.scanner.maven:sonar-maven-plugin:sonar |
|
||||||
-Dmaven.test.skip=true |
|
||||||
-Dsonar.host.url=https://sonarcloud.io |
|
||||||
-Dsonar.organization=apache |
|
||||||
-Dsonar.projectKey=apache-dolphinscheduler |
|
||||||
-Dsonar.login=e4058004bc6be89decf558ac819aa1ecbee57682 |
|
||||||
env: |
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
|
||||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
|
@ -1,40 +0,0 @@ |
|||||||
# |
|
||||||
# Licensed to the Apache Software Foundation (ASF) under one or more |
|
||||||
# contributor license agreements. See the NOTICE file distributed with |
|
||||||
# this work for additional information regarding copyright ownership. |
|
||||||
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
||||||
# (the "License"); you may not use this file except in compliance with |
|
||||||
# the License. You may obtain a copy of the License at |
|
||||||
# |
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0 |
|
||||||
# |
|
||||||
# Unless required by applicable law or agreed to in writing, software |
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
# See the License for the specific language governing permissions and |
|
||||||
# limitations under the License. |
|
||||||
# |
|
||||||
|
|
||||||
logging.config=classpath:combined_logback.xml |
|
||||||
|
|
||||||
# server port |
|
||||||
server.port=12345 |
|
||||||
|
|
||||||
# session config |
|
||||||
server.servlet.session.timeout=7200 |
|
||||||
|
|
||||||
server.servlet.context-path=/dolphinscheduler/ |
|
||||||
|
|
||||||
# file size limit for upload |
|
||||||
spring.servlet.multipart.max-file-size=1024MB |
|
||||||
spring.servlet.multipart.max-request-size=1024MB |
|
||||||
|
|
||||||
#post content |
|
||||||
server.jetty.max-http-post-size=5000000 |
|
||||||
|
|
||||||
spring.messages.encoding=UTF-8 |
|
||||||
|
|
||||||
#i18n classpath folder , file prefix messages, if have many files, use "," seperator |
|
||||||
spring.messages.basename=i18n/messages |
|
||||||
|
|
||||||
server.is-combined-server=true |
|
@ -0,0 +1,181 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.api.service; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||||
|
import org.apache.commons.lang3.time.DateUtils; |
||||||
|
import org.apache.dolphinscheduler.api.enums.Status; |
||||||
|
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||||
|
import org.apache.dolphinscheduler.common.Constants; |
||||||
|
import org.apache.dolphinscheduler.common.enums.UserType; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.AccessToken; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.User; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.AccessTokenMapper; |
||||||
|
import org.junit.After; |
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.InjectMocks; |
||||||
|
import org.mockito.Mock; |
||||||
|
import org.mockito.junit.MockitoJUnitRunner; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Date; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any; |
||||||
|
import static org.mockito.ArgumentMatchers.eq; |
||||||
|
import static org.mockito.Mockito.when; |
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class) |
||||||
|
public class AccessTokenServiceTest { |
||||||
|
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(AccessTokenServiceTest.class); |
||||||
|
|
||||||
|
|
||||||
|
@InjectMocks |
||||||
|
private AccessTokenService accessTokenService ; |
||||||
|
|
||||||
|
@Mock |
||||||
|
private AccessTokenMapper accessTokenMapper; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@After |
||||||
|
public void after(){ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test |
||||||
|
public void testQueryAccessTokenList(){ |
||||||
|
|
||||||
|
IPage<AccessToken> tokenPage = new Page<>(); |
||||||
|
tokenPage.setRecords(getList()); |
||||||
|
tokenPage.setTotal(1L); |
||||||
|
when(accessTokenMapper.selectAccessTokenPage(any(Page.class),eq("zhangsan"),eq(0))).thenReturn(tokenPage); |
||||||
|
|
||||||
|
User user =new User(); |
||||||
|
Map<String, Object> result = accessTokenService.queryAccessTokenList(user,"zhangsan",1,10); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
PageInfo<AccessToken> pageInfo = (PageInfo<AccessToken>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(pageInfo.getTotalCount()>0); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testCreateToken(){ |
||||||
|
|
||||||
|
|
||||||
|
when(accessTokenMapper.insert(any(AccessToken.class))).thenReturn(2); |
||||||
|
Map<String, Object> result = accessTokenService.createToken(1,getDate(),"AccessTokenServiceTest"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testGenerateToken(){ |
||||||
|
|
||||||
|
Map<String, Object> result = accessTokenService.generateToken(Integer.MAX_VALUE,getDate()); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
String token = (String) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertNotNull(token); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testDelAccessTokenById(){ |
||||||
|
|
||||||
|
when(accessTokenMapper.selectById(1)).thenReturn(getEntity()); |
||||||
|
User userLogin = new User(); |
||||||
|
// not exist
|
||||||
|
Map<String, Object> result = accessTokenService.delAccessTokenById(userLogin,0); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST,result.get(Constants.STATUS)); |
||||||
|
// no operate
|
||||||
|
result = accessTokenService.delAccessTokenById(userLogin,1); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||||
|
//success
|
||||||
|
userLogin.setId(1); |
||||||
|
userLogin.setUserType(UserType.ADMIN_USER); |
||||||
|
result = accessTokenService.delAccessTokenById(userLogin,1); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testUpdateToken(){ |
||||||
|
|
||||||
|
when(accessTokenMapper.selectById(1)).thenReturn(getEntity()); |
||||||
|
Map<String, Object> result = accessTokenService.updateToken(1,Integer.MAX_VALUE,getDate(),"token"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
// not exist
|
||||||
|
result = accessTokenService.updateToken(2,Integer.MAX_VALUE,getDate(),"token"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* create entity |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private AccessToken getEntity(){ |
||||||
|
AccessToken accessToken = new AccessToken(); |
||||||
|
accessToken.setId(1); |
||||||
|
accessToken.setUserId(1); |
||||||
|
accessToken.setToken("AccessTokenServiceTest"); |
||||||
|
Date date = DateUtils.addDays(new Date(),30); |
||||||
|
accessToken.setExpireTime(date); |
||||||
|
return accessToken; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* entity list |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private List<AccessToken> getList(){ |
||||||
|
|
||||||
|
List<AccessToken> list = new ArrayList<>(); |
||||||
|
list.add(getEntity()); |
||||||
|
return list; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* get dateStr |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private String getDate(){ |
||||||
|
Date date = DateUtils.addDays(new Date(),30); |
||||||
|
return org.apache.dolphinscheduler.common.utils.DateUtils.dateToString(date); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,219 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.api.service; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||||
|
import org.apache.dolphinscheduler.api.enums.Status; |
||||||
|
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||||
|
import org.apache.dolphinscheduler.api.utils.Result; |
||||||
|
import org.apache.dolphinscheduler.common.Constants; |
||||||
|
import org.apache.dolphinscheduler.common.enums.AlertType; |
||||||
|
import org.apache.dolphinscheduler.common.enums.UserType; |
||||||
|
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.AlertGroup; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.User; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper; |
||||||
|
import org.junit.After; |
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.InjectMocks; |
||||||
|
import org.mockito.Mock; |
||||||
|
import org.mockito.Mockito; |
||||||
|
import org.mockito.junit.MockitoJUnitRunner; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any; |
||||||
|
import static org.mockito.ArgumentMatchers.eq; |
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class) |
||||||
|
public class AlertGroupServiceTest { |
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(AlertGroupServiceTest.class); |
||||||
|
|
||||||
|
@InjectMocks |
||||||
|
private AlertGroupService alertGroupService; |
||||||
|
@Mock |
||||||
|
private AlertGroupMapper alertGroupMapper; |
||||||
|
@Mock |
||||||
|
private UserAlertGroupMapper userAlertGroupMapper; |
||||||
|
|
||||||
|
private String groupName = "AlertGroupServiceTest"; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() { |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@After |
||||||
|
public void after(){ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test |
||||||
|
public void testQueryAlertgroup(){ |
||||||
|
|
||||||
|
Mockito.when(alertGroupMapper.queryAllGroupList()).thenReturn(getList()); |
||||||
|
HashMap<String, Object> result= alertGroupService.queryAlertgroup(); |
||||||
|
logger.info(result.toString()); |
||||||
|
List<AlertGroup> alertGroups = (List<AlertGroup>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(alertGroups)); |
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testListPaging(){ |
||||||
|
IPage<AlertGroup> page = new Page<>(1,10); |
||||||
|
page.setTotal(1L); |
||||||
|
page.setRecords(getList()); |
||||||
|
Mockito.when(alertGroupMapper.queryAlertGroupPage(any(Page.class),eq(groupName))).thenReturn(page); |
||||||
|
User user = new User(); |
||||||
|
// no operate
|
||||||
|
Map<String, Object> result = alertGroupService.listPaging(user,groupName,1,10); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||||
|
//success
|
||||||
|
user.setUserType(UserType.ADMIN_USER); |
||||||
|
result = alertGroupService.listPaging(user,groupName,1,10); |
||||||
|
logger.info(result.toString()); |
||||||
|
PageInfo<AlertGroup> pageInfo = (PageInfo<AlertGroup>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); |
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testCreateAlertgroup(){ |
||||||
|
|
||||||
|
|
||||||
|
Mockito.when(alertGroupMapper.insert(any(AlertGroup.class))).thenReturn(2); |
||||||
|
User user = new User(); |
||||||
|
//no operate
|
||||||
|
Map<String, Object> result = alertGroupService.createAlertgroup(user,groupName, AlertType.EMAIL,groupName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||||
|
user.setUserType(UserType.ADMIN_USER); |
||||||
|
//success
|
||||||
|
result = alertGroupService.createAlertgroup(user,groupName, AlertType.EMAIL,groupName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testUpdateAlertgroup(){ |
||||||
|
|
||||||
|
User user = new User(); |
||||||
|
// no operate
|
||||||
|
Map<String, Object> result = alertGroupService.updateAlertgroup(user,1,groupName, AlertType.SMS,groupName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||||
|
user.setUserType(UserType.ADMIN_USER); |
||||||
|
// not exist
|
||||||
|
result = alertGroupService.updateAlertgroup(user,1,groupName, AlertType.SMS,groupName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.ALERT_GROUP_NOT_EXIST,result.get(Constants.STATUS)); |
||||||
|
//success
|
||||||
|
Mockito.when(alertGroupMapper.selectById(2)).thenReturn(getEntity()); |
||||||
|
result = alertGroupService.updateAlertgroup(user,2,groupName, AlertType.SMS,groupName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testDelAlertgroupById(){ |
||||||
|
|
||||||
|
User user = new User(); |
||||||
|
// no operate
|
||||||
|
Map<String, Object> result = alertGroupService.delAlertgroupById(user,1); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||||
|
user.setUserType(UserType.ADMIN_USER); |
||||||
|
// not exist
|
||||||
|
result = alertGroupService.delAlertgroupById(user,2); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.ALERT_GROUP_NOT_EXIST,result.get(Constants.STATUS)); |
||||||
|
//success
|
||||||
|
Mockito.when(alertGroupMapper.selectById(2)).thenReturn(getEntity()); |
||||||
|
result = alertGroupService.delAlertgroupById(user,2); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testGrantUser(){ |
||||||
|
|
||||||
|
Map<String, Object> result = alertGroupService.grantUser(getLoginUser(),1,"123,321"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testVerifyGroupName(){ |
||||||
|
//group name not exist
|
||||||
|
Result result = alertGroupService.verifyGroupName(getLoginUser(), groupName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS.getMsg(),result.getMsg()); |
||||||
|
Mockito.when(alertGroupMapper.queryByGroupName(groupName)).thenReturn(getList()); |
||||||
|
|
||||||
|
//group name exist
|
||||||
|
result = alertGroupService.verifyGroupName(getLoginUser(), groupName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.ALERT_GROUP_EXIST.getMsg(),result.getMsg()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* create admin user |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private User getLoginUser(){ |
||||||
|
|
||||||
|
User loginUser = new User(); |
||||||
|
loginUser.setUserType(UserType.ADMIN_USER); |
||||||
|
loginUser.setId(99999999); |
||||||
|
return loginUser; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* get list |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private List<AlertGroup> getList(){ |
||||||
|
List<AlertGroup> alertGroups = new ArrayList<>(); |
||||||
|
alertGroups.add(getEntity()); |
||||||
|
return alertGroups; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* get entity |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private AlertGroup getEntity(){ |
||||||
|
AlertGroup alertGroup = new AlertGroup(); |
||||||
|
alertGroup.setId(1); |
||||||
|
alertGroup.setGroupName(groupName); |
||||||
|
alertGroup.setGroupType(AlertType.EMAIL); |
||||||
|
return alertGroup; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,101 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.api.service; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.api.enums.Status; |
||||||
|
import org.apache.dolphinscheduler.common.Constants; |
||||||
|
import org.apache.dolphinscheduler.common.enums.DbType; |
||||||
|
import org.apache.dolphinscheduler.common.model.Server; |
||||||
|
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||||
|
import org.apache.dolphinscheduler.dao.MonitorDBDao; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.MonitorRecord; |
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.InjectMocks; |
||||||
|
import org.mockito.Mock; |
||||||
|
import org.mockito.Mockito; |
||||||
|
import org.mockito.junit.MockitoJUnitRunner; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class) |
||||||
|
public class MonitorServiceTest { |
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(MonitorServiceTest.class); |
||||||
|
|
||||||
|
@InjectMocks |
||||||
|
private MonitorService monitorService; |
||||||
|
@Mock |
||||||
|
private MonitorDBDao monitorDBDao; |
||||||
|
|
||||||
|
|
||||||
|
@Test |
||||||
|
public void testQueryDatabaseState(){ |
||||||
|
|
||||||
|
Mockito.when(monitorDBDao.queryDatabaseState()).thenReturn(getList()); |
||||||
|
Map<String,Object> result = monitorService.queryDatabaseState(null); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
List<MonitorRecord> monitorRecordList = (List<MonitorRecord>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(monitorRecordList)); |
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testQueryMaster(){ |
||||||
|
//TODO need zk
|
||||||
|
// Map<String,Object> result = monitorService.queryMaster(null);
|
||||||
|
// logger.info(result.toString());
|
||||||
|
// Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS));
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testQueryZookeeperState(){ |
||||||
|
//TODO need zk
|
||||||
|
// Map<String,Object> result = monitorService.queryZookeeperState(null);
|
||||||
|
// logger.info(result.toString());
|
||||||
|
// Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS));
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testGetServerListFromZK(){ |
||||||
|
//TODO need zk
|
||||||
|
// List<Server> serverList = monitorService.getServerListFromZK(true);
|
||||||
|
// logger.info(serverList.toString());
|
||||||
|
} |
||||||
|
|
||||||
|
private List<MonitorRecord> getList(){ |
||||||
|
List<MonitorRecord> monitorRecordList = new ArrayList<>(); |
||||||
|
monitorRecordList.add(getEntity()); |
||||||
|
return monitorRecordList; |
||||||
|
} |
||||||
|
|
||||||
|
private MonitorRecord getEntity(){ |
||||||
|
MonitorRecord monitorRecord = new MonitorRecord(); |
||||||
|
monitorRecord.setDbType(DbType.MYSQL); |
||||||
|
return monitorRecord; |
||||||
|
} |
||||||
|
|
||||||
|
private List<Server> getServerList(){ |
||||||
|
List<Server> servers = new ArrayList<>(); |
||||||
|
servers.add(new Server()); |
||||||
|
return servers; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,356 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.api.service; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||||
|
import org.apache.dolphinscheduler.api.enums.Status; |
||||||
|
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||||
|
import org.apache.dolphinscheduler.common.Constants; |
||||||
|
import org.apache.dolphinscheduler.common.enums.UserType; |
||||||
|
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.Project; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.ProjectUser; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.User; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.UserMapper; |
||||||
|
import org.junit.After; |
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.InjectMocks; |
||||||
|
import org.mockito.Mock; |
||||||
|
import org.mockito.Mockito; |
||||||
|
import org.mockito.junit.MockitoJUnitRunner; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class) |
||||||
|
public class ProjectServiceTest { |
||||||
|
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ProjectServiceTest.class); |
||||||
|
|
||||||
|
@InjectMocks |
||||||
|
private ProjectService projectService; |
||||||
|
@Mock |
||||||
|
private ProjectMapper projectMapper; |
||||||
|
@Mock |
||||||
|
private UserMapper userMapper; |
||||||
|
@Mock |
||||||
|
private ProjectUserMapper projectUserMapper; |
||||||
|
@Mock |
||||||
|
private ProcessDefinitionMapper processDefinitionMapper; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private String projectName = "ProjectServiceTest"; |
||||||
|
|
||||||
|
private String userName = "ProjectServiceTest"; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@After |
||||||
|
public void after(){ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testCreateProject(){ |
||||||
|
|
||||||
|
User loginUser = getLoginUser(); |
||||||
|
loginUser.setId(1); |
||||||
|
Map<String, Object> result = projectService.createProject(loginUser, projectName, getDesc()); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
//project name exist
|
||||||
|
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(getProject()); |
||||||
|
result = projectService.createProject(loginUser, projectName, projectName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
//success
|
||||||
|
Mockito.when(projectMapper.insert(Mockito.any(Project.class))).thenReturn(1); |
||||||
|
result = projectService.createProject(loginUser, "test", "test"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testQueryById(){ |
||||||
|
|
||||||
|
//not exist
|
||||||
|
Map<String, Object> result = projectService.queryById(Integer.MAX_VALUE); |
||||||
|
Assert.assertEquals(Status.PROJECT_NOT_FOUNT,result.get(Constants.STATUS)); |
||||||
|
logger.info(result.toString()); |
||||||
|
|
||||||
|
//success
|
||||||
|
Mockito.when(projectMapper.selectById(1)).thenReturn(getProject()); |
||||||
|
result = projectService.queryById(1); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testCheckProjectAndAuth(){ |
||||||
|
|
||||||
|
Mockito.when(projectUserMapper.queryProjectRelation(1, 1)).thenReturn(getProjectUser()); |
||||||
|
User loginUser = getLoginUser(); |
||||||
|
|
||||||
|
Map<String, Object> result = projectService.checkProjectAndAuth(loginUser,null,projectName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Status status = (Status)result.get(Constants.STATUS); |
||||||
|
Assert.assertEquals(Status.PROJECT_NOT_FOUNT,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
Project project = getProject(); |
||||||
|
//USER_NO_OPERATION_PROJECT_PERM
|
||||||
|
project.setUserId(2); |
||||||
|
result = projectService.checkProjectAndAuth(loginUser,project,projectName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
//success
|
||||||
|
project.setUserId(1); |
||||||
|
result = projectService.checkProjectAndAuth(loginUser,project,projectName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testHasProjectAndPerm(){ |
||||||
|
|
||||||
|
// Mockito.when(projectUserMapper.queryProjectRelation(1, 1)).thenReturn(getProjectUser());
|
||||||
|
User loginUser = getLoginUser(); |
||||||
|
Project project = getProject(); |
||||||
|
Map<String, Object> result = new HashMap<>(); |
||||||
|
// not exist user
|
||||||
|
User tempUser = new User(); |
||||||
|
tempUser.setId(Integer.MAX_VALUE); |
||||||
|
boolean checkResult = projectService.hasProjectAndPerm(tempUser,project,result); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertFalse(checkResult); |
||||||
|
|
||||||
|
//success
|
||||||
|
result = new HashMap<>(); |
||||||
|
project.setUserId(1); |
||||||
|
checkResult = projectService.hasProjectAndPerm(loginUser,project,result); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertTrue(checkResult); |
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testQueryProjectListPaging(){ |
||||||
|
|
||||||
|
IPage<Project> page = new Page<>(1,10); |
||||||
|
page.setRecords(getList()); |
||||||
|
page.setTotal(1L); |
||||||
|
Mockito.when(projectMapper.queryProjectListPaging(Mockito.any(Page.class), Mockito.eq(1), Mockito.eq(projectName))).thenReturn(page); |
||||||
|
User loginUser = getLoginUser(); |
||||||
|
|
||||||
|
// project owner
|
||||||
|
Map<String, Object> result = projectService.queryProjectListPaging(loginUser,10,1,projectName); |
||||||
|
logger.info(result.toString()); |
||||||
|
PageInfo<Project> pageInfo = (PageInfo<Project>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); |
||||||
|
|
||||||
|
//admin
|
||||||
|
Mockito.when(projectMapper.queryProjectListPaging(Mockito.any(Page.class), Mockito.eq(0), Mockito.eq(projectName))).thenReturn(page); |
||||||
|
loginUser.setUserType(UserType.ADMIN_USER); |
||||||
|
result = projectService.queryProjectListPaging(loginUser,10,1,projectName); |
||||||
|
logger.info(result.toString()); |
||||||
|
pageInfo = (PageInfo<Project>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); |
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testDeleteProject(){ |
||||||
|
|
||||||
|
Mockito.when(projectMapper.selectById(1)).thenReturn(getProject()); |
||||||
|
User loginUser = getLoginUser(); |
||||||
|
//PROJECT_NOT_FOUNT
|
||||||
|
Map<String, Object> result= projectService.deleteProject(loginUser,12); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.PROJECT_NOT_FOUNT,result.get(Constants.STATUS)); |
||||||
|
loginUser.setId(2); |
||||||
|
//USER_NO_OPERATION_PROJECT_PERM
|
||||||
|
result= projectService.deleteProject(loginUser,1); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
//DELETE_PROJECT_ERROR_DEFINES_NOT_NULL
|
||||||
|
Mockito.when(processDefinitionMapper.queryAllDefinitionList(1)).thenReturn(getProcessDefinitions()); |
||||||
|
loginUser.setUserType(UserType.ADMIN_USER); |
||||||
|
result= projectService.deleteProject(loginUser,1); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.DELETE_PROJECT_ERROR_DEFINES_NOT_NULL,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
//success
|
||||||
|
Mockito.when(projectMapper.deleteById(1)).thenReturn(1); |
||||||
|
Mockito.when(processDefinitionMapper.queryAllDefinitionList(1)).thenReturn(new ArrayList<>()); |
||||||
|
result= projectService.deleteProject(loginUser,1); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testUpdate(){ |
||||||
|
|
||||||
|
User loginUser = getLoginUser(); |
||||||
|
Project project = getProject(); |
||||||
|
project.setId(2); |
||||||
|
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(project); |
||||||
|
Mockito.when( projectMapper.selectById(1)).thenReturn(getProject()); |
||||||
|
// PROJECT_NOT_FOUNT
|
||||||
|
Map<String, Object> result = projectService.update(loginUser,12,projectName,"desc"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.PROJECT_NOT_FOUNT,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
//PROJECT_ALREADY_EXISTS
|
||||||
|
result = projectService.update(loginUser,1,projectName,"desc"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
//success
|
||||||
|
project.setUserId(1); |
||||||
|
Mockito.when(projectMapper.updateById(Mockito.any(Project.class))).thenReturn(1); |
||||||
|
result = projectService.update(loginUser,1,"test","desc"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testQueryAuthorizedProject(){ |
||||||
|
|
||||||
|
User loginUser = getLoginUser(); |
||||||
|
|
||||||
|
Mockito.when(projectMapper.queryAuthedProjectListByUserId(1)).thenReturn(getList()); |
||||||
|
//USER_NO_OPERATION_PERM
|
||||||
|
Map<String, Object> result = projectService.queryAuthorizedProject(loginUser,3); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
//success
|
||||||
|
loginUser.setUserType(UserType.ADMIN_USER); |
||||||
|
result = projectService.queryAuthorizedProject(loginUser,1); |
||||||
|
logger.info(result.toString()); |
||||||
|
List<Project> projects = (List<Project>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); |
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testQueryAllProjectList(){ |
||||||
|
|
||||||
|
Mockito.when(projectMapper.selectList(null)).thenReturn(getList()); |
||||||
|
Mockito.when(processDefinitionMapper.selectList(null)).thenReturn(getProcessDefinitions()); |
||||||
|
|
||||||
|
Map<String, Object> result = projectService.queryAllProjectList(); |
||||||
|
logger.info(result.toString()); |
||||||
|
List<Project> projects = (List<Project>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); |
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testQueryUnauthorizedProject(){ |
||||||
|
// Mockito.when(projectMapper.queryAuthedProjectListByUserId(1)).thenReturn(getList());
|
||||||
|
Mockito.when(projectMapper.queryProjectExceptUserId(2)).thenReturn(getList()); |
||||||
|
|
||||||
|
User loginUser = new User(); |
||||||
|
loginUser.setUserType(UserType.ADMIN_USER); |
||||||
|
|
||||||
|
Map<String, Object> result = projectService.queryUnauthorizedProject(loginUser,2); |
||||||
|
logger.info(result.toString()); |
||||||
|
List<Project> projects = (List<Project>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private Project getProject(){ |
||||||
|
Project project = new Project(); |
||||||
|
project.setId(1); |
||||||
|
project.setName(projectName); |
||||||
|
project.setUserId(1); |
||||||
|
return project; |
||||||
|
} |
||||||
|
|
||||||
|
private List<Project> getList(){ |
||||||
|
List<Project> list = new ArrayList<>(); |
||||||
|
list.add(getProject()); |
||||||
|
return list; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* create admin user |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private User getLoginUser(){ |
||||||
|
|
||||||
|
User loginUser = new User(); |
||||||
|
loginUser.setUserType(UserType.GENERAL_USER); |
||||||
|
loginUser.setUserName(userName); |
||||||
|
loginUser.setId(1); |
||||||
|
return loginUser; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* get project user |
||||||
|
|
||||||
|
*/ |
||||||
|
private ProjectUser getProjectUser(){ |
||||||
|
ProjectUser projectUser = new ProjectUser(); |
||||||
|
projectUser.setProjectId(1); |
||||||
|
projectUser.setUserId(1); |
||||||
|
return projectUser; |
||||||
|
} |
||||||
|
|
||||||
|
private List<ProcessDefinition> getProcessDefinitions(){ |
||||||
|
List<ProcessDefinition> list = new ArrayList<>(); |
||||||
|
ProcessDefinition processDefinition = new ProcessDefinition(); |
||||||
|
processDefinition.setProjectId(1); |
||||||
|
list.add(processDefinition); |
||||||
|
return list; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private String getDesc(){ |
||||||
|
return "projectUserMapper.deleteProjectRelation(projectId,userId)projectUserMappe" + |
||||||
|
".deleteProjectRelation(projectId,userId)projectUserMappe" + |
||||||
|
"r.deleteProjectRelation(projectId,userId)projectUserMapper" + |
||||||
|
".deleteProjectRelation(projectId,userId)projectUserMapper.deleteProjectRelation(projectId,userId)"; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,208 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.api.service; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||||
|
import org.apache.dolphinscheduler.api.enums.Status; |
||||||
|
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||||
|
import org.apache.dolphinscheduler.api.utils.Result; |
||||||
|
import org.apache.dolphinscheduler.common.Constants; |
||||||
|
import org.apache.dolphinscheduler.common.enums.UserType; |
||||||
|
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.Queue; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.User; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.QueueMapper; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.UserMapper; |
||||||
|
import org.junit.After; |
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.InjectMocks; |
||||||
|
import org.mockito.Mock; |
||||||
|
import org.mockito.Mockito; |
||||||
|
import org.mockito.junit.MockitoJUnitRunner; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class) |
||||||
|
public class QueueServiceTest { |
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(QueueServiceTest.class); |
||||||
|
|
||||||
|
@InjectMocks |
||||||
|
private QueueService queueService; |
||||||
|
@Mock |
||||||
|
private QueueMapper queueMapper; |
||||||
|
@Mock |
||||||
|
private UserMapper userMapper; |
||||||
|
private String queueName = "QueueServiceTest"; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() { |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@After |
||||||
|
public void after(){ |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testQueryList(){ |
||||||
|
|
||||||
|
Mockito.when(queueMapper.selectList(null)).thenReturn(getQueueList()); |
||||||
|
Map<String, Object> result = queueService.queryList(getLoginUser()); |
||||||
|
logger.info(result.toString()); |
||||||
|
List<Queue> queueList = (List<Queue>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(queueList)); |
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testQueryListPage(){ |
||||||
|
|
||||||
|
IPage<Queue> page = new Page<>(1,10); |
||||||
|
page.setTotal(1L); |
||||||
|
page.setRecords(getQueueList()); |
||||||
|
Mockito.when(queueMapper.queryQueuePaging(Mockito.any(Page.class), Mockito.eq(queueName))).thenReturn(page); |
||||||
|
Map<String, Object> result = queueService.queryList(getLoginUser(),queueName,1,10); |
||||||
|
logger.info(result.toString()); |
||||||
|
PageInfo<Queue> pageInfo = (PageInfo<Queue>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); |
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testCreateQueue(){ |
||||||
|
|
||||||
|
// queue is null
|
||||||
|
Map<String, Object> result = queueService.createQueue(getLoginUser(),null,queueName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR,result.get(Constants.STATUS)); |
||||||
|
// queueName is null
|
||||||
|
result = queueService.createQueue(getLoginUser(),queueName,null); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR,result.get(Constants.STATUS)); |
||||||
|
// correct
|
||||||
|
result = queueService.createQueue(getLoginUser(),queueName,queueName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testUpdateQueue(){ |
||||||
|
|
||||||
|
Mockito.when(queueMapper.selectById(1)).thenReturn(getQueue()); |
||||||
|
Mockito.when(queueMapper.queryAllQueueList("test", null)).thenReturn(getQueueList()); |
||||||
|
Mockito.when(queueMapper.queryAllQueueList(null, "test")).thenReturn(getQueueList()); |
||||||
|
Mockito.when(userMapper.queryUserListByQueue(queueName)).thenReturn(getUserList()); |
||||||
|
|
||||||
|
// not exist
|
||||||
|
Map<String, Object> result = queueService.updateQueue(getLoginUser(),0,"queue",queueName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.QUEUE_NOT_EXIST.getCode(),((Status)result.get(Constants.STATUS)).getCode()); |
||||||
|
//no need update
|
||||||
|
result = queueService.updateQueue(getLoginUser(),1,queueName,queueName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.NEED_NOT_UPDATE_QUEUE.getCode(),((Status)result.get(Constants.STATUS)).getCode()); |
||||||
|
//queue exist
|
||||||
|
result = queueService.updateQueue(getLoginUser(),1,"test",queueName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.QUEUE_VALUE_EXIST.getCode(),((Status)result.get(Constants.STATUS)).getCode()); |
||||||
|
// queueName exist
|
||||||
|
result = queueService.updateQueue(getLoginUser(),1,"test1","test"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.QUEUE_NAME_EXIST.getCode(),((Status)result.get(Constants.STATUS)).getCode()); |
||||||
|
//success
|
||||||
|
result = queueService.updateQueue(getLoginUser(),1,"test1","test1"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS.getCode(),((Status)result.get(Constants.STATUS)).getCode()); |
||||||
|
|
||||||
|
} |
||||||
|
@Test |
||||||
|
public void testVerifyQueue(){ |
||||||
|
|
||||||
|
Mockito.when(queueMapper.queryAllQueueList(queueName, null)).thenReturn(getQueueList()); |
||||||
|
Mockito.when(queueMapper.queryAllQueueList(null, queueName)).thenReturn(getQueueList()); |
||||||
|
|
||||||
|
//queue null
|
||||||
|
Result result = queueService.verifyQueue(null,queueName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(result.getCode().intValue(), Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode()); |
||||||
|
|
||||||
|
//queueName null
|
||||||
|
result = queueService.verifyQueue(queueName,null); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(result.getCode().intValue(), Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode()); |
||||||
|
|
||||||
|
//exist queueName
|
||||||
|
result = queueService.verifyQueue(queueName,queueName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(result.getCode().intValue(), Status.QUEUE_NAME_EXIST.getCode()); |
||||||
|
|
||||||
|
//exist queue
|
||||||
|
result = queueService.verifyQueue(queueName,"test"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(result.getCode().intValue(), Status.QUEUE_VALUE_EXIST.getCode()); |
||||||
|
|
||||||
|
// success
|
||||||
|
result = queueService.verifyQueue("test","test"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(result.getCode().intValue(), Status.SUCCESS.getCode()); |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
/** |
||||||
|
* create admin user |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private User getLoginUser(){ |
||||||
|
|
||||||
|
User loginUser = new User(); |
||||||
|
loginUser.setUserType(UserType.ADMIN_USER); |
||||||
|
loginUser.setId(99999999); |
||||||
|
return loginUser; |
||||||
|
} |
||||||
|
|
||||||
|
private List<User> getUserList(){ |
||||||
|
List<User> list = new ArrayList<>(); |
||||||
|
list.add(getLoginUser()); |
||||||
|
return list; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* get queue |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private Queue getQueue(){ |
||||||
|
Queue queue = new Queue(); |
||||||
|
queue.setId(1); |
||||||
|
queue.setQueue(queueName); |
||||||
|
queue.setQueueName(queueName); |
||||||
|
return queue; |
||||||
|
} |
||||||
|
|
||||||
|
private List<Queue> getQueueList(){ |
||||||
|
List<Queue> queueList = new ArrayList<>(); |
||||||
|
queueList.add(getQueue()); |
||||||
|
return queueList; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,173 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.api.service; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||||
|
import org.apache.dolphinscheduler.api.enums.Status; |
||||||
|
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||||
|
import org.apache.dolphinscheduler.common.Constants; |
||||||
|
import org.apache.dolphinscheduler.common.enums.UserType; |
||||||
|
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.User; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.WorkerGroup; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.ProcessInstanceMapper; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.WorkerGroupMapper; |
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.InjectMocks; |
||||||
|
import org.mockito.Mock; |
||||||
|
import org.mockito.Mockito; |
||||||
|
import org.mockito.junit.MockitoJUnitRunner; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class) |
||||||
|
public class WorkerGroupServiceTest { |
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(WorkerGroupServiceTest.class); |
||||||
|
|
||||||
|
@InjectMocks |
||||||
|
private WorkerGroupService workerGroupService; |
||||||
|
@Mock |
||||||
|
private WorkerGroupMapper workerGroupMapper; |
||||||
|
@Mock |
||||||
|
private ProcessInstanceMapper processInstanceMapper; |
||||||
|
|
||||||
|
|
||||||
|
private String groupName="groupName000001"; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* create or update a worker group |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testSaveWorkerGroup(){ |
||||||
|
|
||||||
|
User user = new User(); |
||||||
|
// general user add
|
||||||
|
user.setUserType(UserType.GENERAL_USER); |
||||||
|
Map<String, Object> result = workerGroupService.saveWorkerGroup(user, 0, groupName, "127.0.0.1"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals( Status.USER_NO_OPERATION_PERM.getMsg(),(String) result.get(Constants.MSG)); |
||||||
|
|
||||||
|
//success
|
||||||
|
user.setUserType(UserType.ADMIN_USER); |
||||||
|
result = workerGroupService.saveWorkerGroup(user, 0, groupName, "127.0.0.1"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS.getMsg(),(String)result.get(Constants.MSG)); |
||||||
|
// group name exist
|
||||||
|
Mockito.when(workerGroupMapper.selectById(2)).thenReturn(getWorkerGroup(2)); |
||||||
|
Mockito.when(workerGroupMapper.queryWorkerGroupByName(groupName)).thenReturn(getList()); |
||||||
|
result = workerGroupService.saveWorkerGroup(user, 2, groupName, "127.0.0.1"); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.NAME_EXIST,result.get(Constants.STATUS)); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* query worker group paging |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testQueryAllGroupPaging(){ |
||||||
|
|
||||||
|
User user = new User(); |
||||||
|
// general user add
|
||||||
|
user.setUserType(UserType.GENERAL_USER); |
||||||
|
Map<String, Object> result = workerGroupService.queryAllGroupPaging(user, 1, 10, groupName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals((String) result.get(Constants.MSG), Status.USER_NO_OPERATION_PERM.getMsg()); |
||||||
|
//success
|
||||||
|
user.setUserType(UserType.ADMIN_USER); |
||||||
|
Page<WorkerGroup> page = new Page<>(1,10); |
||||||
|
page.setRecords(getList()); |
||||||
|
page.setSize(1L); |
||||||
|
Mockito.when(workerGroupMapper.queryListPaging(Mockito.any(Page.class), Mockito.eq(groupName))).thenReturn(page); |
||||||
|
result = workerGroupService.queryAllGroupPaging(user, 1, 10, groupName); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS.getMsg(),(String)result.get(Constants.MSG)); |
||||||
|
PageInfo<WorkerGroup> pageInfo = (PageInfo<WorkerGroup>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* delete group by id |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testDeleteWorkerGroupById(){ |
||||||
|
|
||||||
|
//DELETE_WORKER_GROUP_BY_ID_FAIL
|
||||||
|
Mockito.when(processInstanceMapper.queryByWorkerGroupIdAndStatus(1, Constants.NOT_TERMINATED_STATES)).thenReturn(getProcessInstanceList()); |
||||||
|
Map<String, Object> result = workerGroupService.deleteWorkerGroupById(1); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.DELETE_WORKER_GROUP_BY_ID_FAIL.getCode(),((Status) result.get(Constants.STATUS)).getCode()); |
||||||
|
|
||||||
|
//correct
|
||||||
|
result = workerGroupService.deleteWorkerGroupById(2); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS.getMsg(),(String)result.get(Constants.MSG)); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testQueryAllGroup(){ |
||||||
|
Mockito.when(workerGroupMapper.queryAllWorkerGroup()).thenReturn(getList()); |
||||||
|
Map<String, Object> result = workerGroupService.queryAllGroup(); |
||||||
|
logger.info(result.toString()); |
||||||
|
Assert.assertEquals(Status.SUCCESS.getMsg(),(String)result.get(Constants.MSG)); |
||||||
|
List<WorkerGroup> workerGroupList = (List<WorkerGroup>) result.get(Constants.DATA_LIST); |
||||||
|
Assert.assertTrue(workerGroupList.size()>0); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* get processInstances |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private List<ProcessInstance> getProcessInstanceList(){ |
||||||
|
|
||||||
|
List<ProcessInstance> processInstances = new ArrayList<>(); |
||||||
|
processInstances.add(new ProcessInstance()); |
||||||
|
return processInstances; |
||||||
|
} |
||||||
|
/** |
||||||
|
* get Group |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
private WorkerGroup getWorkerGroup(int id){ |
||||||
|
WorkerGroup workerGroup = new WorkerGroup(); |
||||||
|
workerGroup.setName(groupName); |
||||||
|
workerGroup.setId(id); |
||||||
|
return workerGroup; |
||||||
|
} |
||||||
|
private WorkerGroup getWorkerGroup(){ |
||||||
|
|
||||||
|
return getWorkerGroup(1); |
||||||
|
} |
||||||
|
|
||||||
|
private List<WorkerGroup> getList(){ |
||||||
|
List<WorkerGroup> list = new ArrayList<>(); |
||||||
|
list.add(getWorkerGroup()); |
||||||
|
return list; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,699 +0,0 @@ |
|||||||
/* |
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
|
||||||
* contributor license agreements. See the NOTICE file distributed with |
|
||||||
* this work for additional information regarding copyright ownership. |
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
|
||||||
* (the "License"); you may not use this file except in compliance with |
|
||||||
* the License. You may obtain a copy of the License at |
|
||||||
* |
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
* |
|
||||||
* Unless required by applicable law or agreed to in writing, software |
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
* See the License for the specific language governing permissions and |
|
||||||
* limitations under the License. |
|
||||||
*/ |
|
||||||
package org.apache.dolphinscheduler.common.utils; |
|
||||||
|
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
import java.math.BigDecimal; |
|
||||||
import java.math.BigInteger; |
|
||||||
import java.nio.ByteBuffer; |
|
||||||
import java.nio.charset.StandardCharsets; |
|
||||||
|
|
||||||
/** |
|
||||||
* Utility class that handles Bytes |
|
||||||
*/ |
|
||||||
public class Bytes { |
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Bytes.class); |
|
||||||
public static final String UTF8_ENCODING = "UTF-8"; |
|
||||||
//An empty instance.
|
|
||||||
public static final byte [] EMPTY_BYTE_ARRAY = new byte [0]; |
|
||||||
|
|
||||||
/** |
|
||||||
* Size of int in bytes |
|
||||||
*/ |
|
||||||
public static final int SIZEOF_INT = Integer.SIZE / Byte.SIZE; |
|
||||||
|
|
||||||
/** |
|
||||||
* Size of long in bytes |
|
||||||
*/ |
|
||||||
public static final int SIZEOF_LONG = Long.SIZE / Byte.SIZE; |
|
||||||
|
|
||||||
/** |
|
||||||
* Size of short in bytes |
|
||||||
*/ |
|
||||||
public static final int SIZEOF_SHORT = Short.SIZE / Byte.SIZE; |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* Put bytes at the specified byte array position. |
|
||||||
* @param tgtBytes the byte array |
|
||||||
* @param tgtOffset position in the array |
|
||||||
* @param srcBytes array to write out |
|
||||||
* @param srcOffset source offset |
|
||||||
* @param srcLength source length |
|
||||||
* @return incremented offset |
|
||||||
*/ |
|
||||||
public static int putBytes(byte[] tgtBytes, int tgtOffset, byte[] srcBytes, |
|
||||||
int srcOffset, int srcLength) { |
|
||||||
System.arraycopy(srcBytes, srcOffset, tgtBytes, tgtOffset, srcLength); |
|
||||||
return tgtOffset + srcLength; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Write a single byte out to the specified byte array position. |
|
||||||
* @param bytes the byte array |
|
||||||
* @param offset position in the array |
|
||||||
* @param b byte to write out |
|
||||||
* @return incremented offset |
|
||||||
*/ |
|
||||||
public static int putByte(byte[] bytes, int offset, byte b) { |
|
||||||
bytes[offset] = b; |
|
||||||
return offset + 1; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Returns a new byte array, copied from the passed ByteBuffer. |
|
||||||
* @param bb A ByteBuffer |
|
||||||
* @return the byte array |
|
||||||
*/ |
|
||||||
public static byte[] toBytes(ByteBuffer bb) { |
|
||||||
int length = bb.limit(); |
|
||||||
byte [] result = new byte[length]; |
|
||||||
System.arraycopy(bb.array(), bb.arrayOffset(), result, 0, length); |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param b Presumed UTF-8 encoded byte array. |
|
||||||
* @return String made from <code>b</code> |
|
||||||
*/ |
|
||||||
public static String toString(final byte [] b) { |
|
||||||
if (b == null) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
return toString(b, 0, b.length); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Joins two byte arrays together using a separator. |
|
||||||
* @param b1 The first byte array. |
|
||||||
* @param sep The separator to use. |
|
||||||
* @param b2 The second byte array. |
|
||||||
* @return two byte arrays together using a separator. |
|
||||||
*/ |
|
||||||
public static String toString(final byte [] b1, |
|
||||||
String sep, |
|
||||||
final byte [] b2) { |
|
||||||
return toString(b1, 0, b1.length) + sep + toString(b2, 0, b2.length); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* This method will convert utf8 encoded bytes into a string. If |
|
||||||
* an UnsupportedEncodingException occurs, this method will eat it |
|
||||||
* and return null instead. |
|
||||||
* |
|
||||||
* @param b Presumed UTF-8 encoded byte array. |
|
||||||
* @param off offset into array |
|
||||||
* @param len length of utf-8 sequence |
|
||||||
* @return String made from <code>b</code> or null |
|
||||||
*/ |
|
||||||
public static String toString(final byte [] b, int off, int len) { |
|
||||||
if (b == null) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
if (len == 0) { |
|
||||||
return ""; |
|
||||||
} |
|
||||||
return new String(b, off, len, StandardCharsets.UTF_8); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a string to a UTF-8 byte array. |
|
||||||
* @param s string |
|
||||||
* @return the byte array |
|
||||||
*/ |
|
||||||
public static byte[] toBytes(String s) { |
|
||||||
return s.getBytes(StandardCharsets.UTF_8); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Convert a boolean to a byte array. True becomes -1 |
|
||||||
* and false becomes 0. |
|
||||||
* |
|
||||||
* @param b value |
|
||||||
* @return <code>b</code> encoded in a byte array. |
|
||||||
*/ |
|
||||||
public static byte [] toBytes(final boolean b) { |
|
||||||
return new byte[] { b ? (byte) -1 : (byte) 0 }; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Reverses {@link #toBytes(boolean)} |
|
||||||
* @param b array |
|
||||||
* @return True or false. |
|
||||||
*/ |
|
||||||
public static boolean toBoolean(final byte [] b) { |
|
||||||
if (b.length != 1) { |
|
||||||
throw new IllegalArgumentException("Array has wrong size: " + b.length); |
|
||||||
} |
|
||||||
return b[0] != (byte) 0; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Convert a long value to a byte array using big-endian. |
|
||||||
* |
|
||||||
* @param val value to convert |
|
||||||
* @return the byte array |
|
||||||
*/ |
|
||||||
public static byte[] toBytes(long val) { |
|
||||||
byte [] b = new byte[8]; |
|
||||||
for (int i = 7; i > 0; i--) { |
|
||||||
b[i] = (byte) val; |
|
||||||
val >>>= 8; |
|
||||||
} |
|
||||||
b[0] = (byte) val; |
|
||||||
return b; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to a long value. Reverses |
|
||||||
* {@link #toBytes(long)} |
|
||||||
* @param bytes array |
|
||||||
* @return the long value |
|
||||||
*/ |
|
||||||
public static long toLong(byte[] bytes) { |
|
||||||
return toLong(bytes, 0, SIZEOF_LONG); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to a long value. Assumes there will be |
|
||||||
* {@link #SIZEOF_LONG} bytes available. |
|
||||||
* |
|
||||||
* @param bytes bytes |
|
||||||
* @param offset offset |
|
||||||
* @return the long value |
|
||||||
*/ |
|
||||||
public static long toLong(byte[] bytes, int offset) { |
|
||||||
return toLong(bytes, offset, SIZEOF_LONG); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to a long value. |
|
||||||
* |
|
||||||
* @param bytes array of bytes |
|
||||||
* @param offset offset into array |
|
||||||
* @param length length of data (must be {@link #SIZEOF_LONG}) |
|
||||||
* @return the long value |
|
||||||
* @throws IllegalArgumentException if length is not {@link #SIZEOF_LONG} or |
|
||||||
* if there's not enough room in the array at the offset indicated. |
|
||||||
*/ |
|
||||||
public static long toLong(byte[] bytes, int offset, final int length) { |
|
||||||
if (length != SIZEOF_LONG || offset + length > bytes.length) { |
|
||||||
throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_LONG); |
|
||||||
} |
|
||||||
long l = 0; |
|
||||||
for(int i = offset; i < offset + length; i++) { |
|
||||||
l <<= 8; |
|
||||||
l ^= bytes[i] & 0xFF; |
|
||||||
} |
|
||||||
return l; |
|
||||||
} |
|
||||||
|
|
||||||
private static IllegalArgumentException |
|
||||||
explainWrongLengthOrOffset(final byte[] bytes, |
|
||||||
final int offset, |
|
||||||
final int length, |
|
||||||
final int expectedLength) { |
|
||||||
String reason; |
|
||||||
if (length != expectedLength) { |
|
||||||
reason = "Wrong length: " + length + ", expected " + expectedLength; |
|
||||||
} else { |
|
||||||
reason = "offset (" + offset + ") + length (" + length + ") exceed the" |
|
||||||
+ " capacity of the array: " + bytes.length; |
|
||||||
} |
|
||||||
return new IllegalArgumentException(reason); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Put a long value out to the specified byte array position. |
|
||||||
* @param bytes the byte array |
|
||||||
* @param offset position in the array |
|
||||||
* @param val long to write out |
|
||||||
* @return incremented offset |
|
||||||
* @throws IllegalArgumentException if the byte array given doesn't have |
|
||||||
* enough room at the offset specified. |
|
||||||
*/ |
|
||||||
public static int putLong(byte[] bytes, int offset, long val) { |
|
||||||
if (bytes.length - offset < SIZEOF_LONG) { |
|
||||||
throw new IllegalArgumentException("Not enough room to put a long at" |
|
||||||
+ " offset " + offset + " in a " + bytes.length + " byte array"); |
|
||||||
} |
|
||||||
for(int i = offset + 7; i > offset; i--) { |
|
||||||
bytes[i] = (byte) val; |
|
||||||
val >>>= 8; |
|
||||||
} |
|
||||||
bytes[offset] = (byte) val; |
|
||||||
return offset + SIZEOF_LONG; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Presumes float encoded as IEEE 754 floating-point "single format" |
|
||||||
* @param bytes byte array |
|
||||||
* @return Float made from passed byte array. |
|
||||||
*/ |
|
||||||
public static float toFloat(byte [] bytes) { |
|
||||||
return toFloat(bytes, 0); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Presumes float encoded as IEEE 754 floating-point "single format" |
|
||||||
* @param bytes array to convert |
|
||||||
* @param offset offset into array |
|
||||||
* @return Float made from passed byte array. |
|
||||||
*/ |
|
||||||
public static float toFloat(byte [] bytes, int offset) { |
|
||||||
return Float.intBitsToFloat(toInt(bytes, offset, SIZEOF_INT)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param bytes byte array |
|
||||||
* @param offset offset to write to |
|
||||||
* @param f float value |
|
||||||
* @return New offset in <code>bytes</code> |
|
||||||
*/ |
|
||||||
public static int putFloat(byte [] bytes, int offset, float f) { |
|
||||||
return putInt(bytes, offset, Float.floatToRawIntBits(f)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param f float value |
|
||||||
* @return the float represented as byte [] |
|
||||||
*/ |
|
||||||
public static byte [] toBytes(final float f) { |
|
||||||
// Encode it as int
|
|
||||||
return Bytes.toBytes(Float.floatToRawIntBits(f)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param bytes byte array |
|
||||||
* @return Return double made from passed bytes. |
|
||||||
*/ |
|
||||||
public static double toDouble(final byte [] bytes) { |
|
||||||
return toDouble(bytes, 0); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param bytes byte array |
|
||||||
* @param offset offset where double is |
|
||||||
* @return Return double made from passed bytes. |
|
||||||
*/ |
|
||||||
public static double toDouble(final byte [] bytes, final int offset) { |
|
||||||
return Double.longBitsToDouble(toLong(bytes, offset, SIZEOF_LONG)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param bytes byte array |
|
||||||
* @param offset offset to write to |
|
||||||
* @param d value |
|
||||||
* @return New offset into array <code>bytes</code> |
|
||||||
*/ |
|
||||||
public static int putDouble(byte [] bytes, int offset, double d) { |
|
||||||
return putLong(bytes, offset, Double.doubleToLongBits(d)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Serialize a double as the IEEE 754 double format output. The resultant |
|
||||||
* array will be 8 bytes long. |
|
||||||
* |
|
||||||
* @param d value |
|
||||||
* @return the double represented as byte [] |
|
||||||
*/ |
|
||||||
public static byte [] toBytes(final double d) { |
|
||||||
// Encode it as a long
|
|
||||||
return Bytes.toBytes(Double.doubleToRawLongBits(d)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Convert an int value to a byte array |
|
||||||
* @param val value |
|
||||||
* @return the byte array |
|
||||||
*/ |
|
||||||
public static byte[] toBytes(int val) { |
|
||||||
byte [] b = new byte[4]; |
|
||||||
for(int i = 3; i > 0; i--) { |
|
||||||
b[i] = (byte) val; |
|
||||||
val >>>= 8; |
|
||||||
} |
|
||||||
b[0] = (byte) val; |
|
||||||
return b; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to an int value |
|
||||||
* @param bytes byte array |
|
||||||
* @return the int value |
|
||||||
*/ |
|
||||||
public static int toInt(byte[] bytes) { |
|
||||||
return toInt(bytes, 0, SIZEOF_INT); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to an int value |
|
||||||
* @param bytes byte array |
|
||||||
* @param offset offset into array |
|
||||||
* @return the int value |
|
||||||
*/ |
|
||||||
public static int toInt(byte[] bytes, int offset) { |
|
||||||
return toInt(bytes, offset, SIZEOF_INT); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to an int value |
|
||||||
* @param bytes byte array |
|
||||||
* @param offset offset into array |
|
||||||
* @param length length of int (has to be {@link #SIZEOF_INT}) |
|
||||||
* @return the int value |
|
||||||
* @throws IllegalArgumentException if length is not {@link #SIZEOF_INT} or |
|
||||||
* if there's not enough room in the array at the offset indicated. |
|
||||||
*/ |
|
||||||
public static int toInt(byte[] bytes, int offset, final int length) { |
|
||||||
if (length != SIZEOF_INT || offset + length > bytes.length) { |
|
||||||
throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_INT); |
|
||||||
} |
|
||||||
int n = 0; |
|
||||||
for(int i = offset; i < (offset + length); i++) { |
|
||||||
n <<= 8; |
|
||||||
n ^= bytes[i] & 0xFF; |
|
||||||
} |
|
||||||
return n; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Put an int value out to the specified byte array position. |
|
||||||
* @param bytes the byte array |
|
||||||
* @param offset position in the array |
|
||||||
* @param val int to write out |
|
||||||
* @return incremented offset |
|
||||||
* @throws IllegalArgumentException if the byte array given doesn't have |
|
||||||
* enough room at the offset specified. |
|
||||||
*/ |
|
||||||
public static int putInt(byte[] bytes, int offset, int val) { |
|
||||||
if (bytes.length - offset < SIZEOF_INT) { |
|
||||||
throw new IllegalArgumentException("Not enough room to put an int at" |
|
||||||
+ " offset " + offset + " in a " + bytes.length + " byte array"); |
|
||||||
} |
|
||||||
for(int i= offset + 3; i > offset; i--) { |
|
||||||
bytes[i] = (byte) val; |
|
||||||
val >>>= 8; |
|
||||||
} |
|
||||||
bytes[offset] = (byte) val; |
|
||||||
return offset + SIZEOF_INT; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Convert a short value to a byte array of {@link #SIZEOF_SHORT} bytes long. |
|
||||||
* @param val value |
|
||||||
* @return the byte array |
|
||||||
*/ |
|
||||||
public static byte[] toBytes(short val) { |
|
||||||
byte[] b = new byte[SIZEOF_SHORT]; |
|
||||||
b[1] = (byte) val; |
|
||||||
val >>= 8; |
|
||||||
b[0] = (byte) val; |
|
||||||
return b; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to a short value |
|
||||||
* @param bytes byte array |
|
||||||
* @return the short value |
|
||||||
*/ |
|
||||||
public static short toShort(byte[] bytes) { |
|
||||||
return toShort(bytes, 0, SIZEOF_SHORT); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to a short value |
|
||||||
* @param bytes byte array |
|
||||||
* @param offset offset into array |
|
||||||
* @return the short value |
|
||||||
*/ |
|
||||||
public static short toShort(byte[] bytes, int offset) { |
|
||||||
return toShort(bytes, offset, SIZEOF_SHORT); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to a short value |
|
||||||
* @param bytes byte array |
|
||||||
* @param offset offset into array |
|
||||||
* @param length length, has to be {@link #SIZEOF_SHORT} |
|
||||||
* @return the short value |
|
||||||
* @throws IllegalArgumentException if length is not {@link #SIZEOF_SHORT} |
|
||||||
* or if there's not enough room in the array at the offset indicated. |
|
||||||
*/ |
|
||||||
public static short toShort(byte[] bytes, int offset, final int length) { |
|
||||||
if (length != SIZEOF_SHORT || offset + length > bytes.length) { |
|
||||||
throw explainWrongLengthOrOffset(bytes, offset, length, SIZEOF_SHORT); |
|
||||||
} |
|
||||||
short n = 0; |
|
||||||
n ^= bytes[offset] & 0xFF; |
|
||||||
n <<= 8; |
|
||||||
n ^= bytes[offset+1] & 0xFF; |
|
||||||
return n; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* This method will get a sequence of bytes from pos to limit, |
|
||||||
* but will restore pos after. |
|
||||||
* @param buf buffer |
|
||||||
* @return byte array |
|
||||||
*/ |
|
||||||
public static byte[] getBytes(ByteBuffer buf) { |
|
||||||
int savedPos = buf.position(); |
|
||||||
byte [] newBytes = new byte[buf.remaining()]; |
|
||||||
buf.get(newBytes); |
|
||||||
buf.position(savedPos); |
|
||||||
return newBytes; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Put a short value out to the specified byte array position. |
|
||||||
* @param bytes the byte array |
|
||||||
* @param offset position in the array |
|
||||||
* @param val short to write out |
|
||||||
* @return incremented offset |
|
||||||
* @throws IllegalArgumentException if the byte array given doesn't have |
|
||||||
* enough room at the offset specified. |
|
||||||
*/ |
|
||||||
public static int putShort(byte[] bytes, int offset, short val) { |
|
||||||
if (bytes.length - offset < SIZEOF_SHORT) { |
|
||||||
throw new IllegalArgumentException("Not enough room to put a short at" |
|
||||||
+ " offset " + offset + " in a " + bytes.length + " byte array"); |
|
||||||
} |
|
||||||
bytes[offset+1] = (byte) val; |
|
||||||
val >>= 8; |
|
||||||
bytes[offset] = (byte) val; |
|
||||||
return offset + SIZEOF_SHORT; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Convert a BigDecimal value to a byte array |
|
||||||
* |
|
||||||
* @param val value |
|
||||||
* @return the byte array |
|
||||||
*/ |
|
||||||
public static byte[] toBytes(BigDecimal val) { |
|
||||||
byte[] valueBytes = val.unscaledValue().toByteArray(); |
|
||||||
byte[] result = new byte[valueBytes.length + SIZEOF_INT]; |
|
||||||
int offset = putInt(result, 0, val.scale()); |
|
||||||
putBytes(result, offset, valueBytes, 0, valueBytes.length); |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to a BigDecimal |
|
||||||
* |
|
||||||
* @param bytes bytes array |
|
||||||
* @return the char value |
|
||||||
*/ |
|
||||||
public static BigDecimal toBigDecimal(byte[] bytes) { |
|
||||||
return toBigDecimal(bytes, 0, bytes.length); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Converts a byte array to a BigDecimal value |
|
||||||
* |
|
||||||
* @param bytes bytes array |
|
||||||
* @param offset offset |
|
||||||
* @param length length |
|
||||||
* @return the char value |
|
||||||
*/ |
|
||||||
public static BigDecimal toBigDecimal(byte[] bytes, int offset, final int length) { |
|
||||||
if (bytes == null || length < SIZEOF_INT + 1 || |
|
||||||
(offset + length > bytes.length)) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
int scale = toInt(bytes, offset); |
|
||||||
byte[] tcBytes = new byte[length - SIZEOF_INT]; |
|
||||||
System.arraycopy(bytes, offset + SIZEOF_INT, tcBytes, 0, length - SIZEOF_INT); |
|
||||||
return new BigDecimal(new BigInteger(tcBytes), scale); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Put a BigDecimal value out to the specified byte array position. |
|
||||||
* |
|
||||||
* @param bytes the byte array |
|
||||||
* @param offset position in the array |
|
||||||
* @param val BigDecimal to write out |
|
||||||
* @return incremented offset |
|
||||||
*/ |
|
||||||
public static int putBigDecimal(byte[] bytes, int offset, BigDecimal val) { |
|
||||||
if (bytes == null) { |
|
||||||
return offset; |
|
||||||
} |
|
||||||
|
|
||||||
byte[] valueBytes = val.unscaledValue().toByteArray(); |
|
||||||
byte[] result = new byte[valueBytes.length + SIZEOF_INT]; |
|
||||||
offset = putInt(result, offset, val.scale()); |
|
||||||
return putBytes(result, offset, valueBytes, 0, valueBytes.length); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param a lower half |
|
||||||
* @param b upper half |
|
||||||
* @return New array that has a in lower half and b in upper half. |
|
||||||
*/ |
|
||||||
public static byte [] add(final byte [] a, final byte [] b) { |
|
||||||
return add(a, b, EMPTY_BYTE_ARRAY); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param a first third |
|
||||||
* @param b second third |
|
||||||
* @param c third third |
|
||||||
* @return New array made from a, b and c |
|
||||||
*/ |
|
||||||
public static byte [] add(final byte [] a, final byte [] b, final byte [] c) { |
|
||||||
byte [] result = new byte[a.length + b.length + c.length]; |
|
||||||
System.arraycopy(a, 0, result, 0, a.length); |
|
||||||
System.arraycopy(b, 0, result, a.length, b.length); |
|
||||||
System.arraycopy(c, 0, result, a.length + b.length, c.length); |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param a array |
|
||||||
* @param length amount of bytes to grab |
|
||||||
* @return First <code>length</code> bytes from <code>a</code> |
|
||||||
*/ |
|
||||||
public static byte [] head(final byte [] a, final int length) { |
|
||||||
if (a.length < length) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
byte [] result = new byte[length]; |
|
||||||
System.arraycopy(a, 0, result, 0, length); |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param a array |
|
||||||
* @param length amount of bytes to snarf |
|
||||||
* @return Last <code>length</code> bytes from <code>a</code> |
|
||||||
*/ |
|
||||||
public static byte [] tail(final byte [] a, final int length) { |
|
||||||
if (a.length < length) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
byte [] result = new byte[length]; |
|
||||||
System.arraycopy(a, a.length - length, result, 0, length); |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param a array |
|
||||||
* @param length new array size |
|
||||||
* @return Value in <code>a</code> plus <code>length</code> prepended 0 bytes |
|
||||||
*/ |
|
||||||
public static byte [] padHead(final byte [] a, final int length) { |
|
||||||
byte[] padding = getPadding(length); |
|
||||||
return add(padding,a); |
|
||||||
} |
|
||||||
|
|
||||||
private static byte[] getPadding(int length) { |
|
||||||
byte[] padding = new byte[length]; |
|
||||||
for (int i = 0; i < length; i++) { |
|
||||||
padding[i] = 0; |
|
||||||
} |
|
||||||
return padding; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param a array |
|
||||||
* @param length new array size |
|
||||||
* @return Value in <code>a</code> plus <code>length</code> appended 0 bytes |
|
||||||
*/ |
|
||||||
public static byte [] padTail(final byte [] a, final int length) { |
|
||||||
byte[] padding = getPadding(length); |
|
||||||
return add(a,padding); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* @param bytes array to hash |
|
||||||
* @param offset offset to start from |
|
||||||
* @param length length to hash |
|
||||||
* @return hash code |
|
||||||
* */ |
|
||||||
public static int hashCode(byte[] bytes, int offset, int length) { |
|
||||||
int hash = 1; |
|
||||||
for (int i = offset; i < offset + length; i++) { |
|
||||||
hash = (31 * hash) + (int) bytes[i]; |
|
||||||
} |
|
||||||
return hash; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param t operands |
|
||||||
* @return Array of byte arrays made from passed array of Text |
|
||||||
*/ |
|
||||||
public static byte [][] toByteArrays(final String [] t) { |
|
||||||
byte [][] result = new byte[t.length][]; |
|
||||||
for (int i = 0; i < t.length; i++) { |
|
||||||
result[i] = Bytes.toBytes(t[i]); |
|
||||||
} |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param column operand |
|
||||||
* @return A byte array of a byte array where first and only entry is |
|
||||||
* <code>column</code> |
|
||||||
*/ |
|
||||||
public static byte [][] toByteArrays(final String column) { |
|
||||||
return toByteArrays(toBytes(column)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @param column operand |
|
||||||
* @return A byte array of a byte array where first and only entry is |
|
||||||
* <code>column</code> |
|
||||||
*/ |
|
||||||
public static byte [][] toByteArrays(final byte [] column) { |
|
||||||
byte [][] result = new byte[1][]; |
|
||||||
result[0] = column; |
|
||||||
return result; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,33 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.common.utils; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class EnumUtils { |
||||||
|
|
||||||
|
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) { |
||||||
|
if (enumName == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
try { |
||||||
|
return Enum.valueOf(enumClass, enumName); |
||||||
|
} catch (final IllegalArgumentException ex) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,125 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.common.utils; |
||||||
|
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets; |
||||||
|
import java.util.regex.Pattern; |
||||||
|
|
||||||
|
|
||||||
|
public class StringUtils { |
||||||
|
|
||||||
|
public static final int INDEX_NOT_FOUND = -1; |
||||||
|
|
||||||
|
public static final String EMPTY = ""; |
||||||
|
|
||||||
|
public static boolean isEmpty(final CharSequence cs) { |
||||||
|
return cs == null || cs.length() == 0; |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isNotEmpty(final CharSequence cs) { |
||||||
|
return !isEmpty(cs); |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isBlank(CharSequence cs){ |
||||||
|
int strLen; |
||||||
|
if (cs == null || (strLen = cs.length()) == 0) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
for (int i = 0; i < strLen; i++) { |
||||||
|
if (Character.isWhitespace(cs.charAt(i)) == false) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isNotBlank(CharSequence str){ |
||||||
|
return !isBlank(str); |
||||||
|
} |
||||||
|
|
||||||
|
public static String substringBefore(final String str, final String separator) { |
||||||
|
if (isBlank(str) || separator == null) { |
||||||
|
return str; |
||||||
|
} |
||||||
|
if (separator.isEmpty()) { |
||||||
|
return EMPTY; |
||||||
|
} |
||||||
|
final int pos = str.indexOf(separator); |
||||||
|
if (pos == INDEX_NOT_FOUND) { |
||||||
|
return str; |
||||||
|
} |
||||||
|
return str.substring(0, pos); |
||||||
|
} |
||||||
|
|
||||||
|
public static String substringAfter(final String str, final String separator) { |
||||||
|
if (isBlank(str)) { |
||||||
|
return str; |
||||||
|
} |
||||||
|
if (separator == null) { |
||||||
|
return EMPTY; |
||||||
|
} |
||||||
|
final int pos = str.indexOf(separator); |
||||||
|
if (pos == INDEX_NOT_FOUND) { |
||||||
|
return EMPTY; |
||||||
|
} |
||||||
|
return str.substring(pos + separator.length()); |
||||||
|
} |
||||||
|
|
||||||
|
public static String substringAfterLast(final String str, final String separator) { |
||||||
|
if (isEmpty(str)) { |
||||||
|
return str; |
||||||
|
} |
||||||
|
if (isEmpty(separator)) { |
||||||
|
return EMPTY; |
||||||
|
} |
||||||
|
final int pos = str.lastIndexOf(separator); |
||||||
|
if (pos == INDEX_NOT_FOUND || pos == str.length() - separator.length()) { |
||||||
|
return EMPTY; |
||||||
|
} |
||||||
|
return str.substring(pos + separator.length()); |
||||||
|
} |
||||||
|
|
||||||
|
public static String getUtf8String(byte[] bytes){ |
||||||
|
return new String(bytes, StandardCharsets.UTF_8); |
||||||
|
} |
||||||
|
|
||||||
|
public static byte[] getUtf8Bytes(String str){ |
||||||
|
return str.getBytes(StandardCharsets.UTF_8); |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean hasChinese(String str) { |
||||||
|
if (str == null) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+"); |
||||||
|
return pattern.matcher(str).find(); |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean hasSpace(String str) { |
||||||
|
if (str == null) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
int len = str.length(); |
||||||
|
for (int i = 0; i < len; i++) { |
||||||
|
if (str.charAt(i) == ' ') { |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
@ -1,35 +0,0 @@ |
|||||||
# |
|
||||||
# Licensed to the Apache Software Foundation (ASF) under one or more |
|
||||||
# contributor license agreements. See the NOTICE file distributed with |
|
||||||
# this work for additional information regarding copyright ownership. |
|
||||||
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
||||||
# (the "License"); you may not use this file except in compliance with |
|
||||||
# the License. You may obtain a copy of the License at |
|
||||||
# |
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0 |
|
||||||
# |
|
||||||
# Unless required by applicable law or agreed to in writing, software |
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
# See the License for the specific language governing permissions and |
|
||||||
# limitations under the License. |
|
||||||
# |
|
||||||
|
|
||||||
# ha or single namenode,If namenode ha needs to copy core-site.xml and hdfs-site.xml |
|
||||||
# to the conf directory,support s3,for example : s3a://dolphinscheduler |
|
||||||
fs.defaultFS=hdfs://mycluster:8020 |
|
||||||
|
|
||||||
# s3 need,s3 endpoint |
|
||||||
fs.s3a.endpoint=http://192.168.199.91:9010 |
|
||||||
|
|
||||||
# s3 need,s3 access key |
|
||||||
fs.s3a.access.key=A3DXS30FO22544RE |
|
||||||
|
|
||||||
# s3 need,s3 secret key |
|
||||||
fs.s3a.secret.key=OloCLq3n+8+sdPHUhJ21XrSxTC+JK |
|
||||||
|
|
||||||
#resourcemanager ha note this need ips , this empty if single |
|
||||||
yarn.resourcemanager.ha.rm.ids=192.168.xx.xx,192.168.xx.xx |
|
||||||
|
|
||||||
# If it is a single resourcemanager, you only need to configure one host name. If it is resourcemanager HA, the default configuration is fine |
|
||||||
yarn.application.status.address=http://ark1:8088/ws/v1/cluster/apps/%s |
|
@ -1,43 +0,0 @@ |
|||||||
# |
|
||||||
# Licensed to the Apache Software Foundation (ASF) under one or more |
|
||||||
# contributor license agreements. See the NOTICE file distributed with |
|
||||||
# this work for additional information regarding copyright ownership. |
|
||||||
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
||||||
# (the "License"); you may not use this file except in compliance with |
|
||||||
# the License. You may obtain a copy of the License at |
|
||||||
# |
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0 |
|
||||||
# |
|
||||||
# Unless required by applicable law or agreed to in writing, software |
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
# See the License for the specific language governing permissions and |
|
||||||
# limitations under the License. |
|
||||||
# |
|
||||||
|
|
||||||
#zookeeper cluster. multiple are separated by commas. eg. 192.168.xx.xx:2181,192.168.xx.xx:2181,192.168.xx.xx:2181 |
|
||||||
zookeeper.quorum=localhost:2181 |
|
||||||
|
|
||||||
#dolphinscheduler root directory |
|
||||||
zookeeper.dolphinscheduler.root=/dolphinscheduler |
|
||||||
|
|
||||||
#zookeeper server dirctory |
|
||||||
#zookeeper.dolphinscheduler.dead.servers=/dolphinscheduler/dead-servers |
|
||||||
#zookeeper.dolphinscheduler.masters=/dolphinscheduler/masters |
|
||||||
#zookeeper.dolphinscheduler.workers=/dolphinscheduler/workers |
|
||||||
|
|
||||||
#zookeeper lock dirctory |
|
||||||
#zookeeper.dolphinscheduler.lock.masters=/dolphinscheduler/lock/masters |
|
||||||
#zookeeper.dolphinscheduler.lock.workers=/dolphinscheduler/lock/workers |
|
||||||
|
|
||||||
#dolphinscheduler failover directory |
|
||||||
#zookeeper.dolphinscheduler.lock.failover.masters=/dolphinscheduler/lock/failover/masters |
|
||||||
#zookeeper.dolphinscheduler.lock.failover.workers=/dolphinscheduler/lock/failover/workers |
|
||||||
#zookeeper.dolphinscheduler.lock.failover.startup.masters=/dolphinscheduler/lock/failover/startup-masters |
|
||||||
|
|
||||||
#dolphinscheduler failover directory |
|
||||||
zookeeper.session.timeout=300 |
|
||||||
zookeeper.connection.timeout=300 |
|
||||||
zookeeper.retry.base.sleep=100 |
|
||||||
zookeeper.retry.max.sleep=30000 |
|
||||||
zookeeper.retry.maxtime=5 |
|
@ -0,0 +1,190 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.common.threadutils; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.common.thread.Stopper; |
||||||
|
import org.apache.dolphinscheduler.common.thread.ThreadPoolExecutors; |
||||||
|
import org.apache.dolphinscheduler.common.thread.ThreadUtils; |
||||||
|
import org.junit.Test; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.util.Calendar; |
||||||
|
import java.util.concurrent.*; |
||||||
|
|
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
|
||||||
|
public class ThreadUtilsTest { |
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ThreadUtilsTest.class); |
||||||
|
/** |
||||||
|
* create a naming thread |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testNewDaemonFixedThreadExecutor() { |
||||||
|
// create core size and max size are all 3
|
||||||
|
ExecutorService testExec = ThreadUtils.newDaemonFixedThreadExecutor("test-exec-thread",10); |
||||||
|
|
||||||
|
for (int i = 0; i < 19; i++) { |
||||||
|
final int index = i; |
||||||
|
testExec.submit(() -> { |
||||||
|
System.out.println("do some work index " + index); |
||||||
|
}); |
||||||
|
} |
||||||
|
assertFalse(testExec.isShutdown()); |
||||||
|
testExec.shutdownNow(); |
||||||
|
assertTrue(testExec.isShutdown()); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* test schedulerThreadExecutor as for print time in scheduler |
||||||
|
* default check thread is 1 |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testNewDaemonScheduleThreadExecutor() { |
||||||
|
|
||||||
|
ScheduledExecutorService scheduleService = ThreadUtils.newDaemonThreadScheduledExecutor("scheduler-thread", 1); |
||||||
|
Calendar start = Calendar.getInstance(); |
||||||
|
Calendar globalTimer = Calendar.getInstance(); |
||||||
|
globalTimer.set(2019, Calendar.DECEMBER, 1, 0, 0, 0); |
||||||
|
// current
|
||||||
|
Calendar end = Calendar.getInstance(); |
||||||
|
end.set(2019, Calendar.DECEMBER, 1, 0, 0, 3); |
||||||
|
Runnable schedulerTask = new Runnable() { |
||||||
|
@Override |
||||||
|
public void run() { |
||||||
|
start.set(2019, Calendar.DECEMBER, 1, 0, 0, 0); |
||||||
|
int index = 0; |
||||||
|
// send heart beat work
|
||||||
|
while (start.getTime().getTime() <= end.getTime().getTime()) { |
||||||
|
System.out.println("worker here"); |
||||||
|
System.out.println(index ++); |
||||||
|
start.add(Calendar.SECOND, 1); |
||||||
|
globalTimer.add(Calendar.SECOND, 1); |
||||||
|
} |
||||||
|
System.out.println("time is " + System.currentTimeMillis()); |
||||||
|
} |
||||||
|
}; |
||||||
|
scheduleService.scheduleAtFixedRate(schedulerTask, 2, 10, TimeUnit.SECONDS); |
||||||
|
assertFalse(scheduleService.isShutdown()); |
||||||
|
try { |
||||||
|
Thread.sleep(60000); |
||||||
|
} catch (InterruptedException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
scheduleService.shutdownNow(); |
||||||
|
assertTrue(scheduleService.isShutdown()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* test stopper is working normal |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testStopper() { |
||||||
|
assertTrue(Stopper.isRunning()); |
||||||
|
Stopper.stop(); |
||||||
|
assertTrue(Stopper.isStoped()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* test threadPoolExecutors with 3 workers and current each 5 tasks |
||||||
|
* @throws InterruptedException |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testThreadInfo() throws InterruptedException { |
||||||
|
ThreadPoolExecutors workers = ThreadPoolExecutors.getInstance("worker", 3); |
||||||
|
for (int i = 0; i < 5; ++i ) { |
||||||
|
int index = i; |
||||||
|
workers.execute(() -> { |
||||||
|
for (int j = 0; j < 10; ++j) { |
||||||
|
try { |
||||||
|
Thread.sleep(1000); |
||||||
|
System.out.printf("worker %d is doing the task", index); |
||||||
|
System.out.println(); |
||||||
|
workers.printStatus(); |
||||||
|
} catch (InterruptedException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
workers.submit(() -> { |
||||||
|
for (int j = 0; j < 10; ++j) { |
||||||
|
try { |
||||||
|
Thread.sleep(1000); |
||||||
|
System.out.printf("worker_2 %d is doing the task", index); |
||||||
|
System.out.println(); |
||||||
|
workers.printStatus(); |
||||||
|
} catch (InterruptedException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
Thread.sleep(50001); |
||||||
|
workers.shutdown(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* test a single daemon thread pool |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testNewDaemonSingleThreadExecutor() { |
||||||
|
ExecutorService threadTest = ThreadUtils.newDaemonSingleThreadExecutor("thread_test"); |
||||||
|
threadTest.execute(() -> { |
||||||
|
for (int i = 0; i < 100; ++i) { |
||||||
|
System.out.println("daemon working "); |
||||||
|
} |
||||||
|
|
||||||
|
}); |
||||||
|
assertFalse(threadTest.isShutdown()); |
||||||
|
threadTest.shutdownNow(); |
||||||
|
assertTrue(threadTest.isShutdown()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testNewDaemonCachedThreadPool() { |
||||||
|
|
||||||
|
ThreadPoolExecutor threadPoolExecutor = ThreadUtils.newDaemonCachedThreadPool("threadTest-"); |
||||||
|
Thread thread1 = threadPoolExecutor.getThreadFactory().newThread(() -> { |
||||||
|
for (int i = 0; i < 10; ++i) { |
||||||
|
System.out.println("this task is with index " + i ); |
||||||
|
} |
||||||
|
}); |
||||||
|
assertTrue(thread1.getName().startsWith("threadTest-")); |
||||||
|
assertFalse(threadPoolExecutor.isShutdown()); |
||||||
|
threadPoolExecutor.shutdown(); |
||||||
|
assertTrue(threadPoolExecutor.isShutdown()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testNewDaemonCachedThreadPoolWithThreadNumber() { |
||||||
|
ThreadPoolExecutor threadPoolExecutor = ThreadUtils.newDaemonCachedThreadPool("threadTest--", 3, 10); |
||||||
|
for (int i = 0; i < 10; ++ i) { |
||||||
|
threadPoolExecutor.getThreadFactory().newThread(() -> { |
||||||
|
assertEquals(3, threadPoolExecutor.getActiveCount()); |
||||||
|
System.out.println("this task is first work to do"); |
||||||
|
}); |
||||||
|
} |
||||||
|
assertFalse(threadPoolExecutor.isShutdown()); |
||||||
|
threadPoolExecutor.shutdown(); |
||||||
|
assertTrue(threadPoolExecutor.isShutdown()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,119 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.common.utils; |
||||||
|
|
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.Mockito; |
||||||
|
import org.powermock.api.mockito.PowerMockito; |
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest; |
||||||
|
import org.powermock.modules.junit4.PowerMockRunner; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.FileNotFoundException; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
@RunWith(PowerMockRunner.class) |
||||||
|
@PrepareForTest({ LoggerFactory.class, FileUtils.class }) |
||||||
|
public class SchemaUtilsTest { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testReplaceBlank() { |
||||||
|
Assert.assertEquals("abc", SchemaUtils.replaceBlank(" abc")); |
||||||
|
Assert.assertEquals("abc", SchemaUtils.replaceBlank("abc ")); |
||||||
|
Assert.assertEquals("abc", SchemaUtils.replaceBlank("a b c")); |
||||||
|
Assert.assertEquals("abc", SchemaUtils.replaceBlank("a b c")); |
||||||
|
Assert.assertEquals("", SchemaUtils.replaceBlank(" ")); |
||||||
|
Assert.assertEquals("", SchemaUtils.replaceBlank(null)); |
||||||
|
Assert.assertEquals("我怕的你", SchemaUtils.replaceBlank("我怕的 你")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testGetSoftVersion() { |
||||||
|
// file not found
|
||||||
|
try { |
||||||
|
SchemaUtils.getSoftVersion(); |
||||||
|
} catch (RuntimeException e) { |
||||||
|
Assert.assertEquals("Failed to get the product version description file. The file could not be found", |
||||||
|
e.getMessage()); |
||||||
|
} |
||||||
|
|
||||||
|
// file exists, fmt is invalid
|
||||||
|
FileUtils.writeContent2File("32432423", "sql/soft_version"); |
||||||
|
Assert.assertEquals("32432423", SchemaUtils.getSoftVersion()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testIsAGreatVersion() { |
||||||
|
// param is null
|
||||||
|
try { |
||||||
|
SchemaUtils.isAGreatVersion(null, null); |
||||||
|
} catch (RuntimeException e) { |
||||||
|
Assert.assertEquals("schemaVersion or version is empty", e.getMessage()); |
||||||
|
} |
||||||
|
|
||||||
|
// param is ""
|
||||||
|
try { |
||||||
|
SchemaUtils.isAGreatVersion("", ""); |
||||||
|
} catch (RuntimeException e) { |
||||||
|
Assert.assertEquals("schemaVersion or version is empty", e.getMessage()); |
||||||
|
} |
||||||
|
Assert.assertFalse(SchemaUtils.isAGreatVersion("1", "1")); |
||||||
|
Assert.assertTrue(SchemaUtils.isAGreatVersion("2", "1")); |
||||||
|
Assert.assertTrue(SchemaUtils.isAGreatVersion("1.1", "1")); |
||||||
|
Assert.assertTrue(SchemaUtils.isAGreatVersion("1.1", "1.0.1")); |
||||||
|
Assert.assertFalse(SchemaUtils.isAGreatVersion("1.1", "1.2")); |
||||||
|
Assert.assertTrue(SchemaUtils.isAGreatVersion("1.1.1", "1.1")); |
||||||
|
Assert.assertTrue(SchemaUtils.isAGreatVersion("10.1.1", "1.01.100")); |
||||||
|
try { |
||||||
|
SchemaUtils.isAGreatVersion("10.1.1", ".1"); |
||||||
|
} catch (Exception e) { |
||||||
|
Assert.assertNotNull(e); |
||||||
|
} |
||||||
|
try { |
||||||
|
SchemaUtils.isAGreatVersion("a.1.1", "b.1"); |
||||||
|
} catch (Exception e) { |
||||||
|
Assert.assertNotNull(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testGetAllSchemaList() { |
||||||
|
//normal
|
||||||
|
PowerMockito.mockStatic(FileUtils.class); |
||||||
|
File[] files = new File[4]; |
||||||
|
files[0] = new File("sql/upgrade/1.2.0_schema"); |
||||||
|
files[1] = new File("sql/upgrade/1.0.1_schema"); |
||||||
|
files[2] = new File("sql/upgrade/1.0.2_schema"); |
||||||
|
files[3] = new File("sql/upgrade/1.1.0_schema"); |
||||||
|
PowerMockito.when(FileUtils.getAllDir("sql/upgrade")).thenReturn(files); |
||||||
|
List<String> real = SchemaUtils.getAllSchemaList(); |
||||||
|
List<String> expect = Arrays.asList("1.0.1_schema", "1.0.2_schema", |
||||||
|
"1.1.0_schema", "1.2.0_schema"); |
||||||
|
Assert.assertTrue(CollectionUtils.isEqualCollection(real, expect)); |
||||||
|
|
||||||
|
//normal
|
||||||
|
files = new File[0]; |
||||||
|
PowerMockito.when(FileUtils.getAllDir("sql/upgrade")).thenReturn(files); |
||||||
|
real = SchemaUtils.getAllSchemaList(); |
||||||
|
Assert.assertNull(real); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,48 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.common.utils; |
||||||
|
|
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.Mock; |
||||||
|
import org.mockito.Mockito; |
||||||
|
import org.powermock.api.mockito.PowerMockito; |
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest; |
||||||
|
import org.powermock.modules.junit4.PowerMockRunner; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
@RunWith(PowerMockRunner.class) |
||||||
|
@PrepareForTest(LoggerFactory.class) |
||||||
|
public class TaskParametersUtilsTest { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testGetParameters() { |
||||||
|
Assert.assertNull(TaskParametersUtils.getParameters("xx", "ttt")); |
||||||
|
Assert.assertNull(TaskParametersUtils.getParameters("SHELL", "ttt")); |
||||||
|
Assert.assertNotNull(TaskParametersUtils.getParameters("SHELL", "{}")); |
||||||
|
Assert.assertNotNull(TaskParametersUtils.getParameters("SQL", "{}")); |
||||||
|
Assert.assertNotNull(TaskParametersUtils.getParameters("SUB_PROCESS", "{}")); |
||||||
|
Assert.assertNotNull(TaskParametersUtils.getParameters("PROCEDURE", "{}")); |
||||||
|
Assert.assertNotNull(TaskParametersUtils.getParameters("MR", "{}")); |
||||||
|
Assert.assertNotNull(TaskParametersUtils.getParameters("SPARK", "{}")); |
||||||
|
Assert.assertNotNull(TaskParametersUtils.getParameters("PYTHON", "{}")); |
||||||
|
Assert.assertNotNull(TaskParametersUtils.getParameters("DEPENDENT", "{}")); |
||||||
|
Assert.assertNotNull(TaskParametersUtils.getParameters("FLINK", "{}")); |
||||||
|
Assert.assertNotNull(TaskParametersUtils.getParameters("HTTP", "{}")); |
||||||
|
} |
||||||
|
} |
@ -1,18 +0,0 @@ |
|||||||
# |
|
||||||
# Licensed to the Apache Software Foundation (ASF) under one or more |
|
||||||
# contributor license agreements. See the NOTICE file distributed with |
|
||||||
# this work for additional information regarding copyright ownership. |
|
||||||
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
||||||
# (the "License"); you may not use this file except in compliance with |
|
||||||
# the License. You may obtain a copy of the License at |
|
||||||
# |
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0 |
|
||||||
# |
|
||||||
# Unless required by applicable law or agreed to in writing, software |
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
# See the License for the specific language governing permissions and |
|
||||||
# limitations under the License. |
|
||||||
# |
|
||||||
|
|
||||||
logging.config=classpath:master_logback.xml |
|
@ -1,18 +0,0 @@ |
|||||||
# |
|
||||||
# Licensed to the Apache Software Foundation (ASF) under one or more |
|
||||||
# contributor license agreements. See the NOTICE file distributed with |
|
||||||
# this work for additional information regarding copyright ownership. |
|
||||||
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
||||||
# (the "License"); you may not use this file except in compliance with |
|
||||||
# the License. You may obtain a copy of the License at |
|
||||||
# |
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0 |
|
||||||
# |
|
||||||
# Unless required by applicable law or agreed to in writing, software |
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, |
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||||
# See the License for the specific language governing permissions and |
|
||||||
# limitations under the License. |
|
||||||
# |
|
||||||
|
|
||||||
logging.config=classpath:worker_logback.xml |
|
@ -0,0 +1,135 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.apache.dolphinscheduler.server.utils; |
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON; |
||||||
|
import org.apache.dolphinscheduler.common.enums.CommandType; |
||||||
|
import org.apache.dolphinscheduler.common.enums.DataType; |
||||||
|
import org.apache.dolphinscheduler.common.enums.Direct; |
||||||
|
import org.apache.dolphinscheduler.common.process.Property; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.util.Calendar; |
||||||
|
import java.util.Date; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.assertNull; |
||||||
|
|
||||||
|
/** |
||||||
|
* Test ParamUtils |
||||||
|
*/ |
||||||
|
public class ParamUtilsTest { |
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ParamUtilsTest.class); |
||||||
|
|
||||||
|
//Define global variables
|
||||||
|
public Map<String, Property> globalParams = new HashMap<>(); |
||||||
|
|
||||||
|
public Map<String, String> globalParamsMap = new HashMap<>(); |
||||||
|
|
||||||
|
public Map<String, Property> localParams = new HashMap<>(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Init params |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
@Before |
||||||
|
public void setUp() throws Exception { |
||||||
|
|
||||||
|
Property property = new Property(); |
||||||
|
property.setProp("global_param"); |
||||||
|
property.setDirect(Direct.IN); |
||||||
|
property.setType(DataType.VARCHAR); |
||||||
|
property.setValue("${system.biz.date}"); |
||||||
|
globalParams.put("global_param", property); |
||||||
|
|
||||||
|
globalParamsMap.put("global_param", "${system.biz.date}"); |
||||||
|
|
||||||
|
Property localProperty = new Property(); |
||||||
|
localProperty.setProp("local_param"); |
||||||
|
localProperty.setDirect(Direct.IN); |
||||||
|
localProperty.setType(DataType.VARCHAR); |
||||||
|
localProperty.setValue("${global_param}"); |
||||||
|
localParams.put("local_param", localProperty); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Test convert |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testConvert() { |
||||||
|
|
||||||
|
//The expected value
|
||||||
|
String expected = "{\"global_param\":{\"direct\":\"IN\",\"prop\":\"global_param\",\"type\":\"VARCHAR\",\"value\":\"20191229\"},\"local_param\":{\"direct\":\"IN\",\"prop\":\"local_param\",\"type\":\"VARCHAR\",\"value\":\"20191229\"}}"; |
||||||
|
|
||||||
|
//The expected value when globalParams is null but localParams is not null
|
||||||
|
String expected1 = "{\"local_param\":{\"direct\":\"IN\",\"prop\":\"local_param\",\"type\":\"VARCHAR\",\"value\":\"20191229\"}}"; |
||||||
|
|
||||||
|
//Define expected date , the month is 0-base
|
||||||
|
Calendar calendar = Calendar.getInstance(); |
||||||
|
calendar.set(2019,11,30); |
||||||
|
Date date = calendar.getTime(); |
||||||
|
|
||||||
|
//Invoke convert
|
||||||
|
Map<String, Property> paramsMap = ParamUtils.convert(globalParams, globalParamsMap, localParams, CommandType.START_PROCESS, date); |
||||||
|
String result = JSON.toJSONString(paramsMap); |
||||||
|
assertEquals(expected, result); |
||||||
|
|
||||||
|
for (Map.Entry<String, Property> entry : paramsMap.entrySet()) { |
||||||
|
|
||||||
|
String key = entry.getKey(); |
||||||
|
Property prop = entry.getValue(); |
||||||
|
logger.info(key + " : " + prop.getValue()); |
||||||
|
} |
||||||
|
|
||||||
|
//Invoke convert with null globalParams
|
||||||
|
Map<String, Property> paramsMap1 = ParamUtils.convert(null, globalParamsMap, localParams, CommandType.START_PROCESS, date); |
||||||
|
String result1 = JSON.toJSONString(paramsMap1); |
||||||
|
assertEquals(expected1, result1); |
||||||
|
|
||||||
|
//Null check, invoke convert with null globalParams and null localParams
|
||||||
|
Map<String, Property> paramsMap2 = ParamUtils.convert(null, globalParamsMap, null, CommandType.START_PROCESS, date); |
||||||
|
assertNull(paramsMap2); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Test the overload method of convert |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testConvert1() { |
||||||
|
|
||||||
|
//The expected value
|
||||||
|
String expected = "{\"global_param\":\"${system.biz.date}\"}"; |
||||||
|
|
||||||
|
//Invoke convert
|
||||||
|
Map<String, String> paramsMap = ParamUtils.convert(globalParams); |
||||||
|
String result = JSON.toJSONString(paramsMap); |
||||||
|
assertEquals(expected, result); |
||||||
|
|
||||||
|
logger.info(result); |
||||||
|
|
||||||
|
//Null check
|
||||||
|
Map<String, String> paramsMap1 = ParamUtils.convert(null); |
||||||
|
assertNull(paramsMap1); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue