API { public Optional<Session> getSession() { if (currentSession != null) { return Optional.of(currentSession); } try { currentSession = sessionManager.getSession(requestLookup.getRequest(), requestLookup.getResponse()) .orElse(null); } catch (SessionManagementException e) { throw new PluginExecutionException( "Cannot retrieve current session for request '" + requestLookup.getRequest() + "' using session manager '" + sessionManager.getClass().getName() + "'.", e); } return Optional.ofNullable(currentSession); } API(SessionManager sessionManager, Authorizer authorizer, RequestLookup requestLookup); RequestLookup getRequestLookup(); static Object callOSGiService(String serviceClassName, String serviceMethodName, Object... args); static Map<String, Object> getOSGiServices(String serviceClassName); static void callMicroService(); static void sendError(int status, String message); static void sendRedirect(String redirectUrl); Session createSession(User user); Optional<Session> getSession(); boolean hasPermission(Permission permission); boolean destroySession();  }
@Test public void testGetSession() throws Exception { Configuration configuration = mock(Configuration.class); when(configuration.getSessionTimeout()).thenReturn(600L); HttpRequest request = mock(HttpRequest.class); when(request.getContextPath()).thenReturn("/test"); SessionManager sessionManager = createSessionManager(); mockGetSession(sessionManager); API api = new API(sessionManager, null, new RequestLookup(null, request, null)); Assert.assertEquals(api.getSession().isPresent(), true); }
@Test public void testGetSessionWhenSessionNotAvailable() throws Exception { HttpRequest request = mock(HttpRequest.class); when(request.getContextPath()).thenReturn("/test"); SessionManager sessionManager = createSessionManager(); when(sessionManager.getSession(request, null)).thenReturn(Optional.empty()); API api = new API(sessionManager, null, new RequestLookup(null, request, null)); Assert.assertEquals(api.getSession().isPresent(), false); }