Skip to content

Commit

Permalink
1627: Add tests for bot factories
Browse files Browse the repository at this point in the history
Reviewed-by: erikj
  • Loading branch information
zhaosongzs authored and erikj79 committed Oct 20, 2022
1 parent 1edeff5 commit 481f311
Show file tree
Hide file tree
Showing 30 changed files with 1,456 additions and 10 deletions.
Expand Up @@ -157,4 +157,8 @@ public String name() {
public String toString() {
return "PullRequestCloserBot@" + remoteRepo.name();
}

public Type getType() {
return type;
}
}
Expand Up @@ -181,4 +181,8 @@ public String name() {
public String toString() {
return "PullRequestPrunerBot";
}

public Map<HostedRepository, Duration> getMaxAges() {
return maxAges;
}
}
@@ -0,0 +1,94 @@
package org.openjdk.skara.bots.bridgekeeper;

import org.junit.jupiter.api.Test;
import org.openjdk.skara.json.JWCC;
import org.openjdk.skara.test.TestBotFactory;
import org.openjdk.skara.test.TestHostedRepository;

import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class BridgekeeperBotFactoryTest {
@Test
public void testCreate() {
String jsonString = """
{
"mirrors": [
"mirror1",
"mirror2",
"mirror3"
],
"data": [
"data1",
"data2",
"data3"
],
"pruned": {
"pruned1": {
"maxage": "P1D"
},
"pruned2": {
"maxage": "PT48H"
},
"pruned3": {
"maxage": "PT4320M"
}
}
}
""";
var jsonConfig = JWCC.parse(jsonString).asObject();

var pruned1 = new TestHostedRepository("pruned1");
var pruned2 = new TestHostedRepository("pruned2");
var pruned3 = new TestHostedRepository("pruned3");
var testBotFactory = TestBotFactory.newBuilder()
.addHostedRepository("mirror1", new TestHostedRepository("mirror1"))
.addHostedRepository("mirror2", new TestHostedRepository("mirror2"))
.addHostedRepository("mirror3", new TestHostedRepository("mirror3"))
.addHostedRepository("data1", new TestHostedRepository("data1"))
.addHostedRepository("data2", new TestHostedRepository("data2"))
.addHostedRepository("data3", new TestHostedRepository("data3"))
.addHostedRepository("pruned1", pruned1)
.addHostedRepository("pruned2", pruned2)
.addHostedRepository("pruned3", pruned3)
.build();

var bots = testBotFactory.createBots(BridgekeeperBotFactory.NAME, jsonConfig);
assertEquals(7, bots.size());

var mirrorPullRequestCloserBots = bots.stream()
.filter(e -> e.getClass().equals(PullRequestCloserBot.class))
.filter(e -> ((PullRequestCloserBot) e).getType().equals(PullRequestCloserBot.Type.MIRROR))
.toList();
var dataPullRequestCloserBots = bots.stream()
.filter(e -> e.getClass().equals(PullRequestCloserBot.class))
.filter(e -> ((PullRequestCloserBot) e).getType().equals(PullRequestCloserBot.Type.DATA))
.toList();
var pullRequestPrunerBots = bots.stream()
.filter(e -> e.getClass().equals(PullRequestPrunerBot.class))
.toList();

// A mirror pullRequestCloserBot for every configured mirror repository
assertEquals(3, mirrorPullRequestCloserBots.size());
// A data pullRequestCloserBot for every configured data repository
assertEquals(3, dataPullRequestCloserBots.size());
// One pullRequestPrunerBot for all configured pruned repository
assertEquals(1, pullRequestPrunerBots.size());

// Check whether each bot is combined with the correct repo
assertEquals("PullRequestCloserBot@mirror1", mirrorPullRequestCloserBots.get(0).toString());
assertEquals("PullRequestCloserBot@mirror2", mirrorPullRequestCloserBots.get(1).toString());
assertEquals("PullRequestCloserBot@mirror3", mirrorPullRequestCloserBots.get(2).toString());
assertEquals("PullRequestCloserBot@data1", dataPullRequestCloserBots.get(0).toString());
assertEquals("PullRequestCloserBot@data2", dataPullRequestCloserBots.get(1).toString());
assertEquals("PullRequestCloserBot@data3", dataPullRequestCloserBots.get(2).toString());

var pullRequestPrunerBot = (PullRequestPrunerBot) pullRequestPrunerBots.get(0);
assertEquals("PullRequestPrunerBot", pullRequestPrunerBot.toString());
var maxAges = pullRequestPrunerBot.getMaxAges();
assertEquals(Duration.ofDays(1), maxAges.get(pruned1));
assertEquals(Duration.ofDays(2), maxAges.get(pruned2));
assertEquals(Duration.ofDays(3), maxAges.get(pruned3));
}
}
@@ -0,0 +1,51 @@
package org.openjdk.skara.bots.censussync;

