Skip to content

Commit

Permalink
Kiota sync client (#4184)
Browse files Browse the repository at this point in the history
* [WIP] switch to the Kiota sync client

* it compiles

* more

* test in ci

* more fixes

* more

* fix

* auth debugging

* Adapt mock and tests to vertx auth

* remove-trailing-slash

* fixes

* more

* minor

* minor revert

* Make mock match endpoint and add stub for basic user

* Restore CI

* Fix exception unwrap

* Fix usernames in realm

* Fix wrong credentials exception handling in integration tests

* Fix assertion

* bupm versions and rename to java-sdk

* bump kiota

---------

Co-authored-by: Carles Arnal <[email protected]>
  • Loading branch information
andreaTP and carlesarnal authored Jan 23, 2024
1 parent 266c718 commit 6c44e36
Show file tree
Hide file tree
Showing 76 changed files with 1,345 additions and 1,861 deletions.
4 changes: 1 addition & 3 deletions .github/test-mvn-deploy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
</dependency>
<dependency>
<groupId>io.apicurio</groupId>
<artifactId>apicurio-registry-client</artifactId>
<artifactId>apicurio-registry-java-sdk</artifactId>
<version>${version.apicurio}</version>
</dependency>
</dependencies>
</project>


Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package io.apicurio.registry;

import com.microsoft.kiota.ApiException;
import io.apicurio.registry.content.ContentHandle;
import io.apicurio.registry.utils.tests.ParallelizableTest;
import io.apicurio.registry.utils.tests.TestUtils;

import io.apicurio.rest.client.auth.exception.NotAuthorizedException;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.junit.jupiter.api.Assertions;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -63,23 +60,4 @@ protected final ContentHandle resourceToContentHandle(String resourceName) {
public static void assertMultilineTextEquals(String expected, String actual) throws Exception {
Assertions.assertEquals(TestUtils.normalizeMultiLineString(expected), TestUtils.normalizeMultiLineString(actual));
}

protected void assertForbidden(ExecutionException executionException) {
Assertions.assertNotNull(executionException.getCause());
Assertions.assertEquals(ApiException.class, executionException.getCause().getClass());
Assertions.assertEquals(403, ((ApiException)executionException.getCause()).getResponseStatusCode());
}

protected void assertNotAuthorized(ExecutionException executionException) {
Assertions.assertNotNull(executionException.getCause());

if (executionException.getCause() instanceof NotAuthorizedException) {
// thrown by the token provider adapter
} else {
// mapped by Kiota
Assertions.assertEquals(ApiException.class, executionException.getCause().getClass());
Assertions.assertEquals(401, ((ApiException) executionException.getCause()).getResponseStatusCode());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import com.microsoft.kiota.ApiException;
import com.microsoft.kiota.RequestAdapter;
import com.microsoft.kiota.authentication.AnonymousAuthenticationProvider;
import com.microsoft.kiota.http.OkHttpRequestAdapter;

import io.apicurio.registry.rest.client.models.ArtifactMetaData;
import io.apicurio.registry.rest.v3.beans.ArtifactReference;
import io.apicurio.rest.client.auth.exception.NotAuthorizedException;
import io.kiota.http.vertx.VertXRequestAdapter;
import io.apicurio.registry.client.auth.VertXAuthFactory;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -78,7 +76,7 @@ protected RestService buildConfluentClient() {
return new RestService("http://localhost:" + testPort + "/apis/ccompat/v7");
}

protected final RequestAdapter anonymousAdapter = new OkHttpRequestAdapter(new AnonymousAuthenticationProvider());
protected final RequestAdapter anonymousAdapter = new VertXRequestAdapter(VertXAuthFactory.defaultVertx);

protected RegistryClient createRestClientV3() {
anonymousAdapter.setBaseUrl(registryV3ApiUrl);
Expand All @@ -101,11 +99,11 @@ protected void deleteGlobalRules(int expectedDefaultRulesCount) throws Exception
// Delete all global rules
TestUtils.retry(() -> {
try {
clientV3.admin().rules().delete().get(3, TimeUnit.SECONDS);
clientV3.admin().rules().delete();
} catch (Exception err) {
// ignore
}
Assertions.assertEquals(expectedDefaultRulesCount, clientV3.admin().rules().get().get(3, TimeUnit.SECONDS).size());
Assertions.assertEquals(expectedDefaultRulesCount, clientV3.admin().rules().get().size());
});
}

Expand All @@ -125,7 +123,7 @@ protected Long createArtifact(String groupId, String artifactId, String artifact
config.headers.add("X-Registry-ArtifactId", artifactId);
config.headers.add("X-Registry-ArtifactType", artifactType);
})
.get(3, TimeUnit.SECONDS);
;

assert( result.getId().equals(artifactId) );
assert( result.getType().equals(artifactType) );
Expand Down Expand Up @@ -181,7 +179,7 @@ protected ArtifactMetaData createArtifactExtendedRaw(String groupId, String arti
config.headers.add("X-Registry-ArtifactType", artifactType);
}
})
.get(3, TimeUnit.SECONDS);
;
}

