-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3642937
commit 72a0cb3
Showing
9 changed files
with
287 additions
and
9 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/com/sitepark/ies/userrepository/core/domain/entity/Identifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.sitepark.ies.userrepository.core.domain.entity; | ||
|
||
import java.util.Optional; | ||
|
||
public final class Identifier { | ||
|
||
private final Long id; | ||
|
||
private final Anchor anchor; | ||
|
||
private Identifier(Long id) { | ||
this.id = id; | ||
this.anchor = null; | ||
} | ||
|
||
private Identifier(Anchor anchor) { | ||
this.id = null; | ||
this.anchor = anchor; | ||
} | ||
|
||
public static Identifier ofId(long id) { | ||
return new Identifier(id); | ||
} | ||
|
||
public static Identifier ofAnchor(Anchor anchor) { | ||
if (anchor == null) { | ||
throw new NullPointerException("anchor is null"); | ||
} | ||
return new Identifier(anchor); | ||
} | ||
|
||
public static Identifier ofString(String identifier) { | ||
if (isId(identifier)) { | ||
return new Identifier(Long.valueOf(identifier)); | ||
} | ||
return new Identifier(Anchor.ofString(identifier)); | ||
} | ||
|
||
public Optional<Long> getId() { | ||
return Optional.ofNullable(this.id); | ||
} | ||
|
||
public Optional<Anchor> getAnchor() { | ||
return Optional.ofNullable(this.anchor); | ||
} | ||
|
||
private static boolean isId(String str) { | ||
|
||
int length = str.length(); | ||
if (length > 19) { | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < length; i++) { | ||
char c = str.charAt(i); | ||
if (c < '0' || c > '9') { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/sitepark/ies/userrepository/core/domain/service/IdentifierResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.sitepark.ies.userrepository.core.domain.service; | ||
|
||
import java.util.Optional; | ||
|
||
import javax.inject.Inject; | ||
|
||
import com.sitepark.ies.userrepository.core.domain.entity.Identifier; | ||
import com.sitepark.ies.userrepository.core.domain.exception.AnchorNotFoundException; | ||
import com.sitepark.ies.userrepository.core.port.UserRepository; | ||
|
||
public class IdentifierResolver { | ||
|
||
private final UserRepository repository; | ||
|
||
@Inject | ||
protected IdentifierResolver(UserRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
public long resolveIdentifier(Identifier identifier) { | ||
|
||
if (identifier.getId().isPresent()) { | ||
return identifier.getId().get(); | ||
} | ||
|
||
Optional<Long> id = this.repository.resolveAnchor(identifier.getAnchor().get()); | ||
if (id.isEmpty()) { | ||
throw new AnchorNotFoundException(identifier.getAnchor().get()); | ||
} | ||
return id.get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/java/com/sitepark/ies/userrepository/core/domain/entity/IdentifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.sitepark.ies.userrepository.core.domain.entity; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class IdentifierTest { | ||
|
||
@Test | ||
void testOfStringToId() { | ||
Identifier identifier = Identifier.ofString("123"); | ||
assertEquals(Optional.of(123L), identifier.getId(), "id exprected"); | ||
} | ||
|
||
@Test | ||
void testOfStringToAnchor() { | ||
Identifier identifier = Identifier.ofString("abc"); | ||
Anchor anchor = Anchor.ofString("abc"); | ||
assertEquals(Optional.of(anchor), identifier.getAnchor(), "anchor exprected"); | ||
} | ||
|
||
@Test | ||
void testOfStringWithLongString() { | ||
Identifier identifier = Identifier.ofString("abcdefghijklmnopqrstuvwxyz"); | ||
Anchor anchor = Anchor.ofString("abcdefghijklmnopqrstuvwxyz"); | ||
assertEquals(Optional.of(anchor), identifier.getAnchor(), "anchor exprected"); | ||
} | ||
|
||
@Test | ||
void testOfStringWithDot() { | ||
Identifier identifier = Identifier.ofString("123.b"); | ||
Anchor anchor = Anchor.ofString("123.b"); | ||
assertEquals(Optional.of(anchor), identifier.getAnchor(), "anchor exprected"); | ||
} | ||
|
||
@Test | ||
void testOfId() { | ||
Identifier identifier = Identifier.ofId(123L); | ||
assertEquals(Optional.of(123L), identifier.getId(), "id exprected"); | ||
} | ||
|
||
@Test | ||
void testOfAnchor() { | ||
Anchor anchor = Anchor.ofString("abc"); | ||
Identifier identifier = Identifier.ofAnchor(anchor); | ||
assertEquals( | ||
Optional.of(Anchor.ofString("abc")), | ||
identifier.getAnchor(), | ||
"anchor exprected"); | ||
} | ||
|
||
@Test | ||
void testOfAnchorWithNull() { | ||
assertThrows(NullPointerException.class, () -> { | ||
Identifier.ofAnchor(null); | ||
}); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...test/java/com/sitepark/ies/userrepository/core/domain/service/IdentifierResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.sitepark.ies.userrepository.core.domain.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.sitepark.ies.userrepository.core.domain.entity.Anchor; | ||
import com.sitepark.ies.userrepository.core.domain.entity.Identifier; | ||
import com.sitepark.ies.userrepository.core.domain.exception.AnchorNotFoundException; | ||
import com.sitepark.ies.userrepository.core.port.UserRepository; | ||
|
||
class IdentifierResolverTest { | ||
|
||
@Test | ||
void testResolveWithId() { | ||
|
||
Identifier identifier = Identifier.ofId(123L); | ||
UserRepository repository = mock(); | ||
IdentifierResolver resolver = new IdentifierResolver(repository); | ||
|
||
long id = resolver.resolveIdentifier(identifier); | ||
|
||
assertEquals(123L, id, "unexpected id"); | ||
} | ||
|
||
@Test | ||
void testResolveWithAnchor() { | ||
|
||
Anchor anchor = Anchor.ofString("abc"); | ||
Identifier identifier = Identifier.ofAnchor(anchor); | ||
UserRepository repository = mock(); | ||
when(repository.resolveAnchor(any())).thenReturn(Optional.of(123L)); | ||
IdentifierResolver resolver = new IdentifierResolver(repository); | ||
|
||
long id = resolver.resolveIdentifier(identifier); | ||
|
||
assertEquals(123L, id, "unexpected id"); | ||
} | ||
|
||
@Test | ||
void testResolveWithAnchorNotFound() { | ||
|
||
Anchor anchor = Anchor.ofString("abc"); | ||
Identifier identifier = Identifier.ofAnchor(anchor); | ||
UserRepository repository = mock(); | ||
when(repository.resolveAnchor(any())).thenReturn(Optional.empty()); | ||
IdentifierResolver resolver = new IdentifierResolver(repository); | ||
|
||
assertThrows(AnchorNotFoundException.class, () -> { | ||
resolver.resolveIdentifier(identifier); | ||
}); | ||
} | ||
} |
Oops, something went wrong.