import org.junit.jupiter.api.Test;
import org.openjdk.skara.json.JWCC;
import org.openjdk.skara.test.TestBotFactory;
import org.openjdk.skara.test.TestHostedRepository;

import static org.junit.jupiter.api.Assertions.*;

class CensusSyncBotFactoryTest {
@Test
void testCreate() {
String jsonString = """
{
"sync": [
{
"method": "unify",
"from": "from1",
"to": "to1",
"version": 1
},
{
"method": "split",
"from": "https://test.org/test.xml",
"to": "to2",
"version": 2
}
]
}
""";
var jsonConfig = JWCC.parse(jsonString).asObject();

var testBotFactory = TestBotFactory.newBuilder()
.addHostedRepository("from1", new TestHostedRepository("from1"))
.addHostedRepository("to1", new TestHostedRepository("to1"))
.addHostedRepository("to2", new TestHostedRepository("to2"))
.build();

var bots = testBotFactory.createBots(CensusSyncBotFactory.NAME, jsonConfig);
assertEquals(2, bots.size());

var censusSyncUnifyBots = bots.stream().filter(e -> e.getClass().equals(CensusSyncUnifyBot.class)).toList();
var censusSyncSplitBots = bots.stream().filter(e -> e.getClass().equals(CensusSyncSplitBot.class)).toList();

assertEquals(1, censusSyncUnifyBots.size());
assertEquals(1, censusSyncSplitBots.size());

assertEquals("CensusSyncUnifyBot(from1->to1@1)", censusSyncUnifyBots.get(0).toString());
assertEquals("CensusSyncSplitBot(https://test.org/test.xml->to2@2)", censusSyncSplitBots.get(0).toString());
}
}
@@ -0,0 +1,52 @@
package org.openjdk.skara.bots.checkout;

import org.junit.jupiter.api.Test;
import org.openjdk.skara.json.JWCC;
import org.openjdk.skara.test.TestBotFactory;
import org.openjdk.skara.test.TestHostedRepository;

import static org.junit.jupiter.api.Assertions.*;

class CheckoutBotFactoryTest {
@Test
public void testCreate() {
String jsonString = """
{
"marks": {
"repo": "mark",
"author": "test_author <test_author@test.com>"
},
"repositories": [
{
"from": {
"repo": "from1",
"branch": "master"
},
"to": "to1"
},
{
"from": {
"repo": "from2",
"branch": "dev"
},
"to": "to2"
}
]
}
""";
var jsonConfig = JWCC.parse(jsonString).asObject();

var testBotFactory = TestBotFactory.newBuilder()
.addHostedRepository("mark", new TestHostedRepository("mark"))
.addHostedRepository("from1", new TestHostedRepository("from1"))
.addHostedRepository("from2", new TestHostedRepository("from2"))
.build();

var bots = testBotFactory.createBots(CheckoutBotFactory.NAME, jsonConfig);
// A checkoutBot for every configured repository
assertEquals(2, bots.size());

assertEquals("CheckoutBot(from1:master, to1)", bots.get(0).toString());
assertEquals("CheckoutBot(from2:dev, to2)", bots.get(1).toString());
}
}
Expand Up @@ -91,4 +91,8 @@ public List<WorkItem> getPeriodicItems() {
public String name() {
return CSRBotFactory.NAME;
}

public IssueProject getProject() {
return project;
}
}
@@ -0,0 +1,72 @@
package org.openjdk.skara.bots.csr;

