Skip to content

Commit 5f9ef87

Browse files
committed
Introduced AggregateDefinitionClient.
1 parent 1b041ef commit 5f9ef87

6 files changed

Lines changed: 374 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package io.serialized.client.aggregate;
2+
3+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
4+
import com.fasterxml.jackson.annotation.PropertyAccessor;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import io.serialized.client.SerializedClientConfig;
7+
import io.serialized.client.SerializedOkHttpClient;
8+
import okhttp3.HttpUrl;
9+
import okhttp3.OkHttpClient;
10+
11+
import java.io.IOException;
12+
import java.util.function.Consumer;
13+
14+
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
15+
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
16+
import static com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS;
17+
18+
public class AggregateDefinitionClient {
19+
20+
private final SerializedOkHttpClient client;
21+
private final HttpUrl apiRoot;
22+
private final ObjectMapper objectMapper;
23+
24+
private AggregateDefinitionClient(Builder builder) {
25+
this.client = new SerializedOkHttpClient(builder.httpClient, builder.objectMapper);
26+
this.apiRoot = builder.apiRoot;
27+
this.objectMapper = builder.objectMapper;
28+
}
29+
30+
public static Builder aggregateClient(SerializedClientConfig config) {
31+
return new Builder(config);
32+
}
33+
34+
/**
35+
* Creates an aggregate type definition from a JSON String value.
36+
*
37+
* @param jsonString a JSON String with a valid definition
38+
* @throws IOException if the given String is not a valid definition
39+
*/
40+
public void createDefinition(String jsonString) throws IOException {
41+
AggregateTypeDefinition aggregateTypeDefinition = objectMapper.readValue(jsonString, AggregateTypeDefinition.class);
42+
createDefinition(aggregateTypeDefinition);
43+
}
44+
45+
public void createDefinition(AggregateTypeDefinition aggregateTypeDefinition) {
46+
HttpUrl url = pathForDefinitions().build();
47+
client.post(url, aggregateTypeDefinition);
48+
}
49+
50+
/**
51+
* Get definition.
52+
*/
53+
public AggregateTypeDefinition getDefinition(String aggregateType) {
54+
HttpUrl url = pathForDefinitions().addPathSegment(aggregateType).build();
55+
return client.get(url, AggregateTypeDefinition.class);
56+
}
57+
58+
/**
59+
* List all definitions.
60+
*/
61+
public AggregateTypeDefinitions listDefinitions() {
62+
HttpUrl url = pathForDefinitions().build();
63+
return client.get(url, AggregateTypeDefinitions.class);
64+
}
65+
66+
/**
67+
* Delete the definition.
68+
*/
69+
public void deleteDefinition(String aggregateType) {
70+
HttpUrl url = pathForDefinitions().addPathSegment(aggregateType).build();
71+
client.delete(url);
72+
}
73+
74+
private HttpUrl.Builder pathForDefinitions() {
75+
return apiRoot.newBuilder()
76+
.addPathSegment("aggregates")
77+
.addPathSegment("definitions");
78+
}
79+
80+
public static class Builder {
81+
82+
private final ObjectMapper objectMapper = new ObjectMapper()
83+
.disable(FAIL_ON_UNKNOWN_PROPERTIES)
84+
.disable(FAIL_ON_EMPTY_BEANS)
85+
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
86+
.setSerializationInclusion(NON_NULL);
87+
88+
private final HttpUrl apiRoot;
89+
private final OkHttpClient httpClient;
90+
91+
Builder(SerializedClientConfig config) {
92+
this.apiRoot = config.apiRoot();
93+
this.httpClient = config.newHttpClient();
94+
}
95+
96+
/**
97+
* Allows object mapper customization.
98+
*/
99+
public Builder configureObjectMapper(Consumer<ObjectMapper> consumer) {
100+
consumer.accept(objectMapper);
101+
return this;
102+
}
103+
104+
public AggregateDefinitionClient build() {
105+
return new AggregateDefinitionClient(this);
106+
}
107+
}
108+
109+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package io.serialized.client.aggregate;
2+
3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import static java.util.Collections.unmodifiableList;
11+
import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE;
12+
13+
public class AggregateTypeDefinition {
14+
15+
private String aggregateType;
16+
private String description;
17+
private List<AggregateTypeRule> rules;
18+
19+
public String aggregateType() {
20+
return aggregateType;
21+
}
22+
23+
public String description() {
24+
return description;
25+
}
26+
27+
public List<AggregateTypeRule> rules() {
28+
return unmodifiableList(rules);
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return HashCodeBuilder.reflectionHashCode(this);
34+
}
35+
36+
@Override
37+
public boolean equals(Object obj) {
38+
return EqualsBuilder.reflectionEquals(this, obj);
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE);
44+
}
45+
46+
public static DefinitionBuilder newAggregateTypeDefinition(String aggregateType) {
47+
return new DefinitionBuilder(aggregateType);
48+
}
49+
50+
public static class DefinitionBuilder {
51+
private final String aggregateType;
52+
private final List<AggregateTypeRule> rules = new ArrayList<>();
53+
54+
private String description;
55+
56+
DefinitionBuilder(String aggregateType) {
57+
this.aggregateType = aggregateType;
58+
}
59+
60+
/**
61+
* @param description Optional description
62+
*/
63+
public DefinitionBuilder description(String description) {
64+
this.description = description;
65+
return this;
66+
}
67+
68+
public DefinitionBuilder withRule(AggregateTypeRule rule) {
69+
this.rules.add(rule);
70+
return this;
71+
}
72+
73+
public AggregateTypeDefinition build() {
74+
AggregateTypeDefinition definition = new AggregateTypeDefinition();
75+
definition.aggregateType = aggregateType;
76+
definition.description = description;
77+
definition.rules = rules;
78+
return definition;
79+
}
80+
81+
}
82+
83+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.serialized.client.aggregate;
2+
3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collection;
9+
import java.util.List;
10+
11+
import static java.util.Collections.emptyList;
12+
import static java.util.Collections.unmodifiableList;
13+
import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE;
14+
15+
public class AggregateTypeDefinitions {
16+
17+
private List<AggregateTypeDefinition> definitions;
18+
private int totalCount;
19+
private boolean hasMore;
20+
21+
public static AggregateTypeDefinitions newDefinitionList(Collection<AggregateTypeDefinition> definitions) {
22+
AggregateTypeDefinitions reactionDefinitions = new AggregateTypeDefinitions();
23+
reactionDefinitions.definitions = new ArrayList<>(definitions);
24+
return reactionDefinitions;
25+
}
26+
27+
public List<AggregateTypeDefinition> definitions() {
28+
return definitions == null ? emptyList() : unmodifiableList(definitions);
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return HashCodeBuilder.reflectionHashCode(this);
34+
}
35+
36+
@Override
37+
public boolean equals(Object obj) {
38+
return EqualsBuilder.reflectionEquals(this, obj);
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE);
44+
}
45+
46+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package io.serialized.client.aggregate;
2+
3+
import org.apache.commons.lang3.Validate;
4+
import org.apache.commons.lang3.builder.EqualsBuilder;
5+
import org.apache.commons.lang3.builder.HashCodeBuilder;
6+
import org.apache.commons.lang3.builder.ToStringBuilder;
7+
8+
import java.util.LinkedHashSet;
9+
import java.util.Set;
10+
11+
import static java.util.Collections.unmodifiableSet;
12+
import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE;
13+
14+
public class AggregateTypeRule {
15+
16+
public enum Type {
17+
UNIQUENESS
18+
}
19+
20+
private Type type;
21+
private String eventType;
22+
private final Set<String> fields = new LinkedHashSet<>();
23+
24+
public static Builder rule(Type type) {
25+
return new Builder(type);
26+
}
27+
28+
public static Builder newRule(Type type, String eventType, String... fields) {
29+
Builder builder = new Builder(type).withEventType(eventType);
30+
for (String field : fields) {
31+
builder.addField(field);
32+
}
33+
return builder;
34+
}
35+
36+
public Type type() {
37+
return type;
38+
}
39+
40+
public String eventType() {
41+
return eventType;
42+
}
43+
44+
public Set<String> fields() {
45+
return unmodifiableSet(fields);
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return HashCodeBuilder.reflectionHashCode(this);
51+
}
52+
53+
@Override
54+
public boolean equals(Object obj) {
55+
return EqualsBuilder.reflectionEquals(this, obj);
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE);
61+
}
62+
63+
public static class Builder {
64+
65+
private final Type type;
66+
private final Set<String> fields = new LinkedHashSet<>();
67+
private String eventType;
68+
69+
public Builder(Type type) {
70+
this.type = type;
71+
}
72+
73+
public Builder withEventType(String eventType) {
74+
this.eventType = eventType;
75+
return this;
76+
}
77+
78+
public Builder addField(String field) {
79+
this.fields.add(field);
80+
return this;
81+
}
82+
83+
public AggregateTypeRule build() {
84+
Validate.notEmpty(eventType, "'eventType' must be set");
85+
Validate.notEmpty(fields, "At least one 'field' must be specified");
86+
87+
AggregateTypeRule aggregateTypeRule = new AggregateTypeRule();
88+
aggregateTypeRule.type = this.type;
89+
aggregateTypeRule.eventType = this.eventType;
90+
aggregateTypeRule.fields.addAll(this.fields);
91+
return aggregateTypeRule;
92+
}
93+
94+
}
95+
96+
}

src/main/java/io/serialized/client/projection/ProjectionDefinitions.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package io.serialized.client.projection;
22

3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
import org.apache.commons.lang3.builder.ToStringBuilder;
6+
37
import java.util.ArrayList;
48
import java.util.Collection;
59
import java.util.List;
610

711
import static java.util.Collections.emptyList;
812
import static java.util.Collections.unmodifiableList;
13+
import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE;
914

1015
public class ProjectionDefinitions {
1116

@@ -21,4 +26,19 @@ public List<ProjectionDefinition> definitions() {
2126
return definitions == null ? emptyList() : unmodifiableList(definitions);
2227
}
2328

29+
@Override
30+
public int hashCode() {
31+
return HashCodeBuilder.reflectionHashCode(this);
32+
}
33+
34+
@Override
35+
public boolean equals(Object obj) {
36+
return EqualsBuilder.reflectionEquals(this, obj);
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return ToStringBuilder.reflectionToString(this, SHORT_PREFIX_STYLE);
42+
}
43+
2444
}

0 commit comments

Comments
 (0)