Skip to content

Commit 753308c

Browse files
author
Mikhail Mikhaylov
committed
Externalize a few classes. Add utility method for Timestamps.
1 parent a4bdf50 commit 753308c

File tree

5 files changed

+130
-45
lines changed

5 files changed

+130
-45
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
*/

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)