]> source.dussan.org Git - archiva.git/blob
491edc2f03ac2d8315b0e8fb069df558d1eec955
[archiva.git] /
1 package org.apache.archiva.metadata.repository.cassandra;
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 com.datastax.oss.driver.api.core.CqlSession;
23 import org.apache.archiva.metadata.model.MetadataFacetFactory;
24 import org.apache.archiva.metadata.repository.AbstractMetadataRepositoryTest;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.MetadataService;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
29 import org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel;
30 import org.easymock.EasyMock;
31 import org.easymock.IMocksControl;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.TestInfo;
36 import org.junit.jupiter.api.TestInstance;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.springframework.test.context.junit.jupiter.SpringExtension;
39
40 import javax.inject.Inject;
41 import javax.inject.Named;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.Arrays;
46 import java.util.List;
47 import java.util.concurrent.atomic.AtomicBoolean;
48
49 import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.truncate;
50 import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.dropTable;
51 import static org.assertj.core.api.Assertions.assertThat;
52
53 /**
54  * @author Olivier Lamy
55  */
56 @ExtendWith( SpringExtension.class )
57 @TestInstance( TestInstance.Lifecycle.PER_CLASS )
58 public class CassandraMetadataRepositoryTest
59     extends AbstractMetadataRepositoryTest
60 {
61     @Inject
62     @Named(value = "archivaEntityManagerFactory#cassandra")
63     CassandraArchivaManager cassandraArchivaManager;
64
65     CassandraMetadataRepository cmr;
66
67     IMocksControl sessionFactoryControl;
68     RepositorySessionFactory sessionFactory;
69
70     IMocksControl sessionControl;
71     RepositorySession session;
72
73     long cTime;
74     int testNum = 0;
75     AtomicBoolean clearedTables = new AtomicBoolean( false );
76
77
78     @Override
79     protected RepositorySessionFactory getSessionFactory( )
80     {
81         return sessionFactory;
82     }
83
84     @Override
85     protected MetadataRepository getRepository( )
86     {
87         return cmr;
88     }
89
90     @BeforeEach
91     public void setUp( TestInfo testInfo )
92         throws Exception
93     {
94         cTime = System.currentTimeMillis( );
95         System.err.println( "Setting up "+(testNum++) + " - " + testInfo.getDisplayName() );
96         super.setUp();
97         System.err.println( "Setting up 2 " + testInfo.getDisplayName( ) + " - " + (System.currentTimeMillis( ) - cTime) );
98         assertMaxTries =1;
99         assertRetrySleepMs=10;
100
101         Path directory = Paths.get( "target/test-repositories" );
102         if ( Files.exists(directory) )
103         {
104             org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
105         }
106
107         List<MetadataFacetFactory> factories = createTestMetadataFacetFactories();
108         MetadataService metadataService = new MetadataService( );
109         metadataService.setMetadataFacetFactories( factories );
110
111         this.cmr = new CassandraMetadataRepository( metadataService, cassandraArchivaManager );
112
113         sessionFactoryControl = EasyMock.createControl( );
114         sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
115         sessionControl = EasyMock.createControl( );
116         session = sessionControl.createMock( RepositorySession.class );
117
118         EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( session );
119
120         sessionFactoryControl.replay();
121
122         if (!clearedTables.get())
123         {
124             clearReposAndNamespace( cassandraArchivaManager );
125             clearedTables.set( true );
126         }
127         System.err.println( "Finished setting up "+testInfo.getDisplayName() + " - " + (System.currentTimeMillis( ) - cTime) );
128     }
129
130     /**
131      * ensure all dependant tables are cleaned up (mailinglist, license, dependencies)
132      *
133      * @throws Exception
134      */
135     @Test
136     public void clean_dependant_tables()
137         throws Exception
138     {
139
140         super.testUpdateProjectVersionMetadataWithAllElements();
141
142         String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( TEST_REPO_ID ) //
143             .withNamespace( TEST_NAMESPACE ) //
144             .withProjectId( TEST_PROJECT ) //
145             .withProjectVersion( TEST_PROJECT_VERSION ) //
146             .withId( TEST_PROJECT_VERSION ) //
147             .build();
148
149         this.cmr.removeProjectVersion( null, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
150
151         assertThat(
152             cmr.getProjectVersion( null , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull();
153
154         assertThat( cmr.getMailingLists( key ) ).isNotNull().isEmpty();
155
156         assertThat( cmr.getLicenses( key ) ).isNotNull().isEmpty();
157
158         assertThat( cmr.getDependencies( key ) ).isNotNull().isEmpty();
159     }
160
161
162     @AfterEach
163     public void shutdown(TestInfo testInfo)
164         throws Exception
165     {
166         System.err.println( "Shutting down " + testInfo.getDisplayName( ) + " - " + ( System.currentTimeMillis( ) - cTime ) );
167         clearReposAndNamespace( cassandraArchivaManager );
168         clearedTables.set( true );
169         super.tearDown();
170         System.err.println( "Shutting down finished" + testInfo.getDisplayName( ) + " - " + ( System.currentTimeMillis( ) - cTime ) );
171     }
172
173     static void clearReposAndNamespace( CassandraArchivaManager cassandraArchivaManager )
174         throws Exception
175     {
176         if (cassandraArchivaManager!=null)
177         {
178             CqlSession session = cassandraArchivaManager.getSession( );
179             {
180                 List<String> tables = Arrays.asList(
181                     cassandraArchivaManager.getProjectFamilyName( ),
182                     cassandraArchivaManager.getNamespaceFamilyName( ),
183                     cassandraArchivaManager.getRepositoryFamilyName( ),
184                     cassandraArchivaManager.getProjectVersionMetadataFamilyName( ),
185                     cassandraArchivaManager.getArtifactMetadataFamilyName( ),
186                     cassandraArchivaManager.getMetadataFacetFamilyName( ),
187                     cassandraArchivaManager.getMailingListFamilyName( ),
188                     cassandraArchivaManager.getLicenseFamilyName( ),
189                     cassandraArchivaManager.getDependencyFamilyName( )
190                 );
191                 for ( String table : tables )
192                 {
193                     session.execute( truncate( table ).build( ) );
194                 }
195
196             }
197         } else {
198             System.err.println( "cassandraArchivaManager is null" );
199         }
200     }
201
202 }