ContractsGitLoader implements ContractsRetriever { protected Path getContractsFolderFromGitRepo() throws IOException { Path location = null; Git git = null; try { if (isSet(this.contractsGit.repository())) { Path repository = Paths.get(getResolvedValue(this.contractsGit.repository())); if (this.gitOperations.isValidGitRepository(repository)) { git = this.gitOperations.openGitRepository(repository); if (this.gitOperations.hasAtLeastOneReference(git.getRepository())) { final PullResult pullResult = executePull(git); if (pullResult.isSuccessful()) { location = moveToCorrectLocation(git); } else { throw new IllegalArgumentException( "There are merge conflicts into an existing git repo. Provider should not deal with merge conflicts. Correct them or delete the repo and execute again the test."); } } else { throw new IllegalArgumentException(String.format("Git repository %s was not cloned correctly.", git.getRepository().getDirectory().getAbsolutePath())); } } else { logger.log(Level.INFO, String.format( "%s directory is not a git directory or does not exists and it is going to be deleted and cloned", repository)); Files.deleteIfExists(repository); Files.createDirectories(repository); git = executeClone(repository); location = moveToCorrectLocation(git); } } else { final Path testGitRepository = Files.createTempDirectory("TestGitRepository"); logger.log(Level.INFO, String.format("Repository is going to be cloned at %s", testGitRepository)); git = executeClone(testGitRepository); location = moveToCorrectLocation(git); } } finally { if (git != null) { git.close(); } } return location; } ContractsGitLoader();  ContractsGitLoader(ContractsGit contractsGit); @Override List<URI> retrieve(); @Override String getName(); @Override void configure(Map<String, Object> configuration);  }
@Test public void should_pull_existing_git_repo_local_branch() throws IOException { final Path path = Paths.get("/tmp"); when(gitOperations.isValidGitRepository(path)).thenReturn(true); when(gitOperations.openGitRepository(path)).thenReturn(git); when(gitOperations.hasAtLeastOneReference(repository)).thenReturn(true); when(gitOperations.pullFromRepository(git, "origin", "master")).thenReturn(pullResult); when(gitOperations.isLocalBranch(git, "master")).thenReturn(true); ContractsGitLoader pactGitLoader = new ContractsGitLoader(TestWithPactRepositoryDirectory.class.getAnnotation(ContractsGit.class)); pactGitLoader.gitOperations = gitOperations; final Path pactsFromGitRepo = pactGitLoader.getContractsFolderFromGitRepo(); verify(gitOperations).pullFromRepository(git, "origin", "master"); verify(gitOperations).checkoutBranch(git, "master"); assertThat(pactsFromGitRepo.toString()).isEqualTo("/tmp"); }
@Test public void should_pull_existing_git_repo() throws IOException { final Path path = Paths.get("/tmp"); when(gitOperations.isValidGitRepository(path)).thenReturn(true); when(gitOperations.openGitRepository(path)).thenReturn(git); when(gitOperations.hasAtLeastOneReference(repository)).thenReturn(true); when(gitOperations.pullFromRepository(git, "origin", "master")).thenReturn(pullResult); when(gitOperations.isLocalBranch(git, "master")).thenReturn(false); ContractsGitLoader pactGitLoader = new ContractsGitLoader(TestWithPactRepositoryDirectory.class.getAnnotation(ContractsGit.class)); pactGitLoader.gitOperations = gitOperations; final Path pactsFromGitRepo = pactGitLoader.getContractsFolderFromGitRepo(); verify(gitOperations).pullFromRepository(git, "origin", "master"); verify(gitOperations).checkoutBranch(git, "master", "origin"); assertThat(pactsFromGitRepo.toString()).isEqualTo("/tmp"); }
@Test public void should_pull_existing_git_repo_and_move_to_subfolder() throws IOException { final Path path = Paths.get("/tmp"); when(gitOperations.isValidGitRepository(path)).thenReturn(true); when(gitOperations.openGitRepository(path)).thenReturn(git); when(gitOperations.hasAtLeastOneReference(repository)).thenReturn(true); when(gitOperations.pullFromRepository(git, "origin", "master")).thenReturn(pullResult); when(gitOperations.isLocalBranch(git, "master")).thenReturn(false); ContractsGitLoader pactGitLoader = new ContractsGitLoader(TestWithPactRepositoryDirectoryAndSubfolder.class.getAnnotation(ContractsGit.class)); pactGitLoader.gitOperations = gitOperations; final Path pactsFromGitRepo = pactGitLoader.getContractsFolderFromGitRepo(); verify(gitOperations).pullFromRepository(git, "origin", "master"); verify(gitOperations).checkoutBranch(git, "master", "origin"); assertThat(pactsFromGitRepo.toString()).isEqualTo("/tmp/pacts"); }
@Test public void should_pull_specific_branch_git_repo_and_use_tag() throws IOException { final Path path = Paths.get("/tmp"); when(gitOperations.isValidGitRepository(path)).thenReturn(true); when(gitOperations.openGitRepository(path)).thenReturn(git); when(gitOperations.hasAtLeastOneReference(repository)).thenReturn(true); when(gitOperations.pullFromRepository(git, "origin", "mybranch")).thenReturn(pullResult); ContractsGitLoader pactGitLoader = new ContractsGitLoader(TestWithPactRepositoryDirectoryAndTag.class.getAnnotation(ContractsGit.class)); pactGitLoader.gitOperations = gitOperations; final Path pactsFromGitRepo = pactGitLoader.getContractsFolderFromGitRepo(); verify(gitOperations).pullFromRepository(git, "origin", "mybranch"); verify(gitOperations).checkoutTag(git, "mytag"); assertThat(pactsFromGitRepo.toString()).isEqualTo("/tmp"); }