Skip to content

Commit

Permalink
(fix): fix annotation and test for google pubsub binding
Browse files Browse the repository at this point in the history
  • Loading branch information
SheheryarAamir committed Mar 19, 2024
1 parent e54f737 commit e2bc117
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.bindings.googlepubsub.annotations;

import io.github.springwolf.core.asyncapi.annotations.AsyncOperationBinding;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
Expand All @@ -16,14 +14,13 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@AsyncOperationBinding
@Inherited
public @interface GooglePubSubAsyncChannelBinding {
String type() default "googlepubsub";

String messageRetentionDuration() default "";

GooglePubsubAsyncMessageStoragePolicy messageStoragePolicy();
GooglePubsubAsyncMessageStoragePolicy messageStoragePolicy() default @GooglePubsubAsyncMessageStoragePolicy;

GooglePubSubAsyncSchemaSetting schemaSettings();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.springwolf.bindings.googlepubsub.annotations;

import io.github.springwolf.core.asyncapi.annotations.AsyncOperationBinding;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
Expand All @@ -11,9 +9,7 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@AsyncOperationBinding
@Inherited
public @interface GooglePubsubAsyncMessageStoragePolicy {

String[] allowedPersistenceRegions = null;
String[] allowedPersistenceRegions() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package io.github.springwolf.bindings.googlepubsub.scanners.channels;

import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubChannelBinding;
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubMessageStoragePolicy;
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubSchemaSettings;
import io.github.springwolf.bindings.googlepubsub.annotations.GooglePubSubAsyncChannelBinding;
import io.github.springwolf.core.asyncapi.scanners.channels.ChannelBindingProcessor;
Expand Down Expand Up @@ -31,17 +32,23 @@ public Optional<ProcessedChannelBinding> process(Method method) {
}

private ProcessedChannelBinding mapToChannelBinding(GooglePubSubAsyncChannelBinding bindingAnnotation) {

GooglePubSubMessageStoragePolicy googlePubsubMessageStoragePolicy = new GooglePubSubMessageStoragePolicy(
Arrays.stream(bindingAnnotation.messageStoragePolicy().allowedPersistenceRegions())
.toList());
GooglePubSubSchemaSettings googlePubSubSchemaSettings = new GooglePubSubSchemaSettings(
bindingAnnotation.schemaSettings().encoding(),
bindingAnnotation.schemaSettings().firstRevisionId(),
bindingAnnotation.schemaSettings().lastRevisionId(),
bindingAnnotation.schemaSettings().name());

return new ProcessedChannelBinding(
bindingAnnotation.type(),
new GooglePubSubChannelBinding(
null,
null,
null,
new GooglePubSubSchemaSettings(
bindingAnnotation.schemaSettings().encoding(),
bindingAnnotation.schemaSettings().firstRevisionId(),
bindingAnnotation.schemaSettings().lastRevisionId(),
bindingAnnotation.schemaSettings().name()),
bindingAnnotation.messageRetentionDuration(),
googlePubsubMessageStoragePolicy,
googlePubSubSchemaSettings,
bindingAnnotation.bindingVersion()));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// SPDX-License-Identifier: Apache-2.0
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubChannelBinding;
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubMessageStoragePolicy;
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubSchemaSettings;
import io.github.springwolf.bindings.googlepubsub.annotations.GooglePubSubAsyncChannelBinding;
import io.github.springwolf.bindings.googlepubsub.annotations.GooglePubSubAsyncSchemaSetting;
import io.github.springwolf.bindings.googlepubsub.annotations.GooglePubsubAsyncMessageStoragePolicy;
import io.github.springwolf.bindings.googlepubsub.scanners.channels.GooglePubSubChannelBindingProcessor;
import io.github.springwolf.core.asyncapi.scanners.channels.ProcessedChannelBinding;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -22,7 +24,13 @@ void processTest() throws NoSuchMethodException {
ProcessedChannelBinding binding = processor.process(method).get();

assertThat(binding.getType()).isEqualTo("googlepubsub");
assertThat(binding.getBinding()).isEqualTo(new GooglePubSubChannelBinding());
GooglePubSubMessageStoragePolicy googlePubsubMessageStoragePolicy =
new GooglePubSubMessageStoragePolicy(List.of());
GooglePubSubSchemaSettings googlePubSubSchemaSettings =
new GooglePubSubSchemaSettings("BINARY", "", "", "project/test");
assertThat(binding.getBinding())
.isEqualTo(new GooglePubSubChannelBinding(
null, "", googlePubsubMessageStoragePolicy, googlePubSubSchemaSettings, "0.2.0"));
}

@Test
Expand All @@ -35,7 +43,6 @@ void processWithoutAnnotationTest() throws NoSuchMethodException {
}

@GooglePubSubAsyncChannelBinding(
messageStoragePolicy = @GooglePubsubAsyncMessageStoragePolicy,
schemaSettings = @GooglePubSubAsyncSchemaSetting(encoding = "BINARY", name = "project/test"))
public void methodWithAnnotation() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.springwolf.asyncapi.v3.bindings.ChannelBinding;
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubChannelBinding;
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubMessageBinding;
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubMessageStoragePolicy;
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubSchemaSettings;
import io.github.springwolf.asyncapi.v3.model.channel.ChannelObject;
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageHeaders;
Expand All @@ -14,7 +15,6 @@
import io.github.springwolf.asyncapi.v3.model.schema.SchemaReference;
import io.github.springwolf.bindings.googlepubsub.annotations.GooglePubSubAsyncChannelBinding;
import io.github.springwolf.bindings.googlepubsub.annotations.GooglePubSubAsyncSchemaSetting;
import io.github.springwolf.bindings.googlepubsub.annotations.GooglePubsubAsyncMessageStoragePolicy;
import io.github.springwolf.bindings.googlepubsub.configuration.SpringwolfGooglePubSubBindingAutoConfiguration;
import io.github.springwolf.core.asyncapi.components.DefaultComponentsService;
import io.github.springwolf.core.asyncapi.components.SwaggerSchemaUtil;
Expand Down Expand Up @@ -44,6 +44,7 @@
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

Expand Down Expand Up @@ -90,8 +91,13 @@ public class CloudStreamFunctionChannelsBindingSchemaSettingIntegrationTest {

private GooglePubSubSchemaSettings googlePubSubSchemaSettings =
new GooglePubSubSchemaSettings("BINARY", "", "", "project/test");

private GooglePubSubMessageStoragePolicy googlePubsubMessageStoragePolicy =
new GooglePubSubMessageStoragePolicy(List.of());
private Map<String, ChannelBinding> channelBindingGooglePubSub = Map.of(
"googlepubsub", new GooglePubSubChannelBinding(null, null, null, googlePubSubSchemaSettings, "0.2.0"));
"googlepubsub",
new GooglePubSubChannelBinding(
null, "", googlePubsubMessageStoragePolicy, googlePubSubSchemaSettings, "0.2.0"));

@Test
void testConsumerBinding() {
Expand Down Expand Up @@ -128,7 +134,6 @@ void testConsumerBinding() {
public static class Configuration {
@Bean
@GooglePubSubAsyncChannelBinding(
messageStoragePolicy = @GooglePubsubAsyncMessageStoragePolicy(),
schemaSettings = @GooglePubSubAsyncSchemaSetting(encoding = "BINARY", name = "project/test"))
public Consumer<String> testPubSubConsumer() {
return System.out::println;
Expand Down

0 comments on commit e2bc117

Please sign in to comment.