Initial commit

This commit is contained in:
Andy Wilkinson
2015-11-30 16:13:06 +00:00
commit 214fec38be
56 changed files with 6314 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.issuebot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Tests for {@link IssueBotApplication}.
*
* @author Andy Wilkinson
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(IssueBotApplication.class)
public class IssueBotApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.issuebot.triage;
import org.junit.Test;
import io.spring.issuebot.triage.github.GitHubOperations;
import io.spring.issuebot.triage.github.Issue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link LabelApplyingTriageListener}.
*
* @author Andy Wilkinson
*/
public class LabelApplyingTriageListenerTests {
private GitHubOperations gitHub = mock(GitHubOperations.class);
private final MonitoredRepository repository = new MonitoredRepository();
private final LabelApplyingTriageListener listener = new LabelApplyingTriageListener(
this.gitHub);
@Test
public void requiresTriage() {
Issue issue = new Issue(null, null, null, null, null);
this.repository.setLabel("test");
this.listener.requiresTriage(issue, this.repository);
verify(this.gitHub).addLabel(issue, "test");
}
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.issuebot.triage;
import java.util.Arrays;
import org.junit.Test;
import io.spring.issuebot.triage.filter.TriageFilter;
import io.spring.issuebot.triage.filter.TriageFilters;
import io.spring.issuebot.triage.github.GitHubOperations;
import io.spring.issuebot.triage.github.Issue;
import io.spring.issuebot.triage.github.Page;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Tests for {@link RepositoryMonitor}.
*
* @author Andy Wilkinson
*/
public class RepositoryMonitorTests {
private final GitHubOperations gitHub = mock(GitHubOperations.class);
private final TriageFilters triageFilters = mock(TriageFilters.class);
private final TriageFilter triageFilter = mock(TriageFilter.class);
private final TriageListener listener = mock(TriageListener.class);
@Test
public void repositoryWithNoIssues() {
MonitoredRepository repository = new MonitoredRepository();
repository.setOrganization("test");
repository.setName("test");
RepositoryMonitor repositoryMonitor = new RepositoryMonitor(this.gitHub,
this.triageFilters, this.listener, Arrays.asList(repository));
given(this.triageFilters.filterForRepository(repository))
.willReturn(this.triageFilter);
given(this.gitHub.getIssues("test", "test")).willReturn(null);
repositoryMonitor.monitor();
verifyNoMoreInteractions(this.listener);
}
@Test
public void repositoryWithIssueRequiringTriage() {
MonitoredRepository repository = new MonitoredRepository();
repository.setOrganization("test");
repository.setName("test");
RepositoryMonitor repositoryMonitor = new RepositoryMonitor(this.gitHub,
this.triageFilters, this.listener, Arrays.asList(repository));
given(this.triageFilters.filterForRepository(repository))
.willReturn(this.triageFilter);
@SuppressWarnings("unchecked")
Page<Issue> page = mock(Page.class);
Issue issue = new Issue(null, null, null, null, null);
given(page.getContent()).willReturn(Arrays.asList(issue));
given(this.gitHub.getIssues("test", "test")).willReturn(page);
given(this.triageFilter.triaged(issue)).willReturn(false);
repositoryMonitor.monitor();
verify(this.listener).requiresTriage(issue, repository);
}
@Test
public void repositoryWithIssueThatHasAlreadyBeenTriaged() {
MonitoredRepository repository = new MonitoredRepository();
repository.setOrganization("test");
repository.setName("test");
RepositoryMonitor repositoryMonitor = new RepositoryMonitor(this.gitHub,
this.triageFilters, this.listener, Arrays.asList(repository));
given(this.triageFilters.filterForRepository(repository))
.willReturn(this.triageFilter);
@SuppressWarnings("unchecked")
Page<Issue> page = mock(Page.class);
Issue issue = new Issue(null, null, null, null, null);
given(page.getContent()).willReturn(Arrays.asList(issue));
given(this.gitHub.getIssues("test", "test")).willReturn(page);
given(this.triageFilter.triaged(issue)).willReturn(true);
repositoryMonitor.monitor();
verifyNoMoreInteractions(this.listener);
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.issuebot.triage.filter;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import io.spring.issuebot.triage.github.Comment;
import io.spring.issuebot.triage.github.GitHubOperations;
import io.spring.issuebot.triage.github.Issue;
import io.spring.issuebot.triage.github.Page;
import io.spring.issuebot.triage.github.User;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link CommentedByCollaboratorTriageFilter}.
*
* @author Andy Wilkinson
*
*/
public class CommentedByCollaboratorTriageFilterTests {
private final GitHubOperations gitHub = mock(GitHubOperations.class);
private final TriageFilter filter = new CommentedByCollaboratorTriageFilter(
Arrays.asList("Adam", "Brenda", "Charlie"), this.gitHub);
private final Issue issue = new Issue(null, null, null, null, null);
@Test
@SuppressWarnings("unchecked")
public void noComments() {
Page<Comment> pageOne = mock(Page.class);
given(pageOne.getContent()).willReturn(Collections.emptyList());
given(this.gitHub.getComments(this.issue)).willReturn(pageOne);
assertThat(this.filter.triaged(this.issue), is(false));
}
@Test
@SuppressWarnings("unchecked")
public void noCommentsByCollaborators() {
Page<Comment> pageOne = mock(Page.class);
given(pageOne.getContent())
.willReturn(Arrays.asList(new Comment(new User("Debbie"))));
given(this.gitHub.getComments(this.issue)).willReturn(pageOne);
assertThat(this.filter.triaged(this.issue), is(false));
}
@Test
@SuppressWarnings("unchecked")
public void commentByCollaboratorOnFirstPage() {
Page<Comment> pageOne = mock(Page.class);
given(pageOne.getContent())
.willReturn(Arrays.asList(new Comment(new User("Brenda"))));
given(this.gitHub.getComments(this.issue)).willReturn(pageOne);
assertThat(this.filter.triaged(this.issue), is(true));
}
@Test
@SuppressWarnings("unchecked")
public void commentByCollaboratorOnLaterPage() {
Page<Comment> pageOne = mock(Page.class);
given(pageOne.getContent())
.willReturn(Arrays.asList(new Comment(new User("Debbie"))));
Page<Comment> pageTwo = mock(Page.class);
given(pageTwo.getContent())
.willReturn(Arrays.asList(new Comment(new User("Brenda"))));
given(pageOne.next()).willReturn(pageTwo);
given(this.gitHub.getComments(this.issue)).willReturn(pageOne);
assertThat(this.filter.triaged(this.issue), is(true));
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.issuebot.triage.filter;
import java.util.Arrays;
import org.junit.Test;
import io.spring.issuebot.triage.github.Issue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Tests for {@link DelegatingTriageFilter}.
*
* @author Andy Wilkinson
*/
public class DelegatingTriageFilterTests {
private TriageFilter delegate1 = mock(TriageFilter.class);
private TriageFilter delegate2 = mock(TriageFilter.class);
private TriageFilter delegate3 = mock(TriageFilter.class);
private TriageFilter filter = new DelegatingTriageFilter(
Arrays.asList(this.delegate1, this.delegate2, this.delegate3));
@Test
public void notTriagedWhenAllDelegatesReturnFalse() {
Issue issue = new Issue(null, null, null, null, null);
assertThat(this.filter.triaged(issue), is(false));
verify(this.delegate1).triaged(issue);
verify(this.delegate2).triaged(issue);
verify(this.delegate3).triaged(issue);
}
@Test
public void triagedAsSoonAsADelegateReturnsTrue() {
Issue issue = new Issue(null, null, null, null, null);
given(this.delegate2.triaged(issue)).willReturn(true);
assertThat(this.filter.triaged(issue), is(true));
verify(this.delegate1).triaged(issue);
verify(this.delegate2).triaged(issue);
verifyNoMoreInteractions(this.delegate3);
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.issuebot.triage.filter;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import io.spring.issuebot.triage.github.Issue;
import io.spring.issuebot.triage.github.Label;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link LabelledTriageFilter}.
*
* @author Andy Wilkinson
*/
public class LabelledTriageFilterTests {
private TriageFilter filter = new LabelledTriageFilter();
@Test
public void issueWithLabels() {
assertThat(this.filter.triaged(
new Issue(null, null, null, Arrays.asList(new Label("test")), null)),
is(true));
}
@Test
public void issueWithNullLabels() {
assertThat(this.filter.triaged(new Issue(null, null, null, null, null)),
is(false));
}
@Test
public void issueWithNoLabels() {
assertThat(
this.filter.triaged(
new Issue(null, null, null, Collections.emptyList(), null)),
is(false));
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.issuebot.triage.filter;
import org.junit.Test;
import io.spring.issuebot.triage.github.Issue;
import io.spring.issuebot.triage.github.Milestone;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link MilestoneAppliedTriageFilter}.
*
* @author Andy Wilkinson
*
*/
public class MilestoneAppliedTriageFilterTests {
private TriageFilter filter = new MilestoneAppliedTriageFilter();
@Test
public void issueWithMilestoneApplied() {
assertThat(
this.filter.triaged(
new Issue(null, null, null, null, new Milestone("test"))),
is(true));
}
@Test
public void issueWithNoMilestoneApplied() {
assertThat(this.filter.triaged(new Issue(null, null, null, null, null)),
is(false));
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.issuebot.triage.filter;
import java.util.Arrays;
import org.junit.Test;
import io.spring.issuebot.triage.github.Issue;
import io.spring.issuebot.triage.github.User;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link OpenedByCollaboratorTriageFilter}.
*
* @author Andy Wilkinson
*/
public class OpenedByCollaboratorTriageFilterTests {
private TriageFilter filter = new OpenedByCollaboratorTriageFilter(
Arrays.asList("Adam", "Brenda", "Charlie"));
@Test
public void openedByCollaborator() {
assertThat(
this.filter.triaged(new Issue(null, null, new User("Adam"), null, null)),
is(true));
}
@Test
public void openedByAnotherUser() {
assertThat(
this.filter
.triaged(new Issue(null, null, new User("Debbie"), null, null)),
is(false));
}
}

View File

@@ -0,0 +1,196 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.issuebot.triage.github;
import java.util.Arrays;
import java.util.Date;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.client.RequestMatcher;
import org.springframework.test.web.client.response.DefaultResponseCreator;
import org.springframework.util.Base64Utils;
import org.springframework.web.client.RestTemplate;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
/**
* Tests for {@link GitHubTemplate}.
*
* @author Andy Wilkinson
*/
public class GitHubTemplateTests {
private final RestTemplate rest = GitHubTemplate.createDefaultRestTemplate("username",
"password");
private final MockRestServiceServer server = MockRestServiceServer
.createServer(this.rest);
private GitHubTemplate gitHub = new GitHubTemplate(this.rest, new RegexLinkParser());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void noIssues() {
this.server.expect(requestTo("https://api.github.com/repos/org/repo/issues"))
.andExpect(method(HttpMethod.GET)).andExpect(basicAuth())
.andRespond(withSuccess("[]", MediaType.APPLICATION_JSON));
Page<Issue> issues = this.gitHub.getIssues("org", "repo");
assertThat(issues.getContent().size(), is(0));
assertThat(issues.next(), is(nullValue()));
}
@Test
public void singlePageOfIssues() {
this.server.expect(requestTo("https://api.github.com/repos/org/repo/issues"))
.andExpect(method(HttpMethod.GET)).andExpect(basicAuth())
.andRespond(withResource("issues-page-one.json"));
Page<Issue> issues = this.gitHub.getIssues("org", "repo");
assertThat(issues.getContent().size(), is(15));
assertThat(issues.next(), is(nullValue()));
}
@Test
public void multiplePagesOfIssues() {
HttpHeaders headers = new HttpHeaders();
headers.set("Link", "<page-two>; rel=\"next\"");
this.server.expect(requestTo("https://api.github.com/repos/org/repo/issues"))
.andExpect(method(HttpMethod.GET)).andExpect(basicAuth())
.andRespond(withResource("issues-page-one.json",
"Link:<page-two>; rel=\"next\""));
this.server.expect(requestTo("page-two")).andExpect(method(HttpMethod.GET))
.andExpect(basicAuth()).andRespond(withResource("issues-page-two.json"));
Page<Issue> pageOne = this.gitHub.getIssues("org", "repo");
assertThat(pageOne.getContent().size(), is(15));
Page<Issue> pageTwo = pageOne.next();
assertThat(pageTwo, is(not(nullValue())));
assertThat(pageTwo.getContent().size(), is(15));
}
@Test
public void rateLimited() {
long reset = System.currentTimeMillis();
HttpHeaders headers = new HttpHeaders();
headers.set("X-RateLimit-Remaining", "0");
headers.set("X-RateLimit-Reset", Long.toString(reset / 1000));
this.server.expect(requestTo("https://api.github.com/repos/org/repo/issues"))
.andExpect(method(HttpMethod.GET)).andExpect(basicAuth())
.andRespond(withStatus(HttpStatus.FORBIDDEN).headers(headers));
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(equalToIgnoringCase(
"Rate limit exceeded. Limit will reset at " + new Date(reset)));
this.gitHub.getIssues("org", "repo");
}
@Test
public void noComments() {
this.server.expect(requestTo("commentsUrl")).andExpect(method(HttpMethod.GET))
.andExpect(basicAuth())
.andRespond(withSuccess("[]", MediaType.APPLICATION_JSON));
Page<Comment> comments = this.gitHub
.getComments(new Issue(null, "commentsUrl", null, null, null));
assertThat(comments.getContent().size(), is(0));
assertThat(comments.next(), is(nullValue()));
}
@Test
public void singlePageOfComments() {
this.server.expect(requestTo("commentsUrl")).andExpect(method(HttpMethod.GET))
.andExpect(basicAuth())
.andRespond(withResource("comments-page-one.json"));
Page<Comment> comments = this.gitHub
.getComments(new Issue(null, "commentsUrl", null, null, null));
assertThat(comments.getContent().size(), is(17));
assertThat(comments.next(), is(nullValue()));
}
@Test
public void multiplePagesOfComments() {
HttpHeaders headers = new HttpHeaders();
headers.set("Link", "<page-two>; rel=\"next\"");
this.server.expect(requestTo("commentsUrl")).andExpect(method(HttpMethod.GET))
.andExpect(basicAuth()).andRespond(withResource("comments-page-one.json",
"Link:<page-two>; rel=\"next\""));
this.server.expect(requestTo("page-two")).andExpect(method(HttpMethod.GET))
.andExpect(basicAuth())
.andRespond(withResource("comments-page-two.json"));
Page<Comment> pageOne = this.gitHub
.getComments(new Issue(null, "commentsUrl", null, null, null));
assertThat(pageOne.getContent().size(), is(17));
Page<Comment> pageTwo = pageOne.next();
assertThat(pageTwo, is(not(nullValue())));
assertThat(pageTwo.getContent().size(), is(3));
}
@Test
public void addLabelToUnlabelledIssue() {
this.server.expect(requestTo("issueUrl")).andExpect(method(HttpMethod.POST))
.andExpect(basicAuth())
.andExpect(content().string("{\"labels\":[\"test\"]}"))
.andRespond(withResource("issue-single-label.json"));
Issue issue = new Issue("issueUrl", null, null, Arrays.asList(), null);
Issue labelledIssue = this.gitHub.addLabel(issue, "test");
assertThat(labelledIssue.getLabels(), hasSize(1));
}
@Test
public void addAdditionalLabelToIssue() {
this.server.expect(requestTo("issueUrl")).andExpect(method(HttpMethod.POST))
.andExpect(basicAuth())
.andExpect(content().string("{\"labels\":[\"test\"]}"))
.andRespond(withResource("issue-two-labels.json"));
Issue issue = new Issue("issueUrl", null, null, Arrays.asList(), null);
Issue labelledIssue = this.gitHub.addLabel(issue, "test");
assertThat(labelledIssue.getLabels(), hasSize(2));
}
private DefaultResponseCreator withResource(String resource, String... headers) {
HttpHeaders httpHeaders = new HttpHeaders();
for (String header : headers) {
String[] components = header.split(":");
httpHeaders.set(components[0], components[1]);
}
return withSuccess(new UrlResource(getClass().getResource(resource)),
MediaType.APPLICATION_JSON).headers(httpHeaders);
}
private RequestMatcher basicAuth() {
return header("Authorization", "Basic "
+ new String(Base64Utils.encode("username:password".getBytes())));
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.issuebot.triage.github;
import java.util.Map;
import org.junit.Test;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link RegexLinkParser}.
*
* @author Andy Wilkinson
*/
public class RegexLinkParserTests {
private final LinkParser linkParser = new RegexLinkParser();
@Test
public void emptyInput() {
assertThat(this.linkParser.parse("").size(), is(0));
}
@Test
public void nullInput() {
assertThat(this.linkParser.parse(null).size(), is(0));
}
@Test
public void singleLink() {
Map<String, String> links = this.linkParser.parse("<url>; rel=\"foo\"");
assertThat(links.size(), is(1));
assertThat(links, hasEntry("foo", "url"));
}
@Test
public void notALink() {
Map<String, String> links = this.linkParser.parse("<url>; foo bar");
assertThat(links.size(), is(0));
}
@Test
public void multipleLinks() {
Map<String, String> links = this.linkParser
.parse("<url-one>; rel=\"foo\", <url-two>; rel=\"bar\"");
assertThat(links.size(), is(2));
assertThat(links, hasEntry("foo", "url-one"));
assertThat(links, hasEntry("bar", "url-two"));
}
}