| 
									
										
										
										
											2020-03-23 15:35:58 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * MIT License
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * Copyright (c) 2020-present Cloudogu GmbH and Contributors
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions:
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in all
 | 
					
						
							|  |  |  |  * copies or substantial portions of the Software.
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 | 
					
						
							|  |  |  |  * SOFTWARE.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							| 
									
										
										
										
											2020-05-23 19:06:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  | package sonia.scm.store;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import org.junit.jupiter.api.BeforeEach;
 | 
					
						
							|  |  |  | import org.junit.jupiter.api.Nested;
 | 
					
						
							|  |  |  | import org.junit.jupiter.api.Test;
 | 
					
						
							|  |  |  | import org.junit.jupiter.api.extension.ExtendWith;
 | 
					
						
							| 
									
										
										
										
											2020-05-23 19:06:02 +02:00
										 |  |  | import org.junit.jupiter.api.io.TempDir;
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  | import org.mockito.Mock;
 | 
					
						
							|  |  |  | import org.mockito.junit.jupiter.MockitoExtension;
 | 
					
						
							|  |  |  | import sonia.scm.SCMContextProvider;
 | 
					
						
							|  |  |  | import sonia.scm.io.DefaultFileSystem;
 | 
					
						
							|  |  |  | import sonia.scm.repository.InitialRepositoryLocationResolver;
 | 
					
						
							|  |  |  | import sonia.scm.repository.RepositoryLocationResolver;
 | 
					
						
							|  |  |  | import sonia.scm.repository.xml.PathBasedRepositoryLocationResolver;
 | 
					
						
							|  |  |  | import sonia.scm.update.PropertyFileAccess;
 | 
					
						
							|  |  |  | import sonia.scm.util.IOUtil;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import java.io.IOException;
 | 
					
						
							|  |  |  | import java.nio.file.Files;
 | 
					
						
							|  |  |  | import java.nio.file.Path;
 | 
					
						
							|  |  |  | import java.nio.file.Paths;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 09:12:57 +02:00
										 |  |  | import static org.assertj.core.api.Assertions.assertThat;
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  | import static org.mockito.ArgumentMatchers.any;
 | 
					
						
							|  |  |  | import static org.mockito.Mockito.lenient;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @ExtendWith(MockitoExtension.class)
 | 
					
						
							|  |  |  | class JAXBPropertyFileAccessTest {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public static final String REPOSITORY_ID = "repoId";
 | 
					
						
							|  |  |  |   public static final String STORE_NAME = "test";
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @Mock
 | 
					
						
							|  |  |  |   SCMContextProvider contextProvider;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   RepositoryLocationResolver locationResolver;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   JAXBPropertyFileAccess fileAccess;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 06:24:33 +01:00
										 |  |  |   @TempDir
 | 
					
						
							|  |  |  |   private Path tempDir;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  |   @BeforeEach
 | 
					
						
							| 
									
										
										
										
											2023-03-22 06:24:33 +01:00
										 |  |  |   void initTempDir() {
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  |     lenient().when(contextProvider.getBaseDirectory()).thenReturn(tempDir.toFile());
 | 
					
						
							|  |  |  |     lenient().when(contextProvider.resolve(any())).thenAnswer(invocation -> tempDir.resolve(invocation.getArgument(0).toString()));
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 09:12:57 +02:00
										 |  |  |     locationResolver = new PathBasedRepositoryLocationResolver(contextProvider, new InitialRepositoryLocationResolver(), new DefaultFileSystem());
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fileAccess = new JAXBPropertyFileAccess(contextProvider, locationResolver);
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 09:12:57 +02:00
										 |  |  |   @Test
 | 
					
						
							|  |  |  |   void shouldRenameGlobalConfigFile() throws IOException {
 | 
					
						
							|  |  |  |     Path baseDirectory = contextProvider.getBaseDirectory().toPath();
 | 
					
						
							|  |  |  |     Path configDirectory = baseDirectory.resolve(StoreConstants.CONFIG_DIRECTORY_NAME);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Files.createDirectories(configDirectory);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Path oldPath = configDirectory.resolve("old" + StoreConstants.FILE_EXTENSION);
 | 
					
						
							|  |  |  |     Files.createFile(oldPath);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fileAccess.renameGlobalConfigurationFrom("old").to("new");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Path newPath = configDirectory.resolve("new" + StoreConstants.FILE_EXTENSION);
 | 
					
						
							|  |  |  |     assertThat(oldPath).doesNotExist();
 | 
					
						
							|  |  |  |     assertThat(newPath).exists();
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  |   @Nested
 | 
					
						
							|  |  |  |   class ForExistingRepository {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @BeforeEach
 | 
					
						
							|  |  |  |     void createRepositoryLocation() {
 | 
					
						
							|  |  |  |       locationResolver.forClass(Path.class).createLocation(REPOSITORY_ID);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Test
 | 
					
						
							| 
									
										
										
										
											2023-03-22 06:24:33 +01:00
										 |  |  |     void shouldMoveStoreFileToRepositoryBasedLocation() throws IOException {
 | 
					
						
							|  |  |  |       createV1StoreFile("myStore.xml");
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       fileAccess.forStoreName(STORE_NAME).moveAsRepositoryStore(Paths.get("myStore.xml"), REPOSITORY_ID);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 09:12:57 +02:00
										 |  |  |       assertThat(tempDir.resolve("repositories").resolve(REPOSITORY_ID).resolve("store").resolve("data").resolve(STORE_NAME).resolve("myStore.xml")).exists();
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Test
 | 
					
						
							| 
									
										
										
										
											2023-03-22 06:24:33 +01:00
										 |  |  |     void shouldMoveAllStoreFilesToRepositoryBasedLocations() throws IOException {
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  |       locationResolver.forClass(Path.class).createLocation("repoId2");
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 06:24:33 +01:00
										 |  |  |       createV1StoreFile(REPOSITORY_ID + ".xml");
 | 
					
						
							|  |  |  |       createV1StoreFile("repoId2.xml");
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       PropertyFileAccess.StoreFileTools statisticStoreAccess = fileAccess.forStoreName(STORE_NAME);
 | 
					
						
							|  |  |  |       statisticStoreAccess.forStoreFiles(statisticStoreAccess::moveAsRepositoryStore);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 09:12:57 +02:00
										 |  |  |       assertThat(tempDir.resolve("repositories").resolve(REPOSITORY_ID).resolve("store").resolve("data").resolve(STORE_NAME).resolve("repoId.xml")).exists();
 | 
					
						
							|  |  |  |       assertThat(tempDir.resolve("repositories").resolve("repoId2").resolve("store").resolve("data").resolve(STORE_NAME).resolve("repoId2.xml")).exists();
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 06:24:33 +01:00
										 |  |  |   private void createV1StoreFile(String name) throws IOException {
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  |     Path v1Dir = tempDir.resolve("var").resolve("data").resolve(STORE_NAME);
 | 
					
						
							|  |  |  |     IOUtil.mkdirs(v1Dir.toFile());
 | 
					
						
							|  |  |  |     Files.createFile(v1Dir.resolve(name));
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @Nested
 | 
					
						
							|  |  |  |   class ForMissingRepository {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Test
 | 
					
						
							| 
									
										
										
										
											2023-03-22 06:24:33 +01:00
										 |  |  |     void shouldIgnoreStoreFile() throws IOException {
 | 
					
						
							|  |  |  |       createV1StoreFile("myStore.xml");
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       fileAccess.forStoreName(STORE_NAME).moveAsRepositoryStore(Paths.get("myStore.xml"), REPOSITORY_ID);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 09:12:57 +02:00
										 |  |  |       assertThat(tempDir.resolve("repositories").resolve(REPOSITORY_ID).resolve("store").resolve("data").resolve(STORE_NAME).resolve("myStore.xml")).doesNotExist();
 | 
					
						
							| 
									
										
										
										
											2019-06-21 14:35:57 +02:00
										 |  |  |     }
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 |