-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced AggregateDefinitionClient.
- Loading branch information
Showing
4 changed files
with
314 additions
and
0 deletions.
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 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 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; | ||
} | ||
|
||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/io/serialized/client/aggregate/AggregateTypeDefinitions.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 @@ | ||
package io.serialized.client.aggregate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.unmodifiableList; | ||
|
||
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); | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/io/serialized/client/aggregate/AggregateTypeRule.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,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; | ||
} | ||
|
||
} | ||
|
||
} |