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.CompletableFuture;
48 import java.util.concurrent.CompletionStage;
49 import java.util.concurrent.atomic.AtomicBoolean;
50 import java.util.stream.Collectors;
52 import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.truncate;
53 import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.dropTable;
54 import static org.assertj.core.api.Assertions.assertThat;
57 * @author Olivier Lamy
59 @ExtendWith( SpringExtension.class )
60 @TestInstance( TestInstance.Lifecycle.PER_CLASS )
61 public class CassandraMetadataRepositoryTest
62 extends AbstractMetadataRepositoryTest
65 @Named(value = "archivaEntityManagerFactory#cassandra")
66 CassandraArchivaManager cassandraArchivaManager;
68 CassandraMetadataRepository cmr;
70 IMocksControl sessionFactoryControl;
71 RepositorySessionFactory sessionFactory;
73 IMocksControl sessionControl;
74 RepositorySession session;
78 final AtomicBoolean clearedTables = new AtomicBoolean( false );
82 protected RepositorySessionFactory getSessionFactory( )
84 return sessionFactory;
88 protected MetadataRepository getRepository( )
94 public void setUp( TestInfo testInfo )
97 cTime = System.currentTimeMillis( );
98 System.err.println( "Setting up - "+(testNum++) + " - " + testInfo.getDisplayName() + " - 0ms");
100 System.err.println( "Setting up - " + testInfo.getDisplayName( ) + " - " + (System.currentTimeMillis( ) - cTime) +"ms");
102 assertRetrySleepMs=10;
104 Path directory = Paths.get( "target/test-repositories" );
105 if ( Files.exists(directory) )
107 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
110 List<MetadataFacetFactory> factories = createTestMetadataFacetFactories();
111 MetadataService metadataService = new MetadataService( );
112 metadataService.setMetadataFacetFactories( factories );
114 this.cmr = new CassandraMetadataRepository( metadataService, cassandraArchivaManager );
116 sessionFactoryControl = EasyMock.createControl( );
117 sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
118 sessionControl = EasyMock.createControl( );
119 session = sessionControl.createMock( RepositorySession.class );
121 EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( session );
123 sessionFactoryControl.replay();
125 if (!clearedTables.get())
127 clearReposAndNamespace( cassandraArchivaManager, clearedTables );
129 System.err.println( "Finished setting up - "+testInfo.getDisplayName() + " - " + (System.currentTimeMillis( ) - cTime) +"ms");
133 * ensure all dependant tables are cleaned up (mailinglist, license, dependencies)
138 public void clean_dependant_tables()
142 super.testUpdateProjectVersionMetadataWithAllElements();
144 String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( TEST_REPO_ID ) //
145 .withNamespace( TEST_NAMESPACE ) //
146 .withProjectId( TEST_PROJECT ) //
147 .withProjectVersion( TEST_PROJECT_VERSION ) //
148 .withId( TEST_PROJECT_VERSION ) //
151 this.cmr.removeProjectVersion( null, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
154 cmr.getProjectVersion( null , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull();
156 assertThat( cmr.getMailingLists( key ) ).isNotNull().isEmpty();
158 assertThat( cmr.getLicenses( key ) ).isNotNull().isEmpty();
160 assertThat( cmr.getDependencies( key ) ).isNotNull().isEmpty();
165 public void shutdown(TestInfo testInfo)
168 System.err.println( "Shutting down - " + (testNum-1) + " - " + testInfo.getDisplayName( ) + " - " + ( System.currentTimeMillis( ) - cTime ) +"ms");
169 clearReposAndNamespace( cassandraArchivaManager, clearedTables );
171 System.err.println( "Shutting down finished - " + testInfo.getDisplayName( ) + " - " + ( System.currentTimeMillis( ) - cTime ) +"ms");
174 static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager, final AtomicBoolean clearedFlag )
177 if (cassandraArchivaManager!=null)
179 CqlSession session = cassandraArchivaManager.getSession( );
181 List<String> tables = Arrays.asList(
182 cassandraArchivaManager.getProjectFamilyName( ),
183 cassandraArchivaManager.getNamespaceFamilyName( ),
184 cassandraArchivaManager.getRepositoryFamilyName( ),
185 cassandraArchivaManager.getProjectVersionMetadataFamilyName( ),
186 cassandraArchivaManager.getArtifactMetadataFamilyName( ),
187 cassandraArchivaManager.getMetadataFacetFamilyName( ),
188 cassandraArchivaManager.getMailingListFamilyName( ),
189 cassandraArchivaManager.getLicenseFamilyName( ),
190 cassandraArchivaManager.getDependencyFamilyName( )
192 CompletableFuture.allOf( tables.stream( ).map( table -> session.executeAsync( truncate( table ).build( ) ) )
193 .map( CompletionStage::toCompletableFuture ).collect( Collectors.toList( ) ).toArray( new CompletableFuture[0] ) )
194 .whenComplete( ( c, e ) -> {
195 if ( clearedFlag != null ) clearedFlag.set( true );
197 System.err.println( "TRUNCATE ERROR DETECTED: " + e.getMessage( ) );
203 System.err.println( "cassandraArchivaManager is null" );
207 static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager)
209 clearReposAndNamespace( cassandraArchivaManager, null );