You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2019/12/07 11:48:33 UTC

[isis] 05/06: ISIS-2218: fixes unit tests.

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit d5a97bdb44adb087517c06edb308d8285fa7a7cf
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Sat Dec 7 11:23:16 2019 +0000

    ISIS-2218: fixes unit tests.
---
 ...uthenticatedWebSessionForIsis_Authenticate.java | 93 ++++++++++++++++++++--
 .../AuthenticatedWebSessionForIsis_SignIn.java     | 81 +++++++++++++++++--
 ...uthenticatedWebSessionForIsis_TestAbstract.java |  2 -
 3 files changed, 160 insertions(+), 16 deletions(-)

diff --git a/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_Authenticate.java b/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_Authenticate.java
index a560257..462c594 100644
--- a/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_Authenticate.java
+++ b/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_Authenticate.java
@@ -19,8 +19,25 @@
 
 package org.apache.isis.viewer.wicket.viewer.integration;
 
+import java.util.Collections;
+import java.util.Locale;
+import java.util.Optional;
+
+import org.apache.isis.applib.services.registry.ServiceRegistry;
+import org.apache.isis.applib.services.session.SessionLoggingService;
+import org.apache.isis.runtime.system.session.IsisSessionFactory;
+import org.apache.isis.security.api.authentication.AuthenticationRequestPassword;
+import org.apache.isis.security.api.authentication.manager.AuthenticationManager;
+import org.apache.isis.security.api.authentication.standard.Authenticator;
+import org.apache.isis.security.api.authentication.standard.RandomCodeGeneratorDefault;
+import org.apache.isis.security.api.authentication.standard.SimpleSession;
+import org.apache.isis.unittestsupport.jmocking.JUnitRuleMockery2;
+import org.apache.isis.webapp.context.IsisWebAppCommonContext;
+import org.apache.wicket.request.Request;
 import org.jmock.Expectations;
+import org.jmock.auto.Mock;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 
 import org.apache.isis.security.api.authentication.AuthenticationRequest;
@@ -30,25 +47,83 @@ import static org.hamcrest.Matchers.not;
 import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertThat;
 
