我们用于测试框架的内部api已更改,我不知道如何适应它

时间:2019-12-18 07:39:48

标签: java api interface

我认为没有人可以帮助我,因为这是专有代码,但是也许我可以得到有关更改内容的提示?

因此,我们有一个使用API​​的测试框架,它们都是内部使用的,但来自不同的组。 现在,API发生了一些变化,我无法在框架中调整代码以使其运行。

这是API更改之前的接口。

  package com.ericsson.stsi.analytics.evaluation.api;

  import java.util.stream.StreamSupport;

  public interface EvaluatorFactory {

 <T extends Evaluator<O>, O extends EvaluatorOptions> T create(Class<T> evaluatorType, O evaluatorOptions);

 <T extends Evaluator<O>, O extends EvaluatorOptions> Class<O> getEvaluatorOptionsType(Class<T> evaluatorType);

 Iterable<Class<? extends Evaluator<? extends EvaluatorOptions>>> getSupportedEvaluators();

      default boolean isEvaluatorSupported(Class<? extends Evaluator<? extends EvaluatorOptions>> evaluatorType) {
          return StreamSupport.stream(getSupportedEvaluators().spliterator(), false)
                  .anyMatch(evaluatorType::isAssignableFrom);
      }
  }

这是我们在框架中拥有的驱动程序类的代码,现在可以给出错误信息。

