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

rest.RestCucumberFeatureLoader Maven / Gradle / Ivy

Go to download

Rest Cucumber allows you to attach a rest client for retrieval of Cucumber feature files and posting of Cucumber test results

There is a newer version: 1.3
Show newest version
package rest;

import java.io.PrintStream;
import java.util.*;
import cucumber.runtime.FeatureBuilder;
import cucumber.runtime.io.Resource;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.model.CucumberFeature;
import cucumber.runtime.model.PathWithLines;

public class RestCucumberFeatureLoader {
   public static final String REST_CLIENT_KEY = "rest client";

   private RestCucumberFeatureLoader() {
   }

   public static List load(ResourceLoader resourceLoader,
      List featurePaths, List filters, PrintStream out) {
      if (resourceLoader instanceof RestMultiLoader) {
         RestMultiLoader restClientResourceLoader = (RestMultiLoader) resourceLoader;
         if (restClientResourceLoader.hasRestClientSet()) {
            List newFeaturePaths = new ArrayList();
            for (int i = 0; i < featurePaths.size(); i++) {
               newFeaturePaths.add(REST_CLIENT_KEY);
            }
            featurePaths = newFeaturePaths;
         }
      }
      final List cucumberFeatures =
         load(resourceLoader, featurePaths, filters);
      if (cucumberFeatures.isEmpty()) {
         if (featurePaths.isEmpty()) {
            out.println(String.format("Got no path to feature directory or feature file"));
         } else if (filters.isEmpty()) {
            out.println(String.format("No features found at %s", featurePaths));
         } else {
            out.println(String.format(
               "None of the features at %s matched the filters: %s", featurePaths,
               filters));
         }
      }
      return cucumberFeatures;
   }

   public static List load(ResourceLoader resourceLoader,
      List featurePaths, List filters) {
      final List cucumberFeatures = new ArrayList();
      final FeatureBuilder builder = new FeatureBuilder(cucumberFeatures);
      for (String featurePath : featurePaths) {
         if (featurePath.equalsIgnoreCase(REST_CLIENT_KEY)) {
            loadFromRestClient(resourceLoader, builder, filters);
         } else {
            loadFromFeaturePath(builder, resourceLoader, featurePath, filters, false);
         }
      }
      Collections.sort(cucumberFeatures, new CucumberFeatureUriComparator());
      return cucumberFeatures;
   }

   private static void loadFromRestClient(ResourceLoader resourceLoader,
      FeatureBuilder builder, List filters) {
      Iterable resources = resourceLoader.resources("", REST_CLIENT_KEY);
      if (!resources.iterator().hasNext()) {
         throw new IllegalArgumentException(
            "No resource found, please ensure you have set a rest client to use with JiraCucumber.");
      }
      for (Resource resource : resources) {
         builder.parse(resource, filters);
      }
   }

   private static void loadFromFeaturePath(FeatureBuilder builder,
      ResourceLoader resourceLoader, String featurePath, final List filters,
      boolean failOnNoResource) {
      PathWithLines pathWithLines = new PathWithLines(featurePath);
      List filtersForPath = new ArrayList(filters);
      filtersForPath.addAll(pathWithLines.lines);
      Iterable resources =
         resourceLoader.resources(pathWithLines.path, ".feature");
      if (failOnNoResource && !resources.iterator().hasNext()) {
         throw new IllegalArgumentException("No resource found for: "
            + pathWithLines.path);
      }
      for (Resource resource : resources) {
         builder.parse(resource, filtersForPath);
      }
   }

   private static class CucumberFeatureUriComparator implements
      Comparator {
      public int compare(CucumberFeature a, CucumberFeature b) {
         return a.getPath().compareTo(b.getPath());
      }
   }
}