]> source.dussan.org Git - archiva.git/blob
ccdb3039f36fcf8a495f65187267a8a55a2f7df5
[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.CompletableFuture;
48 import java.util.concurrent.CompletionStage;
49 import java.util.concurrent.atomic.AtomicBoolean;
50 import java.util.stream.Collectors;
51
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;
55
56 /**
57  * @author Olivier Lamy
58  */
59 @ExtendWith( SpringExtension.class )
60 @TestInstance( TestInstance.Lifecycle.PER_CLASS )
61 public class CassandraMetadataRepositoryTest
62     extends AbstractMetadataRepositoryTest
63 {
64     @Inject
65     @Named(value = "archivaEntityManagerFactory#cassandra")
66     CassandraArchivaManager cassandraArchivaManager;
67
68     CassandraMetadataRepository cmr;
69
70     IMocksControl sessionFactoryControl;
71     RepositorySessionFactory sessionFactory;
72
73     IMocksControl sessionControl;
74     RepositorySession session;
75
76     long cTime;
77     int testNum = 0;
78     final AtomicBoolean clearedTables = new AtomicBoolean( false );
79
80
81     @Override
82     protected RepositorySessionFactory getSessionFactory( )
83     {
84         return sessionFactory;
85     }
86
87     @Override
88     protected MetadataRepository getRepository( )
89     {
90         return cmr;
91     }
92
93     @BeforeEach
94     public void setUp( TestInfo testInfo )
95         throws Exception
96     {
97         cTime = System.currentTimeMillis( );
98         System.err.println( "Setting up - "+(testNum++) + " - " + testInfo.getDisplayName() + " - 0ms");
99         super.setUp();
100         System.err.println( "Setting up - " + testInfo.getDisplayName( ) + " - " + (System.currentTimeMillis( ) - cTime) +"ms");
101         assertMaxTries =1;
102         assertRetrySleepMs=10;
103
104         Path directory = Paths.get( "target/test-repositories" );
105         if ( Files.exists(directory) )
106         {
107             org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
108         }
109
110         List<MetadataFacetFactory> factories = createTestMetadataFacetFactories();
111         MetadataService metadataService = new MetadataService( );
112         metadataService.setMetadataFacetFactories( factories );
113
114         this.cmr = new CassandraMetadataRepository( metadataService, cassandraArchivaManager );
115
116         sessionFactoryControl = EasyMock.createControl( );
117         sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
118         sessionControl = EasyMock.createControl( );
119         session = sessionControl.createMock( RepositorySession.class );
120
121         EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( session );
122
123         sessionFactoryControl.replay();
124
125         if (!clearedTables.get())
126         {
127             clearReposAndNamespace( cassandraArchivaManager, clearedTables );
128         }
129         System.err.println( "Finished setting up - "+testInfo.getDisplayName() + " - " + (System.currentTimeMillis( ) - cTime) +"ms");
130     }
131
132     /**
133      * ensure all dependant tables are cleaned up (mailinglist, license, dependencies)
134      *
135      * @throws Exception
136      */
137     @Test
138     public void clean_dependant_tables()
139         throws Exception
140     {
141
142         super.testUpdateProjectVersionMetadataWithAllElements();
143
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 ) //
149             .build();
150
151         this.cmr.removeProjectVersion( null, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
152
153         assertThat(
154             cmr.getProjectVersion( null , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull();
155
156         assertThat( cmr.getMailingLists( key ) ).isNotNull().isEmpty();
157
158         assertThat( cmr.getLicenses( key ) ).isNotNull().isEmpty();
159
160         assertThat( cmr.getDependencies( key ) ).isNotNull().isEmpty();
161     }
162
163
164     @AfterEach
165     public void shutdown(TestInfo testInfo)
166         throws Exception
167     {
168         System.err.println( "Shutting down - " + (testNum-1) + " - " + testInfo.getDisplayName( ) + " - " + ( System.currentTimeMillis( ) - cTime ) +"ms");
169         clearReposAndNamespace( cassandraArchivaManager, clearedTables );
170         super.tearDown();
171         System.err.println( "Shutting down finished - " + testInfo.getDisplayName( ) + " - " + ( System.currentTimeMillis( ) - cTime ) +"ms");
172     }
173
174     static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager, final AtomicBoolean clearedFlag )
175         throws Exception
176     {
177         if (cassandraArchivaManager!=null)
178         {
179             CqlSession session = cassandraArchivaManager.getSession( );
180             {
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( )
191                 );
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 );
196                         if (e!=null) {
197                             System.err.println( "TRUNCATE ERROR DETECTED: " + e.getMessage( ) );
198                         }
199                     } ).get( )
200                 ;
201             }
202         } else {
203             System.err.println( "cassandraArchivaManager is null" );
204         }
205     }
206
207     static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager)
208         throws Exception {
209         clearReposAndNamespace( cassandraArchivaManager, null );
210     }
211
212 }