|
|
@@ -0,0 +1,198 @@
|
|
|
+package cn.iselab.mooctest.site.service.fromKibug.impl;
|
|
|
+
|
|
|
+import cn.iselab.mooctest.site.common.enums.AppPlatform;
|
|
|
+import cn.iselab.mooctest.site.common.enums.AppStatus;
|
|
|
+import cn.iselab.mooctest.site.common.enums.AppTestType;
|
|
|
+import cn.iselab.mooctest.site.web.exception.IllegalOperationException;
|
|
|
+import cn.iselab.mooctest.site.dao.fromKibug.ApplicationDao;
|
|
|
+import cn.iselab.mooctest.site.dao.fromKibug.IncrementIdDao;
|
|
|
+import cn.iselab.mooctest.site.models.fromKibug.Application;
|
|
|
+import cn.iselab.mooctest.site.models.fromKibug.IncrementId;
|
|
|
+import cn.iselab.mooctest.site.models.fromKibug.LaunchData;
|
|
|
+import cn.iselab.mooctest.site.service.fromKibug.ApplicationService;
|
|
|
+import cn.iselab.mooctest.site.web.data.fromKibug.ApplicationVO;
|
|
|
+import cn.iselab.mooctest.site.web.data.fromKibug.AutoTestVO;
|
|
|
+import cn.iselab.mooctest.site.web.util.Converter;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
+import org.springframework.data.jpa.domain.Specifications;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
+import javax.persistence.criteria.CriteriaQuery;
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
+import javax.persistence.criteria.Root;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ApplicationServiceImpl implements ApplicationService {
|
|
|
+ @Autowired
|
|
|
+ private ApplicationDao applicationDao;
|
|
|
+ @Autowired
|
|
|
+ private IncrementIdDao incrementIdDao;
|
|
|
+
|
|
|
+ private ObjectMapper mapper = new ObjectMapper();
|
|
|
+
|
|
|
+ private static final long AUTO_TEST_APP_UPLOADED_ID = 0L;
|
|
|
+ private static final long MONTH_MILLIS = 30 * 24 * 60 * 60 * 1000;
|
|
|
+ private static final int LOCAL = 0;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Application> search(Long managerId, String keyword, AppPlatform platform, Integer status, int page, int count) throws Exception {
|
|
|
+ Pageable pageable=new PageRequest(page*count,count);
|
|
|
+ Specifications<Application> where = Specifications.where(getWhereClause(managerId, keyword, platform, status));
|
|
|
+ return applicationDao.findAll(where, pageable).getContent();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long createApp(ApplicationVO vo, int from) throws Exception {
|
|
|
+ IncrementId incrementId = incrementIdDao.save(new IncrementId());
|
|
|
+
|
|
|
+ Application application = Converter.convert(Application.class, vo);
|
|
|
+ application.setId(incrementId.getId());
|
|
|
+ application.setUploadedCaseId(vo.getUploadedCaseId());
|
|
|
+ application.setManagerId(vo.getManagerId());
|
|
|
+ application.setPlatform(AppPlatform.parse(vo.getPlatform()).getPlatformId());
|
|
|
+ application.setCreateTimeMillis(System.currentTimeMillis());
|
|
|
+ application.setFromCloud(from);
|
|
|
+ if (vo.getEndTimeMillis()==0) {
|
|
|
+ application.setEndTimeMillis(System.currentTimeMillis() + 2592000000l);
|
|
|
+ }
|
|
|
+ checkComplete(application);
|
|
|
+ applicationDao.save(application);
|
|
|
+ return incrementId.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long createAutoTestApp(AutoTestVO vo) throws Exception {
|
|
|
+ ApplicationVO applicationVo = new ApplicationVO();
|
|
|
+ applicationVo.setManagerId(vo.getManagerId());
|
|
|
+ applicationVo.setUploadedCaseId(AUTO_TEST_APP_UPLOADED_ID);
|
|
|
+ applicationVo.setName(vo.getName());
|
|
|
+ applicationVo.setAppLocation(vo.getAppLocation());
|
|
|
+ applicationVo.setIconLocation(vo.getIconLocation());
|
|
|
+ applicationVo.setPlatform(AppPlatform.ANDROID.toString());
|
|
|
+ applicationVo.setCategory("其他");
|
|
|
+ applicationVo.setEndTimeMillis(System.currentTimeMillis() + MONTH_MILLIS);
|
|
|
+ applicationVo.setTestType(AppTestType.AUTO.getType());
|
|
|
+ applicationVo.validate();
|
|
|
+ return createApp(applicationVo, LOCAL);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Application getAppById(long id) throws Exception {
|
|
|
+ Application application = applicationDao.findOne(id);
|
|
|
+ if (application == null) throw new IllegalOperationException();
|
|
|
+ return application;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateApp(long appId, ApplicationVO vo) throws Exception {
|
|
|
+ Application application = applicationDao.findOne(appId);
|
|
|
+ if (application != null) {
|
|
|
+ vo.setStatus(application.getStatus());//update app should not change application's status
|
|
|
+ Converter.copy(application, vo);
|
|
|
+ application.setPlatform(AppPlatform.parse(vo.getPlatform()).getPlatformId());
|
|
|
+ checkComplete(application);
|
|
|
+ applicationDao.save(application);
|
|
|
+ } else {
|
|
|
+ throw new IllegalOperationException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void autoTestApp(Long appId) throws Exception {
|
|
|
+ Application application = applicationDao.findOne(appId);
|
|
|
+ if (AppTestType.isAuto(application.getTestType())) {
|
|
|
+ application.setTestType((short) (application.getTestType() + 256));
|
|
|
+ applicationDao.save(application);
|
|
|
+ } else {
|
|
|
+ throw new IllegalOperationException("应用没有自动化测试权限");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> getCategories() throws Exception {
|
|
|
+ String[] categories = new String[]{"工具", "娱乐", "购物", "游戏", "教育",
|
|
|
+ "健康健美", "音乐", "新闻", "摄像与录影", "其他"};
|
|
|
+ return Arrays.asList(categories);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkComplete(Application app) {
|
|
|
+ AppPlatform appPlatform = AppPlatform.fromShort(app.getPlatform());
|
|
|
+ LaunchData launchData;
|
|
|
+ try {
|
|
|
+ launchData = mapper.readValue(app.getLaunchData(), LaunchData.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ launchData = new LaunchData();
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean incomplete = false;
|
|
|
+ boolean isAutoTest = AppTestType.isAuto(app.getTestType());
|
|
|
+ boolean isManualTest = AppTestType.isManual(app.getTestType());
|
|
|
+
|
|
|
+ if (isManualTest) {
|
|
|
+ //人工测试需要应用图标,应用安装文件,需求文件
|
|
|
+ incomplete = incomplete || org.springframework.util.StringUtils.isEmpty(app.getIconLocation()) || org.springframework.util.StringUtils.isEmpty(app.getAppLocation())
|
|
|
+ || org.springframework.util.StringUtils.isEmpty(app.getRequireLocation());
|
|
|
+ //如果是安卓的话,则还需要activity,package,driver
|
|
|
+ if (appPlatform == AppPlatform.ANDROID) {
|
|
|
+ incomplete = incomplete || org.springframework.util.StringUtils.isEmpty(launchData.getActivity()) || org.springframework.util.StringUtils.isEmpty(launchData.getPackageName())
|
|
|
+ || org.springframework.util.StringUtils.isEmpty(launchData.getDriver());
|
|
|
+ } else if (appPlatform == AppPlatform.IOS) {//ios需要schema
|
|
|
+ incomplete = incomplete || org.springframework.util.StringUtils.isEmpty(launchData.getSchema());
|
|
|
+ } else if (appPlatform == AppPlatform.JMETER) {//jmeter需要评分文件
|
|
|
+ incomplete = incomplete || org.springframework.util.StringUtils.isEmpty(launchData.getRules());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //如果只是自动测试的话,则只需要应用的安装文件和图标即可
|
|
|
+ if (!isManualTest && isAutoTest) {
|
|
|
+ incomplete = incomplete || org.springframework.util.StringUtils.isEmpty(app.getAppLocation()) || org.springframework.util.StringUtils.isEmpty(app.getIconLocation());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (incomplete) {
|
|
|
+ app.setStatus(AppStatus.NEW.getStatus());
|
|
|
+ } else {
|
|
|
+ app.setStatus(AppStatus.COMPLETE.getStatus());
|
|
|
+ if (isAutoTest) {
|
|
|
+ app.setTestType(AppTestType.setRequestAuto(app.getTestType()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Specification<Application> getWhereClause(Long managerId, String keyword, AppPlatform platform, Integer status) {
|
|
|
+ return new Specification<Application>() {
|
|
|
+ @Override
|
|
|
+ public Predicate toPredicate(Root<Application> a, CriteriaQuery<?> q, CriteriaBuilder cb) {
|
|
|
+ Predicate predicate = cb.conjunction();
|
|
|
+ if (managerId!=null) {
|
|
|
+ predicate.getExpressions().add(
|
|
|
+ cb.equal(a.<Long>get("managerId"), managerId)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if(keyword!=null) {
|
|
|
+ predicate.getExpressions().add(
|
|
|
+ cb.like(a.<String>get("name"), "%" + StringUtils.trim(keyword) + "%")
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if(platform!=null) {
|
|
|
+ predicate.getExpressions().add(
|
|
|
+ cb.equal(a.<Short>get("platform"), platform.getPlatformId())
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if(status!=null) {
|
|
|
+ predicate.getExpressions().add(
|
|
|
+ cb.equal(a.<Integer>get("status"), status)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return predicate;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+}
|