]> source.dussan.org Git - archiva.git/blob
a6c4bf0fe6574a5cd4784878962926d538c5304e
[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         super.setUp();
99         assertMaxTries =1;
100         assertRetrySleepMs=10;
101
102         Path directory = Paths.get( "target/test-repositories" );
103         if ( Files.exists(directory) )
104         {
105             org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
106         }
107
108         List<MetadataFacetFactory> factories = createTestMetadataFacetFactories();
109         MetadataService metadataService = new MetadataService( );
110         metadataService.setMetadataFacetFactories( factories );
111
112         this.cmr = new CassandraMetadataRepository( metadataService, cassandraArchivaManager );
113
114         sessionFactoryControl = EasyMock.createControl( );
115         sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
116         sessionControl = EasyMock.createControl( );
117         session = sessionControl.createMock( RepositorySession.class );
118
119         EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( session );
120
121         sessionFactoryControl.replay();
122
123         if (!clearedTables.get())
124         {
125             clearReposAndNamespace( cassandraArchivaManager, clearedTables );
126         }
127     }
128
129     /**
130      * ensure all dependant tables are cleaned up (mailinglist, license, dependencies)
131      *
132      * @throws Exception
133      */
134     @Test
135     public void clean_dependant_tables()
136         throws Exception
137     {
138
139         super.testUpdateProjectVersionMetadataWithAllElements();
140
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 ) //
146             .build();
147
148         this.cmr.removeProjectVersion( null, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
149
150         assertThat(
151             cmr.getProjectVersion( null , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull();
152
153         assertThat( cmr.getMailingLists( key ) ).isNotNull().isEmpty();
154
155         assertThat( cmr.getLicenses( key ) ).isNotNull().isEmpty();
156
157         assertThat( cmr.getDependencies( key ) ).isNotNull().isEmpty();
158     }
159
160
161     @AfterEach
162     public void shutdown(TestInfo testInfo)
163         throws Exception
164     {
165         clearReposAndNamespace( cassandraArchivaManager, clearedTables );
166         super.tearDown();
167     }
168
169     static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager, final AtomicBoolean clearedFlag )
170         throws Exception
171     {
172         if (cassandraArchivaManager!=null)
173         {
174             CqlSession session = cassandraArchivaManager.getSession( );
175             {
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( )
186                 );
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 );
191                         if (e!=null) {
192                             System.err.println( "TRUNCATE ERROR DETECTED: " + e.getMessage( ) );
193                         }
194                     } ).get( )
195                 ;
196             }
197         } else {
198             System.err.println( "cassandraArchivaManager is null" );
199         }
200     }
201
202     static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager)
203         throws Exception {
204         clearReposAndNamespace( cassandraArchivaManager, null );
205     }
206
207 }