Public class EvaluationDriver extends AbstractDriver {

    public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

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

    private final Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
    private final List<JsonResource> optionsJsons;
    private final Map<String, Map<String, Object>> overridingOptionsMap;

    public EvaluationDriver(final List<JsonResource> optionsJsons) {
        this(optionsJsons, Maps.newHashMap());
    }

    public EvaluationDriver(final List<JsonResource> optionsJsons,
            final Map<String, Map<String, Object>> overridingOptionsMap) {
        this.optionsJsons = optionsJsons;
        this.overridingOptionsMap = overridingOptionsMap;
    }

    /**
     * @param commonEvaluatorInterface
     * @param <FACTORY>
     * @param <EVALUATOR>
     * @param <OPTIONS>
     * @return
     */
    public <FACTORY extends EvaluatorFactory, EVALUATOR extends Evaluator<OPTIONS>, OPTIONS extends EvaluatorOptions> List<ResponseDto<EvaluationDto>> runEvaluation(
            final Class<EVALUATOR> commonEvaluatorInterface, final EvalOptionsTransformer<OPTIONS> transformer,
            final Set<SutParameters> sutParameters) {
        final List<ResponseDto<EvaluationDto>> evaluationResultList = new ArrayList<>();
        final List<Pair<FACTORY, List<Class<EVALUATOR>>>> factoriesWithEvaluators =
                getEvaluatorFactories(commonEvaluatorInterface);

        if (factoriesWithEvaluators.size() == 0) {
            evaluationResultList.add(ResponseDto.failed("No evaluators found for " + commonEvaluatorInterface));
        }

        factoriesWithEvaluators.forEach(factoryAndEvaluatorPair -> factoryAndEvaluatorPair.getRight() // .parallelStream()
                .forEach(evaluator -> {
                    final List<Class<?>> evaluatorInterfaces = ClassUtils.getAllInterfaces(evaluator);
                    Collections.reverse(evaluatorInterfaces);
                    evaluatorInterfaces.add(evaluator);
                    LOGGER.info("Evaluator class {}  interface hierarchy:\n{}", evaluator, evaluatorInterfaces);
                    final Map<String, Object> optionsMap = evaluatorInterfaces.stream().reduce(new LinkedHashMap<>(),
                            this::getOptions, this::mergeOptions);

                    mergeOptions(optionsMap, overridingOptionsMap.get(""));
                    mergeOptions(optionsMap, overridingOptionsMap, evaluator);

                    final Set<Map<String, Object>> listEvaluations =
                            EvaluationWildcard.createListEvaluatons(sutParameters, optionsMap);

                    listEvaluations.parallelStream().forEach(completeOptionsMap -> {
                        final String optionsJson = gson.toJson(completeOptionsMap);
                        final EvaluatorFactory factory = factoryAndEvaluatorPair.getLeft();
                        LOGGER.info("---> Running evaluator {}\nusing factory {}\nwith options:\n{}", evaluator,
                                factory.getClass(), optionsJson);
                        OPTIONS options = gson.fromJson(optionsJson, factory.getEvaluatorOptionsType(evaluator));
                        if (transformer != null) {
                            options = transformer.transform(options);
                        }
                        evaluationResultList.add(runEvaluation(factoryAndEvaluatorPair.getLeft(), evaluator, options));
                    });
                }));

        return evaluationResultList;
    }

    @SuppressWarnings("unchecked")
    private <FACTORY extends EvaluatorFactory, EVALUATOR extends Evaluator<OPTIONS>, OPTIONS extends EvaluatorOptions> List<Pair<FACTORY, List<Class<EVALUATOR>>>> getEvaluatorFactories(
            final Class<EVALUATOR> commonEvaluatorInterface) {
        final List<Pair<FACTORY, List<Class<EVALUATOR>>>> factoriesWithEvaluators = new ArrayList<>();

        for (final EvaluatorFactory factory : ServiceLoader.load(EvaluatorFactory.class)) {
            LOGGER.debug("Checking factory {} for evaluator class implementation", factory.getClass());

            final List<Class<EVALUATOR>> evaluators = new ArrayList<>();
            StreamSupport.stream(factory.getSupportedEvaluators().spliterator(), false)
                    .filter(commonEvaluatorInterface::isAssignableFrom)
                    .forEach((evaluatorType) -> evaluators.add((Class<EVALUATOR>) evaluatorType));

            if (evaluators.size() > 0) {
                LOGGER.debug("Evaluators found for factory {}: {}", factory.getClass(), evaluators);
                factoriesWithEvaluators.add(Pair.of((FACTORY) factory, evaluators));
            }
        }

        return factoriesWithEvaluators;
    }

    private <EVALUATOR extends Evaluator<OPTIONS>, OPTIONS extends EvaluatorOptions> ResponseDto<EvaluationDto> runEvaluation(
            final EvaluatorFactory factory, final Class<EVALUATOR> evaluatorClass, final OPTIONS options) {
        try {
            final EVALUATOR evaluator = factory.create(evaluatorClass, options);
            final EvaluationResult evaluationResult = evaluator.evaluate().get();
            final EvaluationReport evaluationReport = evaluator.createReport(evaluationResult.getId());

            return ResponseDto.ok(new EvaluationDto(evaluationResult, evaluationReport));
        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), ExceptionUtils.getRootCause(e));
            return ResponseDto.failed("Could not run evaluation", e);
        }
    }

    private Map<String, Object> getOptions(final Map<String, Object> map, final Class<?> evaluatorClass) {
        String json = StringUtils.EMPTY;

        try {
            json = optionsJsons.stream()
                    .filter(optionsJson -> evaluatorClass.getSimpleName().equals(optionsJson.getName()))
                    .map(JsonResource::getJsonString).collect(MoreCollectors.onlyElement());
        } catch (final NoSuchElementException nse) {
            LOGGER.debug("No options defined for {}", evaluatorClass);
        }

        if (StringUtils.isEmpty(json)) {
            return map;
        }

        try {
            final Type collectionType = new TypeToken<LinkedHashMap<Object, Object>>() {}.getType();
            return mergeOptions(map, gson.fromJson(json, collectionType));
        } catch (final Exception e) {
            throw new IllegalStateException("Could not create json map for resource: " + evaluatorClass.getSimpleName(),
                    e);
        }
    }

    private Map<String, Object> mergeOptions(final Map<String, Object> mapOne, final Map<String, Object> mapTwo) {
        mapOne.putAll(mapTwo);
        return mapOne;
    }

    private Map<String, Object> mergeOptions(final Map<String, Object> mapOne,
            final Map<String, Map<String, Object>> mapTwo, final Class<?> evaluatorClass) {
        if (mapTwo.containsKey(evaluatorClass.getSimpleName())) {
            mapOne.putAll(mapTwo.get(evaluatorClass.getSimpleName()));
        }
        return mapOne;
    }
}

现在,API的接口如下:

package com.ericsson.stsi.analytics.evaluation.api;

import java.util.stream.StreamSupport;

public interface EvaluatorFactory<T extends Evaluator<O>, O extends EvaluatorOptions> {

    T create(Class<T> evaluatorType, O evaluatorOptions);

    Class<O> getEvaluatorOptionsType(Class<T> evaluatorType);

    Iterable<Class<T>> getSupportedEvaluators();

    default boolean isEvaluatorSupported(Class<?> evaluatorType) {
        return StreamSupport.stream(getSupportedEvaluators().spliterator(), false)
                .anyMatch(evaluatorType::isAssignableFrom);
    }
}

我得到了他们理解的thees错误,但现在确定如何更改代码:

  1. Stream类型的方法filter(Predicate)不是
    适用于 arguments(commonEvaluatorInterface :: isAssignableFrom)

  2. 类类型未定义在此处适用的isAssignableFrom(Object)

  3. 类型不匹配:无法从评估者转换为评估者

  4. 类型不匹配:无法从Object转换为OPTIONS

0 个答案:

没有答案