]> source.dussan.org Git - archiva.git/blob
c1e07a07a097a2b890139a811768d8f102fb59b1
[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.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;
37
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;
49
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;
54
55 /**
56  * @author Olivier Lamy
57  */
58 @ExtendWith( SpringExtension.class )
59 @TestInstance( TestInstance.Lifecycle.PER_CLASS )
60 public class CassandraMetadataRepositoryTest
61     extends AbstractMetadataRepositoryTest
62 {
63     @Inject
64     @Named(value = "archivaEntityManagerFactory#cassandra")
65     CassandraArchivaManager cassandraArchivaManager;
66
67     CassandraMetadataRepository cmr;
68
69     RepositorySessionFactory sessionFactory;
70
71     RepositorySession session;
72
73     long cTime;
74     int testNum = 0;
75     final AtomicBoolean clearedTables = new AtomicBoolean( false );
76
77
78     @Override
79     protected RepositorySessionFactory getSessionFactory( )
80     {
81         return sessionFactory;
82     }
83
84     @Override
85     protected MetadataRepository getRepository( )
86     {
87         return cmr;
88     }
89
90     @BeforeEach
91     public void setUp( TestInfo testInfo )
92         throws Exception
93     {
94         cTime = System.currentTimeMillis( );
95         super.setUp();
96         assertMaxTries =1;
97         assertRetrySleepMs=10;
98
99         Path directory = Paths.get( "target/test-repositories" );
100         if ( Files.exists(directory) )
101         {
102             org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
103         }
104
105         List<MetadataFacetFactory> factories = createTestMetadataFacetFactories();
106         MetadataService metadataService = new MetadataService( );
107         metadataService.setMetadataFacetFactories( factories );
108
109         this.cmr = new CassandraMetadataRepository( metadataService, cassandraArchivaManager );
110
111         sessionFactory = mock( RepositorySessionFactory.class );
112         session = mock( RepositorySession.class );
113
114         when( sessionFactory.createSession( ) ).thenReturn( session );
115
116         if (!clearedTables.get())
117         {
118             clearReposAndNamespace( cassandraArchivaManager, clearedTables );
119         }
120     }
121
122     /**
123      * ensure all dependant tables are cleaned up (mailinglist, license, dependencies)
124      *
125      * @throws Exception
126      */
127     @Test
128     public void clean_dependant_tables()
129         throws Exception
130     {
131
132         super.testUpdateProjectVersionMetadataWithAllElements();
133
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 ) //
139             .build();
140
141         this.cmr.removeProjectVersion( null, TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
142
143         assertThat(
144             cmr.getProjectVersion( null , TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull();
145
146         assertThat( cmr.getMailingLists( key ) ).isNotNull().isEmpty();
147
148         assertThat( cmr.getLicenses( key ) ).isNotNull().isEmpty();
149
150         assertThat( cmr.getDependencies( key ) ).isNotNull().isEmpty();
151     }
152
153
154     @AfterEach
155     public void shutdown(TestInfo testInfo)
156         throws Exception
157     {
158         clearReposAndNamespace( cassandraArchivaManager, clearedTables );
159         super.tearDown();
160     }
161
162     static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager, final AtomicBoolean clearedFlag )
163         throws Exception
164     {
165         if (cassandraArchivaManager!=null)
166         {
167             CqlSession session = cassandraArchivaManager.getSession( );
168             {
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( )
179                 );
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 );
184                         if (e!=null) {
185                             System.err.println( "TRUNCATE ERROR DETECTED: " + e.getMessage( ) );
186                         }
187                     } ).get( )
188                 ;
189             }
190         } else {
191             System.err.println( "cassandraArchivaManager is null" );
192         }
193     }
194
195     static void clearReposAndNamespace( final CassandraArchivaManager cassandraArchivaManager)
196         throws Exception {
197         clearReposAndNamespace( cassandraArchivaManager, null );
198     }
199
200 }