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

io.reactiverse.junit5.web.WebClientParameterProvider Maven / Gradle / Ivy

/*
 * Copyright (c) 2020 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.reactiverse.junit5.web;

import io.vertx.core.Vertx;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
import io.vertx.junit5.ParameterClosingConsumer;
import io.vertx.junit5.ScopedObject;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxExtensionParameterProvider;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.platform.commons.util.AnnotationUtils;
import org.junit.platform.commons.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * WebClient parameter provider.
 *
 * @author Julien Ponge
 * @author Francesco Guardiani
 */
public class WebClientParameterProvider implements VertxExtensionParameterProvider {

  @Override
  public Class type() {
    return WebClient.class;
  }

  @Override
  public String key() {
    return "WebClient";
  }

  @Override
  public WebClient newInstance(ExtensionContext extensionContext, ParameterContext parameterContext) {
    ExtensionContext.Store store = extensionContext.getStore(ExtensionContext.Namespace.GLOBAL);
    ScopedObject scopedObject = store.get(VertxExtension.VERTX_INSTANCE_KEY, ScopedObject.class);
    Objects.requireNonNull(scopedObject, "A Vertx instance must exist, try adding the Vertx parameter as the first method argument");
    Vertx vertx = (Vertx) scopedObject.get();
    WebClientOptions webClientOptions = getWebClientOptions(extensionContext).orElse(new WebClientOptions());
    return WebClient.create(vertx, webClientOptions);
  }

  @Override
  public ParameterClosingConsumer parameterClosingConsumer() {
    return WebClient::close;
  }

  public static Optional getWebClientOptions(ExtensionContext context) {
    Optional> thisTestClass = context.getTestClass();
    if (!thisTestClass.isPresent()) {
      return Optional.empty();
    }
    List webClientOptionsField = AnnotationUtils
      .findPublicAnnotatedFields(thisTestClass.get(), WebClientOptions.class, WebClientOptionsInject.class);
    if (webClientOptionsField.isEmpty()) {
      return Optional.empty();
    }
    return ReflectionUtils
      .tryToReadFieldValue(webClientOptionsField.get(0), context.getTestInstance().get())
      .toOptional().map(o -> (WebClientOptions)o);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy