From 227bb430a607c39e15e867460ad0106823b20bea Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Wed, 13 Jan 2021 23:13:37 +0100 Subject: [PATCH] Add UserRepository --- .../main/java/de/fd/fh/server/user/User.java | 26 ++++++++++ .../java/de/fd/fh/server/user/UserId.java | 25 +++++++++ .../de/fd/fh/server/user/UserRepository.java | 52 +++++++++++++++++++ .../fd/fh/server/user/UserRepositoryTest.java | 40 ++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 fh.fd.ci.server/src/main/java/de/fd/fh/server/user/User.java create mode 100644 fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java create mode 100644 fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserRepository.java create mode 100644 fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserRepositoryTest.java diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/User.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/User.java new file mode 100644 index 0000000..ef18257 --- /dev/null +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/User.java @@ -0,0 +1,26 @@ +package de.fd.fh.server.user; + +import lombok.*; + +@AllArgsConstructor +@Getter +public class User { + + private final UserId id; + + private String name; + + public static User of(String name) + { + return new User(null, name); + } + + public void rename(String name) + { + if (name == null) + { + return; + } + this.name = name; + } +} diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java new file mode 100644 index 0000000..d978069 --- /dev/null +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java @@ -0,0 +1,25 @@ +package de.fd.fh.server.user; + +import dev.morphia.annotations.Embedded; +import lombok.*; +import org.bson.types.ObjectId; + +@Getter +@Embedded +@NoArgsConstructor +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@EqualsAndHashCode(of = {"identifier"}) +public class UserId +{ + private String identifier; + + public static UserId of(final String identifier) + { + return new UserId(identifier); + } + + public static UserId random() + { + return new UserId(new ObjectId().toHexString()); + } +} diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserRepository.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserRepository.java new file mode 100644 index 0000000..dcb1316 --- /dev/null +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserRepository.java @@ -0,0 +1,52 @@ +package de.fd.fh.server.user; + +import com.mongodb.MongoClient; +import com.mongodb.WriteResult; +import dev.morphia.Datastore; +import dev.morphia.Key; +import dev.morphia.Morphia; + +public class UserRepository { + + private final Datastore datastore; + + public UserRepository() + { + System.out.println("UserRepo"); + final Morphia morphia = new Morphia(); + + morphia.mapPackage("de.fd.fh.server.user"); + + this.datastore = morphia.createDatastore(new MongoClient(), "smartwarfare"); + datastore.ensureIndexes(); + } + + UserRepository(Datastore datastore) + { + this.datastore = datastore; + } + + public Key save(final User user) + { + return datastore.save(user); + } + + public User findUserById(final UserId userId) + { + return datastore.createQuery(User.class) + .field("_id.identifier").equal(userId.getIdentifier()) + .first(); + } + + public User findUserByName(final String name) + { + return datastore.createQuery(User.class) + .field("name").equal(name) + .first(); + } + + WriteResult deleteUserById(final UserId userId) + { + return datastore.delete(findUserById(userId)); + } +} diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserRepositoryTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserRepositoryTest.java new file mode 100644 index 0000000..a551286 --- /dev/null +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserRepositoryTest.java @@ -0,0 +1,40 @@ +package de.fd.fh.server.user; + +import dev.morphia.Datastore; +import dev.morphia.Key; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class UserRepositoryTest +{ + @Mock + private Datastore datastore; + + @BeforeEach + public void before() + { + datastore = mock(Datastore.class); + } + + @Test + void given_newUser_when_saveUser_should_storeUserInDatabase() + { + when(datastore.save(any(User.class))) + .thenReturn(new Key<>(User.class, "collection", "id")); + + final User access = new User(UserId.of("userId"), "testName"); + + final Key result = new UserRepository(datastore).save(access); + + assertThat("Key is null", result, notNullValue()); + then(datastore).should().save(any(User.class)); + } +} \ No newline at end of file