-
Notifications
You must be signed in to change notification settings - Fork 1
Introduced AggregateDefinitionClient. #21
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
Open
jeham
wants to merge
1
commit into
main
Choose a base branch
from
14-support-for-defining-uniqueness-constraint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/main/java/io/serialized/client/aggregate/AggregateDefinitionClient.java
This file contains hidden or 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,109 @@ | ||
| package io.serialized.client.aggregate; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
| import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.serialized.client.SerializedClientConfig; | ||
| import io.serialized.client.SerializedOkHttpClient; | ||
| import okhttp3.HttpUrl; | ||
| import okhttp3.OkHttpClient; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL; | ||
| import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; | ||
| import static com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS; | ||
|
|
||
| public class AggregateDefinitionClient { | ||
|
|
||
| private final SerializedOkHttpClient client; | ||
| private final HttpUrl apiRoot; | ||
| private final ObjectMapper objectMapper; | ||
|
|
||
| private AggregateDefinitionClient(Builder builder) { | ||
| this.client = new SerializedOkHttpClient(builder.httpClient, builder.objectMapper); | ||
| this.apiRoot = builder.apiRoot; | ||
| this.objectMapper = builder.objectMapper; | ||
| } | ||
|
|
||
| public static Builder aggregateClient(SerializedClientConfig config) { | ||
| return new Builder(config); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an aggregate type definition from a JSON String value. | ||
| * | ||
| * @param jsonString a JSON String with a valid definition | ||
| * @throws IOException if the given String is not a valid definition | ||
| */ | ||
| public void createDefinition(String jsonString) throws IOException { | ||
| AggregateTypeDefinition aggregateTypeDefinition = objectMapper.readValue(jsonString, AggregateTypeDefinition.class); | ||
| createDefinition(aggregateTypeDefinition); | ||
| } | ||
|
|
||
| public void createDefinition(AggregateTypeDefinition aggregateTypeDefinition) { | ||
| HttpUrl url = pathForDefinitions().build(); | ||
| client.post(url, aggregateTypeDefinition); | ||
| } | ||
|
|
||
| /** | ||
| * Get definition. | ||
| */ | ||
| public AggregateTypeDefinition getDefinition(String aggregateType) { | ||
| HttpUrl url = pathForDefinitions().addPathSegment(aggregateType).build(); | ||
| return client.get(url, AggregateTypeDefinition.class); | ||
| } | ||
|
|
||
| /** | ||
| * List all definitions. | ||
| */ | ||
| public AggregateTypeDefinitions listDefinitions() { | ||
| HttpUrl url = pathForDefinitions().build(); | ||
| return client.get(url, AggregateTypeDefinitions.class); | ||
| } | ||
|
|
||
| /** | ||
| * Delete the definition. | ||
| */ | ||
| public void deleteDefinition(String aggregateType) { | ||
| HttpUrl url = pathForDefinitions().addPathSegment(aggregateType).build(); | ||
| client.delete(url); | ||
| } | ||
|
|
||
| private HttpUrl.Builder pathForDefinitions() { | ||
| return apiRoot.newBuilder() | ||
| .addPathSegment("aggregates") | ||
| .addPathSegment("definitions"); | ||
| } | ||
|
|
||
| public static class Builder { | ||
|
|
||
| private final ObjectMapper objectMapper = new ObjectMapper() | ||
| .disable(FAIL_ON_UNKNOWN_PROPERTIES) | ||
| .disable(FAIL_ON_EMPTY_BEANS) | ||
| .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) | ||
| .setSerializationInclusion(NON_NULL); | ||
|
|
||
| private final HttpUrl apiRoot; | ||
| private final OkHttpClient httpClient; | ||
|
|
||
| Builder(SerializedClientConfig config) { | ||
| this.apiRoot = config.apiRoot(); | ||
| this.httpClient = config.newHttpClient(); | ||
| } | ||
|
|
||
| /** | ||
| * Allows object mapper customization. | ||
| */ | ||
| public Builder configureObjectMapper(Consumer<ObjectMapper> consumer) { | ||
| consumer.accept(objectMapper); | ||
| return this; | ||
| } | ||
|
|
||
| public AggregateDefinitionClient build() { | ||
| return new AggregateDefinitionClient(this); | ||
| } | ||
| } | ||
|
|
||
| } |
83 changes: 83 additions & 0 deletions
83
src/main/java/io/serialized/client/aggregate/AggregateTypeDefinition.java
This file contains hidden or 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,83 @@ | ||
| package io.serialized.client.aggregate; | ||
|
|
||
| import org.apache.commons.lang3.builder.EqualsBuilder; | ||
| import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
| import org.apache.commons.lang3.builder.ToStringBuilder; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static java.util.Collections.unmodifiableList; | ||
| import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE; | ||
|
|
||
| public class AggregateTypeDefinition { | ||
|
|
||
| private String aggregateType; | ||
| private String description; | ||
| private List<AggregateTypeRule> rules; | ||
|
|
||
| public String aggregateType() { | ||
| return aggregateType; | ||
| } | ||
|
|
||
| public String description() { | ||
| return description; | ||
| } | ||
|
|
||
| public List<AggregateTypeRule> rules() { | ||
| return unmodifiableList(rules); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return HashCodeBuilder.reflectionHashCode(this); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| return EqualsBuilder.reflectionEquals(this, obj); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE); | ||
| } | ||
|
|
||
| public static DefinitionBuilder newAggregateTypeDefinition(String aggregateType) { | ||
| return new DefinitionBuilder(aggregateType); | ||
| } | ||
|
|
||
| public static class DefinitionBuilder { | ||
| private final String aggregateType; | ||
| private final List<AggregateTypeRule> rules = new ArrayList<>(); | ||
|
|
||
| private String description; | ||
|
|
||
| DefinitionBuilder(String aggregateType) { | ||
| this.aggregateType = aggregateType; | ||
| } | ||
|
|
||
| /** | ||
| * @param description Optional description | ||
| */ | ||
| public DefinitionBuilder description(String description) { | ||
| this.description = description; | ||
| return this; | ||
| } | ||
|
|
||
| public DefinitionBuilder withRule(AggregateTypeRule rule) { | ||
| this.rules.add(rule); | ||
| return this; | ||
| } | ||
|
|
||
| public AggregateTypeDefinition build() { | ||
| AggregateTypeDefinition definition = new AggregateTypeDefinition(); | ||
| definition.aggregateType = aggregateType; | ||
| definition.description = description; | ||
| definition.rules = rules; | ||
| return definition; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
46 changes: 46 additions & 0 deletions
46
src/main/java/io/serialized/client/aggregate/AggregateTypeDefinitions.java
This file contains hidden or 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,46 @@ | ||
| package io.serialized.client.aggregate; | ||
|
|
||
| import org.apache.commons.lang3.builder.EqualsBuilder; | ||
| import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
| import org.apache.commons.lang3.builder.ToStringBuilder; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
| import static java.util.Collections.unmodifiableList; | ||
| import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE; | ||
|
|
||
| public class AggregateTypeDefinitions { | ||
|
|
||
| private List<AggregateTypeDefinition> definitions; | ||
| private int totalCount; | ||
| private boolean hasMore; | ||
|
|
||
| public static AggregateTypeDefinitions newDefinitionList(Collection<AggregateTypeDefinition> definitions) { | ||
| AggregateTypeDefinitions reactionDefinitions = new AggregateTypeDefinitions(); | ||
| reactionDefinitions.definitions = new ArrayList<>(definitions); | ||
| return reactionDefinitions; | ||
| } | ||
|
|
||
| public List<AggregateTypeDefinition> definitions() { | ||
| return definitions == null ? emptyList() : unmodifiableList(definitions); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return HashCodeBuilder.reflectionHashCode(this); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| return EqualsBuilder.reflectionEquals(this, obj); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE); | ||
| } | ||
|
|
||
| } | ||
96 changes: 96 additions & 0 deletions
96
src/main/java/io/serialized/client/aggregate/AggregateTypeRule.java
This file contains hidden or 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,96 @@ | ||
| package io.serialized.client.aggregate; | ||
|
|
||
| import org.apache.commons.lang3.Validate; | ||
| import org.apache.commons.lang3.builder.EqualsBuilder; | ||
| import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
| import org.apache.commons.lang3.builder.ToStringBuilder; | ||
|
|
||
| import java.util.LinkedHashSet; | ||
| import java.util.Set; | ||
|
|
||
| import static java.util.Collections.unmodifiableSet; | ||
| import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE; | ||
|
|
||
| public class AggregateTypeRule { | ||
|
|
||
| public enum Type { | ||
| UNIQUENESS | ||
| } | ||
|
|
||
| private Type type; | ||
| private String eventType; | ||
| private final Set<String> fields = new LinkedHashSet<>(); | ||
|
|
||
| public static Builder rule(Type type) { | ||
| return new Builder(type); | ||
| } | ||
|
|
||
| public static Builder newRule(Type type, String eventType, String... fields) { | ||
| Builder builder = new Builder(type).withEventType(eventType); | ||
| for (String field : fields) { | ||
| builder.addField(field); | ||
| } | ||
| return builder; | ||
| } | ||
|
|
||
| public Type type() { | ||
| return type; | ||
| } | ||
|
|
||
| public String eventType() { | ||
| return eventType; | ||
| } | ||
|
|
||
| public Set<String> fields() { | ||
| return unmodifiableSet(fields); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return HashCodeBuilder.reflectionHashCode(this); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| return EqualsBuilder.reflectionEquals(this, obj); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE); | ||
| } | ||
|
|
||
| public static class Builder { | ||
|
|
||
| private final Type type; | ||
| private final Set<String> fields = new LinkedHashSet<>(); | ||
| private String eventType; | ||
|
|
||
| public Builder(Type type) { | ||
| this.type = type; | ||
| } | ||
|
|
||
| public Builder withEventType(String eventType) { | ||
| this.eventType = eventType; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder addField(String field) { | ||
| this.fields.add(field); | ||
| return this; | ||
| } | ||
|
|
||
| public AggregateTypeRule build() { | ||
| Validate.notEmpty(eventType, "'eventType' must be set"); | ||
| Validate.notEmpty(fields, "At least one 'field' must be specified"); | ||
|
|
||
| AggregateTypeRule aggregateTypeRule = new AggregateTypeRule(); | ||
| aggregateTypeRule.type = this.type; | ||
| aggregateTypeRule.eventType = this.eventType; | ||
| aggregateTypeRule.fields.addAll(this.fields); | ||
| return aggregateTypeRule; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.