RFR: 1627: Add tests for bot factories

Zhao Song zsong at openjdk.org
Tue Oct 11 19:10:22 UTC 2022


On Tue, 11 Oct 2022 16:17:47 GMT, Erik Joelsson <erikj at openjdk.org> wrote:

> > Hi @erikj79 ,this commit contains test for CSRBotFactory. If you think this is ok, I would add tests for other bot factories as well.
> 
> I started to look around the source and I found TestBotFactory which seems to already be doing most of what I was envisioning here. Only two existing bots are using it for their tests, which is why I've missed it before. With that class you can supply just the JSON for the bot factory under test, and any other entities (i.e. IssueTracker or forge) are added as already constructed objects (using the test implementations).
> 
> Unless it's really needed, I would advice having the JSON defined inline in the test using the new triple quote multiline string literals (https://blogs.oracle.com/javamagazine/post/text-blocks-come-to-java). The size of these snippets should be fairly small as they only need to contain test configuration for a single bot.

Hi Erik, I looked at the TestBotFactory and I thought that if we want to let the IssueTracker or Forge public for all botFactory tests, we should test all botFactories in one test. I don't know if it is a good idea. I prefer to create a test for each botFactory. 

And I tried to use TestBotFactory, in the test, there will be some trivial problems like the name of Hosts and IssueHosts, it will make us need to create Host or IssueHost manually and make our test more complex. So I prefer to retain the config of forges and issueTrackers and still use TestBotRunnerConfiguration to create the hosts. What is your idea?

In the case below, I used the credentials to create HostedRepository and issueTrackers, the default name for the HostedRepository and issueTrackers are all test, so after that, all the bots we got are having the same name. If we want to create HostedRepository or IssueTrackers with specific name, I think use TestBotRunnerConfiguration is a good idea.

    @Test
    public void testCreate(TestInfo testInfo) throws IOException, ConfigurationError {
        try (var credentials = new HostCredentials(testInfo);
             var tempFolder = new TemporaryDirectory()) {

            var repo = credentials.getHostedRepository();
            var repoFolder = tempFolder.path().resolve("repo");
            var localRepo = CheckableRepository.init(repoFolder, repo.repositoryType());
            var issueTracker1 = credentials.getIssueProject();
            var issueTracker2 = credentials.getIssueProject();
            credentials.commitLock(localRepo);
            localRepo.pushAll(repo.url());


            String jsonString = """
                     {
                       "projects": [
                         {
                           "repository": "test at github:jdk1",
                           "issues": "test_bugs/TEST"
                         },
                         {
                           "repository": "test at github:jdk2",
                           "issues": "test_bugs/TEST"
                         },
                         {
                           "repository": "test at github:jdk3",
                           "issues": "test_bugs/TEST"
                         },
                         {
                           "repository": "test at github:jdk4",
                           "issues": "test_bugs/TEST"
                         },
                         {
                           "repository": "test at github:jdk5",
                           "issues": "test_bugs/TEST"
                         },
                         {
                           "repository": "test at github:jdk6",
                           "issues": "test_bugs/TEST2"
                         }
                       ]
                     }
                    """;
            var jsonConfig = JWCC.parse(jsonString).asObject();

            var testBotFactory = TestBotFactory.newBuilder().addHostedRepository("test at github", repo)
                    .addIssueProject("test_bugs/TEST", issueTracker1)
                    .addIssueProject("test_bugs/TEST2", issueTracker2)
                    .build();

            var bots = testBotFactory.createBots("csr", jsonConfig);
            assertEquals(8, bots.size());

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

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

            for (var bot : csrIssueBots) {
                CSRIssueBot csrIssueBot = (CSRIssueBot) bot;
                if (csrIssueBot.toString().equals("CSRIssueBot at TEST")) {
                    assertEquals(5, csrIssueBot.repositories().size());
                } else if (csrIssueBot.toString().equals("CSRIssueBot at TEST2")) {
                    assertEquals(1, csrIssueBot.repositories().size());
                } else {
                    throw new RuntimeException("This issue bot is not expected");
                }
            }
        }
    }
}
`


For the JSON, I would use the new triple quote multiline string literals. Thanks!

-------------

PR: https://git.openjdk.org/skara/pull/1393


More information about the skara-dev mailing list