BranchService { public Branch getUndeletedOrCreateBranch(Repository repository, String branchName, User createdByUser) { logger.debug("getUndeletedOrCreateBranch, name: {}, repository id: {}", branchName, repository.getId()); Branch branch = branchRepository.findByNameAndRepository(branchName, repository); if (branch == null) { branch = createBranch(repository, branchName, createdByUser); } else if (branch.getDeleted()) { undeleteBranch(branch); } return branch; }  Branch createBranch(Repository repository, String branchName, User createdByUser); Branch getUndeletedOrCreateBranch(Repository repository, String branchName, User createdByUser); void undeleteBranch(Branch branch); PollableFuture<Void> asyncDeleteBranch(Long repositoryId, Long branchId); @Transactional void deleteBranch(Long repositoryId, Long branchId); void deleteBranchAsset(Long branchId, Long repositoryId);  }
@Test public void getOrCreateBranch() throws RepositoryNameAlreadyUsedException { Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("repository")); Branch before = branchRepository.findByNameAndRepository("master", repository); assertNull(before); Branch create = branchService.getUndeletedOrCreateBranch(repository, "master", null); assertEquals("master", create.getName()); assertEquals(repository.getId(), create.getRepository().getId()); Branch fromFind = branchRepository.findByNameAndRepository("master", repository); assertEquals("master", fromFind.getName()); assertEquals(repository.getId(), fromFind.getRepository().getId()); Branch get = branchService.getUndeletedOrCreateBranch(repository, "master", null); assertEquals(create.getId(), get.getId()); }
@Test public void getOrCreateBranchNameNull() throws RepositoryNameAlreadyUsedException { Repository repository = repositoryService.createRepository(testIdWatcher.getEntityName("repository")); String branchName = null; Branch before = branchRepository.findByNameAndRepository(branchName, repository); assertNull(before); Branch create = branchService.getUndeletedOrCreateBranch(repository, branchName, null); assertEquals(branchName, create.getName()); assertEquals(repository.getId(), create.getRepository().getId()); Branch fromFind = branchRepository.findByNameAndRepository(branchName, repository); assertEquals(branchName, fromFind.getName()); assertEquals(repository.getId(), fromFind.getRepository().getId()); Branch get = branchService.getUndeletedOrCreateBranch(repository, branchName, null); assertEquals(create.getId(), get.getId()); }