Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
[#141] handle default null and string for AppUserRole
Browse files Browse the repository at this point in the history
  • Loading branch information
jenarp committed Jan 25, 2022
1 parent 085d923 commit c9cf694
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/main/java/de/bonndan/nivio/appuser/AppUserRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,22 @@

public enum AppUserRole {
USER,
ADMIN
ADMIN;


// declare your defaults with constant values
private static final AppUserRole defaultValue = USER;

// `of` as a substitute for `valueOf` handling the default value
public static AppUserRole of(String value) {

if(!value.equals("ADMIN")) return defaultValue;
return AppUserRole.valueOf(value);
}

// `defaultOr` for handling default value for null
public static AppUserRole defaultOr(AppUserRole value) {
return value != null ? value : defaultValue;
}

}
5 changes: 4 additions & 1 deletion src/test/java/de/bonndan/nivio/appuser/AppUserRoleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class AppUserRoleTest {

Expand All @@ -16,6 +17,8 @@ void valueOf() {
// then
assertEquals(AppUserRole.valueOf("ADMIN"), admin);
assertEquals(AppUserRole.valueOf("USER"), user);

// handling default value for null and random string
assertEquals(AppUserRole.of("test"), user);
assertEquals(AppUserRole.defaultOr(null), user);
}
}

0 comments on commit c9cf694

Please sign in to comment.