/** @see junit.framework.TestCase#setUp() */ @Before public void setUp() throws Exception { super.setUp(); trustedLoginFilter = new TrustedLoginFilter(); when(componentManager.get(ServerConfigurationService.class)) .thenReturn(serverConfigurationService); when(componentManager.get(SessionManager.class)).thenReturn(sessionManager); when(componentManager.get(UserDirectoryService.class)).thenReturn(userDirectoryService); when(serverConfigurationService.getBoolean( TrustedLoginFilter.ORG_SAKAIPROJECT_UTIL_TRUSTED_LOGIN_FILTER_ENABLED, true)) .thenReturn(true); when(serverConfigurationService.getString( TrustedLoginFilter.ORG_SAKAIPROJECT_UTIL_TRUSTED_LOGIN_FILTER_SHARED_SECRET, null)) .thenReturn("e2KS54H35j6vS5Z38nK40"); when(serverConfigurationService.getString( TrustedLoginFilter.ORG_SAKAIPROJECT_UTIL_TRUSTED_LOGIN_FILTER_SAFE_HOSTS, trustedLoginFilter.safeHosts)) .thenReturn(trustedLoginFilter.safeHosts); when(request.getRemoteHost()).thenReturn("localhost"); when(request.getHeader("x-sakai-token")) .thenReturn("sw9TTTqlEbGQkELqQuQPq92ydr4=;username;nonce"); // default to non-existing session to exercise more code when(existingSession.getUserEid()).thenReturn(null); when(sessionManager.getCurrentSession()).thenReturn(existingSession); when(sessionManager.startSession()).thenReturn(newSession); when(user.getEid()).thenReturn("username"); when(user.getId()).thenReturn("uuid1234567890"); when(userDirectoryService.getUserByEid("username")).thenReturn(user); trustedLoginFilter.setupTestCase(componentManager); trustedLoginFilter.init(config); }
/** * Process that archives the sites * * @param sites list of SparseSite * @throws InterruptedException */ private void archiveSites( List<SparseSite> sites, String selectedTerm, Session currentSession, User currentUser) throws InterruptedException { if (isLocked()) { throw new IllegalStateException( "Cannot run batch archive, an archive job is already in progress"); } batchArchiveStarted = System.currentTimeMillis(); batchArchiveMessage = rb.getFormattedMessage( "archive.batch.term.text.statusmessage.start", new Object[] {sites.size(), selectedTerm, 0}); batchArchiveStatus = "RUNNING"; log.info( "Batch archive started for term: " + selectedTerm + ". Archiving " + sites.size() + " sites."); Session threadSession = sessionManager.getCurrentSession(); if (threadSession == null) { threadSession = sessionManager.startSession(); } threadSession.setUserId(currentUser.getId()); threadSession.setActive(); sessionManager.setCurrentSession(threadSession); authzGroupService.refreshUser(currentUser.getId()); // counters so we can run this in batches if we have a number of sites to process int archiveCount = 0; try { for (SparseSite s : sites) { log.info("Processing site: " + s.getTitle()); // archive the site archiveService.archive(s.getId()); // compress it // TODO check return value? do we care? try { archiveService.archiveAndZip(s.getId()); } catch (IOException e) { e.printStackTrace(); } archiveCount++; // update message if (archiveCount % 1 == 0) { int percentComplete = (int) (archiveCount * 100) / sites.size(); batchArchiveMessage = rb.getFormattedMessage( "archive.batch.term.text.statusmessage.update", new Object[] {sites.size(), selectedTerm, archiveCount, percentComplete}); } // sleep if we need to and keep sessions alive if (archiveCount > 0 && archiveCount % NUM_SITES_PER_BATCH == 0) { log.info("Sleeping for " + PAUSE_TIME_MS + "ms"); Thread.sleep(PAUSE_TIME_MS); threadSession.setActive(); currentSession.setActive(); } // check timeout if (!isLocked()) { throw new RuntimeException("Timeout occurred while running batch archive"); } } // complete batchArchiveMessage = rb.getFormattedMessage( "archive.batch.term.text.statusmessage.complete", new Object[] {sites.size(), selectedTerm}); log.info("Batch archive complete."); } finally { // reset batchArchiveStatus = STATUS_COMPLETE; batchArchiveStarted = 0; threadSession.clear(); threadSession.invalidate(); } }