From 009fea3ad453b2f4e0f0b7de92c441e654d00bb9 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sun, 11 Dec 2016 19:24:41 +0100 Subject: [PATCH] use lambdas and added javadoc to unit test --- .../scm/security/SyncingRealmHelper.java | 106 +++++---------- .../scm/security/SyncingRealmHelperTest.java | 128 +++++++----------- 2 files changed, 84 insertions(+), 150 deletions(-) diff --git a/scm-core/src/main/java/sonia/scm/security/SyncingRealmHelper.java b/scm-core/src/main/java/sonia/scm/security/SyncingRealmHelper.java index f561f01a2e..4c5b5c4e53 100644 --- a/scm-core/src/main/java/sonia/scm/security/SyncingRealmHelper.java +++ b/scm-core/src/main/java/sonia/scm/security/SyncingRealmHelper.java @@ -26,13 +26,8 @@ * http://bitbucket.org/sdorra/scm-manager * */ - - - package sonia.scm.security; -//~--- non-JDK imports -------------------------------------------------------- - import com.google.common.collect.ImmutableList; import com.google.inject.Inject; @@ -50,25 +45,28 @@ import sonia.scm.user.User; import sonia.scm.user.UserException; import sonia.scm.user.UserManager; import sonia.scm.web.security.AdministrationContext; -import sonia.scm.web.security.PrivilegedAction; -//~--- JDK imports ------------------------------------------------------------ import java.io.IOException; import java.util.Collection; /** - * Helper class for syncing realms. The class should simplify the creation of - * realms, which are syncing authenticated users with the local database. + * Helper class for syncing realms. The class should simplify the creation of realms, which are syncing authenticated + * users with the local database. * * @author Sebastian Sdorra * @since 2.0.0 */ @Extension -public final class SyncingRealmHelper -{ +public final class SyncingRealmHelper { + private final AdministrationContext ctx; + + private final GroupManager groupManager; + + private final UserManager userManager; + /** * Constructs a new SyncingRealmHelper. * @@ -78,16 +76,13 @@ public final class SyncingRealmHelper * @param groupManager group manager */ @Inject - public SyncingRealmHelper(AdministrationContext ctx, UserManager userManager, - GroupManager groupManager) - { + public SyncingRealmHelper(AdministrationContext ctx, UserManager userManager, GroupManager groupManager) { this.ctx = ctx; this.userManager = userManager; this.groupManager = groupManager; } //~--- methods -------------------------------------------------------------- - /** * Create {@link AuthenticationInfo} from user and groups. * @@ -99,8 +94,7 @@ public final class SyncingRealmHelper * @return authentication info */ public AuthenticationInfo createAuthenticationInfo(String realm, User user, - String... groups) - { + String... groups) { return createAuthenticationInfo(realm, user, ImmutableList.copyOf(groups)); } @@ -115,8 +109,7 @@ public final class SyncingRealmHelper * @return authentication info */ public AuthenticationInfo createAuthenticationInfo(String realm, User user, - Collection groups) - { + Collection groups) { SimplePrincipalCollection collection = new SimplePrincipalCollection(); collection.add(user.getId(), realm); @@ -129,77 +122,42 @@ public final class SyncingRealmHelper /** * Stores the group in local database of scm-manager. * - * * @param group group to store */ - public void store(final Group group) - { - ctx.runAsAdmin(new PrivilegedAction() - { - - @Override - public void run() - { - try - { - if (groupManager.get(group.getId()) != null) - { - groupManager.modify(group); - } - else - { - groupManager.create(group); - } + public void store(final Group group) { + ctx.runAsAdmin(() -> { + try { + if (groupManager.get(group.getId()) != null) { + groupManager.modify(group); } - catch (GroupException | IOException ex) - { - throw new AuthenticationException("could not store group", ex); + else { + groupManager.create(group); } } + catch (GroupException | IOException ex) { + throw new AuthenticationException("could not store group", ex); + } }); } /** * Stores the user in local database of scm-manager. * - * * @param user user to store */ - public void store(final User user) - { - ctx.runAsAdmin(new PrivilegedAction() - { - - @Override - public void run() - { - try - { - if (userManager.contains(user.getName())) - { - userManager.modify(user); - } - else - { - userManager.create(user); - } + public void store(final User user) { + ctx.runAsAdmin(() -> { + try { + if (userManager.contains(user.getName())) { + userManager.modify(user); } - catch (UserException | IOException ex) - { - throw new AuthenticationException("could not store user", ex); + else { + userManager.create(user); } } + catch (UserException | IOException ex) { + throw new AuthenticationException("could not store user", ex); + } }); } - - //~--- fields --------------------------------------------------------------- - - /** administration context */ - private final AdministrationContext ctx; - - /** group manager */ - private final GroupManager groupManager; - - /** user manager */ - private final UserManager userManager; } diff --git a/scm-core/src/test/java/sonia/scm/security/SyncingRealmHelperTest.java b/scm-core/src/test/java/sonia/scm/security/SyncingRealmHelperTest.java index 05a8ce009a..2f322ecbe3 100644 --- a/scm-core/src/test/java/sonia/scm/security/SyncingRealmHelperTest.java +++ b/scm-core/src/test/java/sonia/scm/security/SyncingRealmHelperTest.java @@ -68,23 +68,55 @@ import static org.mockito.Mockito.*; import java.io.IOException; /** + * Unit tests for {@link SyncingRealmHelper}. * * @author Sebastian Sdorra */ @RunWith(MockitoJUnitRunner.class) -public class SyncingRealmHelperTest -{ +public class SyncingRealmHelperTest { + + @Mock + private GroupManager groupManager; + + @Mock + private UserManager userManager; + + private SyncingRealmHelper helper; /** - * Method description - * + * Mock {@link AdministrationContext} and create object under test. + */ + @Before + public void setUp() { + AdministrationContext ctx = new AdministrationContext() { + + @Override + public void runAsAdmin(PrivilegedAction action) { + action.run(); + } + + @Override + public void runAsAdmin(Class actionClass) { + try { + runAsAdmin(actionClass.newInstance()); + } + catch (IllegalAccessException | InstantiationException ex) { + throw Throwables.propagate(ex); + } + } + }; + + helper = new SyncingRealmHelper(ctx, userManager, groupManager); + } + + /** + * Tests {@link SyncingRealmHelper#createAuthenticationInfo(String, User, String...)}. */ @Test - public void testCreateAuthenticationInfo() - { + public void testCreateAuthenticationInfo() { User user = new User("tricia"); AuthenticationInfo authInfo = helper.createAuthenticationInfo("unit-test", - user, "heartOfGold"); + user, "heartOfGold"); assertNotNull(authInfo); assertEquals("tricia", authInfo.getPrincipals().getPrimaryPrincipal()); @@ -97,14 +129,13 @@ public class SyncingRealmHelperTest } /** - * Method description + * Tests {@link SyncingRealmHelper#store(Group)}. * * @throws GroupException * @throws IOException */ @Test - public void testStoreGroupCreate() throws GroupException, IOException - { + public void testStoreGroupCreate() throws GroupException, IOException { Group group = new Group("unit-test", "heartOfGold"); helper.store(group); @@ -112,15 +143,13 @@ public class SyncingRealmHelperTest } /** - * Method description - * + * Tests {@link SyncingRealmHelper#store(Group)} with thrown {@link GroupException}. * * @throws GroupException * @throws IOException */ @Test(expected = AuthenticationException.class) - public void testStoreGroupFailure() throws GroupException, IOException - { + public void testStoreGroupFailure() throws GroupException, IOException { Group group = new Group("unit-test", "heartOfGold"); doThrow(GroupException.class).when(groupManager).create(group); @@ -128,14 +157,13 @@ public class SyncingRealmHelperTest } /** - * Method description + * Tests {@link SyncingRealmHelper#store(Group)} with an existing group. * * @throws GroupException * @throws IOException */ @Test - public void testStoreGroupModify() throws GroupException, IOException - { + public void testStoreGroupModify() throws GroupException, IOException { Group group = new Group("unit-test", "heartOfGold"); when(groupManager.get("heartOfGold")).thenReturn(group); @@ -145,14 +173,13 @@ public class SyncingRealmHelperTest } /** - * Method description + * Tests {@link SyncingRealmHelper#store(User)}. * * @throws UserException * @throws IOException */ @Test - public void testStoreUserCreate() throws UserException, IOException - { + public void testStoreUserCreate() throws UserException, IOException { User user = new User("tricia"); helper.store(user); @@ -160,15 +187,13 @@ public class SyncingRealmHelperTest } /** - * Method description + * Tests {@link SyncingRealmHelper#store(User)} with a thrown {@link UserException}. * - * - * @throws IOException * @throws UserException + * @throws IOException */ @Test(expected = AuthenticationException.class) - public void testStoreUserFailure() throws UserException, IOException - { + public void testStoreUserFailure() throws UserException, IOException { User user = new User("tricia"); doThrow(UserException.class).when(userManager).create(user); @@ -176,14 +201,13 @@ public class SyncingRealmHelperTest } /** - * Method description + * Tests {@link SyncingRealmHelper#store(User)} with an existing user. * * @throws UserException * @throws IOException */ @Test - public void testStoreUserModify() throws UserException, IOException - { + public void testStoreUserModify() throws UserException, IOException { when(userManager.contains("tricia")).thenReturn(Boolean.TRUE); User user = new User("tricia"); @@ -191,52 +215,4 @@ public class SyncingRealmHelperTest helper.store(user); verify(userManager, times(1)).modify(user); } - - //~--- set methods ---------------------------------------------------------- - - /** - * Method description - * - */ - @Before - public void setUp() - { - AdministrationContext ctx = new AdministrationContext() - { - - @Override - public void runAsAdmin(PrivilegedAction action) - { - action.run(); - } - - @Override - public void runAsAdmin(Class actionClass) - { - try - { - runAsAdmin(actionClass.newInstance()); - } - catch (IllegalAccessException | InstantiationException ex) - { - throw Throwables.propagate(ex); - } - } - }; - - helper = new SyncingRealmHelper(ctx, userManager, groupManager); - } - - //~--- fields --------------------------------------------------------------- - - /** Field description */ - @Mock - private GroupManager groupManager; - - /** Field description */ - private SyncingRealmHelper helper; - - /** Field description */ - @Mock - private UserManager userManager; }