Skip to content

feat: Implement GetSpecSchema #180

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

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -181,4 +181,18 @@ private WriteMessage processDeleteStaleRequest(Write.Request request)
messageDeleteStale.getSourceName(),
new Date(messageDeleteStale.getSyncTime().getSeconds() * 1000));
}

@Override
public void getSpecSchema(
io.cloudquery.plugin.v3.GetSpecSchema.Request request,
StreamObserver<io.cloudquery.plugin.v3.GetSpecSchema.Response> responseObserver) {
io.cloudquery.plugin.v3.GetSpecSchema.Response.Builder builder =
io.cloudquery.plugin.v3.GetSpecSchema.Response.newBuilder();
String schema = this.plugin.getJson_schema();
if (schema != null && !schema.isBlank()) {
builder.setJsonSchema(schema);
}
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
}
}
1 change: 1 addition & 0 deletions lib/src/main/java/io/cloudquery/plugin/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public abstract class Plugin {
@NonNull protected final String name;
@NonNull protected final String version;
@Setter protected Logger logger;
@Setter protected String json_schema;
protected ClientMeta client;

public void init(String spec, NewClientOptions options) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.cloudquery.internal.servers.plugin.v3;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;

Expand All @@ -9,6 +10,7 @@
import io.cloudquery.messages.WriteInsert;
import io.cloudquery.messages.WriteMigrateTable;
import io.cloudquery.plugin.Plugin;
import io.cloudquery.plugin.v3.GetSpecSchema;
import io.cloudquery.plugin.v3.PluginGrpc;
import io.cloudquery.plugin.v3.PluginGrpc.PluginStub;
import io.cloudquery.plugin.v3.Write;
Expand All @@ -25,12 +27,14 @@
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import lombok.Getter;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.junit.Rule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -91,6 +95,31 @@ public void shouldSendWriteDeleteStaleMessage() throws Exception {
verify(plugin).write(any(WriteDeleteStale.class));
}

@Test
public void shouldSendNullJSONSchema() throws Exception {
NullResponseStream<GetSpecSchema.Response> responseObserver = new NullResponseStream<>();

pluginStub.getSpecSchema(GetSpecSchema.Request.getDefaultInstance(), responseObserver);
responseObserver.await();

verify(plugin).getJson_schema();
assertFalse(responseObserver.getValue().hasJsonSchema());
}

@Test
public void shouldSendNonNullJSONSchema() throws Exception {
Mockito.doReturn("{}").when(plugin).getJson_schema();

NullResponseStream<GetSpecSchema.Response> responseObserver = new NullResponseStream<>();

pluginStub.getSpecSchema(GetSpecSchema.Request.getDefaultInstance(), responseObserver);
responseObserver.await();

verify(plugin).getJson_schema();
assertTrue(responseObserver.getValue().hasJsonSchema());
assertEquals("{}", responseObserver.getValue().getJsonSchema());
}

private static Write.Request generateMigrateTableMessage() throws IOException {
Table table = Table.builder().name("test").build();
return Write.Request.newBuilder()
Expand Down Expand Up @@ -121,12 +150,18 @@ private Write.Request generateDeleteStaleMessage() {

private static class NullResponseStream<T> implements StreamObserver<T> {
private final CountDownLatch countDownLatch = new CountDownLatch(1);
@Getter private T value;
@Getter private Throwable error;

@Override
public void onNext(T value) {}
public void onNext(T value) {
this.value = value;
}

@Override
public void onError(Throwable t) {}
public void onError(Throwable t) {
this.error = t;
}

@Override
public void onCompleted() {
Expand Down