protected ArtifactMetaData updateArtifactExtendedRaw(String groupId, String artifactId, String artifactType, String artifactContent, List<ArtifactReference> artifactReferences) throws Exception {
Expand All @@ -207,7 +205,7 @@ protected ArtifactMetaData updateArtifactExtendedRaw(String groupId, String arti
config.headers.add("X-Registry-ArtifactId", artifactId);
config.headers.add("X-Registry-ArtifactType", artifactType);
})
.get(3, TimeUnit.SECONDS);
;
}

protected Long createArtifactVersion(String artifactId, String artifactType, String artifactContent) throws Exception {
Expand All @@ -225,15 +223,15 @@ protected Long createArtifactVersion(String groupId, String artifactId, String a
.byArtifactId(artifactId)
.versions()
.post(content, config -> {config.headers.add("X-Registry-ArtifactType", artifactType); })
.get(3, TimeUnit.SECONDS);
;

assert( version.getId().equals(artifactId) );
assert( version.getType().equals(artifactType) );

return version.getGlobalId();
}

protected void createArtifactRule(String groupId, String artifactId, RuleType ruleType, String ruleConfig) throws ExecutionException, InterruptedException, TimeoutException {
protected void createArtifactRule(String groupId, String artifactId, RuleType ruleType, String ruleConfig) {
var rule = new io.apicurio.registry.rest.client.models.Rule();
rule.setConfig(ruleConfig);
rule.setType(io.apicurio.registry.rest.client.models.RuleType.forValue(ruleType.value()));
Expand All @@ -244,28 +242,25 @@ protected void createArtifactRule(String groupId, String artifactId, RuleType ru
.artifacts()
.byArtifactId(artifactId)
.rules()
.post(rule)
.get(3, TimeUnit.SECONDS);
.post(rule);
}

@SuppressWarnings("deprecation")
protected io.apicurio.registry.rest.client.models.Rule createGlobalRule(RuleType ruleType, String ruleConfig) throws ExecutionException, InterruptedException, TimeoutException {
protected io.apicurio.registry.rest.client.models.Rule createGlobalRule(RuleType ruleType, String ruleConfig) {
var rule = new io.apicurio.registry.rest.client.models.Rule();
rule.setConfig(ruleConfig);
rule.setType(io.apicurio.registry.rest.client.models.RuleType.forValue(ruleType.value()));

clientV3
.admin()
.rules()
.post(rule)
.get(3, TimeUnit.SECONDS);
.post(rule);
// TODO: verify this get
return clientV3
.admin()
.rules()
.byRule(ruleType.value())
.get()
.get(3, TimeUnit.SECONDS);
.get();
}

/**
Expand Down Expand Up @@ -312,21 +307,19 @@ protected List<ArtifactReferenceDto> toReferenceDtos(List<ArtifactReference> ref
.collect(Collectors.toList());
}

protected void assertForbidden(ExecutionException executionException) {
Assertions.assertNotNull(executionException.getCause());
Assertions.assertEquals(ApiException.class, executionException.getCause().getClass());
Assertions.assertEquals(403, ((ApiException)executionException.getCause()).getResponseStatusCode());
protected void assertForbidden(Exception exception) {
Assertions.assertEquals(ApiException.class, exception.getClass());
Assertions.assertEquals(403, ((ApiException)exception).getResponseStatusCode());
}

protected void assertNotAuthorized(ExecutionException executionException) {
Assertions.assertNotNull(executionException.getCause());

if (executionException.getCause() instanceof NotAuthorizedException) {
protected void assertNotAuthorized(Exception exception) {
if (exception instanceof NotAuthorizedException) {
// thrown by the token provider adapter
} else {
// mapped by Kiota
Assertions.assertEquals(ApiException.class, executionException.getCause().getClass());
Assertions.assertEquals(401, ((ApiException) executionException.getCause()).getResponseStatusCode());
Assertions.assertEquals(ApiException.class, exception.getClass());
Assertions.assertEquals(401, ((ApiException) exception).getResponseStatusCode());
}
}

}

This file was deleted.

5 changes: 2 additions & 3 deletions app/src/test/java/io/apicurio/registry/MigrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

@QuarkusTest
public class MigrationTest extends AbstractResourceTestBase {
Expand All @@ -19,12 +18,12 @@ public void migrateData() throws Exception {
clientV3.admin().importEscaped().post(originalData, config -> {
// TODO: this header should be injected by Kiota
config.headers.add("Content-Type", "application/zip");
}).get(10, TimeUnit.SECONDS);
});
clientV3.admin().importEscaped().post(migratedData, config -> {
// TODO: this header should be injected by Kiota
config.headers.add("Content-Type", "application/zip");
config.headers.add("X-Registry-Preserve-GlobalId", "false");
config.headers.add("X-Registry-Preserve-ContentId", "false");
}).get(40, TimeUnit.SECONDS);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package io.apicurio.registry.auth;

import com.microsoft.kiota.ApiException;
import com.microsoft.kiota.authentication.AnonymousAuthenticationProvider;
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
import com.microsoft.kiota.http.OkHttpRequestAdapter;
import io.apicurio.common.apps.config.Info;
import io.apicurio.registry.AbstractResourceTestBase;
import io.apicurio.registry.rest.client.RegistryClient;
import io.apicurio.registry.types.ArtifactType;
import io.apicurio.registry.utils.tests.ApicurioTestTags;
import io.apicurio.registry.utils.tests.AuthTestProfileAnonymousCredentials;
import io.apicurio.registry.utils.tests.JWKSMockServer;
import io.kiota.http.vertx.VertXRequestAdapter;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.apicurio.registry.client.auth.VertXAuthFactory;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import static io.apicurio.registry.client.auth.VertXAuthFactory.buildOIDCWebClient;
import static org.junit.jupiter.api.Assertions.assertTrue;

@QuarkusTest
@TestProfile(AuthTestProfileAnonymousCredentials.class)
Expand All @@ -34,24 +33,24 @@ public class AuthTestAnonymousCredentials extends AbstractResourceTestBase {

@Test
public void testWrongCreds() throws Exception {
var adapter = new OkHttpRequestAdapter(
new BaseBearerTokenAuthenticationProvider(
new OidcAccessTokenProvider(authServerUrl, JWKSMockServer.WRONG_CREDS_CLIENT_ID, "secret")));
var adapter = new VertXRequestAdapter(buildOIDCWebClient(authServerUrl, JWKSMockServer.WRONG_CREDS_CLIENT_ID, "test55"));
adapter.setBaseUrl(registryV3ApiUrl);
RegistryClient client = new RegistryClient(adapter);
var executionException = Assertions.assertThrows(ExecutionException.class, () -> {
client.groups().byGroupId(groupId).artifacts().get().get(3, TimeUnit.SECONDS);

var exception = Assertions.assertThrows(Exception.class, () -> {
client.groups().byGroupId(groupId).artifacts().get();
});
assertNotAuthorized(executionException);

assertTrue(exception.getMessage().contains("Unauthorized"));
}

@Test
public void testNoCredentials() throws Exception {
var adapter = new OkHttpRequestAdapter(new AnonymousAuthenticationProvider());
var adapter = new VertXRequestAdapter(VertXAuthFactory.defaultVertx);
adapter.setBaseUrl(registryV3ApiUrl);
RegistryClient client = new RegistryClient(adapter);
// Read-only operation should work without any credentials.
var results = client.search().artifacts().get(config -> config.queryParameters.group = groupId).get(3, TimeUnit.SECONDS);
var results = client.search().artifacts().get(config -> config.queryParameters.group = groupId);
Assertions.assertTrue(results.getCount() >= 0);

// Write operation should fail without any credentials
Expand All @@ -61,7 +60,7 @@ public void testNoCredentials() throws Exception {
" \"namespace\" : \"my.example\",\r\n" +
" \"fields\" : [{\"name\" : \"age\", \"type\" : \"int\"}]\r\n" +
"}";
var executionException = Assertions.assertThrows(ExecutionException.class, () -> {
var exception = Assertions.assertThrows(ApiException.class, () -> {
var content = new io.apicurio.registry.rest.client.models.ArtifactContent();
content.setContent(data);
client
Expand All @@ -71,10 +70,8 @@ public void testNoCredentials() throws Exception {
.post(content, config -> {
config.headers.add("X-Registry-ArtifactType", ArtifactType.AVRO);
config.headers.add("X-Registry-ArtifactId", "testNoCredentials");
}).get(3, TimeUnit.SECONDS);
});
});
Assertions.assertNotNull(executionException.getCause());
Assertions.assertEquals(ApiException.class, executionException.getCause().getClass());
Assertions.assertEquals(401, ((ApiException)executionException.getCause()).getResponseStatusCode());
Assertions.assertEquals(401, exception.getResponseStatusCode());
}
}
Loading

0 comments on commit 6c44e36

Please sign in to comment.