View Javadoc

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.rest.client.IntegrationTestUtil;
21  import com.atlassian.jira.rest.client.api.IssueRestClient;
22  import com.atlassian.jira.rest.client.api.domain.Comment;
23  import com.atlassian.jira.rest.client.api.domain.Issue;
24  import com.atlassian.jira.rest.client.internal.json.TestConstants;
25  import com.google.common.base.Objects;
26  import com.google.common.base.Predicate;
27  import com.google.common.collect.Iterables;
28  import com.google.common.collect.Lists;
29  import org.junit.Before;
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.assertEquals;
36  import static org.junit.Assert.assertFalse;
37  
38  /**
39   * Those tests mustn't change anything on server side, as jira is restored only once
40   */
41  public class AsynchronousIssueRestClientCommentTest extends AbstractAsynchronousRestClientTest {
42  
43      private static boolean alreadyRestored;
44  
45      @Before
46      public void setup() {
47          if (!alreadyRestored) {
48              IntegrationTestUtil.restoreAppropriateJiraData(TestConstants.DEFAULT_JIRA_DUMP_FILE, administration);
49              alreadyRestored = true;
50          }
51      }
52  
53      @Test
54      @JiraBuildNumberDependent(BN_JIRA_5)
55      public void testAddCommentToIssue() {
56          testAddCommentToIssueImpl("TST-5", Comment.valueOf("Simple test comment."));
57      }
58  
59      @Test
60      @JiraBuildNumberDependent(BN_JIRA_5)
61      public void testAddCommentToIssueAsAnonymousUser() {
62          setAnonymousMode();
63          testAddCommentToIssueImpl("ANONEDIT-1", Comment.valueOf("Simple test comment."));
64      }
65  
66      @Test
67      @JiraBuildNumberDependent(BN_JIRA_5)
68      public void testAddCommentToIssueWithGroupLevelVisibility() {
69          final Comment comment = Comment.createWithGroupLevel("Simple test comment restricted for admins.",
70                  IntegrationTestUtil.GROUP_JIRA_ADMINISTRATORS);
71          final String issueKey = "ANONEDIT-1";
72          final Comment addedComment = testAddCommentToIssueImpl(issueKey, comment);
73  
74          // try to get as anonymous user
75          setAnonymousMode();
76  
77          final IssueRestClient issueClient = client.getIssueClient();
78          final Issue issue = issueClient.getIssue(issueKey).claim();
79  
80          // test if we can see added comment
81          assertFalse(hasComment(issue.getComments(), addedComment.getId()));
82      }
83  
84      @Test
85      @JiraBuildNumberDependent(BN_JIRA_5)
86      public void testAddCommentToIssueWithRoleLevelVisibility() {
87          final Comment comment = Comment.createWithRoleLevel("Simple test comment restricted for role Administrators.",
88                  IntegrationTestUtil.ROLE_ADMINISTRATORS);
89          final String issueKey = "ANONEDIT-1";
90          final Comment addedComment = testAddCommentToIssueImpl(issueKey, comment);
91  
92          // try to get as anonymous user
93          setAnonymousMode();
94  
95          final IssueRestClient issueClient = client.getIssueClient();
96          final Issue issue = issueClient.getIssue(issueKey).claim();
97  
98          // test if we can see added comment
99          assertFalse(hasComment(issue.getComments(), addedComment.getId()));
100     }
101 
102     private boolean hasComment(final Iterable<Comment> comments, final Long id) {
103         return Iterables.filter(comments, new Predicate<Comment>() {
104             @Override
105             public boolean apply(Comment input) {
106                 return Objects.equal(input.getId(), id);
107             }
108         }).iterator().hasNext();
109     }
110 
111     private Comment testAddCommentToIssueImpl(final String issueKey, final Comment comment) {
112         final IssueRestClient issueClient = client.getIssueClient();
113         final Issue issue = issueClient.getIssue(issueKey).claim();
114         final List<Comment> initialComments = Lists.newArrayList(issue.getComments());
115 
116         issueClient.addComment(issue.getCommentsUri(), comment).claim();
117 
118         final Issue issueWithComments = issueClient.getIssue(issueKey).claim();
119         final List<Comment> newComments = Lists.newArrayList(issueWithComments.getComments());
120         newComments.removeAll(initialComments);
121         assertEquals(1, Iterables.size(newComments));
122         Comment addedComment = newComments.get(0);
123         assertEquals(comment.getBody(), addedComment.getBody());
124         assertEquals(comment.getVisibility(), addedComment.getVisibility());
125         return addedComment;
126     }
127 }