All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.matheusesoft.alm.api.utils.FacadeAPI Maven / Gradle / Ivy

The newest version!
package com.github.matheusesoft.alm.api.utils;

import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.matheusesoft.alm.api.AlmRunOptions;
import com.github.matheusesoft.alm.api.Client;
import com.github.matheusesoft.alm.api.EnumRunStatus;
import com.github.matheusesoft.alm.api.RunWithAlmTest;
import com.github.matheusesoft.alm.api.model.Release;
import com.github.matheusesoft.alm.api.model.ReleaseCycle;
import com.github.matheusesoft.alm.api.model.Run;
import com.github.matheusesoft.alm.api.model.Test;
import com.github.matheusesoft.alm.api.model.TestInstance;
import com.github.matheusesoft.alm.api.model.TestInstances;
import com.github.matheusesoft.alm.api.model.TestSet;

public class FacadeAPI {
	private final static Logger Log = LoggerFactory.getLogger(FacadeAPI.class);
	
	public static void createRunAndAttachment(Class clazz) throws Exception {
		if(Client.get().getOptions() != null && Client.get().getOptions().ativa()) {
			if(!Client.get().getOptions().somentePassed() || (Client.get().getOptions().somentePassed() && RunWithAlmTest.getRunStatus() == EnumRunStatus.STATUS_PASSED)) {
				Client.get().login();
				try {
					TestInstance testInstance = identifyTestInstance(clazz);
					Test teste = Client.get().loadTest(testInstance);
					EnumRunStatus status = RunWithAlmTest.getRunStatus() != null ? RunWithAlmTest.getRunStatus() : getAlmRunOptions(clazz).runStatus();
					Run run = Client.get().createRun(testInstance, teste);
					String reportPath = getAlmRunOptions(clazz) != null ? getAlmRunOptions(clazz).reportDir() : RunWithAlmTest.getReportDir();
					Client.get().createRunAttachment(run, reportPath);
					if (Client.get().getOptions().gravacao()) {
						String customReportPath = getAlmRunOptions(clazz) != null ? getAlmRunOptions(clazz).customReportDir() : RunWithAlmTest.getCustomReportDir();
						Client.get().createRunAttachmentCustom(run, customReportPath);
					}
					run = Client.get().loadRun(run.id());
					run.status(status.getValor());
					Client.get().updateRun(run);
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					Client.get().logout();
				}
			}
		}
	}

	private static TestInstance identifyTestInstance(Class clazz) throws Exception {
		AlmRunOptions options = getAlmRunOptions(clazz);
		TestInstance returnTestInstance = null;
		
		boolean notIdCycle = Client.get().getOptions().cycleId().equals("");
		
		if(notIdCycle) {
			if(options == null) {
				Log.info("identifyTestInstanceByReleasesName - RunWithAlmTest");
				returnTestInstance = identifyTestInstanceByReleasesName(Client.get().getOptions().releaseName(), Client.get().getOptions().cycleName(),RunWithAlmTest.getTestSetName(), RunWithAlmTest.getTestInstanceName());
			} else {
				Log.info("identifyTestInstanceByReleasesName - AlmRunOptions");
				returnTestInstance = identifyTestInstanceByReleasesName(Client.get().getOptions().releaseName(), 
						Client.get().getOptions().cycleName(), options.testSetName(), options.testInstanceName());
			}
		} else	{
			if(options == null) {
				Log.info("identifyTestInstanceByIdCycle - RunWithAlmTest");
				returnTestInstance = identifyTestInstanceByIdCycle(Client.get().getOptions().cycleId(), 
						RunWithAlmTest.getTestSetName(), RunWithAlmTest.getTestInstanceName());
			} else {
				Log.info("identifyTestInstanceByIdCycle - AlmRunOptions");
				returnTestInstance = identifyTestInstanceByIdCycle(Client.get().getOptions().cycleId(), 
						options.testSetName(), options.testInstanceName());
			}
		}
		
		return returnTestInstance;
	}
	
	private static TestInstance identifyTestInstanceByIdCycle(String cycleId, String testSetName, String testInstanceName) throws Exception {
		TestInstance returnTestInstance = null;
		ReleaseCycle releaseCycle = Client.get().loadReleaseCycle(cycleId);
		TestSet testSet = Client.get().loadTestSet(testSetName, releaseCycle.id());
		TestInstances testInstances = Client.get().loadTestInstances(testSet);
		for (TestInstance testI : testInstances.entities()) {
			if(testI.name().toLowerCase().trim().contains(testInstanceName.toLowerCase().trim())){
				returnTestInstance = testI;
				break;
			}
		}
		return returnTestInstance;
	}
	
	private static TestInstance identifyTestInstanceByReleasesName(String releaseName, String cycleName, String testSetName, String testInstanceName) throws Exception {
		TestInstance returnTestInstance = null;
		Release release = Client.get().loadRelease(releaseName);
		ReleaseCycle releaseCycle = Client.get().loadReleaseCycle(cycleName, release.id());
		TestSet testSet = Client.get().loadTestSet(testSetName, releaseCycle.id());
		TestInstances testInstances = Client.get().loadTestInstances(testSet);
		for (TestInstance testI : testInstances.entities()) {
			if(testI.name().toLowerCase().trim().contains(testInstanceName.toLowerCase().trim())){
				returnTestInstance = testI;
				break;
			}
		}
		return returnTestInstance;
	}
	
	private static AlmRunOptions getAlmRunOptions(Class clazz) {
          for (Method m : clazz.getDeclaredMethods()) {
                 if (m.isAnnotationPresent(AlmRunOptions.class)){
                        return (AlmRunOptions) m.getAnnotation(AlmRunOptions.class);
                 }
          }
		return null;
	  }
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy