1 package org.apache.archiva.metadata.repository.cassandra;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
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;
54 * @author Olivier Lamy
56 @ExtendWith( SpringExtension.class )
57 @TestInstance( TestInstance.Lifecycle.PER_CLASS )
58 public class CassandraMetadataRepositoryTest
59 extends AbstractMetadataRepositoryTest
62 @Named(value = "archivaEntityManagerFactory#cassandra")
63 CassandraArchivaManager cassandraArchivaManager;
65 CassandraMetadataRepository cmr;
67 IMocksControl sessionFactoryControl;
68 RepositorySessionFactory sessionFactory;
70 IMocksControl sessionControl;
71 RepositorySession session;
75 AtomicBoolean clearedTables = new AtomicBoolean( false );
79 protected RepositorySessionFactory getSessionFactory( )
81 return sessionFactory;
85 protected MetadataRepository getRepository( )
91 public void setUp( TestInfo testInfo )
94 cTime = System.currentTimeMillis( );
95 System.err.println( "Setting up "+(testNum++) + " - " + testInfo.getDisplayName() );
97 System.err.println( "Setting up 2 " + testInfo.getDisplayName( ) + " - " + (System.currentTimeMillis( ) - cTime) );
99 assertRetrySleepMs=10;
101 Path directory = Paths.get( "target/test-repositories" );
102 if ( Files.exists(directory) )
104 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
107 List<MetadataFacetFactory> factories = createTestMetadataFacetFactories();
108 MetadataService metadataService = new MetadataService( );
109 metadataService.setMetadataFacetFactories( factories );
111 this.cmr = new CassandraMetadataRepository( metadataService, cassandraArchivaManager );
113 sessionFactoryControl = EasyMock.createControl( );
114 sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
115 sessionControl = EasyMock.createControl( );
116 session = sessionControl.createMock( RepositorySession.class );
118 EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( session );
120 sessionFactoryControl.replay();
122 if (!clearedTables.get())
124 clearReposAndNamespace( cassandraArchivaManager );
125 clearedTables.set( true );
127 System.err.println( "Finished setting up "+testInfo.getDisplayName() + " - " + (System.currentTimeMillis( ) - cTime) );
131 * ensure all dependant tables are cleaned up (mailinglist, license, dependencies)
136 public void clean_dependant_tables()
140 super.testUpdateProjectVersionMetadataWithAllElements();
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 ) //
149 this.cmr.removeProjectVersion( null, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
152 cmr.getProjectVersion( null , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull();
154 assertThat( cmr.getMailingLists( key ) ).isNotNull().isEmpty();
156 assertThat( cmr.getLicenses( key ) ).isNotNull().isEmpty();
158 assertThat( cmr.getDependencies( key ) ).isNotNull().isEmpty();
163 public void shutdown(TestInfo testInfo)
166 System.err.println( "Shutting down " + testInfo.getDisplayName( ) + " - " + ( System.currentTimeMillis( ) - cTime ) );
167 clearReposAndNamespace( cassandraArchivaManager );
168 clearedTables.set( true );
170 System.err.println( "Shutting down finished" + testInfo.getDisplayName( ) + " - " + ( System.currentTimeMillis( ) - cTime ) );
173 static void clearReposAndNamespace( CassandraArchivaManager cassandraArchivaManager )
176 if (cassandraArchivaManager!=null)
178 CqlSession session = cassandraArchivaManager.getSession( );
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( )
191 for ( String table : tables )
193 session.execute( truncate( table ).build( ) );
198 System.err.println( "cassandraArchivaManager is null" );