// Path: src/main/java/com/frameworkium/core/common/reporting/TestIdUtils.java
// public class TestIdUtils {
// 
//   private TestIdUtils() {
//     // hide default constructor for this util class
//   }
// 
//   /**
//    * Get TMS Link or Issue ID value.
//    *
//    * @param iMethod the {@link IMethodInstance} to check for test ID annotations.
//    * @return Optional of either the {@link TmsLink} or {@link Issue} value.
//    * @throws IllegalStateException if {@link TmsLink} and {@link Issue}
//    *                               are specified inconstantly.
//    * @deprecated Use
//    *     {@link TestIdUtils#getIssueOrTmsLinkValues(IMethodInstance)} instead
//    */
//   @Deprecated
//   public static Optional<String> getIssueOrTmsLinkValue(IMethodInstance iMethod) {
//     Method method = iMethod.getMethod().getConstructorOrMethod().getMethod();
//     return getIssueOrTmsLinkValue(method);
//   }
// 
//   /**
//    * Get {@link TmsLink} or {@link Issue} for a method.
//    * If both are specified it will return jus the {@link TmsLink} value.
//    *
//    * @param method the method to check for test ID annotations.
//    * @return Optional of the {@link TmsLink} or {@link Issue} value.
//    * @deprecated Use
//    *     {@link TestIdUtils#getIssueOrTmsLinkValues(Method)} instead.
//    */
//   @Deprecated
//   public static Optional<String> getIssueOrTmsLinkValue(Method method) {
//     TmsLink tcIdAnnotation = method.getAnnotation(TmsLink.class);
//     Issue issueAnnotation = method.getAnnotation(Issue.class);
// 
//     if (nonNull(tcIdAnnotation)) {
//       return Optional.of(tcIdAnnotation.value());
//     } else if (nonNull(issueAnnotation)) {
//       return Optional.of(issueAnnotation.value());
//     } else {
//       return Optional.empty();
//     }
//   }
// 
//   /**
//    * Get list of {@link TmsLink} or {@link Issue}.
//    *
//    * @param iMethod the {@link IMethodInstance} to check for test ID annotations.
//    * @return List of either the {@link TmsLink} or {@link Issue} value.
//    * @throws IllegalStateException if {@link TmsLink} and {@link Issue}
//    *                               are specified inconstantly.
//    */
//   public static List<String> getIssueOrTmsLinkValues(IMethodInstance iMethod) {
//     Method method = iMethod.getMethod().getConstructorOrMethod().getMethod();
//     return getIssueOrTmsLinkValues(method);
//   }
// 
//   /**
//    * Get a list of {@link TmsLink} or {@link Issue} for a method.
//    * If both are specified it will return just the list of {@link TmsLink} values.
//    *
//    * @param method the method to check for test Id annotations.
//    * @return List of {@link TmsLink} or {@link Issue} values.
//    */
//   public static List<String> getIssueOrTmsLinkValues(Method method) {
//     TmsLink[] tcIdAnnotations = method.getAnnotationsByType(TmsLink.class);
//     Issue[] issueAnnotations = method.getAnnotationsByType(Issue.class);
//     if (tcIdAnnotations.length > 0) {
//       return Stream.of(tcIdAnnotations).map(TmsLink::value).collect(Collectors.toList());
//     }
//     if (issueAnnotations.length > 0) {
//       return Stream.of(issueAnnotations).map(Issue::value).collect(Collectors.toList());
//     }
//     return Collections.emptyList();
//   }
// }


import static com.frameworkium.core.common.properties.Property.JIRA_URL;
import static com.frameworkium.core.common.properties.Property.JQL_QUERY;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import com.frameworkium.core.common.reporting.TestIdUtils;
import com.frameworkium.core.common.reporting.jira.service.Search;
import java.lang.reflect.Method;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
package com.frameworkium.core.common.listeners;



public class MethodInterceptor implements IMethodInterceptor {

  private static final Logger logger = LogManager.getLogger();

  @Override
  public List<IMethodInstance> intercept(
      List<IMethodInstance> methods, ITestContext context) {

    return filterTestsToRunByJQL(methods);
  }

