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.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.TestInfo;
34 import org.junit.jupiter.api.TestInstance;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.springframework.test.context.junit.jupiter.SpringExtension;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.util.Arrays;
44 import java.util.List;
45 import java.util.concurrent.CompletableFuture;
46 import java.util.concurrent.CompletionStage;
47 import java.util.concurrent.atomic.AtomicBoolean;
48 import java.util.stream.Collectors;
50 import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.truncate;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.when;
56 * @author Olivier Lamy
58 @ExtendWith( SpringExtension.class )
59 @TestInstance( TestInstance.Lifecycle.PER_CLASS )
60 public class CassandraMetadataRepositoryTest
61 extends AbstractMetadataRepositoryTest
64 @Named(value = "archivaEntityManagerFactory#cassandra")
65 CassandraArchivaManager cassandraArchivaManager;
67 CassandraMetadataRepository cmr;
69 RepositorySessionFactory sessionFactory;
71 RepositorySession session;
75 final 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( );
97 assertRetrySleepMs=10;
99 Path directory = Paths.get( "target/test-repositories" );
100 if ( Files.exists(directory) )
102 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
105 List<MetadataFacetFactory> factories = createTestMetadataFacetFactories();
106 MetadataService metadataService = new MetadataService( );
107 metadataService.setMetadataFacetFactories( factories );
109 this.cmr = new CassandraMetadataRepository( metadataService, cassandraArchivaManager );
111 sessionFactory = mock( RepositorySessionFactory.class );
112 session = mock( RepositorySession.class );
114 when( sessionFactory.createSession( ) ).thenReturn( session );
116 if (!clearedTables.get())
118 clearReposAndNamespace( cassandraArchivaManager, clearedTables );
123 * ensure all dependant tables are cleaned up (mailinglist, license, dependencies)
128 public void clean_dependant_tables()
132 super.testUpdateProjectVersionMetadataWithAllElements();
134 String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( TEST_REPO_ID ) //
135 .withNamespace( TEST_NAMESPACE ) //
136 .withProjectId( TEST_PROJECT ) //
137 .withProjectVersion( TEST_PROJECT_VERSION ) //
138 .withId( TEST_PROJECT_VERSION ) //
141 this.cmr.removeProjectVersion( null, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
144 cmr.getProjectVersion( null , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull();
146 assertThat( cmr.getMailingLists( key ) ).isNotNull().isEmpty();
148 assertThat( cmr.getLicenses( key ) ).isNotNull().isEmpty();
150 assertThat( cmr.getDependencies( key ) ).isNotNull().isEmpty();
155 public void shutdown(TestInfo testInfo)
158 clearReposAndNamespace( cassandraArchivaManager, clearedTables );
162 static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager, final AtomicBoolean clearedFlag )
165 if (cassandraArchivaManager!=null)
167 CqlSession session = cassandraArchivaManager.getSession( );
169 List<String> tables = Arrays.asList(
170 cassandraArchivaManager.getProjectFamilyName( ),
171 cassandraArchivaManager.getNamespaceFamilyName( ),
172 cassandraArchivaManager.getRepositoryFamilyName( ),
173 cassandraArchivaManager.getProjectVersionMetadataFamilyName( ),
174 cassandraArchivaManager.getArtifactMetadataFamilyName( ),
175 cassandraArchivaManager.getMetadataFacetFamilyName( ),
176 cassandraArchivaManager.getMailingListFamilyName( ),
177 cassandraArchivaManager.getLicenseFamilyName( ),
178 cassandraArchivaManager.getDependencyFamilyName( )
180 CompletableFuture.allOf( tables.stream( ).map( table -> session.executeAsync( truncate( table ).build( ) ) )
181 .map( CompletionStage::toCompletableFuture ).collect( Collectors.toList( ) ).toArray( new CompletableFuture[0] ) )
182 .whenComplete( ( c, e ) -> {
183 if ( clearedFlag != null ) clearedFlag.set( true );
185 System.err.println( "TRUNCATE ERROR DETECTED: " + e.getMessage( ) );
191 System.err.println( "cassandraArchivaManager is null" );
195 static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager)
197 clearReposAndNamespace( cassandraArchivaManager, null );