Skip to content

Commit 9d662fe

Browse files
Merge pull request #186 from SpineEventEngine/enrichment-enhancement
Enhancements for testing
2 parents 6386222 + 162d5fa commit 9d662fe

File tree

13 files changed

+237
-127
lines changed

13 files changed

+237
-127
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ allprojects {
5454
apply plugin: 'idea'
5555

5656
group = 'org.spine3'
57-
version = '0.5.9-SNAPSHOT'
57+
version = '0.5.18-SNAPSHOT'
5858
}
5959

6060
ext {

client/src/main/java/org/spine3/base/Mismatch.java

Lines changed: 54 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@
2020

2121
package org.spine3.base;
2222

23-
import com.google.protobuf.Any;
2423
import com.google.protobuf.Message;
2524
import org.spine3.protobuf.AnyPacker;
2625

2726
import javax.annotation.Nullable;
2827

2928
import static org.spine3.protobuf.Values.pack;
3029

31-
32-
/** Factories for constructing {@link ValueMismatch} instances for different types of attributes. */
30+
/**
31+
* Factories for constructing {@link ValueMismatch} instances for different types of attributes.
32+
*
33+
* @author Alexander Yevsyukov
34+
* @author Andrey Lavrov
35+
*/
3336
public class Mismatch {
3437

3538
private Mismatch() {
@@ -38,148 +41,120 @@ private Mismatch() {
3841
/**
3942
* Creates a {@link ValueMismatch} instance for a string attribute.
4043
*
41-
* @param expected the value expected by a command, or {@code null} if the command expects not populated field
42-
* @param actual the value found in an entity, or {@code null} if the value is not set
43-
* @param requested the value requested as new one in the original command
44-
* @param version the current version of the entity
44+
* @param expected the value expected by a command, or {@code null} if the command expects not populated field
45+
* @param actual the value found in an entity, or {@code null} if the value is not set
46+
* @param version the current version of the entity
4547
* @return info on the mismatch
4648
*/
47-
public static ValueMismatch of(@Nullable String expected, @Nullable String actual, String requested, int version) {
49+
public static ValueMismatch of(@Nullable String expected, @Nullable String actual, int version) {
4850
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
4951
if (expected != null) {
5052
builder.setExpected(pack(expected));
5153
}
52-
5354
if (actual != null) {
5455
builder.setActual(pack(actual));
5556
}
56-
57-
builder.setRequested(pack(requested));
5857
builder.setVersion(version);
59-
6058
return builder.build();
6159
}
6260

6361
/**
6462
* Creates a {@link ValueMismatch} instance for a integer attribute.
6563
*
66-
* @param expected the value expected by a command
67-
* @param actual the value actual in an entity
68-
* @param requested the value requested as new one in the original command
69-
* @param version the current version of the entity
64+
* @param expected the value expected by a command
65+
* @param actual the value actual in an entity
66+
* @param version the current version of the entity
7067
* @return info on the mismatch
7168
*/
72-
public static ValueMismatch of(int expected, int actual, int requested, int version) {
73-
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
74-
builder.setExpected(pack(expected));
75-
builder.setActual(pack(actual));
76-
builder.setRequested(pack(requested));
77-
builder.setVersion(version);
78-
69+
public static ValueMismatch of(int expected, int actual, int version) {
70+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder()
71+
.setExpected(pack(expected))
72+
.setActual(pack(actual))
73+
.setVersion(version);
7974
return builder.build();
8075
}
8176

8277
/**
8378
* Creates a {@link ValueMismatch} instance for a long integer attribute.
8479
*
85-
* @param expected the value expected by a command
86-
* @param actual the value actual in an entity
87-
* @param requested the value requested as new one in the original command
88-
* @param version the current version of the entity
80+
* @param expected the value expected by a command
81+
* @param actual the value actual in an entity
82+
* @param version the current version of the entity
8983
* @return info on the mismatch
9084
*/
91-
public static ValueMismatch of(long expected, long actual, long requested, int version) {
92-
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
93-
builder.setExpected(pack(expected));
94-
builder.setActual(pack(actual));
95-
builder.setRequested(pack(requested));
96-
builder.setVersion(version);
97-
85+
public static ValueMismatch of(long expected, long actual, int version) {
86+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder()
87+
.setExpected(pack(expected))
88+
.setActual(pack(actual))
89+
.setVersion(version);
9890
return builder.build();
9991
}
10092

10193
/**
10294
* Creates a {@link ValueMismatch} instance for a float attribute.
10395
*
104-
* @param expected the value expected by a command
105-
* @param actual the value actual in an entity
106-
* @param requested the value requested as new one in the original command
107-
* @param version the current version of the entity
96+
* @param expected the value expected by a command
97+
* @param actual the value actual in an entity
98+
* @param version the current version of the entity
10899
* @return info on the mismatch
109100
*/
110-
public static ValueMismatch of(float expected, float actual, float requested, int version) {
111-
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
112-
builder.setExpected(pack(expected));
113-
builder.setActual(pack(actual));
114-
builder.setRequested(pack(requested));
115-
builder.setVersion(version);
116-
101+
public static ValueMismatch of(float expected, float actual, int version) {
102+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder()
103+
.setExpected(pack(expected))
104+
.setActual(pack(actual))
105+
.setVersion(version);
117106
return builder.build();
118107
}
119108

120109
/**
121110
* Creates a {@link ValueMismatch} instance for a double attribute.
122111
*
123-
* @param expected the value expected by a command
124-
* @param actual the value actual in an entity
125-
* @param requested the value requested as new one in the original command
126-
* @param version the current version of the entity
112+
* @param expected the value expected by a command
113+
* @param actual the value actual in an entity
114+
* @param version the current version of the entity
127115
* @return info on the mismatch
128116
*/
129-
public static ValueMismatch of(double expected, double actual, double requested, int version) {
130-
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
131-
builder.setExpected(pack(expected));
132-
builder.setActual(pack(actual));
133-
builder.setRequested(pack(requested));
134-
builder.setVersion(version);
135-
117+
public static ValueMismatch of(double expected, double actual, int version) {
118+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder()
119+
.setExpected(pack(expected))
120+
.setActual(pack(actual))
121+
.setVersion(version);
136122
return builder.build();
137123
}
138124

139125
/**
140126
* Creates a {@link ValueMismatch} instance for a boolean attribute.
141127
*
142-
* @param expected the value expected by a command
143-
* @param actual the value actual in an entity
144-
* @param requested the value requested as new one in the original command
145-
* @param version the current version of the entity
128+
* @param expected the value expected by a command
129+
* @param actual the value actual in an entity
130+
* @param version the current version of the entity
146131
* @return info on the mismatch
147132
*/
148-
public static ValueMismatch of(boolean expected, boolean actual, boolean requested, int version) {
149-
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
150-
builder.setExpected(pack(expected));
151-
builder.setActual(pack(actual));
152-
builder.setRequested(pack(requested));
153-
builder.setVersion(version);
154-
133+
public static ValueMismatch of(boolean expected, boolean actual, int version) {
134+
final ValueMismatch.Builder builder = ValueMismatch.newBuilder()
135+
.setExpected(pack(expected))
136+
.setActual(pack(actual))
137+
.setVersion(version);
155138
return builder.build();
156139
}
157140

158141
/**
159142
* Creates a {@link ValueMismatch} instance for a Message attribute.
160143
*
161-
* @param expected the value expected by a command, or {@code null} if the command expects not populated field
162-
* @param actual the value actual in an entity, or {@code null} if the value is not set
163-
* @param requested the value requested as new one in the original command
164-
* @param version the current version of the entity
144+
* @param expected the value expected by a command, or {@code null} if the command expects not populated field
145+
* @param actual the value actual in an entity, or {@code null} if the value is not set
146+
* @param version the current version of the entity
165147
* @return info on the mismatch
166148
*/
167-
public static ValueMismatch of(@Nullable Message expected, @Nullable Message actual, Message requested, int version) {
149+
public static ValueMismatch of(@Nullable Message expected, @Nullable Message actual, int version) {
168150
final ValueMismatch.Builder builder = ValueMismatch.newBuilder();
169-
170151
if (expected != null) {
171152
builder.setExpected(AnyPacker.pack(expected));
172153
}
173-
174154
if (actual != null) {
175155
builder.setActual(AnyPacker.pack(actual));
176156
}
177-
178-
final Any requestedAny = AnyPacker.pack(requested);
179-
180-
builder.setRequested(requestedAny);
181157
builder.setVersion(version);
182-
183158
return builder.build();
184159
}
185160
}

client/src/main/java/org/spine3/protobuf/Timestamps.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ public static Timestamp getCurrentTime() {
109109
return result;
110110
}
111111

112+
/**
113+
* Obtains system time.
114+
*
115+
* <p>Unlike {@link #getCurrentTime()} this method <strong>always</strong> uses system time millis.
116+
*
117+
* @return current system time
118+
*/
119+
public static Timestamp systemTime() {
120+
return com.google.protobuf.util.Timestamps.fromMillis(System.currentTimeMillis());
121+
}
122+
112123
/**
113124
* The provider of the current time.
114125
*
@@ -133,6 +144,13 @@ public static void setProvider(Provider provider) {
133144
timeProvider.set(checkNotNull(provider));
134145
}
135146

147+
/**
148+
* Sets the default current time provider that obtains current time from system millis.
149+
*/
150+
public static void resetProvider() {
151+
timeProvider.set(new SystemTimeProvider());
152+
}
153+
136154
/**
137155
* Default implementation of current time provider based on {@link System#currentTimeMillis()}.
138156
*

client/src/main/proto/spine/base/command.proto

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import "google/protobuf/duration.proto";
3232
import "google/protobuf/any.proto";
3333

3434
import "spine/annotations.proto";
35+
import "spine/validate.proto";
3536
import "spine/users/user_id.proto";
3637
import "spine/users/tenant_id.proto";
3738
import "spine/time/zone_offset.proto";
@@ -57,32 +58,38 @@ message CommandId {
5758
// There should be <strong>one and only one</strong> command handler associated with the type of the command.
5859
//
5960
message Command {
61+
6062
// The message of the command wrapped into `Any`.
61-
google.protobuf.Any message = 1;
63+
google.protobuf.Any message = 1 [(required).value = true];
6264

6365
// Service information about the environment in which the command was created.
64-
CommandContext context = 2;
66+
CommandContext context = 2 [(required).value = true];
6567
}
6668

6769
// Meta-information about the command and the environment, which generated the command.
6870
message CommandContext {
71+
6972
// The id of the command.
70-
CommandId command_id = 1;
73+
CommandId command_id = 1 [(required).value = true];
7174

7275
// The user who created the command.
73-
users.UserId actor = 2;
76+
users.UserId actor = 2 [(required).value = true];
7477

7578
// The time when the command was created.
76-
google.protobuf.Timestamp timestamp = 3;
79+
google.protobuf.Timestamp timestamp = 3 [(required).value = true];
7780

7881
// The zone offset from which the command is made.
79-
time.ZoneOffset zone_offset = 4;
82+
time.ZoneOffset zone_offset = 4 [(required).value = true];
8083

8184
// The ID of a tenant in a multitenant application.
85+
//
86+
// This field is not populated in single-tenant applications.
87+
//
8288
users.TenantId tenant_id = 5;
8389

8490
// A command may be scheduled. This type defines scheduling options.
8591
message Schedule {
92+
8693
// The delay between the moment of receiving a command at the server and its delivery to the target.
8794
google.protobuf.Duration delay = 1;
8895

@@ -101,12 +108,15 @@ message CommandContext {
101108
Schedule schedule = 6;
102109

103110
// The version of the entity for which this command is intended.
111+
//
104112
// A client app may not fill in this attribute if the command applies to any version of the entity.
113+
//
105114
int32 target_version = 7;
106115
}
107116

108117
// Enumeration of possible failures when validating a command.
109118
enum CommandValidationError {
119+
110120
// Reserved value.
111121
UNKNOWN = 0;
112122

@@ -122,6 +132,7 @@ enum CommandValidationError {
122132

123133
// This enumeration defines possible statuses of command processing.
124134
enum CommandStatus {
135+
125136
// Reserved value.
126137
UNDEFINED = 0;
127138

client/src/main/proto/spine/base/failure.proto

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import "google/protobuf/timestamp.proto";
3232
import "google/protobuf/struct.proto";
3333

3434
import "spine/annotations.proto";
35+
import "spine/validate.proto";
3536

3637
// A business failure is a condition in business, which does not allow to
3738
// satisfy a request or perform an operation.
@@ -41,33 +42,37 @@ import "spine/annotations.proto";
4142
// See `spine.base.Error` for definition of a technical error.
4243
//
4344
message Failure {
45+
4446
// The instance of the failure generated by an aggregate or another part of business logic code.
45-
google.protobuf.Any instance = 1;
47+
google.protobuf.Any instance = 1 [(required).value = true];
4648

4749
// A stacktrace of from the code, which generated the failure instance (if available).
4850
string stacktrace = 2;
4951

5052
// The moment when the failure instance (not this message) was created.
51-
google.protobuf.Timestamp timestamp = 3;
53+
google.protobuf.Timestamp timestamp = 3 [(required).value = true];
5254

5355
// Additional information on the failure.
5456
map<string, google.protobuf.Value> attributes = 4;
5557
}
5658

57-
// A failure to handle a command because an entity contains a value different than requested in a command.
59+
// A failure to handle a command because an entity contains a value different than expected in a command.
5860
message ValueMismatch {
59-
// An expected value.
61+
62+
option (required_field) = "expected|actual";
63+
64+
// A value which a command expected in an entity field.
65+
//
6066
// This field is not populated if the command expects to initialize the attribute.
67+
//
6168
google.protobuf.Any expected = 1;
6269

6370
// An actual value in the entity.
71+
//
6472
// This field is not populated if the entity does not have the attribute set.
73+
//
6574
google.protobuf.Any actual = 2;
6675

67-
// An optional attribute that may contain a value requested as the replacement
68-
// for the `expected` value in the failed command.
69-
google.protobuf.Any requested = 3;
70-
71-
// A version of the entity, which generated the failure.
72-
int32 version = 4;
73-
}
76+
// A version of the entity which generated the failure.
77+
int32 version = 3 [(required).value = true];
78+
}

0 commit comments

Comments
 (0)