Skip to content

Commit cfea954

Browse files
committed
* implemented integration tests for adding comment
* implemented unit tests for adding comments * renamed resource class for comment
1 parent b214c29 commit cfea954

6 files changed

Lines changed: 121 additions & 22 deletions

File tree

src/main/java/de/linsin/github/rest/domain/Comment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public class Comment {
2626
private Status status;
2727

2828
public Comment() {
29-
status = Status.saved;
29+
status = Status.unsaved;
3030
}
3131

3232
// TODO find out other statuses
3333
public enum Status {
34-
saved;
34+
saved, unsaved
3535
}
3636

3737
public String getComment() {

src/main/java/de/linsin/github/rest/resource/IssueCommentRequest.java renamed to src/main/java/de/linsin/github/rest/resource/CommentIssueRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
*
2121
* @author David Linsin - dlinsin@gmail.com
2222
*/
23-
public class IssueCommentRequest extends Request {
23+
public class CommentIssueRequest extends Request {
2424
private String comment;
2525

26-
public IssueCommentRequest(String argLogin, String argToken, String argComment) {
26+
public CommentIssueRequest(String argLogin, String argToken, String argComment) {
2727
super(argLogin, argToken);
2828
comment = argComment;
2929
}

src/main/java/de/linsin/github/rest/resource/IssueCommentResponse.java renamed to src/main/java/de/linsin/github/rest/resource/CommentIssueResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @author David Linsin - dlinsin@gmail.com
2424
*/
25-
public class IssueCommentResponse {
25+
public class CommentIssueResponse {
2626
private Comment comment;
2727

2828
public Comment getComment() {
@@ -35,7 +35,7 @@ public void setComment(Comment argComment) {
3535

3636
@Override
3737
public String toString() {
38-
return "IssueCommentResponse{" +
38+
return "CommentIssueResponse{" +
3939
"comment=" + comment +
4040
'}';
4141
}

src/main/java/de/linsin/github/rest/service/IssueBrowser.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import de.linsin.github.rest.domain.Comment;
2323
import de.linsin.github.rest.domain.Issue;
2424
import de.linsin.github.rest.domain.Repository;
25-
import de.linsin.github.rest.resource.IssueCommentRequest;
25+
import de.linsin.github.rest.resource.CommentIssueRequest;
2626
import de.linsin.github.rest.resource.IssueRequest;
2727
import de.linsin.github.rest.resource.IssueResponse;
2828
import de.linsin.github.rest.resource.IssuesResponse;
2929
import de.linsin.github.rest.resource.Request;
30-
import de.linsin.github.rest.resource.IssueCommentResponse;
30+
import de.linsin.github.rest.resource.CommentIssueResponse;
3131
import org.springframework.util.Assert;
3232
import org.springframework.web.client.RestTemplate;
3333

@@ -217,14 +217,12 @@ public Comment comment(Repository argRepository, Issue argIssue, Comment argComm
217217
Assert.isTrue(argIssue.getNumber() > 0);
218218
Assert.hasText(argComment.getComment());
219219

220-
IssueCommentRequest req = new IssueCommentRequest(username, apiToken, argComment.getComment());
221-
IssueCommentResponse resp = template.postForObject(COMMENT_ISSUE_URL, req, IssueCommentResponse.class, argRepository.getOwner(), argRepository.getName(), String.valueOf(argIssue.getNumber()));
220+
CommentIssueRequest req = new CommentIssueRequest(username, apiToken, argComment.getComment());
221+
CommentIssueResponse resp = template.postForObject(COMMENT_ISSUE_URL, req, CommentIssueResponse.class, argRepository.getOwner(), argRepository.getName(), String.valueOf(argIssue.getNumber()));
222222

223223
return resp.getComment();
224224
}
225225

226-
// TODO implement comment
227-
228226
protected List<Issue> doBrowse(Repository argRepository, String argUrl) {
229227
RestTemplate template = initTemplate();
230228
IssuesResponse resp = template.getForObject(argUrl, IssuesResponse.class, argRepository.getOwner(), argRepository.getName());

src/test/java/de/linsin/github/rest/service/IssueBrowserIntegrationTest.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717

1818
import java.util.List;
1919

20+
import de.linsin.github.rest.domain.Comment;
2021
import de.linsin.github.rest.domain.Issue;
2122
import de.linsin.github.rest.domain.Repository;
2223
import static org.junit.Assert.*;
2324
import org.junit.Before;
24-
import org.junit.Test;
2525
import org.junit.Ignore;
26+
import org.junit.Test;
2627
import org.springframework.web.client.HttpClientErrorException;
2728

2829
/**
@@ -320,7 +321,7 @@ public void edit_issue_invalid_repo() {
320321
}
321322
}
322323

323-
// TODO check this
324+
// TODO this test doesn't work due to an issue with github
324325
@Test
325326
@Ignore
326327
public void edit_issue_invalid_user() {
@@ -346,6 +347,46 @@ public void edit_issue_invalid_number() {
346347
classUnderTest.edit(repo, issue);
347348
}
348349

350+
@Test
351+
public void comment_issue() {
352+
Repository repo = setupTestRepo();
353+
Issue issue = setUpTestIssue(repo);
354+
Comment comment = new Comment();
355+
comment.setComment("dummy comment");
356+
Comment savedComment = classUnderTest.comment(repo, issue, comment);
357+
assertEquals(Comment.Status.saved.name(), savedComment.getStatus());
358+
}
359+
360+
@Test(expected = HttpClientErrorException.class)
361+
public void comment_issue_invalid_repo() {
362+
Repository repo = setupTestRepo();
363+
Issue issue = setUpTestIssue(repo);
364+
repo.setName(noGoodRepoName);
365+
Comment comment = new Comment();
366+
comment.setComment("dummy comment");
367+
classUnderTest.comment(repo, issue, comment);
368+
}
369+
370+
@Test(expected = HttpClientErrorException.class)
371+
public void comment_issue_invalid_user() {
372+
Repository repo = setupTestRepo();
373+
Issue issue = setUpTestIssue(repo);
374+
repo.setOwner(invalidUsername);
375+
Comment comment = new Comment();
376+
comment.setComment("dummy comment");
377+
classUnderTest.comment(repo, issue, comment);
378+
}
379+
380+
@Test(expected = HttpClientErrorException.class)
381+
public void comment_issue_invalid_number() {
382+
Repository repo = setupTestRepo();
383+
Issue issue = setUpTestIssue(repo);
384+
issue.setNumber(99999);
385+
Comment comment = new Comment();
386+
comment.setComment("dummy comment");
387+
classUnderTest.comment(repo, issue, comment);
388+
}
389+
349390
private Issue setUpTestIssue(Repository argRepo) {
350391
Issue newIssue = new Issue();
351392
String title = "Issue " + System.currentTimeMillis();

src/test/java/de/linsin/github/rest/service/IssueBrowserTest.java

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import de.linsin.github.rest.domain.Issue;
1919
import de.linsin.github.rest.resource.IssueResponse;
2020
import de.linsin.github.rest.resource.IssuesResponse;
21+
import de.linsin.github.rest.resource.CommentIssueResponse;
2122
import de.linsin.github.rest.domain.Repository;
23+
import de.linsin.github.rest.domain.Comment;
2224
import static org.easymock.classextension.EasyMock.*;
2325
import static org.easymock.EasyMock.eq;
2426
import org.junit.After;
@@ -253,10 +255,7 @@ public void reopen_issue_null_repo_passed() {
253255

254256
@Test
255257
public void edit_issue() {
256-
Issue issue = new Issue();
257-
issue.setNumber(1);
258-
issue.setTitle("test");
259-
issue.setBody("test body");
258+
Issue issue = setupTestIssue();
260259
Repository repo = setupTestRepo();
261260
IssueResponse response = new IssueResponse();
262261
response.setIssue(issue);
@@ -267,6 +266,14 @@ public void edit_issue() {
267266
verify(mockRestTemplate);
268267
}
269268

269+
private Issue setupTestIssue() {
270+
Issue issue = new Issue();
271+
issue.setNumber(1);
272+
issue.setTitle("test");
273+
issue.setBody("test body");
274+
return issue;
275+
}
276+
270277
@Test(expected = IllegalArgumentException.class)
271278
public void edit_issue_no_number_in_issue() {
272279
classUnderTest.edit(setupTestRepo(), new Issue());
@@ -294,13 +301,66 @@ public void edit_issue_null_issue_passed() {
294301

295302
@Test(expected = NullPointerException.class)
296303
public void edit_issue_null_repo_passed() {
297-
Issue issue = new Issue();
298-
issue.setNumber(1);
299-
issue.setTitle("test");
300-
issue.setBody("test body");
304+
Issue issue = setupTestIssue();
301305
classUnderTest.edit(null, issue);
302306
}
303307

308+
@Test
309+
public void comment_issue() {
310+
Repository repo = setupTestRepo();
311+
Issue issue = setupTestIssue();
312+
Comment comment = new Comment();
313+
comment.setComment("hello moto");
314+
315+
CommentIssueResponse response = new CommentIssueResponse();
316+
response.setComment(comment);
317+
expect(mockRestTemplate.postForObject(eq(IssueBrowser.COMMENT_ISSUE_URL), anyObject(), eq(CommentIssueResponse.class), eq(repo.getOwner()),
318+
eq(repo.getName()), eq(String.valueOf(issue.getNumber())))).andReturn(response);
319+
replay(mockRestTemplate);
320+
assertNotNull(classUnderTest.comment(repo, issue, comment));
321+
verify(mockRestTemplate);
322+
}
323+
324+
@Test(expected = IllegalArgumentException.class)
325+
public void comment_issue_no_number_in_issue() {
326+
Repository repo = setupTestRepo();
327+
Issue issue = new Issue();
328+
Comment comment = new Comment();
329+
comment.setComment("hello moto");
330+
classUnderTest.comment(repo, issue, comment);
331+
}
332+
333+
@Test(expected = IllegalArgumentException.class)
334+
public void comment_issue_no_comment_text() {
335+
Repository repo = setupTestRepo();
336+
Issue issue = setupTestIssue();
337+
Comment comment = new Comment();
338+
classUnderTest.comment(repo, issue, comment);
339+
}
340+
341+
@Test(expected = NullPointerException.class)
342+
public void comment_issue_null_repo_passed() {
343+
Issue issue = setupTestIssue();
344+
Comment comment = new Comment();
345+
comment.setComment("hello moto");
346+
classUnderTest.comment(null, issue, comment);
347+
}
348+
349+
@Test(expected = NullPointerException.class)
350+
public void comment_issue_null_issue_passed() {
351+
Repository repo = setupTestRepo();
352+
Comment comment = new Comment();
353+
comment.setComment("hello moto");
354+
classUnderTest.comment(repo, null, comment);
355+
}
356+
357+
@Test(expected = NullPointerException.class)
358+
public void comment_issue_null_comment_passed() {
359+
Repository repo = setupTestRepo();
360+
Issue issue = setupTestIssue();
361+
classUnderTest.comment(repo, issue, null);
362+
}
363+
304364
private Repository setupTestRepo() {
305365
Repository repo = new Repository();
306366
repo.setName("area51");

0 commit comments

Comments
 (0)