  private List<IMethodInstance> filterTestsToRunByJQL(
      List<IMethodInstance> methodsToBeFiltered) {

    if (!(JQL_QUERY.isSpecified() && JIRA_URL.isSpecified())) {
      // Can't run the JQL without both JIRA_URL and JQL_QUERY
      return methodsToBeFiltered;
    }

    logger.info("Filtering specified tests to run with JQL query results");

    List<String> testIDsFromJQL =
        new Search(JQL_QUERY.getValue()).getKeys();

    List<IMethodInstance> methodsToRun = methodsToBeFiltered.stream()
// Path: src/main/java/com/frameworkium/core/common/reporting/TestIdUtils.java
// public class TestIdUtils {
// 
//   private TestIdUtils() {
//     // hide default constructor for this util class
//   }
// 
//   /**
//    * Get TMS Link or Issue ID value.
//    *
//    * @param iMethod the {@link IMethodInstance} to check for test ID annotations.
//    * @return Optional of either the {@link TmsLink} or {@link Issue} value.
//    * @throws IllegalStateException if {@link TmsLink} and {@link Issue}
//    *                               are specified inconstantly.
//    * @deprecated Use
//    *     {@link TestIdUtils#getIssueOrTmsLinkValues(IMethodInstance)} instead
//    */
//   @Deprecated
//   public static Optional<String> getIssueOrTmsLinkValue(IMethodInstance iMethod) {
//     Method method = iMethod.getMethod().getConstructorOrMethod().getMethod();
//     return getIssueOrTmsLinkValue(method);
//   }
// 
//   /**
//    * Get {@link TmsLink} or {@link Issue} for a method.
//    * If both are specified it will return jus the {@link TmsLink} value.
//    *
//    * @param method the method to check for test ID annotations.
//    * @return Optional of the {@link TmsLink} or {@link Issue} value.
//    * @deprecated Use
//    *     {@link TestIdUtils#getIssueOrTmsLinkValues(Method)} instead.
//    */
//   @Deprecated
//   public static Optional<String> getIssueOrTmsLinkValue(Method method) {
//     TmsLink tcIdAnnotation = method.getAnnotation(TmsLink.class);
//     Issue issueAnnotation = method.getAnnotation(Issue.class);
// 
//     if (nonNull(tcIdAnnotation)) {
//       return Optional.of(tcIdAnnotation.value());
//     } else if (nonNull(issueAnnotation)) {
//       return Optional.of(issueAnnotation.value());
//     } else {
//       return Optional.empty();
//     }
//   }
// 
//   /**
//    * Get list of {@link TmsLink} or {@link Issue}.
//    *
//    * @param iMethod the {@link IMethodInstance} to check for test ID annotations.
//    * @return List of either the {@link TmsLink} or {@link Issue} value.
//    * @throws IllegalStateException if {@link TmsLink} and {@link Issue}
//    *                               are specified inconstantly.
//    */
//   public static List<String> getIssueOrTmsLinkValues(IMethodInstance iMethod) {
//     Method method = iMethod.getMethod().getConstructorOrMethod().getMethod();
//     return getIssueOrTmsLinkValues(method);
//   }
// 
//   /**
//    * Get a list of {@link TmsLink} or {@link Issue} for a method.
//    * If both are specified it will return just the list of {@link TmsLink} values.
//    *
//    * @param method the method to check for test Id annotations.
//    * @return List of {@link TmsLink} or {@link Issue} values.
//    */
//   public static List<String> getIssueOrTmsLinkValues(Method method) {
//     TmsLink[] tcIdAnnotations = method.getAnnotationsByType(TmsLink.class);
//     Issue[] issueAnnotations = method.getAnnotationsByType(Issue.class);
//     if (tcIdAnnotations.length > 0) {
//       return Stream.of(tcIdAnnotations).map(TmsLink::value).collect(Collectors.toList());
//     }
//     if (issueAnnotations.length > 0) {
//       return Stream.of(issueAnnotations).map(Issue::value).collect(Collectors.toList());
//     }
//     return Collections.emptyList();
//   }
// }


// Path: src/main/java/com/frameworkium/core/common/listeners/MethodInterceptor.java
import static com.frameworkium.core.common.properties.Property.JIRA_URL;
import static com.frameworkium.core.common.properties.Property.JQL_QUERY;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import com.frameworkium.core.common.reporting.TestIdUtils;
import com.frameworkium.core.common.reporting.jira.service.Search;
import java.lang.reflect.Method;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;

package com.frameworkium.core.common.listeners;



public class MethodInterceptor implements IMethodInterceptor {

  private static final Logger logger = LogManager.getLogger();

  @Override
  public List<IMethodInstance> intercept(
      List<IMethodInstance> methods, ITestContext context) {

    return filterTestsToRunByJQL(methods);
  }

  private List<IMethodInstance> filterTestsToRunByJQL(
      List<IMethodInstance> methodsToBeFiltered) {

    if (!(JQL_QUERY.isSpecified() && JIRA_URL.isSpecified())) {
      // Can't run the JQL without both JIRA_URL and JQL_QUERY
      return methodsToBeFiltered;
    }

    logger.info("Filtering specified tests to run with JQL query results");

    List<String> testIDsFromJQL =
        new Search(JQL_QUERY.getValue()).getKeys();

    List<IMethodInstance> methodsToRun = methodsToBeFiltered.stream()
        .filter(m -> TestIdUtils.getIssueOrTmsLinkValue(m).isPresent())