Browse Source
* change log level to debug * add unit test * update escheduler-dao unit test * update escheduler-api unit test * update escheduler-api unit testpull/2/head
dailidong
5 years ago
committed by
GitHub
40 changed files with 508 additions and 298 deletions
@ -0,0 +1,91 @@ |
|||||||
|
/* |
||||||
|
* 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 cn.escheduler.api.controller; |
||||||
|
|
||||||
|
import cn.escheduler.api.ApiApplicationServer; |
||||||
|
import cn.escheduler.api.enums.Status; |
||||||
|
import cn.escheduler.api.service.SessionService; |
||||||
|
import cn.escheduler.api.utils.Result; |
||||||
|
import cn.escheduler.common.enums.UserType; |
||||||
|
import cn.escheduler.common.utils.JSONUtils; |
||||||
|
import cn.escheduler.dao.model.User; |
||||||
|
import org.apache.commons.lang3.StringUtils; |
||||||
|
import org.junit.*; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||||
|
import org.springframework.http.MediaType; |
||||||
|
import org.springframework.test.context.junit4.SpringRunner; |
||||||
|
import org.springframework.test.web.servlet.MockMvc; |
||||||
|
import org.springframework.test.web.servlet.MvcResult; |
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
||||||
|
import org.springframework.util.LinkedMultiValueMap; |
||||||
|
import org.springframework.util.MultiValueMap; |
||||||
|
import org.springframework.web.context.WebApplicationContext; |
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
||||||
|
|
||||||
|
@RunWith(SpringRunner.class) |
||||||
|
@SpringBootTest(classes = ApiApplicationServer.class) |
||||||
|
public class AbstractControllerTest { |
||||||
|
private static Logger logger = LoggerFactory.getLogger(AbstractControllerTest.class); |
||||||
|
public static final String SESSION_ID = "sessionId"; |
||||||
|
|
||||||
|
protected MockMvc mockMvc; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private WebApplicationContext webApplicationContext; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private SessionService sessionService; |
||||||
|
|
||||||
|
protected User user; |
||||||
|
protected String sessionId; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() { |
||||||
|
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); |
||||||
|
createSession(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@After |
||||||
|
public void after(){ |
||||||
|
sessionService.signOut("127.0.0.1", user); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private void createSession(){ |
||||||
|
|
||||||
|
User loginUser = new User(); |
||||||
|
loginUser.setId(1); |
||||||
|
loginUser.setUserType(UserType.GENERAL_USER); |
||||||
|
|
||||||
|
user = loginUser; |
||||||
|
|
||||||
|
String session = sessionService.createSession(loginUser, "127.0.0.1"); |
||||||
|
sessionId = session; |
||||||
|
|
||||||
|
Assert.assertTrue(StringUtils.isNotEmpty(session)); |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,53 @@ |
|||||||
|
# base spring data source configuration |
||||||
|
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource |
||||||
|
spring.datasource.driver-class-name=com.mysql.jdbc.Driver |
||||||
|
spring.datasource.url=jdbc:mysql://192.168.10.32:3306/escheduler?characterEncoding=UTF-8 |
||||||
|
spring.datasource.username=root |
||||||
|
spring.datasource.password=root@123 |
||||||
|
|
||||||
|
# connection configuration |
||||||
|
spring.datasource.initialSize=5 |
||||||
|
# min connection number |
||||||
|
spring.datasource.minIdle=5 |
||||||
|
# max connection number |
||||||
|
spring.datasource.maxActive=50 |
||||||
|
|
||||||
|
# max wait time for get a connection in milliseconds. if configuring maxWait, fair locks are enabled by default and concurrency efficiency decreases. |
||||||
|
# If necessary, unfair locks can be used by configuring the useUnfairLock attribute to true. |
||||||
|
spring.datasource.maxWait=60000 |
||||||
|
|
||||||
|
# milliseconds for check to close free connections |
||||||
|
spring.datasource.timeBetweenEvictionRunsMillis=60000 |
||||||
|
|
||||||
|
# the Destroy thread detects the connection interval and closes the physical connection in milliseconds if the connection idle time is greater than or equal to minEvictableIdleTimeMillis. |
||||||
|
spring.datasource.timeBetweenConnectErrorMillis=60000 |
||||||
|
|
||||||
|
# the longest time a connection remains idle without being evicted, in milliseconds |
||||||
|
spring.datasource.minEvictableIdleTimeMillis=300000 |
||||||
|
|
||||||
|
#the SQL used to check whether the connection is valid requires a query statement. If validation Query is null, testOnBorrow, testOnReturn, and testWhileIdle will not work. |
||||||
|
spring.datasource.validationQuery=SELECT 1 |
||||||
|
#check whether the connection is valid for timeout, in seconds |
||||||
|
spring.datasource.validationQueryTimeout=3 |
||||||
|
|
||||||
|
# when applying for a connection, if it is detected that the connection is idle longer than time Between Eviction Runs Millis, |
||||||
|
# validation Query is performed to check whether the connection is valid |
||||||
|
spring.datasource.testWhileIdle=true |
||||||
|
|
||||||
|
#execute validation to check if the connection is valid when applying for a connection |
||||||
|
spring.datasource.testOnBorrow=true |
||||||
|
#execute validation to check if the connection is valid when the connection is returned |
||||||
|
spring.datasource.testOnReturn=false |
||||||
|
spring.datasource.defaultAutoCommit=true |
||||||
|
spring.datasource.keepAlive=true |
||||||
|
|
||||||
|
# open PSCache, specify count PSCache for every connection |
||||||
|
spring.datasource.poolPreparedStatements=true |
||||||
|
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 |
||||||
|
|
||||||
|
# data quality analysis is not currently in use. please ignore the following configuration |
||||||
|
# task record flag |
||||||
|
task.record.flag=false |
||||||
|
task.record.datasource.url=jdbc:mysql://192.168.xx.xx:3306/etl?characterEncoding=UTF-8 |
||||||
|
task.record.datasource.username=xx |
||||||
|
task.record.datasource.password=xx |
@ -0,0 +1,53 @@ |
|||||||
|
# base spring data source configuration |
||||||
|
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource |
||||||
|
spring.datasource.driver-class-name=com.mysql.jdbc.Driver |
||||||
|
spring.datasource.url=jdbc:mysql://192.168.10.32:3306/escheduler?characterEncoding=UTF-8 |
||||||
|
spring.datasource.username=root |
||||||
|
spring.datasource.password=root@123 |
||||||
|
|
||||||
|
# connection configuration |
||||||
|
spring.datasource.initialSize=5 |
||||||
|
# min connection number |
||||||
|
spring.datasource.minIdle=5 |
||||||
|
# max connection number |
||||||
|
spring.datasource.maxActive=50 |
||||||
|
|
||||||
|
# max wait time for get a connection in milliseconds. if configuring maxWait, fair locks are enabled by default and concurrency efficiency decreases. |
||||||
|
# If necessary, unfair locks can be used by configuring the useUnfairLock attribute to true. |
||||||
|
spring.datasource.maxWait=60000 |
||||||
|
|
||||||
|
# milliseconds for check to close free connections |
||||||
|
spring.datasource.timeBetweenEvictionRunsMillis=60000 |
||||||
|
|
||||||
|
# the Destroy thread detects the connection interval and closes the physical connection in milliseconds if the connection idle time is greater than or equal to minEvictableIdleTimeMillis. |
||||||
|
spring.datasource.timeBetweenConnectErrorMillis=60000 |
||||||
|
|
||||||
|
# the longest time a connection remains idle without being evicted, in milliseconds |
||||||
|
spring.datasource.minEvictableIdleTimeMillis=300000 |
||||||
|
|
||||||
|
#the SQL used to check whether the connection is valid requires a query statement. If validation Query is null, testOnBorrow, testOnReturn, and testWhileIdle will not work. |
||||||
|
spring.datasource.validationQuery=SELECT 1 |
||||||
|
#check whether the connection is valid for timeout, in seconds |
||||||
|
spring.datasource.validationQueryTimeout=3 |
||||||
|
|
||||||
|
# when applying for a connection, if it is detected that the connection is idle longer than time Between Eviction Runs Millis, |
||||||
|
# validation Query is performed to check whether the connection is valid |
||||||
|
spring.datasource.testWhileIdle=true |
||||||
|
|
||||||
|
#execute validation to check if the connection is valid when applying for a connection |
||||||
|
spring.datasource.testOnBorrow=true |
||||||
|
#execute validation to check if the connection is valid when the connection is returned |
||||||
|
spring.datasource.testOnReturn=false |
||||||
|
spring.datasource.defaultAutoCommit=true |
||||||
|
spring.datasource.keepAlive=true |
||||||
|
|
||||||
|
# open PSCache, specify count PSCache for every connection |
||||||
|
spring.datasource.poolPreparedStatements=true |
||||||
|
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 |
||||||
|
|
||||||
|
# data quality analysis is not currently in use. please ignore the following configuration |
||||||
|
# task record flag |
||||||
|
task.record.flag=false |
||||||
|
task.record.datasource.url=jdbc:mysql://192.168.xx.xx:3306/etl?characterEncoding=UTF-8 |
||||||
|
task.record.datasource.username=xx |
||||||
|
task.record.datasource.password=xx |
@ -0,0 +1,100 @@ |
|||||||
|
/* |
||||||
|
* 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 cn.escheduler.server.zk; |
||||||
|
|
||||||
|
import cn.escheduler.common.thread.ThreadPoolExecutors; |
||||||
|
import org.apache.zookeeper.server.ServerConfig; |
||||||
|
import org.apache.zookeeper.server.ZooKeeperServerMain; |
||||||
|
import org.apache.zookeeper.server.quorum.QuorumPeerConfig; |
||||||
|
import org.junit.Before; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.util.Properties; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* just for test |
||||||
|
*/ |
||||||
|
public class StandaloneZKServerForTest { |
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(StandaloneZKServerForTest.class); |
||||||
|
|
||||||
|
private static volatile ZooKeeperServerMain zkServer = null; |
||||||
|
|
||||||
|
|
||||||
|
@Before |
||||||
|
public void before() { |
||||||
|
logger.info("standalone zookeeper server for test service start "); |
||||||
|
|
||||||
|
ThreadPoolExecutors.getInstance().execute(new Runnable() { |
||||||
|
@Override |
||||||
|
public void run() { |
||||||
|
|
||||||
|
//delete zk data dir ?
|
||||||
|
File zkFile = new File(System.getProperty("java.io.tmpdir"), "zookeeper"); |
||||||
|
// if(zkFile.exists()){
|
||||||
|
// zkFile.delete();
|
||||||
|
// }
|
||||||
|
startStandaloneServer("2000", zkFile.getAbsolutePath(), "2181", "10", "5"); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* start zk server |
||||||
|
* @param tickTime zookeeper ticktime |
||||||
|
* @param dataDir zookeeper data dir |
||||||
|
* @param clientPort zookeeper client port |
||||||
|
* @param initLimit zookeeper init limit |
||||||
|
* @param syncLimit zookeeper sync limit |
||||||
|
*/ |
||||||
|
private void startStandaloneServer(String tickTime, String dataDir, String clientPort, String initLimit, String syncLimit) { |
||||||
|
Properties props = new Properties(); |
||||||
|
props.setProperty("tickTime", tickTime); |
||||||
|
props.setProperty("dataDir", dataDir); |
||||||
|
props.setProperty("clientPort", clientPort); |
||||||
|
props.setProperty("initLimit", initLimit); |
||||||
|
props.setProperty("syncLimit", syncLimit); |
||||||
|
|
||||||
|
QuorumPeerConfig quorumConfig = new QuorumPeerConfig(); |
||||||
|
try { |
||||||
|
quorumConfig.parseProperties(props); |
||||||
|
|
||||||
|
if(zkServer == null ){ |
||||||
|
|
||||||
|
synchronized (StandaloneZKServerForTest.class){ |
||||||
|
if(zkServer == null ){ |
||||||
|
zkServer = new ZooKeeperServerMain(); |
||||||
|
final ServerConfig config = new ServerConfig(); |
||||||
|
config.readFrom(quorumConfig); |
||||||
|
zkServer.runFromConfig(config); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} catch (Exception e) { |
||||||
|
logger.error("start standalone server fail!", e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue