Skip to content

Commit 290e1c7

Browse files
Merge pull request #124 from SpineEventEngine/changes-for-gae
Externalize a few classes. Add utility method for Timestamps.
2 parents 13142ac + a0cfcab commit 290e1c7

File tree

6 files changed

+166
-53
lines changed

6 files changed

+166
-53
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,17 @@ public static int compare(@Nullable Timestamp t1, @Nullable Timestamp t2) {
8383
}
8484
int result = Long.compare(t1.getSeconds(), t2.getSeconds());
8585
result = (result == 0) ?
86-
Integer.compare(t1.getNanos(), t2.getNanos()) :
87-
result;
86+
Integer.compare(t1.getNanos(), t2.getNanos()) :
87+
result;
8888
return result;
8989
}
9090

9191
/**
9292
* Calculates if the {@code timestamp} is between the {@code start} and {@code finish} timestamps.
9393
*
9494
* @param timestamp the timestamp to check if it is between the {@code start} and {@code finish}
95-
* @param start the first point in time, must be before the {@code finish} timestamp
96-
* @param finish the second point in time, must be after the {@code start} timestamp
95+
* @param start the first point in time, must be before the {@code finish} timestamp
96+
* @param finish the second point in time, must be after the {@code start} timestamp
9797
* @return true if the {@code timestamp} is after the {@code start} and before the {@code finish} timestamps,
9898
* false otherwise
9999
*/
@@ -138,6 +138,17 @@ public static Date convertToDate(TimestampOrBuilder timestamp) {
138138
return date;
139139
}
140140

141+
/**
142+
* Retrieves total nanoseconds from {@link Timestamp}.
143+
*
144+
* @return long value
145+
*/
146+
public static long convertToNanos(TimestampOrBuilder timestamp) {
147+
final long nanosFromSeconds = timestamp.getSeconds() * MILLIS_PER_SECOND * NANOS_PER_MILLISECOND;
148+
final long totalNanos = nanosFromSeconds + timestamp.getNanos();
149+
return totalNanos;
150+
}
151+
141152
private static class TimestampComparator implements Comparator<Timestamp>, Serializable {
142153
@Override
143154
public int compare(Timestamp t1, Timestamp t2) {
@@ -164,7 +175,6 @@ public static Timestamp minutesAgo(int value) {
164175
}
165176

166177
/**
167-
*
168178
* @param value a positive number of minutes
169179
* @return the moment `value` seconds ago
170180
*/

client/src/test/java/org/spine3/protobuf/TimestampsShould.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
import static org.junit.Assert.*;
3131
import static org.spine3.protobuf.Timestamps.MILLIS_PER_SECOND;
3232
import static org.spine3.protobuf.Timestamps.convertToDate;
33+
import static org.spine3.protobuf.Timestamps.convertToNanos;
3334
import static org.spine3.test.Tests.hasPrivateUtilityConstructor;
3435

3536
@SuppressWarnings("InstanceMethodNamingConvention")
3637
public class TimestampsShould {
3738

39+
private static final int NANOS_IN_SECOND = 1000000000;
40+
3841
private static final Duration TEN_SECONDS = Durations.ofSeconds(10);
3942

4043
private static final Duration MINUTE = Durations.ofMinutes(1);
@@ -77,8 +80,14 @@ public void compare_two_timestamps_return_negative_int_if_first_is_null() {
7780
public void compare_two_timestamps_return_zero_if_timestamps_are_equal() {
7881
final int secs = 256;
7982
final int nanos = 512;
80-
final Timestamp time1 = Timestamp.newBuilder().setSeconds(secs).setNanos(nanos).build();
81-
final Timestamp time2 = Timestamp.newBuilder().setSeconds(secs).setNanos(nanos).build();
83+
final Timestamp time1 = Timestamp.newBuilder()
84+
.setSeconds(secs)
85+
.setNanos(nanos)
86+
.build();
87+
final Timestamp time2 = Timestamp.newBuilder()
88+
.setSeconds(secs)
89+
.setNanos(nanos)
90+
.build();
8291

8392
final int result = Timestamps.compare(time1, time2);
8493

@@ -158,7 +167,8 @@ public void compare_two_timestamps_using_comparator_return_negative_int_if_first
158167
final Timestamp time1 = getCurrentTime();
159168
final Timestamp time2 = add(time1, TEN_SECONDS);
160169

161-
final int result = Timestamps.comparator().compare(time1, time2);
170+
final int result = Timestamps.comparator()
171+
.compare(time1, time2);
162172

163173
assertTrue(result < 0);
164174
}
@@ -167,10 +177,17 @@ public void compare_two_timestamps_using_comparator_return_negative_int_if_first
167177
public void compare_two_timestamps_using_comparator_return_zero_if_timestamps_are_equal() {
168178
final int secs = 256;
169179
final int nanos = 512;
170-
final Timestamp time1 = Timestamp.newBuilder().setSeconds(secs).setNanos(nanos).build();
171-
final Timestamp time2 = Timestamp.newBuilder().setSeconds(secs).setNanos(nanos).build();
172-
173-
final int result = Timestamps.comparator().compare(time1, time2);
180+
final Timestamp time1 = Timestamp.newBuilder()
181+
.setSeconds(secs)
182+
.setNanos(nanos)
183+
.build();
184+
final Timestamp time2 = Timestamp.newBuilder()
185+
.setSeconds(secs)
186+
.setNanos(nanos)
187+
.build();
188+
189+
final int result = Timestamps.comparator()
190+
.compare(time1, time2);
174191

175192
assertEquals(0, result);
176193
}
@@ -180,7 +197,8 @@ public void compare_two_timestamps_using_comparator_return_positive_int_if_first
180197
final Timestamp currentTime = getCurrentTime();
181198
final Timestamp timeAfterCurrent = add(currentTime, TEN_SECONDS);
182199

183-
final int result = Timestamps.comparator().compare(timeAfterCurrent, currentTime);
200+
final int result = Timestamps.comparator()
201+
.compare(timeAfterCurrent, currentTime);
184202

185203
assertTrue(result > 0);
186204
}
@@ -195,4 +213,14 @@ public void convert_timestamp_to_date_to_nearest_second() {
195213

196214
assertEquals(expectedTime.getSeconds(), actualSeconds);
197215
}
216+
217+
@Test
218+
public void convert_timestamp_to_nanos() {
219+
final Timestamp expectedTime = getCurrentTime();
220+
221+
final long nanos = convertToNanos(expectedTime);
222+
final long expectedNanos = expectedTime.getSeconds() * NANOS_IN_SECOND + expectedTime.getNanos();
223+
224+
assertEquals(expectedNanos, nanos);
225+
}
198226
}

server/src/test/java/org/spine3/server/aggregate/AggregateRepositoryShould.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.spine3.test.project.command.CreateProject;
3535
import org.spine3.test.project.event.ProjectCreated;
3636
import org.spine3.test.project.event.TaskAdded;
37+
import org.spine3.testdata.ProjectAggregate;
3738
import org.spine3.type.CommandClass;
3839
import org.spine3.validate.Validate;
3940

@@ -58,23 +59,6 @@ public void setUp() {
5859
repository = new ProjectAggregateRepository(boundedContext);
5960
}
6061

61-
private static class ProjectAggregate extends Aggregate<ProjectId, Project, Project.Builder> {
62-
63-
public ProjectAggregate(ProjectId id) {
64-
super(id);
65-
}
66-
67-
@Assign
68-
public ProjectCreated handle(CreateProject cmd, CommandContext ctx) {
69-
return projectCreatedEvent(cmd.getProjectId());
70-
}
71-
72-
@Assign
73-
public TaskAdded handle(AddTask cmd, CommandContext ctx) {
74-
return taskAddedEvent(cmd.getProjectId());
75-
}
76-
}
77-
7862
private static class ProjectAggregateRepository extends AggregateRepository<ProjectId, ProjectAggregate> {
7963
private ProjectAggregateRepository(BoundedContext boundedContext) {
8064
super(boundedContext);

server/src/test/java/org/spine3/server/entity/EntityShould.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.junit.Test;
2828
import org.spine3.test.project.Project;
2929
import org.spine3.test.project.ProjectId;
30+
import org.spine3.testdata.TestEntity;
3031

3132
import static com.google.protobuf.util.TimeUtil.getCurrentTime;
3233
import static org.junit.Assert.assertEquals;
@@ -87,7 +88,7 @@ public void store_state() {
8788
@Test
8889
public void validate_state_when_set_it() {
8990
entity.setState(state, 0, TimeUtil.getCurrentTime());
90-
assertTrue(entity.isValidateMethodCalled);
91+
assertTrue(entity.isValidateMethodCalled());
9192
}
9293

9394
@Test(expected = NullPointerException.class)
@@ -115,7 +116,8 @@ public void set_default_state() {
115116
final long expectedTimeSec = currentTimeSeconds();
116117

117118
assertEquals(entity.getDefaultState(), entity.getState());
118-
assertEquals(expectedTimeSec, entity.whenModified().getSeconds());
119+
assertEquals(expectedTimeSec, entity.whenModified()
120+
.getSeconds());
119121
assertEquals(0, entity.getVersion());
120122
}
121123

@@ -135,7 +137,8 @@ public void record_modification_time_when_incrementing_version() {
135137
entity.incrementVersion();
136138
final long expectedTimeSec = currentTimeSeconds();
137139

138-
assertEquals(expectedTimeSec, entity.whenModified().getSeconds());
140+
assertEquals(expectedTimeSec, entity.whenModified()
141+
.getSeconds());
139142
}
140143

141144
@Test
@@ -156,7 +159,8 @@ public void record_modification_time_when_updating_state() {
156159
entity.incrementState(state);
157160
final long expectedTimeSec = currentTimeSeconds();
158161

159-
assertEquals(expectedTimeSec, entity.whenModified().getSeconds());
162+
assertEquals(expectedTimeSec, entity.whenModified()
163+
.getSeconds());
160164
}
161165

162166
@Test
@@ -168,41 +172,29 @@ public void return_id_class() {
168172

169173
@Test
170174
public void return_id_simple_class_name() {
171-
final String expected = entity.getId().getClass().getSimpleName();
175+
final String expected = entity.getId()
176+
.getClass()
177+
.getSimpleName();
172178
final String actual = entity.getShortIdTypeName();
173179
assertEquals(expected, actual);
174180
}
175181

176182
@Test
177183
public void return_id_protobuf_type_name() {
178184
final EntityWithMessageId entityWithMessageId = new EntityWithMessageId();
179-
final String expected = ProjectId.getDescriptor().getName();
185+
final String expected = ProjectId.getDescriptor()
186+
.getName();
180187
final String actual = entityWithMessageId.getShortIdTypeName();
181188
assertEquals(expected, actual);
182189
}
183190

184191
private static Project newProject() {
185192
final Project.Builder project = Project.newBuilder()
186-
.setProjectId(newProjectId())
187-
.setStatus(newUuid());
193+
.setProjectId(newProjectId())
194+
.setStatus(newUuid());
188195
return project.build();
189196
}
190197

191-
private static class TestEntity extends Entity<String, Project> {
192-
193-
private boolean isValidateMethodCalled = false;
194-
195-
protected TestEntity(String id) {
196-
super(id);
197-
}
198-
199-
@Override
200-
protected void validate(Project state) throws IllegalStateException {
201-
super.validate(state);
202-
isValidateMethodCalled = true;
203-
}
204-
}
205-
206198
private static class EntityWithUnsupportedId extends Entity<Exception, Project> {
207199

208200
protected EntityWithUnsupportedId(Exception id) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2016, TeamDev Ltd. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
package org.spine3.testdata;
22+
23+
import org.spine3.base.CommandContext;
24+
import org.spine3.server.Assign;
25+
import org.spine3.server.aggregate.Aggregate;
26+
import org.spine3.test.project.Project;
27+
import org.spine3.test.project.ProjectId;
28+
import org.spine3.test.project.command.AddTask;
29+
import org.spine3.test.project.command.CreateProject;
30+
import org.spine3.test.project.event.ProjectCreated;
31+
import org.spine3.test.project.event.TaskAdded;
32+
33+
import static org.spine3.testdata.TestEventMessageFactory.projectCreatedEvent;
34+
import static org.spine3.testdata.TestEventMessageFactory.taskAddedEvent;
35+
36+
public class ProjectAggregate extends Aggregate<ProjectId, Project, Project.Builder> {
37+
38+
public ProjectAggregate(ProjectId id) {
39+
super(id);
40+
}
41+
42+
@Assign
43+
public ProjectCreated handle(CreateProject cmd, CommandContext ctx) {
44+
return projectCreatedEvent(cmd.getProjectId());
45+
}
46+
47+
@Assign
48+
public TaskAdded handle(AddTask cmd, CommandContext ctx) {
49+
return taskAddedEvent(cmd.getProjectId());
50+
}
51+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016, TeamDev Ltd. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
package org.spine3.testdata;
22+
23+
import org.spine3.server.entity.Entity;
24+
import org.spine3.test.project.Project;
25+
26+
/**
27+
* Extracted from {@link org.spine3.server.entity.EntityShould}
28+
*
29+
* @author Mikhail Mikhaylov
30+
*/
31+
public class TestEntity extends Entity<String, Project> {
32+
33+
private boolean isValidateMethodCalled = false;
34+
35+
public TestEntity(String id) {
36+
super(id);
37+
}
38+
39+
@Override
40+
protected void validate(Project state) throws IllegalStateException {
41+
super.validate(state);
42+
isValidateMethodCalled = true;
43+
}
44+
45+
public boolean isValidateMethodCalled() {
46+
return isValidateMethodCalled;
47+
}
48+
}

0 commit comments

Comments
 (0)