-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(fix): added annotation for google pubsub message binding (#677)
* (fix): added annotation for google pubsub message binding * (fix): CloudStreamFunctionChannelsScanner constructor issue * (fix): updated cloudstream googlepubsub example and added missing binding from auto-configuration * (fix): update annotation package, fix typo and removed unwanted target and retention annotation * refactor(core): use AnnotatedElement in MessageBindingProcessor Continuation of 86960c6 --------- Co-authored-by: sheheryar aamir <[email protected]>
- Loading branch information
1 parent
86960c6
commit 56f4090
Showing
24 changed files
with
215 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
.../github/springwolf/bindings/googlepubsub/annotations/GooglePubSubAsyncMessageBinding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.bindings.googlepubsub.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* {@code @GooglePubSubAsyncMessageBinding} is a method-level annotation. | ||
* It configures the message binding for the Google pubsub protocol. | ||
* @see io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubMessageBinding | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(value = {ElementType.METHOD, ElementType.ANNOTATION_TYPE}) | ||
@Inherited | ||
public @interface GooglePubSubAsyncMessageBinding { | ||
String type() default "googlepubsub"; | ||
|
||
String orderingKey() default ""; | ||
|
||
GooglePubSubAsyncMessageSchema schema() default @GooglePubSubAsyncMessageSchema; | ||
|
||
String bindingVersion() default "0.2.0"; | ||
} |
14 changes: 14 additions & 0 deletions
14
...o/github/springwolf/bindings/googlepubsub/annotations/GooglePubSubAsyncMessageSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.bindings.googlepubsub.annotations; | ||
|
||
import java.lang.annotation.Inherited; | ||
|
||
/** | ||
* {@code @GooglePubSubAsyncMessageSchema} is a method-level annotation. | ||
* It configures the message schema for the Google pubsub protocol. | ||
* @see io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubSchema | ||
*/ | ||
@Inherited | ||
public @interface GooglePubSubAsyncMessageSchema { | ||
String name() default ""; | ||
} |
9 changes: 9 additions & 0 deletions
9
...b/springwolf/bindings/googlepubsub/annotations/GooglePubSubAsyncMessageStoragePolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.bindings.googlepubsub.annotations; | ||
|
||
import java.lang.annotation.Inherited; | ||
|
||
@Inherited | ||
public @interface GooglePubSubAsyncMessageStoragePolicy { | ||
String[] allowedPersistenceRegions() default {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
...b/springwolf/bindings/googlepubsub/annotations/GooglePubsubAsyncMessageStoragePolicy.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ringwolf/bindings/googlepubsub/scanners/messages/GooglePubSubMessageBindingProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.bindings.googlepubsub.scanners.messages; | ||
|
||
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubMessageBinding; | ||
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubSchema; | ||
import io.github.springwolf.bindings.googlepubsub.annotations.GooglePubSubAsyncMessageBinding; | ||
import io.github.springwolf.core.asyncapi.scanners.bindings.messages.MessageBindingProcessor; | ||
import io.github.springwolf.core.asyncapi.scanners.bindings.messages.ProcessedMessageBinding; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.context.EmbeddedValueResolverAware; | ||
import org.springframework.util.StringValueResolver; | ||
|
||
import java.lang.reflect.AnnotatedElement; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
public class GooglePubSubMessageBindingProcessor implements MessageBindingProcessor, EmbeddedValueResolverAware { | ||
private StringValueResolver resolver; | ||
|
||
@Override | ||
public void setEmbeddedValueResolver(StringValueResolver resolver) { | ||
this.resolver = resolver; | ||
} | ||
|
||
@Override | ||
public Optional<ProcessedMessageBinding> process(AnnotatedElement annotatedElement) { | ||
return Arrays.stream(annotatedElement.getAnnotations()) | ||
.filter(GooglePubSubAsyncMessageBinding.class::isInstance) | ||
.map(GooglePubSubAsyncMessageBinding.class::cast) | ||
.findAny() | ||
.map(this::mapToMessageBinding); | ||
} | ||
|
||
private ProcessedMessageBinding mapToMessageBinding(GooglePubSubAsyncMessageBinding bindingAnnotation) { | ||
GooglePubSubSchema.GooglePubSubSchemaBuilder googlePubSubSchemaBuilder = GooglePubSubSchema.builder(); | ||
if (StringUtils.isNotBlank(bindingAnnotation.schema().name())) { | ||
googlePubSubSchemaBuilder.name(bindingAnnotation.schema().name()); | ||
} | ||
GooglePubSubMessageBinding.GooglePubSubMessageBindingBuilder bindingBuilder = | ||
GooglePubSubMessageBinding.builder().schema(googlePubSubSchemaBuilder.build()); | ||
if (StringUtils.isNotBlank(bindingAnnotation.orderingKey())) { | ||
bindingBuilder.orderingKey(bindingAnnotation.orderingKey()); | ||
} | ||
if (StringUtils.isNotBlank(bindingAnnotation.bindingVersion())) { | ||
bindingBuilder.bindingVersion(bindingAnnotation.bindingVersion()); | ||
} | ||
return new ProcessedMessageBinding(bindingAnnotation.type(), bindingBuilder.build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...pringwolf-googlepubsub-binding/src/test/java/GooglePubSubMessageBindingProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubMessageBinding; | ||
import io.github.springwolf.asyncapi.v3.bindings.googlepubsub.GooglePubSubSchema; | ||
import io.github.springwolf.bindings.googlepubsub.annotations.GooglePubSubAsyncMessageBinding; | ||
import io.github.springwolf.bindings.googlepubsub.annotations.GooglePubSubAsyncMessageSchema; | ||
import io.github.springwolf.bindings.googlepubsub.scanners.messages.GooglePubSubMessageBindingProcessor; | ||
import io.github.springwolf.core.asyncapi.scanners.bindings.messages.ProcessedMessageBinding; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class GooglePubSubMessageBindingProcessorTest { | ||
private final GooglePubSubMessageBindingProcessor processor = new GooglePubSubMessageBindingProcessor(); | ||
|
||
@Test | ||
void processTest() throws NoSuchMethodException { | ||
// given | ||
Method method = GooglePubSubMessageBindingProcessorTest.class.getMethod("methodWithAnnotation"); | ||
|
||
// when | ||
ProcessedMessageBinding binding = processor.process(method).get(); | ||
|
||
// then | ||
assertThat(binding.getType()).isEqualTo("googlepubsub"); | ||
assertThat(binding.getBinding()) | ||
.isEqualTo( | ||
new GooglePubSubMessageBinding(null, "key", new GooglePubSubSchema("project/test"), "0.2.0")); | ||
} | ||
|
||
@Test | ||
void processWithoutAnnotationTest() throws NoSuchMethodException { | ||
// given | ||
Method method = GooglePubSubChannelBindingProcessorTest.class.getMethod("methodWithoutAnnotation"); | ||
|
||
// when | ||
Optional<ProcessedMessageBinding> binding = processor.process(method); | ||
|
||
// then | ||
assertThat(binding).isNotPresent(); | ||
} | ||
|
||
@GooglePubSubAsyncMessageBinding( | ||
orderingKey = "key", | ||
schema = @GooglePubSubAsyncMessageSchema(name = "project/test")) | ||
public void methodWithAnnotation() {} | ||
|
||
public void methodWithoutAnnotation() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.