]> source.dussan.org Git - archiva.git/blob
171d6be600eca5ac8155947363196a3d9099d238
[archiva.git] /
1 package org.apache.archiva.metadata.repository.jcr;
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.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.model.MetadataFacetFactory;
24 import org.apache.archiva.metadata.repository.AbstractMetadataRepositoryTest;
25 import org.apache.archiva.metadata.repository.DefaultMetadataResolver;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.MetadataSessionException;
28 import org.apache.archiva.metadata.repository.RepositorySession;
29 import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34
35 import javax.jcr.Node;
36 import javax.jcr.NodeIterator;
37 import javax.jcr.RepositoryException;
38 import javax.jcr.Session;
39 import javax.jcr.query.QueryResult;
40 import javax.jcr.query.Row;
41 import javax.jcr.query.RowIterator;
42 import java.io.IOException;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.util.Collection;
47 import java.util.Map;
48
49 import static org.apache.archiva.metadata.repository.jcr.JcrConstants.PROJECT_VERSION_NODE_TYPE;
50 import static org.assertj.core.api.Assertions.assertThat;
51
52 /**
53  * Using static sessionFactory and repository, because initialization is expensive if we rebuild the whole repository for
54  * each test.
55  */
56 public class JcrMetadataRepositoryTest
57     extends AbstractMetadataRepositoryTest
58 {
59
60     private static JcrRepositorySessionFactory sessionFactory;
61     private static JcrMetadataRepository repository;
62
63     @Override
64     public JcrMetadataRepository getRepository( )
65     {
66         return repository;
67     }
68
69     @Override
70     public JcrRepositorySessionFactory getSessionFactory( )
71     {
72         return sessionFactory;
73     }
74
75     @BeforeClass
76     public static void setupSpec( ) throws IOException, InvalidFileStoreVersionException
77     {
78         Path directory = Paths.get( "target/test-repositories" );
79         if ( Files.exists( directory ) )
80         {
81             org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
82         }
83
84         Map<String, MetadataFacetFactory> factories = createTestMetadataFacetFactories( );
85         JcrRepositorySessionFactory jcrSessionFactory = new JcrRepositorySessionFactory( );
86         jcrSessionFactory.setMetadataResolver( new DefaultMetadataResolver( ) );
87         jcrSessionFactory.setMetadataFacetFactories( factories );
88
89         jcrSessionFactory.open( );
90         sessionFactory = jcrSessionFactory;
91         repository = jcrSessionFactory.getMetadataRepository( );
92
93     }
94
95     @Before
96     public void setup() throws MetadataRepositoryException, RepositoryException, MetadataSessionException
97     {
98         try( JcrRepositorySession session = (JcrRepositorySession) getSessionFactory().createSession() ) {
99             Session jcrSession = session.getJcrSession( );
100             if (jcrSession.itemExists( "/repositories/test" ))
101             {
102                 jcrSession.removeItem( "/repositories/test" );
103                 session.save( );
104             }
105         }
106     }
107
108     @AfterClass
109     public static void stopSpec( )
110         throws Exception
111     {
112         if ( repository != null )
113         {
114             try
115             {
116                 repository.close( );
117             }
118             catch ( Throwable e )
119             {
120                 //
121             }
122         }
123         if ( sessionFactory != null )
124         {
125             try
126             {
127                 sessionFactory.close( );
128             }
129             catch ( Throwable e )
130             {
131                 //
132             }
133         }
134     }
135
136
137     @Test
138     public void testSearchArtifactsByKey( )
139         throws Exception
140     {
141         try ( RepositorySession session = sessionFactory.createSession( ) )
142         {
143             createArtifactWithData( session );
144         }
145
146
147         tryAssert( ( ) -> {
148             try ( RepositorySession session = sessionFactory.createSession( ) )
149             {
150                 session.refreshAndDiscard( );
151                 Session jcrSession = ( (JcrRepositorySession) session ).getJcrSession( );
152                 assertThat(jcrSession.propertyExists( "/repositories/test/content/mytest/myproject/1.0/url" )).isTrue();
153
154                 Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "url", TEST_URL, false );
155                 assertThat( artifactsByProperty ).isNotNull( ).isNotEmpty( );
156             }
157         } );
158
159     }
160
161
162     @Test
163     public void testSearchArtifactsByKeyExact()
164         throws Exception {
165         try (RepositorySession session = sessionFactory.createSession())
166         {
167             createArtifactWithData( session );
168         }
169         try (RepositorySession session = sessionFactory.createSession())
170         {
171             session.refreshAndDiscard();
172             tryAssert(() -> {
173                 Session jcrSession = ( (JcrRepositorySession) session ).getJcrSession( );
174                 assertThat(jcrSession.propertyExists( "/repositories/test/content/mytest/myproject/1.0/url" )).isTrue();
175                 Collection<ArtifactMetadata> artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "url", TEST_URL, true);
176                 assertThat(artifactsByProperty).describedAs( "Artifact search by url=%s must give a result.", TEST_URL ).isNotNull().isNotEmpty();
177                 artifactsByProperty = repository.searchArtifacts( session, TEST_REPO_ID, "org.name", "pache", true );
178                 assertThat( artifactsByProperty ).describedAs( "Artifact search by text org.name='pache' must be empty" ).isNotNull( ).isEmpty( );
179             } );
180         }
181     }
182 }