AbstractModuleRepositoriesServiceImpl implements ModuleRepositoriesService { @Override public ModuleRepositories load(final Path path) { final org.uberfire.java.nio.file.Path nioPath = Paths.convert(path); if (ioService.exists(nioPath)) { final String content = ioService.readAllString(nioPath); return contentHandler.toModel(content); } else { return create(path); } } AbstractModuleRepositoriesServiceImpl();  AbstractModuleRepositoriesServiceImpl(final IOService ioService,
                                                 final ModuleRepositoryResolver repositoryResolver,
                                                 final ModuleRepositoriesContentHandler contentHandler,
                                                 final CommentedOptionFactory commentedOptionFactory); @Override ModuleRepositories create(final Path path); @Override ModuleRepositories load(final Path path); @Override Path save(final Path resource,
                     final ModuleRepositories moduleRepositories,
                     final String comment);  }
@Test public void testLoadExisting() { final String xml = "<project-repositories>\n" + " <repositories>\n" + " <repository>\n" + " <include>true</include>\n" + " <metadata>\n" + " <id>id</id>\n" + " <url>url</url>\n" + " <source>LOCAL</source>\n" + " </metadata>\n" + " </repository>\n" + " </repositories>\n" + "</project-repositories>"; final Path path = mock(Path.class); when(path.toURI()).thenReturn("file: final org.uberfire.java.nio.file.Path nioPath = Paths.convert(path); when(ioService.exists(eq(nioPath))).thenReturn(true); when(ioService.readAllString(eq(nioPath))).thenReturn(xml); final ModuleRepositories repositories = service.load(path); assertNotNull(repositories); assertNotNull(repositories.getRepositories()); assertEquals(1, repositories.getRepositories().size()); final ModuleRepositories.ModuleRepository repository = repositories.getRepositories().iterator().next(); assertEquals("id", repository.getMetadata().getId()); assertEquals("url", repository.getMetadata().getUrl()); assertEquals(MavenRepositorySource.LOCAL, repository.getMetadata().getSource()); assertEquals(true, repository.isIncluded()); verify(contentHandler, times(1)).toModel(eq(xml)); }
@Test public void testLoadNonExisting() { final String xml = "<project-repositories>\n" + " <repositories>\n" + " <repository>\n" + " <include>true</include>\n" + " <metadata>\n" + " <id>local</id>\n" + " <url>local-url</url>\n" + " <source>LOCAL</source>\n" + " </metadata>\n" + " </repository>\n" + " </repositories>\n" + "</project-repositories>"; final Path path = mock(Path.class); when(path.toURI()).thenReturn("file: final org.uberfire.java.nio.file.Path nioPath = Paths.convert(path); when(ioService.exists(eq(nioPath))).thenReturn(false); final Set<MavenRepositoryMetadata> metadata = new HashSet<MavenRepositoryMetadata>(); metadata.add(new MavenRepositoryMetadata("local", "local-url", MavenRepositorySource.LOCAL)); when(repositoryResolver.getRemoteRepositoriesMetaData()).thenReturn(metadata); final ModuleRepositories repositories = service.load(path); assertNotNull(repositories); assertNotNull(repositories.getRepositories()); assertEquals(1, repositories.getRepositories().size()); final ModuleRepositories.ModuleRepository repository = repositories.getRepositories().iterator().next(); assertEquals("local", repository.getMetadata().getId()); assertEquals("local-url", repository.getMetadata().getUrl()); assertEquals(MavenRepositorySource.LOCAL, repository.getMetadata().getSource()); assertEquals(true, repository.isIncluded()); verify(ioService, times(1)).startBatch(any(FileSystem.class), any(CommentedOption.class)); verify(ioService, times(1)).write(eq(nioPath), eq(xml)); verify(ioService, times(1)).endBatch(); }
@Test public void testFiltered() { final String xml = "<project-repositories>\n" + " <repositories>\n" + " <repository>\n" + " <include>true</include>\n" + " <metadata>\n" + " <id>id1</id>\n" + " <url>url1</url>\n" + " <source>LOCAL</source>\n" + " </metadata>\n" + " </repository>\n" + " <repository>\n" + " <include>false</include>\n" + " <metadata>\n" + " <id>id2</id>\n" + " <url>url2</url>\n" + " <source>LOCAL</source>\n" + " </metadata>\n" + " </repository>\n" + " </repositories>\n" + "</project-repositories>"; final Path path = mock(Path.class); when(path.toURI()).thenReturn("file: final org.uberfire.java.nio.file.Path nioPath = Paths.convert(path); when(ioService.exists(eq(nioPath))).thenReturn(true); when(ioService.readAllString(eq(nioPath))).thenReturn(xml); final ModuleRepositories repositories = service.load(path); assertNotNull(repositories); assertNotNull(repositories.getRepositories()); assertEquals(2, repositories.getRepositories().size()); final MavenRepositoryMetadata[] metadatas = repositories.filterByIncluded(); assertEquals(1, metadatas.length); final MavenRepositoryMetadata metadata = metadatas[0]; assertEquals("id1", metadata.getId()); assertEquals("url1", metadata.getUrl()); assertEquals(MavenRepositorySource.LOCAL, metadata.getSource()); }