You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepositoriesNamespaceTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package org.apache.archiva.metadata.repository.cassandra;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.metadata.model.ProjectMetadata;
  21. import org.apache.archiva.metadata.repository.cassandra.model.Namespace;
  22. import org.apache.archiva.metadata.repository.cassandra.model.Repository;
  23. import org.junit.jupiter.api.AfterAll;
  24. import org.junit.jupiter.api.AfterEach;
  25. import org.junit.jupiter.api.BeforeAll;
  26. import org.junit.jupiter.api.BeforeEach;
  27. import org.junit.jupiter.api.Test;
  28. import org.junit.jupiter.api.extension.ExtendWith;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.springframework.test.context.ContextConfiguration;
  32. import org.springframework.test.context.junit.jupiter.SpringExtension;
  33. import org.testcontainers.containers.CassandraContainer;
  34. import org.testcontainers.containers.output.Slf4jLogConsumer;
  35. import org.testcontainers.utility.DockerImageName;
  36. import javax.inject.Inject;
  37. import javax.inject.Named;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. /**
  40. * @author Olivier Lamy
  41. */
  42. @ExtendWith( SpringExtension.class )
  43. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml" } )
  44. public class RepositoriesNamespaceTest
  45. {
  46. private static final Logger LOGGER = LoggerFactory.getLogger( RepositoriesNamespaceTest.class );
  47. private static final CassandraContainer CASSANDRA =
  48. new CassandraContainer(DockerImageName.parse("cassandra")
  49. .withTag(System.getProperty("cassandraVersion","3.11.2")));
  50. @Inject
  51. @Named( value = "archivaEntityManagerFactory#cassandra" )
  52. CassandraArchivaManager cassandraArchivaManager;
  53. CassandraMetadataRepository cmr;
  54. @BeforeAll
  55. public static void initCassandra()
  56. throws Exception {
  57. CASSANDRA.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("org.apache.archiva.metadata.repository.cassandra.logs")));
  58. CASSANDRA.start();
  59. System.setProperty("cassandra.host", CASSANDRA.getHost());
  60. System.setProperty("cassandra.port", CASSANDRA.getMappedPort(9042).toString());
  61. }
  62. @AfterAll
  63. public static void stopCassandra()
  64. throws Exception {
  65. CASSANDRA.close();
  66. }
  67. @BeforeEach
  68. public void setup()
  69. throws Exception
  70. {
  71. cmr = new CassandraMetadataRepository( null, cassandraArchivaManager );
  72. if ( !cassandraArchivaManager.started() )
  73. {
  74. cassandraArchivaManager.start();
  75. }
  76. CassandraMetadataRepositoryTest.clearReposAndNamespace( cassandraArchivaManager );
  77. }
  78. @AfterEach
  79. public void shutdown()
  80. throws Exception
  81. {
  82. CassandraMetadataRepositoryTest.clearReposAndNamespace( cassandraArchivaManager );
  83. cassandraArchivaManager.shutdown();
  84. }
  85. @Test
  86. public void testMetadataRepo()
  87. throws Exception
  88. {
  89. Repository r = null;
  90. Namespace n = null;
  91. try
  92. {
  93. cmr.updateNamespace( null , "release", "org" );
  94. r = cmr.getRepository( "release" );
  95. assertThat( r ).isNotNull();
  96. assertThat( cmr.getNamespaces( "release" ) ).isNotEmpty().hasSize( 1 );
  97. n = cmr.getNamespace( "release", "org" );
  98. assertThat( n ).isNotNull();
  99. assertThat( n.getRepository() ).isNotNull();
  100. cmr.updateNamespace( null, "release", "org.apache" );
  101. r = cmr.getRepository( "release" );
  102. assertThat( r ).isNotNull();
  103. assertThat( cmr.getNamespaces( "release" ) ).isNotEmpty().hasSize( 2 );
  104. cmr.removeNamespace(null , "release", "org.apache" );
  105. assertThat( cmr.getNamespaces( "release" ) ).isNotEmpty().hasSize( 1 );
  106. assertThat( cmr.getNamespaces( "release" ) ).containsExactly( "org" );
  107. ProjectMetadata projectMetadata = new ProjectMetadata();
  108. projectMetadata.setId( "theproject" );
  109. projectMetadata.setNamespace( "org" );
  110. cmr.updateProject(null , "release", projectMetadata );
  111. assertThat( cmr.getProjects(null , "release", "org" ) ).isNotEmpty().hasSize( 1 ).containsExactly(
  112. "theproject" );
  113. cmr.removeProject(null , "release", "org", "theproject" );
  114. assertThat( cmr.getProjects(null , "release", "org" ) ).isEmpty();
  115. cmr.removeRepository(null , "release" );
  116. r = cmr.getRepository( "release" );
  117. assertThat( r ).isNull();
  118. }
  119. catch ( Exception e )
  120. {
  121. LOGGER.error( e.getMessage(), e );
  122. throw e;
  123. }
  124. finally
  125. {
  126. CassandraMetadataRepositoryTest.clearReposAndNamespace( cassandraArchivaManager );
  127. }
  128. }
  129. }