View Javadoc
1   package org.apache.archiva.rest.services;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.admin.model.beans.ManagedRepository;
23  import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
24  import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
25  import org.apache.archiva.rest.api.services.RepositoriesService;
26  import org.junit.Test;
27  
28  import java.io.File;
29  
30  /**
31   * @author Olivier Lamy
32   */
33  public class ManagedRepositoriesServiceTest
34      extends AbstractArchivaRestTest
35  {
36  
37  
38      @Test
39      public void addManagedRepo()
40          throws Exception
41      {
42          ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
43  
44          ManagedRepository repo = getTestManagedRepository();
45          if ( service.getManagedRepository( repo.getId() ) != null )
46          {
47              service.deleteManagedRepository( repo.getId(), true );
48              assertNull( service.getManagedRepository( repo.getId() ) );
49          }
50          service.addManagedRepository( repo );
51          repo = service.getManagedRepository( repo.getId() );
52          assertNotNull( repo );
53  
54          assertEquals( getTestManagedRepository().getDescription(), repo.getDescription() );
55  
56          RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
57  
58          int timeout = 20000;
59          while ( timeout > 0 && repositoriesService.alreadyScanning( repo.getId() ) )
60          {
61              Thread.sleep( 500 );
62              timeout -= 500;
63          }
64  
65          service.deleteManagedRepository( repo.getId(), true );
66          assertNull( service.getManagedRepository( repo.getId() ) );
67      }
68  
69      @Test
70      public void updateManagedRepo()
71          throws Exception
72      {
73          ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
74  
75          ManagedRepository repo = getTestManagedRepository();
76          if ( service.getManagedRepository( repo.getId() ) != null )
77          {
78              service.deleteManagedRepository( repo.getId(), true );
79              assertNull( service.getManagedRepository( repo.getId() ) );
80          }
81          service.addManagedRepository( repo );
82  
83          RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
84  
85          int timeout = 20000;
86          while ( timeout > 0 && repositoriesService.alreadyScanning( repo.getId() ) )
87          {
88              Thread.sleep( 500 );
89              timeout -= 500;
90          }
91  
92          repo = service.getManagedRepository( repo.getId() );
93          assertNotNull( repo );
94          assertEquals( "test", repo.getName() );
95          // toto is foo in French :-)
96          repo.setName( "toto" );
97  
98          service.updateManagedRepository( repo );
99  
100         repo = service.getManagedRepository( repo.getId() );
101         assertNotNull( repo );
102         assertEquals( "toto", repo.getName() );
103 
104         timeout = 20000;
105         while ( timeout > 0 && repositoriesService.alreadyScanning( repo.getId() ) )
106         {
107             Thread.sleep( 500 );
108             timeout -= 500;
109         }
110 
111         service.deleteManagedRepository( repo.getId(), true );
112         assertNull( service.getManagedRepository( repo.getId() ) );
113 
114     }
115 
116     //@Test
117     public void fileLocationExists()
118         throws Exception
119     {
120         ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
121         File target = new File( "target" );
122 
123         assertTrue( service.fileLocationExists( target.getCanonicalPath() ) );
124 
125         // normally should not exists :-)
126         assertFalse( service.fileLocationExists( "/fooofofof/foddfdofd/dedede/kdeo" ) );
127 
128     }
129 
130     @Test
131     public void getManagedRepositoryStatistics()
132         throws Exception
133     {
134 
135         String testRepoId = "test-repo";
136         // force guest user creation if not exists
137         if ( getUserService( authorizationHeader ).getGuestUser() == null )
138         {
139             assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
140         }
141 
142         RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
143 
144         createAndIndexRepo( testRepoId,
145                             new File( System.getProperty( "basedir" ), "src/test/repo-with-osgi" ).getAbsolutePath() );
146 
147         repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
148 
149         int timeout = 20000;
150         while ( timeout > 0 && repositoriesService.alreadyScanning( testRepoId ) )
151         {
152             Thread.sleep( 500 );
153             timeout -= 500;
154         }
155 
156         ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
157 
158         ArchivaRepositoryStatistics archivaRepositoryStatistics =
159             service.getManagedRepositoryStatistics( testRepoId, "en" );
160 
161         assertNotNull( archivaRepositoryStatistics );
162 
163         log.info( "archivaRepositoryStatistics: {}", archivaRepositoryStatistics.toString() );
164 
165         assertEquals( 92, archivaRepositoryStatistics.getNewFileCount() );
166         assertEquals( 92, archivaRepositoryStatistics.getTotalFileCount() );
167 
168         deleteTestRepo( testRepoId );
169     }
170 
171 
172 }