Browse Source

Add UserRepository

pr-readme
Steffen Nitschke 4 years ago
parent
commit
227bb430a6
  1. 26
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/User.java
  2. 25
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserId.java
  3. 52
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/UserRepository.java
  4. 40
      fh.fd.ci.server/src/test/java/de/fd/fh/server/user/UserRepositoryTest.java

26
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;
}
}

25
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());
}
}

52
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<User> 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));
}
}

40
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<User> result = new UserRepository(datastore).save(access);
assertThat("Key is null", result, notNullValue());
then(datastore).should().save(any(User.class));
}
}
Loading…
Cancel
Save