Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve client factories #744

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static Collection<String> parseOciBomArtifacts(String version) {
if (!extraPropertiesExtension.has('ociArtifacts')) {
String ociVersion = libs.oci.bom.get().version
def ociArtifacts = parseOciBomArtifacts(ociVersion)
.stream().filter(artifact -> artifact != "oci-java-sdk-shaded-full").toList()

String ociProjectVersion = new XmlSlurper().parse("checkouts/oci-java-sdk/bmc-bom/pom.xml").version.text()
def ociProjectArtifacts = parseOciBomArtifacts(ociProjectVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@ protected AbstractSdkClientFactory(
}

/**
* Builds the client.
* Builds the client based on its builder to make sure user can configure
* required parameters in the builder.
*
* @param clientBuilder The builder for client
* @param authenticationDetailsProvider The authentication details provider
* @return The client to build
*/
protected abstract @NonNull T build(@NonNull AbstractAuthenticationDetailsProvider authenticationDetailsProvider);
protected abstract @NonNull T build(
@NonNull B clientBuilder,
@NonNull AbstractAuthenticationDetailsProvider authenticationDetailsProvider
);

/**
* Set the HTTP provider for this client. This is injected by the application context, in order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,10 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

if (isRxJava2) {
writeRxJava2Clients(e, packageName, simpleName);
} else {
if (isReactor) {
writeReactorClients(e, packageName, simpleName);
} else {
if (isAsync) {
factoryClassNames.add(writeClientFactory(e, packageName, simpleName));
}
}
} else if (isReactor) {
writeReactorClients(e, packageName, simpleName);
} else if (isAsync) {
factoryClassNames.add(writeClientFactory(e, packageName, simpleName));
}
}

Expand Down Expand Up @@ -414,12 +410,13 @@ private String writeClientFactory(Element e, String packageName, String simpleNa
final MethodSpec.Builder buildMethod = MethodSpec.methodBuilder("build");

buildMethod.returns(ClassName.get(packageName, simpleName))
.addParameter(builderType, "clientBuilder")
.addParameter(authProviderType, "authenticationDetailsProvider")
.addAnnotation(Singleton.class)
.addAnnotation(requiresSpec.build())
.addAnnotation(preDestroy.build())
.addModifiers(Modifier.PROTECTED)
.addCode("return builder.build(authenticationDetailsProvider);");
.addCode("return clientBuilder.build(authenticationDetailsProvider);");
if (isBootstrapCompatible) {
buildMethod.addAnnotation(BootstrapContextCompatible.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.micronaut.oraclecloud.database;

import com.oracle.bmc.database.DatabaseAsyncClient;
import com.oracle.bmc.database.DatabaseClient;
import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Singleton;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@MicronautTest
public class DatabaseFactoryTest {

public static final String ENDPOINT = "http://my_endpoint";
public static final String ASYNC_ENDPOINT = "http://my_endpoint_async";
private final DatabaseClient databaseClient;
private final DatabaseAsyncClient databaseAsyncClient;

public DatabaseFactoryTest(
DatabaseClient databaseClient,
DatabaseAsyncClient databaseAsyncClient
) {
this.databaseClient = databaseClient;
this.databaseAsyncClient = databaseAsyncClient;
}

@Test
void testDataBaseClientConfiguration() {
assertEquals(ENDPOINT, databaseClient.getEndpoint());
}

@Test
void testDataBaseAsyncClientConfiguration() {
assertEquals(ASYNC_ENDPOINT, databaseAsyncClient.getEndpoint());
}

@Singleton
static class DatabaseClientBuilderListener
implements BeanCreatedEventListener<DatabaseClient.Builder> {
@Override
public DatabaseClient.Builder onCreated(
@NonNull BeanCreatedEvent<DatabaseClient.Builder> event
) {
DatabaseClient.Builder builder = event.getBean();
builder.endpoint(DatabaseFactoryTest.ENDPOINT);
return builder;
}
}

@Singleton
static class DatabaseAsyncClientBuilderListener
implements BeanCreatedEventListener<DatabaseAsyncClient.Builder> {
@Override
public DatabaseAsyncClient.Builder onCreated(
@NonNull BeanCreatedEvent<DatabaseAsyncClient.Builder> event
) {
DatabaseAsyncClient.Builder builder = event.getBean();
builder.endpoint(DatabaseFactoryTest.ASYNC_ENDPOINT);
return builder;
}
}

}
Loading