View Javadoc
1   package org.apache.archiva.metadata.repository.stats;
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 junit.framework.TestCase;
23  import org.apache.archiva.metadata.repository.MetadataRepository;
24  import org.apache.archiva.metadata.repository.RepositorySessionFactory;
25  import org.apache.commons.io.FileUtils;
26  import org.apache.jackrabbit.commons.JcrUtils;
27  import org.apache.jackrabbit.core.TransientRepository;
28  
29  import javax.inject.Inject;
30  import javax.jcr.ImportUUIDBehavior;
31  import javax.jcr.NamespaceRegistry;
32  import javax.jcr.Node;
33  import javax.jcr.RepositoryException;
34  import javax.jcr.Session;
35  import javax.jcr.SimpleCredentials;
36  import javax.jcr.Workspace;
37  import javax.jcr.nodetype.NodeTypeManager;
38  import javax.jcr.nodetype.NodeTypeTemplate;
39  import java.io.File;
40  import java.io.IOException;
41  import java.util.Calendar;
42  import java.util.Date;
43  import java.util.zip.GZIPInputStream;
44  
45  import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
46  import org.junit.After;
47  import org.junit.Before;
48  import org.junit.Test;
49  import org.junit.runner.RunWith;
50  
51  import static org.mockito.Mockito.*;
52  
53  @RunWith( ArchivaBlockJUnit4ClassRunner.class )
54  public class JcrRepositoryStatisticsGatheringTest
55      extends TestCase
56  {
57      private static final int TOTAL_FILE_COUNT = 1000;
58  
59      private static final int NEW_FILE_COUNT = 500;
60  
61      private static final String TEST_REPO = "test-repo";
62  
63      private RepositoryStatisticsManager repositoryStatisticsManager;
64  
65      private MetadataRepository metadataRepository;
66  
67      @Inject
68      private RepositorySessionFactory repositorySessionFactory;
69  
70      private Session session;
71  
72      @Override
73      @Before
74      public void setUp()
75          throws Exception
76      {
77          super.setUp();
78  
79          File confFile = new File( "src/test/repository.xml" );
80          File dir = new File( "target/jcr" );
81          FileUtils.deleteDirectory( dir );
82  
83          assertTrue( confFile.exists() );
84          assertFalse( dir.exists() );
85  
86          TransientRepository repository = new TransientRepository( confFile, dir );
87          session = repository.login( new SimpleCredentials( "username", "password".toCharArray() ) );
88  
89          // TODO: perhaps have an archiva-jcr-utils module shared by these plugins that does this and can contain
90          //      structure information
91          Workspace workspace = session.getWorkspace();
92          NamespaceRegistry registry = workspace.getNamespaceRegistry();
93          registry.registerNamespace( "archiva", "http://archiva.apache.org/jcr/" );
94  
95          NodeTypeManager nodeTypeManager = workspace.getNodeTypeManager();
96          registerMixinNodeType( nodeTypeManager, "archiva:namespace" );
97          registerMixinNodeType( nodeTypeManager, "archiva:project" );
98          registerMixinNodeType( nodeTypeManager, "archiva:projectVersion" );
99          registerMixinNodeType( nodeTypeManager, "archiva:artifact" );
100         registerMixinNodeType( nodeTypeManager, "archiva:facet" );
101 
102         metadataRepository = mock( MetadataRepository.class );
103         when( metadataRepository.canObtainAccess( Session.class ) ).thenReturn( true );
104         when( metadataRepository.obtainAccess( Session.class ) ).thenReturn( session );
105 
106         repositoryStatisticsManager = new DefaultRepositoryStatisticsManager();
107     }
108 
109     private static void registerMixinNodeType( NodeTypeManager nodeTypeManager, String type )
110         throws RepositoryException
111     {
112         NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
113         nodeType.setMixin( true );
114         nodeType.setName( type );
115         nodeTypeManager.registerNodeType( nodeType, false );
116     }
117 
118     @Override
119     @After
120     public void tearDown()
121         throws Exception
122     {
123         if ( session != null )
124         {
125             session.logout();
126         }
127 
128         super.tearDown();
129     }
130 
131     @Test
132     public void testJcrStatisticsQuery()
133         throws Exception
134     {
135         Calendar cal = Calendar.getInstance();
136         Date endTime = cal.getTime();
137         cal.add( Calendar.HOUR, -1 );
138         Date startTime = cal.getTime();
139 
140         loadContentIntoRepo( TEST_REPO );
141         loadContentIntoRepo( "another-repo" );
142 
143         repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO, startTime, endTime,
144                                                             TOTAL_FILE_COUNT, NEW_FILE_COUNT );
145 
146         RepositoryStatistics expectedStatistics = new RepositoryStatistics();
147         expectedStatistics.setNewFileCount( NEW_FILE_COUNT );
148         expectedStatistics.setTotalFileCount( TOTAL_FILE_COUNT );
149         expectedStatistics.setScanEndTime( endTime );
150         expectedStatistics.setScanStartTime( startTime );
151         expectedStatistics.setTotalArtifactFileSize( 95954585 );
152         expectedStatistics.setTotalArtifactCount( 269 );
153         expectedStatistics.setTotalGroupCount( 1 );
154         expectedStatistics.setTotalProjectCount( 43 );
155         expectedStatistics.setTotalCountForType( "zip", 1 );
156         expectedStatistics.setTotalCountForType( "gz", 1 ); // FIXME: should be tar.gz
157         expectedStatistics.setTotalCountForType( "java-source", 10 );
158         expectedStatistics.setTotalCountForType( "jar", 108 );
159         expectedStatistics.setTotalCountForType( "xml", 3 );
160         expectedStatistics.setTotalCountForType( "war", 2 );
161         expectedStatistics.setTotalCountForType( "pom", 144 );
162         expectedStatistics.setRepositoryId( TEST_REPO );
163 
164         verify( metadataRepository ).addMetadataFacet( TEST_REPO, expectedStatistics );
165     }
166 
167     private void loadContentIntoRepo( String repoId )
168         throws RepositoryException, IOException
169     {
170         Node n = JcrUtils.getOrAddNode( session.getRootNode(), "repositories" );
171         n = JcrUtils.getOrAddNode( n, repoId );
172         n = JcrUtils.getOrAddNode( n, "content" );
173         n = JcrUtils.getOrAddNode( n, "org" );
174         n = JcrUtils.getOrAddNode( n, "apache" );
175 
176         GZIPInputStream inputStream = new GZIPInputStream( getClass().getResourceAsStream( "/artifacts.xml.gz" ) );
177         session.importXML( n.getPath(), inputStream, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW );
178         session.save();
179     }
180 }