123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- package org.apache.archiva.admin.repository.admin;
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
- import org.apache.archiva.admin.model.RepositoryAdminException;
- import org.apache.archiva.admin.model.admin.RepositoryTaskAdministration;
- import org.apache.archiva.admin.model.beans.ScanStatus;
- import org.apache.archiva.components.taskqueue.TaskQueue;
- import org.apache.archiva.components.taskqueue.TaskQueueException;
- import org.apache.archiva.components.taskqueue.execution.TaskQueueExecutor;
- import org.apache.archiva.repository.ManagedRepository;
- import org.apache.archiva.repository.RepositoryRegistry;
- import org.apache.archiva.repository.storage.StorageAsset;
- import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
- import org.apache.archiva.scheduler.indexing.IndexingArchivaTaskScheduler;
- import org.apache.archiva.scheduler.repository.model.RepositoryArchivaTaskScheduler;
- import org.apache.archiva.scheduler.repository.model.RepositoryTask;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import org.mockito.ArgumentCaptor;
-
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.List;
-
- import static org.junit.jupiter.api.Assertions.*;
- import static org.mockito.Mockito.*;
-
- /**
- * @author Martin Stockhammer <martin_s@apache.org>
- */
- @Tag( "archiva-admin" )
- @DisplayName( "Unit Tests for RepositoryTaskAdministration" )
- public class RepositoryTaskAdministrationTest
- {
-
- private RepositoryTaskAdministration taskAdministration;
- private TaskQueueExecutor<ArtifactIndexingTask> indexingTaskExecutor;
- private TaskQueueExecutor<RepositoryTask> scanningTaskExecutor;
- private RepositoryArchivaTaskScheduler repositoryArchivaTaskScheduler;
- private IndexingArchivaTaskScheduler indexingArchivaTaskScheduler;
- private RepositoryRegistry registry;
-
- @BeforeEach
- public void init() {
- registry = mock( RepositoryRegistry.class );
- indexingTaskExecutor = mock( TaskQueueExecutor.class );
- scanningTaskExecutor = mock( TaskQueueExecutor.class );
- repositoryArchivaTaskScheduler = mock( RepositoryArchivaTaskScheduler.class );
- indexingArchivaTaskScheduler = mock( IndexingArchivaTaskScheduler.class );
- this.taskAdministration = new DefaultRepositoryTaskAdministration( registry, indexingTaskExecutor,
- scanningTaskExecutor, repositoryArchivaTaskScheduler, indexingArchivaTaskScheduler );
- }
-
- @Test
- public void testScanStatus() throws RepositoryAdminException, TaskQueueException
- {
- TaskQueue queue = mock( TaskQueue.class );
- TaskQueue indexQueue = mock( TaskQueue.class );
-
- List<RepositoryTask> scanList = new ArrayList<>( );
- RepositoryTask scanTask1 = new RepositoryTask( );
- scanTask1.setRepositoryId( "abcde" );
- scanTask1.setScanAll( true );
- scanTask1.setResourceFile( null );
- scanList.add( scanTask1 );
- RepositoryTask scanTask2 = new RepositoryTask( );
- scanTask2.setRepositoryId( "testrepo2" );
- scanTask2.setScanAll( true );
- scanTask2.setResourceFile( null );
- scanList.add( scanTask1 );
-
- List<ArtifactIndexingTask> indexList = new ArrayList<>( );
- ArtifactIndexingTask indexTask1 = mock( ArtifactIndexingTask.class, RETURNS_DEEP_STUBS );
- when( indexTask1.getRepository( ).getId( ) ).thenReturn( "indexrepo1" );
- when( indexTask1.isExecuteOnEntireRepo( ) ).thenReturn( true );
- when( indexTask1.getResourceFile( ) ).thenReturn( null );
- indexList.add( indexTask1 );
- ArtifactIndexingTask indexTask2 = mock( ArtifactIndexingTask.class, RETURNS_DEEP_STUBS );
- when( indexTask2.getRepository( ).getId( ) ).thenReturn( "indexrepo2" );
- when( indexTask2.isExecuteOnEntireRepo( ) ).thenReturn( true );
- when( indexTask2.getResourceFile( ) ).thenReturn( null );
- indexList.add( indexTask2 );
-
- when( scanningTaskExecutor.getQueue( ) ).thenReturn( queue );
- when( indexingTaskExecutor.getQueue( ) ).thenReturn( indexQueue );
- when( queue.getQueueSnapshot( ) ).thenReturn( scanList );
- when( indexQueue.getQueueSnapshot( ) ).thenReturn( indexList );
- ScanStatus currentScanStatus = taskAdministration.getCurrentScanStatus( );
- assertNotNull( currentScanStatus );
- assertNotNull( currentScanStatus.getIndexingQueue( ) );
- assertNotNull( currentScanStatus.getScanQueue( ) );
- assertEquals( 2, currentScanStatus.getScanQueue( ).size( ) );
- assertEquals( 2, currentScanStatus.getIndexingQueue( ).size( ) );
- }
-
- @Test
- public void testScanStatusWithId() throws RepositoryAdminException, TaskQueueException
- {
- TaskQueue queue = mock( TaskQueue.class );
- TaskQueue indexQueue = mock( TaskQueue.class );
-
- List<RepositoryTask> scanList = new ArrayList<>( );
- RepositoryTask scanTask1 = new RepositoryTask( );
- scanTask1.setRepositoryId( "abcde" );
- scanTask1.setScanAll( true );
- scanTask1.setResourceFile( null );
- scanList.add( scanTask1 );
- RepositoryTask scanTask2 = new RepositoryTask( );
- scanTask2.setRepositoryId( "testrepo2" );
- scanTask2.setScanAll( true );
- scanTask2.setResourceFile( null );
- scanList.add( scanTask1 );
-
- List<ArtifactIndexingTask> indexList = new ArrayList<>( );
- ArtifactIndexingTask indexTask1 = mock( ArtifactIndexingTask.class, RETURNS_DEEP_STUBS );
- when( indexTask1.getRepository( ).getId( ) ).thenReturn( "indexrepo1" );
- when( indexTask1.isExecuteOnEntireRepo( ) ).thenReturn( true );
- when( indexTask1.getResourceFile( ) ).thenReturn( null );
- indexList.add( indexTask1 );
- ArtifactIndexingTask indexTask2 = mock( ArtifactIndexingTask.class, RETURNS_DEEP_STUBS );
- when( indexTask2.getRepository( ).getId( ) ).thenReturn( "indexrepo2" );
- when( indexTask2.isExecuteOnEntireRepo( ) ).thenReturn( true );
- when( indexTask2.getResourceFile( ) ).thenReturn( null );
- indexList.add( indexTask2 );
-
- when( scanningTaskExecutor.getQueue( ) ).thenReturn( queue );
- when( indexingTaskExecutor.getQueue( ) ).thenReturn( indexQueue );
- when( queue.getQueueSnapshot( ) ).thenReturn( scanList );
- when( indexQueue.getQueueSnapshot( ) ).thenReturn( indexList );
- when( registry.getManagedRepository( "indexrepo2" ) ).thenReturn( mock( ManagedRepository.class ) );
- ScanStatus currentScanStatus = taskAdministration.getCurrentScanStatus( "indexrepo2");
- assertNotNull( currentScanStatus );
- assertNotNull( currentScanStatus.getIndexingQueue( ) );
- assertNotNull( currentScanStatus.getScanQueue( ) );
- assertEquals( 0, currentScanStatus.getScanQueue( ).size( ) );
- assertEquals( 1, currentScanStatus.getIndexingQueue( ).size( ) );
- }
-
- @Test
- public void testScheduleFullScan() throws RepositoryAdminException, TaskQueueException
- {
- when( registry.getManagedRepository( "internal" ) ).thenReturn( mock( ManagedRepository.class ) );
- taskAdministration.scheduleFullScan( "internal" );
- verify( repositoryArchivaTaskScheduler, times(1) ).queueTask( any() );
- verify( indexingArchivaTaskScheduler, times(1) ).queueTask( any() );
- }
-
- @Test
- public void testScheduleIndexScan() throws RepositoryAdminException, TaskQueueException
- {
- when( registry.getManagedRepository( "internal" ) ).thenReturn( mock( ManagedRepository.class ) );
- taskAdministration.scheduleIndexFullScan( "internal" );
- ArgumentCaptor<ArtifactIndexingTask> captor = ArgumentCaptor.forClass( ArtifactIndexingTask.class );
- verify( repositoryArchivaTaskScheduler, times(0) ).queueTask( any() );
- verify( indexingArchivaTaskScheduler, times(1) ).queueTask( captor.capture() );
- assertTrue(captor.getValue().isExecuteOnEntireRepo());
- }
-
- @Test
- public void testScheduleIndexScanWithFile() throws RepositoryAdminException, TaskQueueException
- {
- ManagedRepository managedRepo = mock( ManagedRepository.class, RETURNS_DEEP_STUBS );
- when( registry.getManagedRepository( "internal" ) ).thenReturn( managedRepo );
- StorageAsset asset = mock( StorageAsset.class );
- when( asset.getFilePath( ) ).thenReturn( Paths.get( "abc/def/ghij.pom" ) );
- when( asset.exists( ) ).thenReturn( true );
- when( registry.getManagedRepository( "internal" ).getAsset( "abc/def/ghij.pom" ) ).thenReturn( asset );
- taskAdministration.scheduleIndexScan( "internal", "abc/def/ghij.pom" );
- ArgumentCaptor<ArtifactIndexingTask> captor = ArgumentCaptor.forClass( ArtifactIndexingTask.class );
- verify( repositoryArchivaTaskScheduler, times(0) ).queueTask( any() );
- verify( indexingArchivaTaskScheduler, times(1) ).queueTask( captor.capture() );
- ArtifactIndexingTask caption = captor.getValue( );
- assertFalse(caption.isExecuteOnEntireRepo());
- assertEquals( "abc/def/ghij.pom", caption.getResourceFile( ).toString() );
- }
-
- @Test
- public void testScheduleMetadataScan() throws RepositoryAdminException, TaskQueueException
- {
- when( registry.getManagedRepository( "internal" ) ).thenReturn( mock( ManagedRepository.class ) );
- taskAdministration.scheduleMetadataFullScan( "internal" );
- ArgumentCaptor<RepositoryTask> captor = ArgumentCaptor.forClass( RepositoryTask.class );
- verify( repositoryArchivaTaskScheduler, times(1) ).queueTask( captor.capture( ) );
- verify( indexingArchivaTaskScheduler, times(0) ).queueTask( any() );
- assertTrue(captor.getValue().isScanAll());
- }
-
- @Test
- public void testScheduleMetadataUpdateScan() throws RepositoryAdminException, TaskQueueException
- {
- when( registry.getManagedRepository( "internal" ) ).thenReturn( mock( ManagedRepository.class ) );
- taskAdministration.scheduleMetadataUpdateScan( "internal" );
- ArgumentCaptor<RepositoryTask> captor = ArgumentCaptor.forClass( RepositoryTask.class );
- verify( repositoryArchivaTaskScheduler, times(1) ).queueTask( captor.capture( ) );
- verify( indexingArchivaTaskScheduler, times(0) ).queueTask( any() );
- assertFalse(captor.getValue().isScanAll());
- }
-
-
- @Test
- void cancelAllTasks() throws TaskQueueException, RepositoryAdminException
- {
- TaskQueue queue = mock( TaskQueue.class );
- TaskQueue indexQueue = mock( TaskQueue.class );
-
- List<RepositoryTask> scanList = new ArrayList<>( );
- RepositoryTask scanTask1 = new RepositoryTask( );
- scanTask1.setRepositoryId( "abcde" );
- scanTask1.setScanAll( true );
- scanTask1.setResourceFile( null );
- scanList.add( scanTask1 );
- RepositoryTask scanTask2 = new RepositoryTask( );
- scanTask2.setRepositoryId( "testrepo2" );
- scanTask2.setScanAll( true );
- scanTask2.setResourceFile( null );
- scanList.add( scanTask1 );
-
- List<ArtifactIndexingTask> indexList = new ArrayList<>( );
- ArtifactIndexingTask indexTask1 = mock( ArtifactIndexingTask.class, RETURNS_DEEP_STUBS );
- when( indexTask1.getRepository( ).getId( ) ).thenReturn( "indexrepo1" );
- when( indexTask1.isExecuteOnEntireRepo( ) ).thenReturn( true );
- when( indexTask1.getResourceFile( ) ).thenReturn( null );
- indexList.add( indexTask1 );
- ArtifactIndexingTask indexTask2 = mock( ArtifactIndexingTask.class, RETURNS_DEEP_STUBS );
- when( indexTask2.getRepository( ).getId( ) ).thenReturn( "indexrepo2" );
- when( indexTask2.isExecuteOnEntireRepo( ) ).thenReturn( true );
- when( indexTask2.getResourceFile( ) ).thenReturn( null );
- indexList.add( indexTask2 );
-
- when( scanningTaskExecutor.getQueue( ) ).thenReturn( queue );
- when( indexingTaskExecutor.getQueue( ) ).thenReturn( indexQueue );
- when( queue.getQueueSnapshot( ) ).thenReturn( scanList );
- when( indexQueue.getQueueSnapshot( ) ).thenReturn( indexList );
- taskAdministration.cancelTasks( "indexrepo1" );
- ArgumentCaptor<List> scanCaptor = ArgumentCaptor.forClass( List.class );
- ArgumentCaptor<List> indexCaptor = ArgumentCaptor.forClass( List.class );
-
- verify( queue ).removeAll( scanCaptor.capture() );
- verify( indexQueue ).removeAll( indexCaptor.capture() );
-
- List scanCancelList = scanCaptor.getValue( );
- List indexCancelList = indexCaptor.getValue( );
- assertEquals( 0, scanCancelList.size( ) );
- assertEquals( 1, indexCancelList.size( ) );
- assertEquals( "indexrepo1", ( (ArtifactIndexingTask) indexCancelList.get( 0 ) ).getRepository( ).getId( ) );
-
-
- }
- }
|