Skip to content

Commit

Permalink
ability to customize delegate client for Micronaut (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed May 6, 2022
1 parent f6faa44 commit d5b25cb
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -153,6 +154,23 @@ public static Client create(Object unitTest) {
return build(unitTest).start();
}


/**
* Creates a new client either reusing the existing application context if the unit test
* implements {@link ApplicationContextProvider} or starting a new custom one.
*
* @param delegate the delegate client to be used to perform HTTP requests
* @param unitTest the current test
* @return a builder for a client using either existing or a custom {@link ApplicationContext}
*/
public static Client create(Client delegate, Object unitTest) {
if (unitTest instanceof ApplicationContextProvider) {
return new Micronaut((ApplicationContextProvider) unitTest, delegate);
}
assertProviderIfPossible(unitTest);
return build(unitTest).start();
}

/**
* Creates a lazy initialized client using the provided application context.
*
Expand All @@ -164,6 +182,18 @@ public static Client createLazy(Supplier<?> unitTestProvider, ApplicationContext
return new Micronaut(provider, LazyClient.create(() -> Http.create(unitTestProvider.get())));
}

/**
* Creates a lazy initialized client using the provided application context.
*
* @param delegateCreator the function to create a client to perform HTTP requests for given unit test
* @param unitTestProvider the current test provider
* @param provider the context provider
* @return a builder for a client using either existing or a custom {@link ApplicationContext}
*/
public static Client createLazy(Function<Object, Client> delegateCreator, Supplier<?> unitTestProvider, ApplicationContextProvider provider) {
return new Micronaut(provider, LazyClient.create(() -> delegateCreator.apply(unitTestProvider.get())));
}

/**
* Creates a new client using the provided application context.
*
Expand All @@ -175,6 +205,18 @@ public static Client create(Object unitTest, ApplicationContextProvider provider
return new Micronaut(provider, Http.create(unitTest));
}

/**
* Creates a new client using the provided application context.
*
* @param delegate the delegate client to be used to perform HTTP requests
* @param unitTest the current test
* @param provider the context provider
* @return a builder for a client using either existing or a custom {@link ApplicationContext}
*/
public static Client create(Client delegate, Object unitTest, ApplicationContextProvider provider) {
return new Micronaut(provider, delegate);
}

private Micronaut(ApplicationContextProvider contextProvider, Client delegate) {
this.contextProvider = contextProvider;
this.delegate = delegate;
Expand Down

0 comments on commit d5b25cb

Please sign in to comment.