From 3b2a7b46a9a8f32dd6c0b236841ac9df452e9ba6 Mon Sep 17 00:00:00 2001 From: fazer1929 Date: Tue, 28 Jun 2022 12:25:24 +0530 Subject: [PATCH] Issue#3: Added tests for Cryo Tests Include: 1. Check Repository URL 2. Check determining current branch 3. Check Fetch PR list 4. Check future branch setup 5. Check single PR merge 6. Check multiple PR merge 7. Check PR merge with Dependency --- pom.xml | 24 ++--- src/test/java/CryoAccess.java | 188 ++++++++++++++++++++++++++++++++++ src/test/java/TestCryo.java | 101 ++++++++++++++++++ 3 files changed, 298 insertions(+), 15 deletions(-) create mode 100644 src/test/java/CryoAccess.java create mode 100644 src/test/java/TestCryo.java diff --git a/pom.xml b/pom.xml index 32c81f4..c9bf321 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,6 @@ 1.8 1.8 - 2.15 1.1.6.Final @@ -90,18 +89,6 @@ argparse4j ${version.net.sourceforge.argparse4j} - - org.testng - testng - ${version.testng} - test - - - org.mockito - mockito-core - ${version.mockito} - test - org.jboss.logging jboss-logging @@ -119,6 +106,12 @@ jboss-logmanager ${version.org.jboss.logmanager} + + junit + junit + 4.12 + test + @@ -174,6 +167,7 @@ wildfly-build-config ${org.wildfly.wildfly.build.config.version} + @@ -187,7 +181,7 @@ - + jboss-public-repository-group @@ -204,4 +198,4 @@ - + \ No newline at end of file diff --git a/src/test/java/CryoAccess.java b/src/test/java/CryoAccess.java new file mode 100644 index 0000000..862b253 --- /dev/null +++ b/src/test/java/CryoAccess.java @@ -0,0 +1,188 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright (c) 2020, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +import org.jboss.set.aphrodite.Aphrodite; +import org.jboss.set.aphrodite.config.AphroditeConfig; +import org.jboss.set.aphrodite.config.IssueTrackerConfig; +import org.jboss.set.aphrodite.config.RepositoryConfig; +import org.jboss.set.aphrodite.config.StreamConfig; +import org.jboss.set.aphrodite.config.StreamType; +import org.jboss.set.aphrodite.repository.services.common.RepositoryType; +import org.jboss.set.aphrodite.repository.services.github.GithubPullRequestHomeService; +import org.jboss.set.aphrodite.spi.AphroditeException; +import org.jboss.set.cryo.Cryo; +import org.jboss.set.cryo.process.BisectablePullRequest; +import org.jboss.set.cryo.process.ExecuteProcess; + +public class CryoAccess extends Cryo { + private static final String testDirLocation = System.getProperty("user.dir") + "/cryo-tests/"; + protected static final String[] COMMAND_GIT_CHECKOUT_MASTER = new String[] { "git", "checkout", "master" }; + protected static final String[] COMMAND_GIT_DELETE_FUTURE = new String[] { "git", "branch", "-D", "masterfuture" }; + protected static final String[] COMMAND_GIT_LOG = new String[] { "git", "log" }; + protected static Aphrodite aphro; + + CryoAccess() { + super(new File(testDirLocation), true, false, false, new HashSet(), + CryoAccess.createIncludeList(new String[] { "1" }), "future", "", new String[] {}); + } + + CryoAccess(String[] includeList) { + super(new File(testDirLocation), true, false, false, new HashSet(), + CryoAccess.createIncludeList(includeList), "future", "", new String[] {}); + } + + private static List createIncludeList(String[] includeList) { + List include; + include = new ArrayList<>(); + for (String pr : includeList) + include.add(pr); + return include; + } + + public boolean createOperationCenter() { + return super.createOperationCenter(); + } + + public boolean determineRepositoryURL() { + return super.determineRepositoryURL(); + } + + public boolean determineCurrentBranch() { + return super.determineCurrentBranch(); + } + + public boolean fetchPRList(String repoUrl) throws MalformedURLException { + determineCurrentBranch(); + super.repositoryURL = new URL(repoUrl); + return super.fetchPRList(); + } + + public URL getRepositoryURL() { + return super.repositoryURL; + } + + public void initializeAphrodite() throws MalformedURLException { + + RepositoryConfig githubService = new RepositoryConfig("https://github.com/", "fazer1929", + "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", + RepositoryType.GITHUB); + List repositoryConfigs = new ArrayList<>(); + repositoryConfigs.add(githubService); + + List issueTrackerConfigs = new ArrayList<>(); + + StreamConfig streamService = new StreamConfig( + new URL("https://raw.githubusercontent.com/jboss-set/jboss-streams/master/streams.json"), StreamType.JSON); + List streamConfigs = new ArrayList<>(); + streamConfigs.add(streamService); + + try { + AphroditeConfig aphroditeConfig = new AphroditeConfig(issueTrackerConfigs, repositoryConfigs, streamConfigs); + this.aphrodite = Aphrodite.instance(aphroditeConfig); + GithubPullRequestHomeService GithubPullRequestHomeService = new GithubPullRequestHomeService(aphrodite); + super.aphrodite = this.aphrodite; + } catch (AphroditeException e) { + e.printStackTrace(); + } + } + + public void setUpCryo(String repoURL) throws MalformedURLException { + ProcessBuilder processBuilder = new ProcessBuilder( + new String[] { "git", "clone", repoURL }); + ExecuteProcess executeProcess = new ExecuteProcess(processBuilder); + executeProcess.getProcessResult(); + + this.createOperationCenter(); + if (CryoAccess.aphro == null) { + this.initializeAphrodite(); + CryoAccess.aphro = super.aphrodite; + } else { + this.aphrodite = CryoAccess.aphro; + super.aphrodite = CryoAccess.aphro; + } + } + + private void cleanupFutureBranch() { + ProcessBuilder processBuilder = new ProcessBuilder( + COMMAND_GIT_CHECKOUT_MASTER); + processBuilder.directory(new File(testDirLocation)); + ExecuteProcess executeProcess = new ExecuteProcess(processBuilder); + executeProcess.getProcessResult(); + processBuilder = new ProcessBuilder( + COMMAND_GIT_DELETE_FUTURE); + processBuilder.directory(new File(testDirLocation)); + executeProcess = new ExecuteProcess(processBuilder); + executeProcess.getProcessResult(); + } + + public boolean setUpFutureBranch() { + if (super.setUpFutureBranch()) { + cleanupFutureBranch(); + return true; + } + return false; + } + + private boolean checkShaExists() { + // Getting latest commit ids of every eligible PR + List shas = new ArrayList<>(); + for (BisectablePullRequest bpr : coldStorage) { + shas.add(bpr.getPullRequest().getCommits().get(0).getSha()); + } + boolean includesAll = true; + + // Getting the logs of directory cryo created + + ProcessBuilder processBuilder = new ProcessBuilder( + COMMAND_GIT_LOG); + processBuilder.directory(new File(testDirLocation)); + ExecuteProcess executeProcess = new ExecuteProcess(processBuilder); + String logs = executeProcess.getProcessResult().getOutput(); + + // Checking id all the commits exist + for (String sha : shas) { + includesAll = logs.contains(sha); + if (!includesAll) + break; + } + return includesAll; + } + + public boolean mergePRs() { + try { + cleanupFutureBranch(); + super.createStorage(); + if (checkShaExists()) + return true; + return false; + } catch (Exception e) { + return false; + } + } +} \ No newline at end of file diff --git a/src/test/java/TestCryo.java b/src/test/java/TestCryo.java new file mode 100644 index 0000000..0337eb7 --- /dev/null +++ b/src/test/java/TestCryo.java @@ -0,0 +1,101 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright (c) 2020, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +import static junit.framework.TestCase.assertEquals; + +import java.net.MalformedURLException; +import java.net.URL; + +import org.jboss.set.cryo.process.ExecuteProcess; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestCryo { + private static CryoAccess cryo; + private static String repoURL; + protected static final String[] COMMAND_REMOVE_TEST_DIR = new String[]{"rm", "-rf", "cryo-tests"}; + + public TestCryo() throws MalformedURLException { + repoURL = "https://github.com/jboss-set/cryo-tests"; + cryo = new CryoAccess(); + System.setProperty("aphrodite.config", "/home/abagrawa/Desktop/aphrodite.properties.json"); + cryo.setUpCryo(repoURL); + + } + + @BeforeClass + public static void setup() throws MalformedURLException { + System.out.println("Lol"); + } + + @Test + public void test1DetermineRepositoryURL() throws MalformedURLException { + assertEquals(cryo.determineRepositoryURL(), true); + assertEquals(cryo.getRepositoryURL(), new URL(repoURL)); + } + + @Test + public void test2DetermineCurrentBranch() { + assertEquals(cryo.determineCurrentBranch(), true); + } + + @Test + public void test3FetchPRList() throws MalformedURLException { + assertEquals(cryo.fetchPRList(repoURL), true); + } + + @Test + public void test4SetupFutureBranch() throws Exception { + cryo.fetchPRList(repoURL); + assertEquals(cryo.setUpFutureBranch(), true); + } + + @Test + public void test5MergeSinglePR() throws MalformedURLException { + assertEquals(cryo.mergePRs(), true); + } + + @Test + public void test6MergemultiplePRs() throws MalformedURLException { + cryo = new CryoAccess(new String[]{"2", "3"}); + cryo.setUpCryo(repoURL); + assertEquals(cryo.mergePRs(), true); + } + + @Test + public void test7MergePRWithDependency() throws MalformedURLException { + cryo = new CryoAccess(new String[]{"10", "11", "12"}); + cryo.setUpCryo(repoURL); + assertEquals(cryo.mergePRs(), true); + } + + @AfterClass + public static void removeDir() { + ProcessBuilder processBuilder = new ProcessBuilder(COMMAND_REMOVE_TEST_DIR); + ExecuteProcess executeProcess = new ExecuteProcess(processBuilder); + executeProcess.getProcessResult(); + } +} \ No newline at end of file