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( );
100 assertRetrySleepMs=10;
102 Path directory = Paths.get( "target/test-repositories" );
103 if ( Files.exists(directory) )
105 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
108 List<MetadataFacetFactory> factories = createTestMetadataFacetFactories();
109 MetadataService metadataService = new MetadataService( );
110 metadataService.setMetadataFacetFactories( factories );
112 this.cmr = new CassandraMetadataRepository( metadataService, cassandraArchivaManager );
114 sessionFactoryControl = EasyMock.createControl( );
115 sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
116 sessionControl = EasyMock.createControl( );
117 session = sessionControl.createMock( RepositorySession.class );
119 EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( session );
121 sessionFactoryControl.replay();
123 if (!clearedTables.get())
125 clearReposAndNamespace( cassandraArchivaManager, clearedTables );
130 * ensure all dependant tables are cleaned up (mailinglist, license, dependencies)
135 public void clean_dependant_tables()
139 super.testUpdateProjectVersionMetadataWithAllElements();
141 String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( TEST_REPO_ID ) //
142 .withNamespace( TEST_NAMESPACE ) //
143 .withProjectId( TEST_PROJECT ) //
144 .withProjectVersion( TEST_PROJECT_VERSION ) //
145 .withId( TEST_PROJECT_VERSION ) //
148 this.cmr.removeProjectVersion( null, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
151 cmr.getProjectVersion( null , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull();
153 assertThat( cmr.getMailingLists( key ) ).isNotNull().isEmpty();
155 assertThat( cmr.getLicenses( key ) ).isNotNull().isEmpty();
157 assertThat( cmr.getDependencies( key ) ).isNotNull().isEmpty();
162 public void shutdown(TestInfo testInfo)
165 clearReposAndNamespace( cassandraArchivaManager, clearedTables );
169 static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager, final AtomicBoolean clearedFlag )
172 if (cassandraArchivaManager!=null)
174 CqlSession session = cassandraArchivaManager.getSession( );
176 List<String> tables = Arrays.asList(
177 cassandraArchivaManager.getProjectFamilyName( ),
178 cassandraArchivaManager.getNamespaceFamilyName( ),
179 cassandraArchivaManager.getRepositoryFamilyName( ),
180 cassandraArchivaManager.getProjectVersionMetadataFamilyName( ),
181 cassandraArchivaManager.getArtifactMetadataFamilyName( ),
182 cassandraArchivaManager.getMetadataFacetFamilyName( ),
183 cassandraArchivaManager.getMailingListFamilyName( ),
184 cassandraArchivaManager.getLicenseFamilyName( ),
185 cassandraArchivaManager.getDependencyFamilyName( )
187 CompletableFuture.allOf( tables.stream( ).map( table -> session.executeAsync( truncate( table ).build( ) ) )
188 .map( CompletionStage::toCompletableFuture ).collect( Collectors.toList( ) ).toArray( new CompletableFuture[0] ) )
189 .whenComplete( ( c, e ) -> {
190 if ( clearedFlag != null ) clearedFlag.set( true );
192 System.err.println( "TRUNCATE ERROR DETECTED: " + e.getMessage( ) );
198 System.err.println( "cassandraArchivaManager is null" );
202 static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager)
204 clearReposAndNamespace( cassandraArchivaManager, null );