Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Feb 6, 2024
1 parent 3d35aeb commit e86f366
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ protected Builder(AccessToken accessToken) {
}

public Builder id(String id) {
if (id == null) {
throw new NullPointerException("id is null");
}
Objects.requireNonNull(id, "id is null");
if (!Identifier.isId(id)) {
throw new IllegalArgumentException(id + " is not an id");
}
Expand All @@ -224,9 +222,7 @@ public Builder id(String id) {
}

public Builder user(String user) {
if (user == null) {
throw new NullPointerException("user is null");
}
Objects.requireNonNull(user, "user is null");
if (!Identifier.isId(user)) {
throw new IllegalArgumentException(user + " is not an user id");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

public final class Identifier {

private static final int MAX_ID_LENGTH = 19;

private static final String ZERO_ID = "0";

private final String id;

private final Anchor anchor;
Expand All @@ -22,21 +26,20 @@ private Identifier(Anchor anchor) {
}

public static Identifier ofId(String id) {
if (!isId(id)) {
throw new IllegalArgumentException("invalid id: " + id);
}
return new Identifier(id);
}

public static Identifier ofAnchor(Anchor anchor) {
if (anchor == null) {
throw new NullPointerException("anchor is null");
}
Objects.requireNonNull(anchor, "anchor is null");
return new Identifier(anchor);
}

@JsonCreator
public static Identifier ofString(String identifier) {
if (identifier == null) {
throw new NullPointerException("identifier is null");
}
Objects.requireNonNull(identifier, "identifier is null");
if (identifier.isBlank()) {
throw new IllegalArgumentException("identifier is blank");
}
Expand All @@ -56,12 +59,12 @@ public Optional<Anchor> getAnchor() {

public static boolean isId(String str) {

if (str.equals("0")) {
if (ZERO_ID.equals(str)) {
throw new IllegalArgumentException("id should be greater than 0");
}

int length = str.length();
if (length > 19) {
if (length > MAX_ID_LENGTH) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ protected Builder(User user) {
}

public Builder id(String id) {
if (id == null) {
throw new NullPointerException("id is null");
}
Objects.requireNonNull(id, "id is null");
if (!Identifier.isId(id)) {
throw new IllegalArgumentException(id + " is not an id");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ public class InvalidAnchorException extends UserRepositoryException {

private static final long serialVersionUID = 1L;

public InvalidAnchorException(String name) {
super();
this.name = name;
}


public InvalidAnchorException(String name, String message) {
super(message);
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@ public UserRepositoryException() {
public UserRepositoryException(String message) {
super(message);
}
public UserRepositoryException(String message, Throwable t) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
@SuppressWarnings("PMD.TooManyMethods")
@SuppressFBWarnings({
"PI_DO_NOT_REUSE_PUBLIC_IDENTIFIERS_CLASS_NAMES",
"NP_NULL_PARAM_DEREF_NONVIRTUAL"
"NP_NULL_PARAM_DEREF_NONVIRTUAL",
"NP_NULL_PARAM_DEREF_ALL_TARGETS_DANGEROUS"
})
class AccessTokenTest {

Expand All @@ -52,12 +53,26 @@ void testSetUser() throws JsonProcessingException {
}

@Test
void testSetInvalidUser() throws JsonProcessingException {
void testSetUserWithNull() throws JsonProcessingException {
assertThrows(NullPointerException.class, () -> {
AccessToken.builder().user(null);
});
}

@Test
void testSetUserWithZero() throws JsonProcessingException {
assertThrows(IllegalArgumentException.class, () -> {
AccessToken.builder().user("0");
});
}

@Test
void testSetUserWithInvalidValue() throws JsonProcessingException {
assertThrows(IllegalArgumentException.class, () -> {
AccessToken.builder().user("1x");
});
}

@Test
void testSetName() throws JsonProcessingException {
AccessToken accessToken = AccessToken.builder()
Expand Down Expand Up @@ -115,12 +130,26 @@ void testGetEmptyId() throws JsonProcessingException {
}

@Test
void testSetInvalidId() throws JsonProcessingException {
void testSetIdWithNull() throws JsonProcessingException {
assertThrows(NullPointerException.class, () -> {
AccessToken.builder().id(null);
});
}

@Test
void testSetIdWithZero() throws JsonProcessingException {
assertThrows(IllegalArgumentException.class, () -> {
AccessToken.builder().id("0");
});
}

@Test
void testSetIdWithInvalidValue() throws JsonProcessingException {
assertThrows(IllegalArgumentException.class, () -> {
AccessToken.builder().id("1x");
});
}

@Test
void testSetToken() throws JsonProcessingException {
AccessToken accessToken = this.createBuilderWithRequiredValues()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,40 @@ void testOfStringWithDot() {
assertEquals(Optional.of(anchor), identifier.getAnchor(), "anchor exprected");
}

@Test
void testOfStringWithNull() {
assertThrows(NullPointerException.class, () -> {
Identifier.ofString(null);
});
}

@Test
void testOfStringWithBlank() {
assertThrows(IllegalArgumentException.class, () -> {
Identifier.ofString(" ");
});
}

@Test
void testOfId() {
Identifier identifier = Identifier.ofId("123");
assertEquals(Optional.of("123"), identifier.getId(), "id exprected");
}

@Test
void testOfIdWithZero() {
assertThrows(IllegalArgumentException.class, () -> {
Identifier.ofId("0");
});
}

@Test
void testOfIdWithInvalidId() {
assertThrows(IllegalArgumentException.class, () -> {
Identifier.ofId("0x");
});
}

@Test
void testOfAnchor() {
Anchor anchor = Anchor.ofString("abc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ void testSetLogin() {
assertEquals("peterpan", user.getLogin(), "unexpected login");
}

@Test
void testSetIdentifierWithId() {
Identifier id = Identifier.ofId("123");
User user = this.createBuilderWithRequiredValues()
.identifier(id)
.build();
assertEquals("123", user.getId().get(), "unexpected id");
}

@Test
void testSetIdentifierWithAnchor() {
Identifier anchor = Identifier.ofAnchor(Anchor.ofString("abc"));
User user = this.createBuilderWithRequiredValues()
.identifier(anchor)
.build();
assertEquals("abc", user.getAnchor().get().getName(), "unexpected anchor");
}

@Test
void testSetId() {
User user = this.createBuilderWithRequiredValues()
Expand All @@ -78,12 +96,57 @@ void testGetEmptyId() {
}

@Test
void testSetInvalidId() {
void testGetIdentifierWithId() {
User user = this.createBuilderWithRequiredValues()
.id("123")
.build();
assertEquals(
Identifier.ofId("123"),
user.getIdentifier().get(),
"unexpected identifier");
}

@Test
void testGetIdentifierWithAnchor() {
User user = this.createBuilderWithRequiredValues()
.anchor("abc")
.build();
assertEquals(
Identifier.ofAnchor(Anchor.ofString("abc")),
user.getIdentifier().get(),
"unexpected identifier");
}

@Test
void testGetEmptyIdentifier() {
User user = this.createBuilderWithRequiredValues()
.build();
assertTrue(
user.getIdentifier().isEmpty(),
"identifier should be empty");
}

@Test
void testSetIdWithZero() {
assertThrows(IllegalArgumentException.class, () -> {
User.builder().id("0");
}, "id 0 should't be allowed");
}

@Test
void testSetIdWithNull() {
assertThrows(NullPointerException.class, () -> {
User.builder().id(null);
}, "null should't be allowed");
}

@Test
void testSeIdWithInvalidValue() {
assertThrows(IllegalArgumentException.class, () -> {
User.builder().id("0x");
}, "invalid id should't be allowed");
}

@Test
void testSetAnchor() {
Anchor anchor = Anchor.ofString("user.pan.peter");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
})
@SuppressFBWarnings({
"PI_DO_NOT_REUSE_PUBLIC_IDENTIFIERS_CLASS_NAMES",
"NP_NULL_PARAM_DEREF_NONVIRTUAL"
"NP_NULL_PARAM_DEREF_NONVIRTUAL",
"NP_NULL_PARAM_DEREF_ALL_TARGETS_DANGEROUS"
})
class UserValidityTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
@SuppressWarnings("PMD.TooManyMethods")
@SuppressFBWarnings({
"PI_DO_NOT_REUSE_PUBLIC_IDENTIFIERS_CLASS_NAMES",
"NP_NULL_PARAM_DEREF_NONVIRTUAL"
"NP_NULL_PARAM_DEREF_NONVIRTUAL",
"NP_NULL_PARAM_DEREF_ALL_TARGETS_DANGEROUS"
})
class LdapIdentityTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ void testIdRef() {
assertEquals(Optional.of("123"), ref.getId(), "unexpected id");
}

@Test
void testIdRefWithInvalidId() {
assertThrows(IllegalArgumentException.class, () -> {
Ref.ofId("1x");
});
}

@Test
void testWorkarroundConstructor() {
Ref ref = new Ref();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.sitepark.ies.userrepository.core.domain.exception;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import com.sitepark.ies.userrepository.core.domain.entity.Anchor;

class AnchorNotFoundExceptionTest {

@Test
void test() {
Anchor anchor = Anchor.ofString("abc");
AnchorNotFoundException e = new AnchorNotFoundException(anchor);
assertEquals(anchor, e.getAnchor(), "unexpecatd anchor");
assertThat(e.getMessage(), containsString(" abc "));
}
}

0 comments on commit e86f366

Please sign in to comment.