001package com.thed.model;
002
003import com.thed.agent.ZephyrAgent;
004import com.thed.util.ZeeConstants;
005import com.thed.zblast.parser.model.TCRCatalogTreeTestcase;
006import com.thed.zblast.util.RestUtil;
007import com.thed.zblast.util.ResultPoster;
008import com.thed.zblast.util.TestcaseUploader;
009
010import java.text.DateFormat;
011import java.text.SimpleDateFormat;
012import java.util.*;
013import java.util.concurrent.Callable;
014import java.util.concurrent.ExecutorService;
015import java.util.concurrent.Executors;
016import java.util.concurrent.Future;
017import java.util.logging.Logger;
018
019/**
020 * Created by Raviteja on 22-02-2018.
021 */
022public class QueueProcessor implements Callable<List<AutomationJobReport>> {
023
024    private Logger logger = Logger.getLogger(this.getClass().getName());
025    private Queue<AutomationJobDetail> jobQueue;
026    private Long userId;
027    private ExecutorService childThreads;
028
029    public Queue<AutomationJobDetail> getJobQueue() {
030        return jobQueue;
031    }
032
033    public void setJobQueue(Queue<AutomationJobDetail> jobQueue) {
034        this.jobQueue = jobQueue;
035    }
036
037    public QueueProcessor(Queue<AutomationJobDetail> jobQueue, Long userId) {
038        this.jobQueue=jobQueue;
039        this.userId=userId;
040        childThreads= Executors.newFixedThreadPool(5);
041    }
042
043    @Override
044    public List<AutomationJobReport> call() throws Exception {
045    SimpleDateFormat sdf = new SimpleDateFormat("E_dd MMM yyyy_HH:mm");
046    List<Future<AutomationJobReport>> futureList=new ArrayList<>();
047    List<AutomationJobReport> jobreportList=new ArrayList<>();
048    while(jobQueue.peek()!=null){
049        AutomationJobReport jobReport =new AutomationJobReport();
050        AutomationJobDetail job=jobQueue.poll();
051        Long scheduleId=0l;
052        if(job.getFromFileWatcher()) {
053            scheduleId = job.getId();
054        }else {
055            scheduleId = job.getScheduleId();
056        }
057        jobReport.setJobStart(new Date());
058        jobReport.setAutomationJob(job);
059        job.setCycleName(getCycleName(job,sdf));
060        job.setPhaseName(getPhaseName(job,sdf));
061        job.setCycleId(job.getCycleId());
062        job.setCyclePhaseName(job.getCyclePhaseName());
063        job.setCyclePhaseId(job.getCyclePhaseId());
064        job.setIsTimeStamp(false);
065        try {
066            RestUtil.updateStatus(scheduleId, ZeeConstants.ZBLAST_JOB_STATUS_INPROGRESS,job,0);
067            TestcaseUploader uploader=new TestcaseUploader();
068                        List<TCRCatalogTreeTestcase> testcases=uploader.uploadTestcases(job,userId,scheduleId);
069                        ZephyrConfigModel zephyrData=uploader.getZephyrData();
070            futureList.add(childThreads.submit(new ResultPoster(job,userId,testcases,zephyrData,jobReport)));
071        } catch (Exception e) {
072            logger.severe("Error while running automation job "+e.getMessage());
073        }
074    }
075    while(!futureList.isEmpty()){
076        ListIterator<Future<AutomationJobReport>> futureIterator=futureList.listIterator();
077        while (futureIterator.hasNext()){
078            Future<AutomationJobReport> future=futureIterator.next();
079            if(future.isDone()){
080                AutomationJobReport automationJobReport = future.get();
081               ZephyrAgent.updateFolderWatcher(automationJobReport);
082                jobreportList.add(automationJobReport);
083                futureIterator.remove();
084            }
085        }
086        futureIterator=futureList.listIterator();
087    }
088    childThreads.shutdownNow();
089    return jobreportList;
090    }
091
092    private String getCycleName(AutomationJobDetail job, DateFormat sdf) {
093        String cycleName=job.getCycleName();
094        if(job.getIsTimeStamp()){
095           String dateFormatForCycleCreation = sdf.format(new Date());
096           if(cycleName !=null && !cycleName.isEmpty()){
097                if(cycleName.length()>255){
098                    cycleName=cycleName.substring(0,cycleName.length()-(cycleName.length()-255));
099                }
100            }
101            cycleName=cycleName+"_"+dateFormatForCycleCreation;
102        }
103        return cycleName;
104    }
105
106    private String getPhaseName(AutomationJobDetail job, SimpleDateFormat sdf) {
107        String phaseName=job.getPhaseName();
108        if(job.getIsTimeStamp()){
109            String dateFormatForCycleCreation = sdf.format(new Date());
110            if(phaseName !=null && !phaseName.isEmpty()){
111                if(phaseName.length()>225){
112                    phaseName=phaseName.substring(0,phaseName.length()-(phaseName.length()-225));
113                }
114            }
115            phaseName=phaseName+"_"+dateFormatForCycleCreation;
116        }
117        return phaseName;
118    }
119}