WorkspaceProjectServiceImpl implements WorkspaceProjectService { @Override public void addBranch(final String newBranchName, final String baseBranchName, final WorkspaceProject project, final String userIdentifier) { final Branch baseBranch = project .getRepository() .getBranch(baseBranchName) .orElseThrow(() -> new IllegalStateException("The base branch [" + baseBranchName + "] does not exists")); final org.uberfire.java.nio.file.Path baseBranchPath = pathUtil.convert(baseBranch.getPath()); final String newBranchPathURI = pathUtil.replaceBranch(newBranchName, baseBranch.getPath().toURI()); try { final org.uberfire.java.nio.file.Path newBranchPath = ioService.get(new URI(newBranchPathURI)); baseBranchPath .getFileSystem() .provider() .copy(baseBranchPath, newBranchPath); final BranchPermissions branchPermissions = spaceConfigStorageRegistry .get(project.getSpace().getName()) .loadBranchPermissions(baseBranchName, project.getRepository().getIdentifier()); spaceConfigStorageRegistry .get(project.getSpace().getName()) .saveBranchPermissions(newBranchName, project.getRepository().getIdentifier(), branchPermissions); final Repository repository = repositoryService.getRepositoryFromSpace( project.getSpace(), project.getRepository().getAlias()); repositoryUpdatedEvent.fire(new RepositoryUpdatedEvent(repository)); newBranchEvent.fire(new NewBranchEvent(repository, newBranchName, baseBranchName, userIdentifier)); } catch (URISyntaxException e) { throw new RuntimeException(e); } } WorkspaceProjectServiceImpl(); @Inject  WorkspaceProjectServiceImpl(final OrganizationalUnitService organizationalUnitService,
                                       final RepositoryService repositoryService,
                                       final SpacesAPI spaces,
                                       final Event<NewProjectEvent> newProjectEvent,
                                       final Event<RepositoryUpdatedEvent> repositoryUpdatedEvent,
                                       final Event<NewBranchEvent> newBranchEvent,
                                       final Instance<ModuleService<? extends Module>> moduleServices,
                                       final ModuleRepositoryResolver repositoryResolver,
                                       @Named("ioStrategy") final IOService ioService,
                                       final SpaceConfigStorageRegistry spaceConfigStorageRegistry,
                                       final PathUtil pathUtil,
                                       final ChangeRequestService changeRequestService,
                                       final POMService pomService); @Override Collection<WorkspaceProject> getAllWorkspaceProjects(); @Override Collection<WorkspaceProject> getAllWorkspaceProjects(final OrganizationalUnit organizationalUnit); @Override Collection<WorkspaceProject> getAllWorkspaceProjectsByName(final OrganizationalUnit organizationalUnit,
                                                                      final String name); Collection<WorkspaceProject> getAllWorkspaceProjectsByName(final OrganizationalUnit organizationalUnit,
                                                                      final String name,
                                                                      final boolean includeDeleted); @Override boolean spaceHasNoProjectsWithName(final OrganizationalUnit organizationalUnit,
                                              final String name,
                                              final WorkspaceProject projectToIgnore); @Override WorkspaceProject newProject(final OrganizationalUnit organizationalUnit,
                                       final POM pom); @Override WorkspaceProject newProject(final OrganizationalUnit organizationalUnit,
                                       final POM pom,
                                       final DeploymentMode mode); @Override WorkspaceProject newProject(final OrganizationalUnit organizationalUnit,
                                       final POM pom,
                                       final DeploymentMode mode,
                                       final List<Contributor> contributors); @Override WorkspaceProject newProject(final OrganizationalUnit organizationalUnit,
                                       final POM pom,
                                       final DeploymentMode mode,
                                       final List<Contributor> contributors,
                                       final Repository templateRepository); @Override WorkspaceProject newProject(final OrganizationalUnit organizationalUnit,
                                       final POM pom,
                                       final DeploymentMode mode,
                                       final List<Contributor> contributors,
                                       final Repository templateRepository,
                                       final String remoteRepositoryUrl); @Override String createFreshProjectName(final OrganizationalUnit organizationalUnit,
                                         final String name); @Override WorkspaceProject resolveProject(final Repository repository); @Override WorkspaceProject resolveProject(final Space space,
                                           final Branch branch); @Override WorkspaceProject resolveProject(final Space space,
                                           final Module module); @Override WorkspaceProject resolveProject(final Space space,
                                           final String name); @Override WorkspaceProject resolveProject(final Space space,
                                           final String projectName,
                                           final String branchName); @Override WorkspaceProject resolveProjectByRepositoryAlias(final Space space,
                                                            final String repositoryAlias); @Override WorkspaceProject resolveProject(final Space space,
                                           final Path path); @Override WorkspaceProject resolveProject(Path path); @Override void addBranch(final String newBranchName,
                          final String baseBranchName,
                          final WorkspaceProject project,
                          final String userIdentifier); @Override void removeBranch(final String branchName,
                             final WorkspaceProject project,
                             final String userIdentifier);  }
