Browse Source

Add AccessRepository

pr-readme
Steffen Nitschke 4 years ago
parent
commit
0a3fb00301
  1. 16
      Multi-Chess.xml
  2. 12
      build.gradle
  3. 9
      fh.fd.ci.server/build.gradle
  4. 46
      fh.fd.ci.server/src/main/java/de/fd/fh/server/access/Access.java
  5. 58
      fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessRepository.java
  6. 6
      fh.fd.ci.server/src/main/java/de/fd/fh/server/access/Role.java
  7. 42
      fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessRepositoryTest.java
  8. 31
      fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessTokenTest.java

16
Multi-Chess.xml

@ -0,0 +1,16 @@
<code_scheme name="Multi-Chess" version="173">
<option name="FORMATTER_TAGS_ENABLED" value="true" />
<codeStyleSettings language="JAVA">
<option name="BRACE_STYLE" value="2" />
<option name="CLASS_BRACE_STYLE" value="2" />
<option name="METHOD_BRACE_STYLE" value="2" />
<option name="LAMBDA_BRACE_STYLE" value="2" />
<option name="ELSE_ON_NEW_LINE" value="true" />
<option name="CATCH_ON_NEW_LINE" value="true" />
<option name="FINALLY_ON_NEW_LINE" value="true" />
<option name="IF_BRACE_FORCE" value="3" />
<option name="DOWHILE_BRACE_FORCE" value="3" />
<option name="WHILE_BRACE_FORCE" value="3" />
<option name="FOR_BRACE_FORCE" value="3" />
</codeStyleSettings>
</code_scheme>

12
build.gradle

@ -10,10 +10,6 @@ repositories {
jcenter()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
subprojects {
apply plugin: 'java'
@ -27,7 +23,15 @@ subprojects {
compileOnly 'org.projectlombok:lombok:1.18.16'
annotationProcessor 'org.projectlombok:lombok:1.18.16'
testImplementation('org.junit.jupiter:junit-jupiter:5.7.0')
testImplementation('org.mockito:mockito-core:3.7.0')
testImplementation('org.hamcrest:hamcrest-core:2.2')
testCompileOnly 'org.projectlombok:lombok:1.18.16'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.16'
}
test {
useJUnitPlatform()
}
}

9
fh.fd.ci.server/build.gradle

@ -2,4 +2,13 @@ dependencies {
implementation project(':fh.fd.ci.shared')
compile 'com.sparkjava:spark-core:2.9.3'
compile 'dev.morphia.morphia:core:1.5.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.5'
task startServer(type: JavaExec){
main = "de.fd.fh.ServerApp"
description = "Start server"
classpath = sourceSets.main.runtimeClasspath
}
}

46
fh.fd.ci.server/src/main/java/de/fd/fh/server/access/Access.java

@ -0,0 +1,46 @@
package de.fd.fh.server.access;
import de.fd.fh.server.user.UserId;
import dev.morphia.annotations.Embedded;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Entity("login")
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class Access
{
@Id
private String _id;
private String name;
private String password;
@Embedded
private UserId userId;
@Embedded
private AccessToken token;
private Role role;
void removeToken()
{
this.token = null;
}
void setToken(final AccessToken token)
{
this.token = token;
}
void updatePassword(final String newPassword)
{
this.password = newPassword;
}
}

58
fh.fd.ci.server/src/main/java/de/fd/fh/server/access/AccessRepository.java

@ -0,0 +1,58 @@
package de.fd.fh.server.access;
import com.mongodb.MongoClient;
import com.mongodb.WriteResult;
import de.fd.fh.server.user.UserId;
import dev.morphia.Datastore;
import dev.morphia.Key;
import dev.morphia.Morphia;
public class AccessRepository
{
private final Datastore datastore;
public AccessRepository()
{
System.out.println("AccessRepo");
final Morphia morphia = new Morphia();
morphia.mapPackage("de.fd.fh.server.access");
this.datastore = morphia.createDatastore(new MongoClient(), "smartwarfare");
datastore.ensureIndexes();
}
AccessRepository(Datastore datastore)
{
this.datastore = datastore;
}
public Key<Access> save(final Access access)
{
return datastore.save(access);
}
Access findByUserName(final String name)
{
return datastore.createQuery(Access.class)
.field("name").equal(name).first();
}
Access findByToken(final String token)
{
return datastore.createQuery(Access.class)
.field("token.token").equal(token).first();
}
Access findByUserId(final UserId userId)
{
return datastore.createQuery(Access.class)
.field("userId.identifier").equal(userId.getIdentifier()).first();
}
WriteResult deleteLoginByUserId(final UserId userId)
{
return datastore.delete(datastore.createQuery(Access.class)
.field("userId.identifier").equal(userId.getIdentifier()).first());
}
}

6
fh.fd.ci.server/src/main/java/de/fd/fh/server/access/Role.java

@ -0,0 +1,6 @@
package de.fd.fh.server.access;
public enum Role
{
ADMIN, USER
}

42
fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessRepositoryTest.java

@ -0,0 +1,42 @@
package de.fd.fh.server.access;
import de.fd.fh.server.user.UserId;
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 AccessRepositoryTest
{
@Mock
private Datastore datastore;
@BeforeEach
public void before()
{
datastore = mock(Datastore.class);
}
@Test
void given_newUser_when_saveUser_should_storeUserInDatabase()
{
when(datastore.save(any(Access.class)))
.thenReturn(new Key<>(Access.class, "collection", "id"));
final Access access = new Access("testId", "testName", "testPwd", UserId.of("userId"),
null, Role.USER);
final Key<Access> result = new AccessRepository(datastore).save(access);
assertThat("Key is null", result, notNullValue());
then(datastore).should().save(any(Access.class));
}
}

31
fh.fd.ci.server/src/test/java/de/fd/fh/server/access/AccessTokenTest.java

@ -0,0 +1,31 @@
package de.fd.fh.server.access;
import de.fd.fh.server.user.UserId;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.*;
class AccessTokenTest
{
@Test
void given_accessData_when_createAccessToken_should_createGeneratedToken()
{
final Access access = new Access(
"testId",
"testName",
"testPwd",
UserId.of("12345"),
null,
Role.USER);
final AccessToken result = AccessToken.of(access);
assertNotNull(result.getCreatedDate());
assertNotNull(result.getToken());
assertThat(result.getToken().length(), equalTo(64));
assertEquals(result.getRole(), Role.USER);
assertEquals(result.getUserId(), UserId.of("12345"));
}
}
Loading…
Cancel
Save