(refactor) Rename gitHubBeforeAfter to nonRecordingGitHub #1076

[#1076]
This commit is contained in:
Akash Rindhe
2021-04-18 18:41:10 +08:00
parent 649d7ed87f
commit 092815747a
19 changed files with 36 additions and 36 deletions

View File

@@ -41,7 +41,7 @@ Once you have credentials setup, you add new test classes and test methods as yo
Keep `useProxy` enabled and iterate on your tests as needed. With `useProxy` enabled your tests will interact with Keep `useProxy` enabled and iterate on your tests as needed. With `useProxy` enabled your tests will interact with
GitHub - you will need to clean up your server-state between runs. This can be done manually to start with. GitHub - you will need to clean up your server-state between runs. This can be done manually to start with.
Once your test code is somewhat stable, use `getGitHubBeforeAfter()` to get a `GitHub` instance for test setup and cleanup. Once your test code is somewhat stable, use `getNonRecordingGitHub()` to get a `GitHub` instance for test setup and cleanup.
Interactions with that `GitHub` instance will not be recorded as part of the test, keeping the test data files to a minimum. Interactions with that `GitHub` instance will not be recorded as part of the test, keeping the test data files to a minimum.
#### Running tests against your personal GitHub user account #### Running tests against your personal GitHub user account

View File

@@ -45,7 +45,7 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
*/ */
protected GitHub gitHub; protected GitHub gitHub;
private GitHub gitHubBeforeAfter; private GitHub nonRecordingGitHub;
protected final String baseFilesClassPath = this.getClass().getName().replace('.', '/'); protected final String baseFilesClassPath = this.getClass().getName().replace('.', '/');
protected final String baseRecordPath = "src/test/resources/" + baseFilesClassPath + "/wiremock"; protected final String baseRecordPath = "src/test/resources/" + baseFilesClassPath + "/wiremock";
@@ -115,9 +115,9 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
} }
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
gitHubBeforeAfter = getGitHubBuilder().withEndpoint("https://api.github.com/").build(); nonRecordingGitHub = getGitHubBuilder().withEndpoint("https://api.github.com/").build();
} else { } else {
gitHubBeforeAfter = null; nonRecordingGitHub = null;
} }
} }
@@ -210,7 +210,7 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
tempGitHubRepositories.add(fullName); tempGitHubRepositories.add(fullName);
try { try {
GHRepository repository = getGitHubBeforeAfter().getRepository(fullName); GHRepository repository = getNonRecordingGitHub().getRepository(fullName);
if (repository != null) { if (repository != null) {
repository.delete(); repository.delete();
} }
@@ -227,22 +227,22 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
* *
* @return a github instance after checking Authentication * @return a github instance after checking Authentication
*/ */
public GitHub getGitHubBeforeAfter() { public GitHub getNonRecordingGitHub() {
verifyAuthenticated(gitHubBeforeAfter); verifyAuthenticated(nonRecordingGitHub);
return gitHubBeforeAfter; return nonRecordingGitHub;
} }
protected void kohsuke() { protected void kohsuke() {
// No-op for now // No-op for now
// Generally this means the test is doing something that requires additional access rights // Generally this means the test is doing something that requires additional access rights
// Not always clear which ones. // Not always clear which ones.
// TODO: Add helpers that assert the expected rights using gitHubBeforeAfter and only when proxy is enabled // TODO: Add helpers that assert the expected rights using nonRecordingGitHub and only when proxy is enabled
// String login = getUserTest().getLogin(); // String login = getUserTest().getLogin();
// assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2")); // assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
} }
private GHCreateRepositoryBuilder getCreateBuilder(String name) throws IOException { private GHCreateRepositoryBuilder getCreateBuilder(String name) throws IOException {
GitHub github = getGitHubBeforeAfter(); GitHub github = getNonRecordingGitHub();
if (mockGitHub.isTestWithOrg()) { if (mockGitHub.isTestWithOrg()) {
return github.getOrganization(GITHUB_API_TEST_ORG).createRepository(name); return github.getOrganization(GITHUB_API_TEST_ORG).createRepository(name);

View File

@@ -91,7 +91,7 @@ public class AppTest extends AbstractGitHubWireMockTest {
private void cleanupUserRepository(final String name) throws IOException { private void cleanupUserRepository(final String name) throws IOException {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
cleanupRepository(getUser(getGitHubBeforeAfter()).getLogin() + "/" + name); cleanupRepository(getUser(getNonRecordingGitHub()).getLogin() + "/" + name);
} }
} }
@@ -585,7 +585,7 @@ public class AppTest extends AbstractGitHubWireMockTest {
// System.out.println(hook); // System.out.println(hook);
} finally { } finally {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
r = getGitHubBeforeAfter().getOrganization(GITHUB_API_TEST_ORG).getRepository("github-api"); r = getNonRecordingGitHub().getOrganization(GITHUB_API_TEST_ORG).getRepository("github-api");
for (GHHook h : r.getHooks()) { for (GHHook h : r.getHooks()) {
h.delete(); h.delete();
} }
@@ -1063,7 +1063,7 @@ public class AppTest extends AbstractGitHubWireMockTest {
void cleanupLabel(String name) { void cleanupLabel(String name) {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
try { try {
GHLabel t = getGitHubBeforeAfter().getRepository("hub4j-test-org/test-labels").getLabel(name); GHLabel t = getNonRecordingGitHub().getRepository("hub4j-test-org/test-labels").getLabel(name);
t.delete(); t.delete();
} catch (IOException e) { } catch (IOException e) {

View File

@@ -29,7 +29,7 @@ public class GHContentIntegrationTest extends AbstractGitHubWireMockTest {
@After @After
public void cleanup() throws Exception { public void cleanup() throws Exception {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
repo = getGitHubBeforeAfter().getRepository("hub4j-test-org/GHContentIntegrationTest"); repo = getNonRecordingGitHub().getRepository("hub4j-test-org/GHContentIntegrationTest");
try { try {
GHContent content = repo.getFileContent(createdFilename); GHContent content = repo.getFileContent(createdFilename);
if (content != null) { if (content != null) {

View File

@@ -26,7 +26,7 @@ public class GHDiscussionTest extends AbstractGitHubWireMockTest {
public void cleanupDiscussions() throws Exception { public void cleanupDiscussions() throws Exception {
// only need to clean up if we're pointing to the live site // only need to clean up if we're pointing to the live site
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
for (GHDiscussion discussion : getGitHubBeforeAfter().getOrganization(GITHUB_API_TEST_ORG) for (GHDiscussion discussion : getNonRecordingGitHub().getOrganization(GITHUB_API_TEST_ORG)
.getTeamBySlug(TEAM_SLUG) .getTeamBySlug(TEAM_SLUG)
.listDiscussions()) { .listDiscussions()) {
discussion.delete(); discussion.delete();

View File

@@ -20,7 +20,7 @@ public class GHMilestoneTest extends AbstractGitHubWireMockTest {
return; return;
} }
for (GHMilestone milestone : getRepository(getGitHubBeforeAfter()).listMilestones(GHIssueState.ALL)) { for (GHMilestone milestone : getRepository(getNonRecordingGitHub()).listMilestones(GHIssueState.ALL)) {
if ("Original Title".equals(milestone.getTitle()) || "Updated Title".equals(milestone.getTitle()) if ("Original Title".equals(milestone.getTitle()) || "Updated Title".equals(milestone.getTitle())
|| "Unset Test Milestone".equals(milestone.getTitle())) { || "Unset Test Milestone".equals(milestone.getTitle())) {
milestone.delete(); milestone.delete();

View File

@@ -25,7 +25,7 @@ public class GHOrganizationTest extends AbstractGitHubWireMockTest {
return; return;
} }
GHTeam team = getGitHubBeforeAfter().getOrganization(GITHUB_API_TEST_ORG).getTeamByName(TEAM_NAME_CREATE); GHTeam team = getNonRecordingGitHub().getOrganization(GITHUB_API_TEST_ORG).getTeamByName(TEAM_NAME_CREATE);
if (team != null) { if (team != null) {
team.delete(); team.delete();
} }

View File

@@ -74,7 +74,7 @@ public class GHProjectCardTest extends AbstractGitHubWireMockTest {
public void after() throws IOException { public void after() throws IOException {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
if (card != null) { if (card != null) {
card = getGitHubBeforeAfter().getProjectCard(card.getId()); card = getNonRecordingGitHub().getProjectCard(card.getId());
try { try {
card.delete(); card.delete();
card = null; card = null;
@@ -83,7 +83,7 @@ public class GHProjectCardTest extends AbstractGitHubWireMockTest {
} }
} }
if (column != null) { if (column != null) {
column = getGitHubBeforeAfter().getProjectColumn(column.getId()); column = getNonRecordingGitHub().getProjectColumn(column.getId());
try { try {
column.delete(); column.delete();
column = null; column = null;
@@ -92,7 +92,7 @@ public class GHProjectCardTest extends AbstractGitHubWireMockTest {
} }
} }
if (project != null) { if (project != null) {
project = getGitHubBeforeAfter().getProject(project.getId()); project = getNonRecordingGitHub().getProject(project.getId());
try { try {
project.delete(); project.delete();
project = null; project = null;

View File

@@ -48,7 +48,7 @@ public class GHProjectColumnTest extends AbstractGitHubWireMockTest {
public void after() throws IOException { public void after() throws IOException {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
if (column != null) { if (column != null) {
column = getGitHubBeforeAfter().getProjectColumn(column.getId()); column = getNonRecordingGitHub().getProjectColumn(column.getId());
try { try {
column.delete(); column.delete();
column = null; column = null;
@@ -57,7 +57,7 @@ public class GHProjectColumnTest extends AbstractGitHubWireMockTest {
} }
} }
if (project != null) { if (project != null) {
project = getGitHubBeforeAfter().getProject(project.getId()); project = getNonRecordingGitHub().getProject(project.getId());
try { try {
project.delete(); project.delete();
project = null; project = null;

View File

@@ -69,7 +69,7 @@ public class GHProjectTest extends AbstractGitHubWireMockTest {
public void after() throws IOException { public void after() throws IOException {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
if (project != null) { if (project != null) {
project = getGitHubBeforeAfter().getProject(project.getId()); project = getNonRecordingGitHub().getProject(project.getId());
try { try {
project.delete(); project.delete();
project = null; project = null;

View File

@@ -31,7 +31,7 @@ public class GHPullRequestTest extends AbstractGitHubWireMockTest {
return; return;
} }
for (GHPullRequest pr : getRepository(this.getGitHubBeforeAfter()).getPullRequests(GHIssueState.OPEN)) { for (GHPullRequest pr : getRepository(this.getNonRecordingGitHub()).getPullRequests(GHIssueState.OPEN)) {
pr.close(); pr.close();
} }
} }

View File

@@ -25,7 +25,7 @@ public class GHTagTest extends AbstractGitHubWireMockTest {
} }
try { try {
GHRef ref = getRepository(this.getGitHubBeforeAfter()).getRef("tags/create_tag_test"); GHRef ref = getRepository(this.getNonRecordingGitHub()).getRef("tags/create_tag_test");
if (ref != null) { if (ref != null) {
ref.delete(); ref.delete();
} }

View File

@@ -35,7 +35,7 @@ public class GHTreeBuilderTest extends AbstractGitHubWireMockTest {
@After @After
public void cleanup() throws Exception { public void cleanup() throws Exception {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
repo = getGitHubBeforeAfter().getRepository(REPO_NAME); repo = getNonRecordingGitHub().getRepository(REPO_NAME);
Arrays.asList(PATH_SCRIPT, PATH_README, PATH_DATA1, PATH_DATA2).forEach(path -> { Arrays.asList(PATH_SCRIPT, PATH_README, PATH_DATA1, PATH_DATA2).forEach(path -> {
try { try {
GHContent content = repo.getFileContent(path); GHContent content = repo.getFileContent(path);

View File

@@ -371,7 +371,7 @@ public class GHWorkflowRunTest extends AbstractGitHubWireMockTest {
return; return;
} }
GHRepository nonRecordingRepo = getGitHubBeforeAfter().getRepository(REPO_NAME); GHRepository nonRecordingRepo = getNonRecordingGitHub().getRepository(REPO_NAME);
Awaitility.await().pollInterval(Duration.ofSeconds(5)).atMost(Duration.ofSeconds(60)).until(() -> { Awaitility.await().pollInterval(Duration.ofSeconds(5)).atMost(Duration.ofSeconds(60)).until(() -> {
return condition.apply(nonRecordingRepo); return condition.apply(nonRecordingRepo);

View File

@@ -23,7 +23,7 @@ public class GHWorkflowTest extends AbstractGitHubWireMockTest {
@After @After
public void cleanup() throws Exception { public void cleanup() throws Exception {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
repo = getGitHubBeforeAfter().getRepository(REPO_NAME); repo = getNonRecordingGitHub().getRepository(REPO_NAME);
// we need to make sure the workflow is enabled before the tests // we need to make sure the workflow is enabled before the tests
GHWorkflow workflow = repo.getWorkflow("test-workflow.yml"); GHWorkflow workflow = repo.getWorkflow("test-workflow.yml");

View File

@@ -43,11 +43,11 @@ public class GitHubCachingTest extends AbstractGitHubWireMockTest {
@Before @Before
public void setupRepo() throws Exception { public void setupRepo() throws Exception {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
for (GHPullRequest pr : getRepository(this.getGitHubBeforeAfter()).getPullRequests(GHIssueState.OPEN)) { for (GHPullRequest pr : getRepository(this.getNonRecordingGitHub()).getPullRequests(GHIssueState.OPEN)) {
pr.close(); pr.close();
} }
try { try {
GHRef ref = getRepository(this.getGitHubBeforeAfter()).getRef(testRefName); GHRef ref = getRepository(this.getNonRecordingGitHub()).getRef(testRefName);
ref.delete(); ref.delete();
} catch (IOException e) { } catch (IOException e) {
} }

View File

@@ -73,7 +73,7 @@ public class OkHttpConnectorTest extends AbstractGitHubWireMockTest {
@Before @Before
public void setupRepo() throws Exception { public void setupRepo() throws Exception {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
GHRepository repo = getRepository(getGitHubBeforeAfter()); GHRepository repo = getRepository(getNonRecordingGitHub());
repo.setDescription("Resetting"); repo.setDescription("Resetting");
// Let things settle a bit between tests when working against the live site // Let things settle a bit between tests when working against the live site
@@ -252,7 +252,7 @@ public class OkHttpConnectorTest extends AbstractGitHubWireMockTest {
// Get Tricky - make a change via a different client // Get Tricky - make a change via a different client
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
GHRepository altRepo = getRepository(getGitHubBeforeAfter()); GHRepository altRepo = getRepository(getNonRecordingGitHub());
altRepo.setDescription("Tricky"); altRepo.setDescription("Tricky");
} }

View File

@@ -45,11 +45,11 @@ public class GitHubCachingTest extends AbstractGitHubWireMockTest {
@Before @Before
public void setupRepo() throws Exception { public void setupRepo() throws Exception {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
for (GHPullRequest pr : getRepository(this.getGitHubBeforeAfter()).getPullRequests(GHIssueState.OPEN)) { for (GHPullRequest pr : getRepository(this.getNonRecordingGitHub()).getPullRequests(GHIssueState.OPEN)) {
pr.close(); pr.close();
} }
try { try {
GHRef ref = getRepository(this.getGitHubBeforeAfter()).getRef(testRefName); GHRef ref = getRepository(this.getNonRecordingGitHub()).getRef(testRefName);
ref.delete(); ref.delete();
} catch (IOException e) { } catch (IOException e) {
} }

View File

@@ -79,7 +79,7 @@ public class OkHttpConnectorTest extends AbstractGitHubWireMockTest {
@Before @Before
public void setupRepo() throws Exception { public void setupRepo() throws Exception {
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
GHRepository repo = getRepository(getGitHubBeforeAfter()); GHRepository repo = getRepository(getNonRecordingGitHub());
repo.setDescription("Resetting"); repo.setDescription("Resetting");
// Let things settle a bit between tests when working against the live site // Let things settle a bit between tests when working against the live site
@@ -261,7 +261,7 @@ public class OkHttpConnectorTest extends AbstractGitHubWireMockTest {
// Get Tricky - make a change via a different client // Get Tricky - make a change via a different client
if (mockGitHub.isUseProxy()) { if (mockGitHub.isUseProxy()) {
GHRepository altRepo = getRepository(getGitHubBeforeAfter()); GHRepository altRepo = getRepository(getNonRecordingGitHub());
altRepo.setDescription("Tricky"); altRepo.setDescription("Tricky");
} }