@Test public void addBranchTest() throws URISyntaxException { final WorkspaceProject project = mock(WorkspaceProject.class); doReturn(repository1).when(project).getRepository(); doReturn(repository1).when(repositoryService).getRepositoryFromSpace(any(), any()); final List<Branch> repo1Branches = Arrays.asList(makeBranch("repo1-branch1", repository1.getAlias()), makeBranch("repo1-branch2", repository1.getAlias())); when(repository1.getBranches()).thenReturn(repo1Branches); when(repository1.getBranch(anyString())).then(inv -> repo1Branches.stream().filter(b -> b.getName().equals(inv.getArgumentAt(0, String.class))).findFirst()); doReturn(new Space("my-space")).when(project).getSpace(); doReturn(mock(SpaceConfigStorage.class)).when(spaceConfigStorageRegistry).get("my-space"); final org.uberfire.java.nio.file.Path baseBranchPath = mock(org.uberfire.java.nio.file.Path.class); final Path path = repository1.getBranches().stream().filter(b -> b.getName().equals("repo1-branch1")).findFirst().get().getPath(); final FileSystem fileSystem = mock(FileSystem.class); final FileSystemProvider fileSystemProvider = mock(FileSystemProvider.class); doReturn(fileSystemProvider).when(fileSystem).provider(); doReturn(fileSystem).when(baseBranchPath).getFileSystem(); doReturn(baseBranchPath).when(pathUtil).convert(path); doReturn(repository1).when(repositoryService).getRepository(any()); Branch newBranch = makeBranch("new-branch", "repo1"); Branch branch1Branch = makeBranch("repo1-branch1", "repo1"); when(repository1.getBranch(any(Path.class))).thenReturn(Optional.of(newBranch)).thenReturn(Optional.of(branch1Branch)); final org.uberfire.java.nio.file.Path newBranchPath = mock(org.uberfire.java.nio.file.Path.class); doReturn(newBranchPath).when(ioService).get(new URI("default: doReturn("default: final ArgumentCaptor<NewBranchEvent> newBranchEventArgumentCaptor = ArgumentCaptor.forClass(NewBranchEvent.class); workspaceProjectService.addBranch("new-branch", "repo1-branch1", project, "user"); verify(fileSystemProvider).copy(baseBranchPath, newBranchPath); verify(repositoryUpdatedEvent).fire(any()); verify(newBranchEvent).fire(newBranchEventArgumentCaptor.capture()); final NewBranchEvent newBranchEvent = newBranchEventArgumentCaptor.getValue(); assertEquals("new-branch", newBranchEvent.getNewBranchName()); assertEquals("repo1-branch1", newBranchEvent.getFromBranchName()); assertEquals(repository1, newBranchEvent.getRepository()); }