-public class AuthenticatedWebSessionForIsis_Authenticate 
-extends AuthenticatedWebSessionForIsis_TestAbstract {
+public class AuthenticatedWebSessionForIsis_Authenticate {
+
+
+    @Rule
+    public final JUnitRuleMockery2 context =
+            JUnitRuleMockery2.createFor(JUnitRuleMockery2.Mode.INTERFACES_AND_CLASSES);
+
+    @Mock
+    protected Request mockRequest;
+    private AuthenticationManager authMgr;
+    @Mock protected Authenticator mockAuthenticator;
+    @Mock protected IsisWebAppCommonContext mockCommonContext;
+    @Mock protected IsisSessionFactory mockIsisSessionFactory;
+    @Mock protected ServiceRegistry mockServiceRegistry;
+
+    protected AuthenticatedWebSessionForIsis webSession;
 
     @Before
     public void setUp() throws Exception {
-        super.setUp();
+        authMgr = new AuthenticationManager(Collections.singletonList(mockAuthenticator), new RandomCodeGeneratorDefault());
+
+        context.checking(new Expectations() {
+            {
+                allowing(mockCommonContext).getServiceRegistry();
+                will(returnValue(mockServiceRegistry));
+
+                allowing(mockServiceRegistry).lookupService(SessionLoggingService.class);
+                will(returnValue(Optional.empty()));
+
+                allowing(mockCommonContext).lookupServiceElseFail(IsisSessionFactory.class);
+                will(returnValue(mockIsisSessionFactory));
+
+                allowing(mockIsisSessionFactory).doInSession(with(any(Runnable.class)));
+                // ignore
+
+                // must provide explicit expectation, since Locale is final.
+                allowing(mockRequest).getLocale();
+                will(returnValue(Locale.getDefault()));
+
+                // stub everything else out
+                ignoring(mockRequest);
+            }
+        });
+
     }
 
+    protected void setupWebSession() {
+        webSession = new AuthenticatedWebSessionForIsis(mockRequest) {
+            private static final long serialVersionUID = 1L;
+
+            {
+                commonContext = mockCommonContext;
+            }
+
+            @Override
+            protected AuthenticationManager getAuthenticationManager() {
+                return authMgr;
+            }
+        };
+    }
+
+
+
     @Test
     public void delegatesToAuthenticationManagerAndCachesAuthSessionIfOk() {
 
         context.checking(new Expectations() {
             {
-                oneOf(mockAuthMgr).authenticate(with(any(AuthenticationRequest.class)));
+                oneOf(mockAuthenticator).canAuthenticate(AuthenticationRequestPassword.class);
+                will(returnValue(true));
+                oneOf(mockAuthenticator).authenticate(with(any(AuthenticationRequest.class)), with(any(String.class)));
+                will(returnValue(new SimpleSession(null, null)));
             }
         });
 
-        super.setupWebSession();
-        
+        setupWebSession();
+
         assertThat(webSession.authenticate("jsmith", "secret"), is(true));
         assertThat(webSession.getAuthenticationSession(), is(not(nullValue())));
     }
@@ -57,12 +132,14 @@ extends AuthenticatedWebSessionForIsis_TestAbstract {
     public void delegatesToAuthenticationManagerAndHandlesIfNotAuthenticated() {
         context.checking(new Expectations() {
             {
-                oneOf(mockAuthMgr).authenticate(with(any(AuthenticationRequest.class)));
+                oneOf(mockAuthenticator).canAuthenticate(AuthenticationRequestPassword.class);
+                will(returnValue(true));
+                oneOf(mockAuthenticator).authenticate(with(any(AuthenticationRequest.class)), with(any(String.class)));
                 will(returnValue(null));
             }
         });
         
-        super.setupWebSession();
+        setupWebSession();
         
         assertThat(webSession.authenticate("jsmith", "secret"), is(false));
         assertThat(webSession.getAuthenticationSession(), is(nullValue()));
diff --git a/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_SignIn.java b/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_SignIn.java
index a9e030b..9024086 100644
--- a/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_SignIn.java
+++ b/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_SignIn.java
@@ -19,8 +19,24 @@
 
 package org.apache.isis.viewer.wicket.viewer.integration;
 
+import java.util.Collections;
+import java.util.Locale;
+import java.util.Optional;
+
+import org.apache.isis.applib.services.registry.ServiceRegistry;
+import org.apache.isis.applib.services.session.SessionLoggingService;
+import org.apache.isis.runtime.system.session.IsisSessionFactory;
+import org.apache.isis.security.api.authentication.AuthenticationRequestPassword;
+import org.apache.isis.security.api.authentication.manager.AuthenticationManager;
+import org.apache.isis.security.api.authentication.standard.Authenticator;
+import org.apache.isis.security.api.authentication.standard.RandomCodeGeneratorDefault;
+import org.apache.isis.unittestsupport.jmocking.JUnitRuleMockery2;
+import org.apache.isis.webapp.context.IsisWebAppCommonContext;
+import org.apache.wicket.request.Request;
 import org.jmock.Expectations;
+import org.jmock.auto.Mock;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 
 import org.apache.isis.security.api.authentication.AuthenticationRequest;
@@ -28,25 +44,78 @@ import org.apache.isis.security.api.authentication.AuthenticationRequest;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertThat;
 
-public class AuthenticatedWebSessionForIsis_SignIn 
-extends AuthenticatedWebSessionForIsis_TestAbstract {
+public class AuthenticatedWebSessionForIsis_SignIn {
+
+    @Rule
+    public final JUnitRuleMockery2 context =
+            JUnitRuleMockery2.createFor(JUnitRuleMockery2.Mode.INTERFACES_AND_CLASSES);
+
+    @Mock
+    protected Request mockRequest;
+    private AuthenticationManager authMgr;
+    @Mock protected Authenticator mockAuthenticator;
+    @Mock protected IsisWebAppCommonContext mockCommonContext;
+    @Mock protected IsisSessionFactory mockIsisSessionFactory;
+    @Mock protected ServiceRegistry mockServiceRegistry;
+
+    protected AuthenticatedWebSessionForIsis webSession;
 
     @Before
     public void setUp() throws Exception {
-        super.setUp();
+        authMgr = new AuthenticationManager(Collections.singletonList(mockAuthenticator), new RandomCodeGeneratorDefault());
     }
 
     @Test
     public void signInJustDelegatesToAuthenticateAndSavesState() {
         context.checking(new Expectations() {
             {
-                oneOf(mockAuthMgr).authenticate(with(any(AuthenticationRequest.class)));
+                allowing(mockCommonContext).getServiceRegistry();
+                will(returnValue(mockServiceRegistry));
+
+                allowing(mockServiceRegistry).lookupService(SessionLoggingService.class);
+                will(returnValue(Optional.empty()));
+
+                allowing(mockCommonContext).lookupServiceElseFail(IsisSessionFactory.class);
+                will(returnValue(mockIsisSessionFactory));
+
+                allowing(mockIsisSessionFactory).doInSession(with(any(Runnable.class)));
+                // ignore
+
+                // must provide explicit expectation, since Locale is final.
+                allowing(mockRequest).getLocale();
+                will(returnValue(Locale.getDefault()));
+
+                // stub everything else out
+                ignoring(mockRequest);
+            }
+        });
+
+        context.checking(new Expectations() {
+            {
+                oneOf(mockAuthenticator).canAuthenticate(AuthenticationRequestPassword.class);
+                will(returnValue(true));
+                oneOf(mockAuthenticator).authenticate(with(any(AuthenticationRequest.class)), with(any(String.class)));
             }
         });
 
-        super.setupWebSession();
-        
+        webSession = new AuthenticatedWebSessionForIsis(mockRequest) {
+            private static final long serialVersionUID = 1L;
+
+            {
+                commonContext = mockCommonContext;
+            }
+
+            @Override
+            protected AuthenticationManager getAuthenticationManager() {
+                return authMgr;
+            }
+        };
+
+
+        // when
         webSession.signIn("john", "secret");
+
+        // then
         assertThat(webSession.isSignedIn(), is(true));
     }
 }
diff --git a/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_TestAbstract.java b/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_TestAbstract.java
index a3bbad1..c33ea57 100644
--- a/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_TestAbstract.java
+++ b/core/viewers/wicket/viewer/src/test/java/org/apache/isis/viewer/wicket/viewer/integration/AuthenticatedWebSessionForIsis_TestAbstract.java
@@ -86,8 +86,6 @@ public abstract class AuthenticatedWebSessionForIsis_TestAbstract {
             protected AuthenticationManager getAuthenticationManager() {
                 return mockAuthMgr;
             }
-
-            
         };
     }