1   /*
2    * Copyright (C) 2012 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package it;
18  
19  import com.atlassian.jira.nimblefunctests.annotation.JiraBuildNumberDependent;
20  import com.atlassian.jira.nimblefunctests.annotation.RestoreOnce;
21  import com.atlassian.jira.rest.client.IntegrationTestUtil;
22  import com.atlassian.jira.rest.client.IssueRestClient;
23  import com.atlassian.jira.rest.client.domain.Comment;
24  import com.atlassian.jira.rest.client.domain.Issue;
25  import com.atlassian.jira.rest.client.internal.json.TestConstants;
26  import com.google.common.base.Objects;
27  import com.google.common.base.Predicate;
28  import com.google.common.collect.Iterables;
29  import com.google.common.collect.Lists;
30  import org.junit.Test;
31  
32  import java.util.List;
33  
34  import static com.atlassian.jira.rest.client.internal.ServerVersionConstants.BN_JIRA_5;
35  import static org.junit.Assert.*;
36  
37  /**
38   * Those tests mustn't change anything on server side, as jira is restored only once
39   */
40  @RestoreOnce(TestConstants.DEFAULT_JIRA_DUMP_FILE)
41  public class JerseyIssueRestClientCommentTest extends AbstractJerseyRestClientTest  {
42  
43  	@Test
44  	@JiraBuildNumberDependent(BN_JIRA_5)
45  	public void testAddCommentToIssue() {
46  		testAddCommentToIssueImpl("TST-5", Comment.valueOf("Simple test comment."));
47  	}
48  
49  	@Test
50  	@JiraBuildNumberDependent(BN_JIRA_5)
51  	public void testAddCommentToIssueAsAnonymousUser() {
52  		setAnonymousMode();
53  		testAddCommentToIssueImpl("ANONEDIT-1", Comment.valueOf("Simple test comment."));
54  	}
55  
56  	@Test
57  	@JiraBuildNumberDependent(BN_JIRA_5)
58  	public void testAddCommentToIssueWithGroupLevelVisibility() {
59  		final Comment comment = Comment.createWithGroupLevel("Simple test comment restricted for admins.",
60  				IntegrationTestUtil.GROUP_JIRA_ADMINISTRATORS);
61  		final String issueKey = "ANONEDIT-1";
62  		final Comment addedComment = testAddCommentToIssueImpl(issueKey, comment);
63  
64  		// try to get as anonymous user
65  		setAnonymousMode();
66  
67  		final IssueRestClient issueClient = client.getIssueClient();
68  		final Issue issue = issueClient.getIssue(issueKey, pm);
69  
70  		// test if we can see added comment
71  		assertFalse(hasComment(issue.getComments(), addedComment.getId()));
72  	}
73  
74  	@Test
75  	@JiraBuildNumberDependent(BN_JIRA_5)
76  	public void testAddCommentToIssueWithRoleLevelVisibility() {
77  		final Comment comment = Comment.createWithRoleLevel("Simple test comment restricted for role Administrators.",
78  				IntegrationTestUtil.ROLE_ADMINISTRATORS);
79  		final String issueKey = "ANONEDIT-1";
80  		final Comment addedComment = testAddCommentToIssueImpl(issueKey, comment);
81  
82  		// try to get as anonymous user
83  		setAnonymousMode();
84  
85  		final IssueRestClient issueClient = client.getIssueClient();
86  		final Issue issue = issueClient.getIssue(issueKey, pm);
87  
88  		// test if we can see added comment
89  		assertFalse(hasComment(issue.getComments(), addedComment.getId()));
90  	}
91  	
92  	private boolean hasComment(final Iterable<Comment> comments, final Long id) {
93  		return Iterables.filter(comments, new Predicate<Comment>() {
94  			@Override
95  			public boolean apply(Comment input) {
96  				return Objects.equal(input.getId(), id);
97  			}
98  		}).iterator().hasNext();
99  	}
100 
101 	private Comment testAddCommentToIssueImpl(final String issueKey, final Comment comment) {
102 		final IssueRestClient issueClient = client.getIssueClient();
103 		final Issue issue = issueClient.getIssue(issueKey, pm);
104 		final List<Comment> initialComments = Lists.newArrayList(issue.getComments());
105 
106 		issueClient.addComment(pm, issue.getCommentsUri(), comment);
107 
108 		final Issue issueWithComments = issueClient.getIssue(issueKey, pm);
109 		final List<Comment> newComments = Lists.newArrayList(issueWithComments.getComments());
110 		newComments.removeAll(initialComments);
111 		assertEquals(1, Iterables.size(newComments));
112 		Comment addedComment = newComments.get(0);
113 		assertEquals(comment.getBody(), addedComment.getBody());
114 		assertEquals(comment.getVisibility(), addedComment.getVisibility());
115 		return addedComment;
116 	}
117 }