import org.junit.jupiter.api.Test;
import org.openjdk.skara.json.*;
import org.openjdk.skara.test.*;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CSRBotFactoryTest {
@Test
public void testCreate() {
String jsonString = """
{
"projects": [
{
"repository": "repo1",
"issues": "test_bugs/TEST"
},
{
"repository": "repo2",
"issues": "test_bugs/TEST"
},
{
"repository": "repo3",
"issues": "test_bugs/TEST2"
}
]
}
""";
var jsonConfig = JWCC.parse(jsonString).asObject();

var testBotFactory = TestBotFactory.newBuilder()
.addHostedRepository("repo1", new TestHostedRepository("repo1"))
.addHostedRepository("repo2", new TestHostedRepository("repo2"))
.addHostedRepository("repo3", new TestHostedRepository("repo3"))
.addIssueProject("test_bugs/TEST", new TestIssueProject(null, "TEST"))
.addIssueProject("test_bugs/TEST2", new TestIssueProject(null, "TEST2"))
.build();

var bots = testBotFactory.createBots(CSRBotFactory.NAME, jsonConfig);
assertEquals(5, bots.size());

var csrPullRequestBots = bots.stream().filter(e -> e.getClass().equals(CSRPullRequestBot.class)).toList();
var csrIssueBots = bots.stream().filter(e -> e.getClass().equals(CSRIssueBot.class)).toList();

// A CSRPullRequestBot for every configured repository
assertEquals(3, csrPullRequestBots.size());
// A CSRIssueBot for each unique IssueProject
assertEquals(2, csrIssueBots.size());

var CSRPullRequestBot1 = (CSRPullRequestBot) csrPullRequestBots.get(0);
var CSRPullRequestBot2 = (CSRPullRequestBot) csrPullRequestBots.get(1);
var CSRPullRequestBot3 = (CSRPullRequestBot) csrPullRequestBots.get(2);
assertEquals("CSRPullRequestBot@repo1", CSRPullRequestBot1.toString());
assertEquals("CSRPullRequestBot@repo2", CSRPullRequestBot2.toString());
assertEquals("CSRPullRequestBot@repo3", CSRPullRequestBot3.toString());
assertEquals("TEST", CSRPullRequestBot1.getProject().name());
assertEquals("TEST", CSRPullRequestBot2.getProject().name());
assertEquals("TEST2", CSRPullRequestBot3.getProject().name());

for (var bot : csrIssueBots) {
CSRIssueBot csrIssueBot = (CSRIssueBot) bot;
if (csrIssueBot.toString().equals("CSRIssueBot@TEST")) {
assertEquals(2, csrIssueBot.repositories().size());
} else if (csrIssueBot.toString().equals("CSRIssueBot@TEST2")) {
assertEquals(1, csrIssueBot.repositories().size());
} else {
throw new RuntimeException("This CSRIssueBot is not expected");
}
}
}
}
Expand Up @@ -95,7 +95,7 @@ public Collection<WorkItem> run(Path scratchPath) {

@Override
public String toString() {
return "FowardBot@(" + fromHostedRepo.name() + ":" + fromBranch.name() +
return "ForwardBot@(" + fromHostedRepo.name() + ":" + fromBranch.name() +
"-> " + toHostedRepo.name() + ":" + toBranch.name() + ")";
}

Expand Down
@@ -0,0 +1,51 @@
package org.openjdk.skara.bots.forward;

import org.junit.jupiter.api.Test;
import org.openjdk.skara.json.JWCC;
import org.openjdk.skara.test.TemporaryDirectory;
import org.openjdk.skara.test.TestBotFactory;
import org.openjdk.skara.test.TestHostedRepository;

import java.util.Comparator;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.*;

class ForwardBotFactoryTest {
@Test
public void testCreate() {
try (var tempFolder = new TemporaryDirectory()) {
String jsonString = """
{
"repositories": {
"repo1": {
"from": "from1:master",
"to": "to1:master"
},
"repo2": {
"from": "from2:dev",
"to": "to2:test"
}
}
}
""";
var jsonConfig = JWCC.parse(jsonString).asObject();

var testBotFactory = TestBotFactory.newBuilder()
.addHostedRepository("from1", new TestHostedRepository("from1"))
.addHostedRepository("from2", new TestHostedRepository("from2"))
.addHostedRepository("to1", new TestHostedRepository("to1"))
.addHostedRepository("to2", new TestHostedRepository("to2"))
.storagePath(tempFolder.path().resolve("storage"))
.build();

var bots = testBotFactory.createBots(ForwardBotFactory.NAME, jsonConfig);
bots = bots.stream().sorted(Comparator.comparing(Objects::toString)).toList();
//A forwardBot for every configured repo
assertEquals(2, bots.size());

assertEquals("ForwardBot@(from1:master-> to1:master)", bots.get(0).toString());
assertEquals("ForwardBot@(from2:dev-> to2:test)", bots.get(1).toString());
}
}
}
Expand Up @@ -191,4 +191,28 @@ public Converter resolve(Path scratchPath) throws IOException {

return new HgToGitConverter(replacements, corrections, lowercase, punctuated, authors, contributors, sponsors);
}

public String getConfigurationRef() {
return configurationRef;
}

public List<String> getReplacementsFile() {
return replacementsFile;
}

public List<String> getCorrectionsFile() {
return correctionsFile;
}

public List<String> getAuthorsFile() {
return authorsFile;
}

public List<String> getContributorsFile() {
return contributorsFile;
}

public List<String> getSponsorsFile() {
return sponsorsFile;
}
}

1 comment on commit 481f311

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.