Skip to content
This repository was archived by the owner on May 9, 2019. It is now read-only.

Commit d8bae49

Browse files
committed
Add UserRepositoryTest
1 parent ff9fe1f commit d8bae49

File tree

2 files changed

+116
-8
lines changed

2 files changed

+116
-8
lines changed

build.sbt

+9-8
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ lazy val transactionImpl = (project in file("transaction-impl"))
158158
itemImpl,
159159
testkit % "test"
160160
).settings(
161-
version := "1.0-SNAPSHOT",
162-
libraryDependencies ++= Seq(
163-
lagomJavadslPersistenceCassandra,
164-
lagomJavadslTestKit,
165-
lagomJavadslKafkaBroker,
166-
cassandraExtras
167-
)
161+
version := "1.0-SNAPSHOT",
162+
libraryDependencies ++= Seq(
163+
lagomJavadslPersistenceCassandra,
164+
lagomJavadslTestKit,
165+
lagomJavadslKafkaBroker,
166+
cassandraExtras
168167
)
168+
)
169169

170170
lazy val userApi = (project in file("user-api"))
171171
.settings(commonSettings: _*)
@@ -181,7 +181,8 @@ lazy val userApi = (project in file("user-api"))
181181
lazy val userImpl = (project in file("user-impl"))
182182
.settings(commonSettings: _*)
183183
.enablePlugins(LagomJava)
184-
.dependsOn(userApi, tools)
184+
.dependsOn(userApi, tools,
185+
testkit % "test")
185186
.settings(
186187
version := "1.0-SNAPSHOT",
187188
libraryDependencies ++= Seq(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.example.auction.user.impl;
2+
3+
import com.example.auction.pagination.PaginatedSequence;
4+
import com.example.auction.user.api.User;
5+
import com.example.testkit.Await;
6+
import com.example.testkit.DoNothingTopicFactory;
7+
import com.example.testkit.ReadSideTestDriver;
8+
import com.lightbend.lagom.internal.javadsl.api.broker.TopicFactory;
9+
import com.lightbend.lagom.javadsl.persistence.Offset;
10+
import com.lightbend.lagom.javadsl.persistence.ReadSide;
11+
import com.lightbend.lagom.javadsl.testkit.ServiceTest;
12+
import org.junit.AfterClass;
13+
import org.junit.Before;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
import java.util.UUID;
18+
import java.util.concurrent.ExecutionException;
19+
import java.util.concurrent.TimeoutException;
20+
import java.util.concurrent.atomic.AtomicInteger;
21+
22+
import static com.lightbend.lagom.javadsl.testkit.ServiceTest.bind;
23+
import static com.lightbend.lagom.javadsl.testkit.ServiceTest.defaultSetup;
24+
import static org.junit.Assert.assertEquals;
25+
26+
public class UserRepositoryTest {
27+
28+
private final static ServiceTest.Setup setup = defaultSetup().withCassandra(true)
29+
.configureBuilder(b ->
30+
// by default, cassandra-query-journal delays propagation of events by 10sec. In test we're using
31+
// a 1 node cluster so this delay is not necessary.
32+
b.configure("cassandra-query-journal.eventual-consistency-delay", "0")
33+
.overrides(bind(ReadSide.class).to(ReadSideTestDriver.class),
34+
bind(TopicFactory.class).to(DoNothingTopicFactory.class))
35+
);
36+
37+
private static ServiceTest.TestServer testServer;
38+
39+
@BeforeClass
40+
public static void beforeAll() {
41+
testServer = ServiceTest.startServer(setup);
42+
}
43+
44+
@AfterClass
45+
public static void afterAll() {
46+
testServer.stop();
47+
}
48+
49+
private ReadSideTestDriver testDriver = testServer.injector().instanceOf(ReadSideTestDriver.class);
50+
private UserRepository UserRepository = testServer.injector().instanceOf(UserRepository.class);
51+
private AtomicInteger offset;
52+
53+
private final UUID userId = UUID.randomUUID();
54+
private final String name = "admin";
55+
private final String email = "[email protected]";
56+
private final String password = PUserCommand.hashPassword("admin");
57+
58+
private final PUser userCreated = new PUser(userId, name, email, password);
59+
60+
@Before
61+
public void restartOffset() {
62+
offset = new AtomicInteger(1);
63+
}
64+
65+
@Test
66+
public PaginatedSequence<User> shouldGetUsers() throws InterruptedException, ExecutionException, TimeoutException {
67+
return Await.result(UserRepository.getUsers( 0, 10));
68+
}
69+
70+
71+
public void shouldCreateUser(UUID userId) throws InterruptedException, ExecutionException, TimeoutException {
72+
feed(new PUserEvent.PUserCreated(userCreated));
73+
PaginatedSequence<User> users = shouldGetUsers();
74+
assertEquals(1, users.getCount());
75+
User expected = new User(userId, name, email);
76+
assertEquals(expected, users.getItems().get(0));
77+
}
78+
79+
80+
@Test
81+
public void shouldPaginateUserRetrieval() throws InterruptedException, ExecutionException, TimeoutException {
82+
for (int i = 0; i < 25; i++) {
83+
84+
feed(new PUserEvent.PUserCreated(userCreated));
85+
}
86+
87+
88+
PaginatedSequence<User> createdUsers = Await.result(UserRepository.getUsers( 1, 10));
89+
assertEquals(25, createdUsers.getCount());
90+
assertEquals(10, createdUsers.getItems().size());
91+
// default ordering is time DESC so page 2 of size 10 over a set of 25 returns item ids 5-14. On that seq, the fifth item is id=10
92+
assertEquals("user10", createdUsers.getItems().get(4).getName());
93+
94+
}
95+
96+
private User buildFixture(UUID id) {
97+
return new User(id,name,email);
98+
}
99+
private void feed(PUserEvent userEvent) throws InterruptedException, ExecutionException, TimeoutException {
100+
Await.result(testDriver.feed(userEvent, Offset.sequence(offset.getAndIncrement())));
101+
}
102+
103+
104+
105+
}
106+
107+

0 commit comments

Comments
 (0)