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.rest.client.IntegrationTestUtil;
20  import com.atlassian.jira.rest.client.TestUtil;
21  import com.atlassian.jira.rest.client.api.domain.EntityHelper;
22  import com.atlassian.jira.rest.client.api.domain.Filter;
23  import com.atlassian.jira.rest.client.api.domain.util.ErrorCollection;
24  import com.atlassian.jira.rest.client.internal.json.TestConstants;
25  import com.atlassian.jira.util.lang.Pair;
26  import com.google.common.collect.ImmutableList;
27  import com.google.common.collect.Iterables;
28  import org.apache.commons.lang.StringUtils;
29  import org.junit.Before;
30  import org.junit.Rule;
31  import org.junit.Test;
32  import org.junit.rules.ExpectedException;
33  
34  import java.util.List;
35  
36  import static com.atlassian.jira.rest.client.IntegrationTestUtil.USER_ADMIN_60;
37  import static com.atlassian.jira.rest.client.IntegrationTestUtil.resolveURI;
38  import static org.hamcrest.MatcherAssert.assertThat;
39  import static org.hamcrest.Matchers.anyOf;
40  import static org.hamcrest.Matchers.is;
41  import static org.junit.Assert.assertEquals;
42  
43  public class AsynchronousSearchRestClientFiltersTest extends AbstractAsynchronousRestClientTest {
44  
45      private static boolean alreadyRestored;
46  
47      @Before
48      public void setup() {
49          if (!alreadyRestored) {
50              IntegrationTestUtil.restoreAppropriateJiraData(TestConstants.JIRA_DUMP_WITH_FILTERS_FILE, administration);
51              alreadyRestored = true;
52          }
53      }
54  
55      public static final Filter FILTER_10000_OLD = new Filter(resolveURI("rest/api/latest/filter/10000"), 10000L,
56              "Bugs in Test project", StringUtils.EMPTY, "project = TST AND issuetype = Bug",
57              resolveURI("secure/IssueNavigator.jspa?mode=hide&requestId=10000"),
58              resolveURI("rest/api/latest/search?jql=project+%3D+TST+AND+issuetype+%3D+Bug"),
59              USER_ADMIN_60, true);
60  
61      public static final Filter FILTER_10000_NEW = new Filter(resolveURI("rest/api/latest/filter/10000"), 10000L,
62              "Bugs in Test project", StringUtils.EMPTY, "project = TST AND issuetype = Bug",
63              resolveURI("issues/?filter=10000"),
64              resolveURI("rest/api/latest/search?jql=project+%3D+TST+AND+issuetype+%3D+Bug"),
65              USER_ADMIN_60, true);
66  
67      public static final Filter FILTER_10001_OLD = new Filter(resolveURI("rest/api/latest/filter/10001"), 10001L,
68              "Tasks in Test project - not favuorite filter", StringUtils.EMPTY, "project = TST AND issuetype = Task",
69              resolveURI("secure/IssueNavigator.jspa?mode=hide&requestId=10001"),
70              resolveURI("rest/api/latest/search?jql=project+%3D+TST+AND+issuetype+%3D+Task"),
71              USER_ADMIN_60, false);
72  
73      public static final Filter FILTER_10001_NEW = new Filter(resolveURI("rest/api/latest/filter/10001"), 10001L,
74              "Tasks in Test project - not favuorite filter", StringUtils.EMPTY, "project = TST AND issuetype = Task",
75              resolveURI("issues/?filter=10001"),
76              resolveURI("rest/api/latest/search?jql=project+%3D+TST+AND+issuetype+%3D+Task"),
77              USER_ADMIN_60, false);
78  
79      public static final Filter FILTER_10002_OLD = new Filter(resolveURI("rest/api/latest/filter/10002"), 10002L,
80              "All new features! (shared with everyone)", "This filter returns all issues of type \"New Fature\".", "issuetype = \"New Feature\"",
81              resolveURI("secure/IssueNavigator.jspa?mode=hide&requestId=10002"),
82              resolveURI("rest/api/latest/search?jql=issuetype+%3D+%22New+Feature%22"),
83              USER_ADMIN_60, true);
84  
85      public static final Filter FILTER_10002_NEW = new Filter(resolveURI("rest/api/latest/filter/10002"), 10002L,
86              "All new features! (shared with everyone)", "This filter returns all issues of type \"New Fature\".", "issuetype = \"New Feature\"",
87              resolveURI("issues/?filter=10002"),
88              resolveURI("rest/api/latest/search?jql=issuetype+%3D+%22New+Feature%22"),
89              USER_ADMIN_60, true);
90  
91      public static final Filter FILTER_10003_OLD = new Filter(resolveURI("rest/api/latest/filter/10003"), 10003L,
92              "Resolved bugs", "For testing shares.", "issuetype = Bug AND status = Resolved",
93              resolveURI("secure/IssueNavigator.jspa?mode=hide&requestId=10003"),
94              resolveURI("rest/api/latest/search?jql=issuetype+%3D+Bug+AND+status+%3D+Resolved"),
95              USER_ADMIN_60, true);
96  
97      public static final Filter FILTER_10003_NEW = new Filter(resolveURI("rest/api/latest/filter/10003"), 10003L,
98              "Resolved bugs", "For testing shares.", "issuetype = Bug AND status = Resolved",
99              resolveURI("issues/?filter=10003"),
100             resolveURI("rest/api/latest/search?jql=issuetype+%3D+Bug+AND+status+%3D+Resolved"),
101             USER_ADMIN_60, true);
102 
103     public static final Filter FILTER_10004_OLD = new Filter(resolveURI("rest/api/latest/filter/10004"), 10004L,
104             "All in project Test", "For testing subscriptions.", "project = TST",
105             resolveURI("secure/IssueNavigator.jspa?mode=hide&requestId=10004"),
106             resolveURI("rest/api/latest/search?jql=project+%3D+TST"),
107             USER_ADMIN_60, true);
108 
109     public static final Filter FILTER_10004_NEW = new Filter(resolveURI("rest/api/latest/filter/10004"), 10004L,
110             "All in project Test", "For testing subscriptions.", "project = TST",
111             resolveURI("issues/?filter=10004"),
112             resolveURI("rest/api/latest/search?jql=project+%3D+TST"),
113             USER_ADMIN_60, true);
114 
115 
116     @Rule
117     public ExpectedException thrown = ExpectedException.none();
118 
119     @Test
120     public void testGetFavouriteFilters() throws Exception {
121         final Iterable<Filter> filters = client.getSearchClient().getFavouriteFilters().claim();
122         final List<Pair<Filter, Filter>> expectedFilters = ImmutableList.of(
123                 Pair.of(FILTER_10000_OLD, FILTER_10000_NEW),
124                 Pair.of(FILTER_10002_OLD, FILTER_10002_NEW),
125                 Pair.of(FILTER_10003_OLD, FILTER_10003_NEW),
126                 Pair.of(FILTER_10004_OLD, FILTER_10004_NEW));
127         assertEquals(expectedFilters.size(), Iterables.size(filters));
128         for (Pair<Filter, Filter> pair : expectedFilters) {
129             final Filter actualFilter = EntityHelper.findEntityById(filters, pair.first().getId());
130             assertThat(actualFilter, anyOf(is(pair.first()), is(pair.second())));
131         }
132     }
133 
134     @Test
135     public void testGetFilterByUrl() throws Exception {
136         final List<Pair<Filter, Filter>> expectedFilters = ImmutableList.of(
137                 Pair.of(FILTER_10000_OLD, FILTER_10000_NEW),
138                 Pair.of(FILTER_10001_OLD, FILTER_10001_NEW),
139                 Pair.of(FILTER_10002_OLD, FILTER_10002_NEW),
140                 Pair.of(FILTER_10003_OLD, FILTER_10003_NEW));
141         for (Pair<Filter, Filter> pair : expectedFilters) {
142             final Filter actualFilter = client.getSearchClient().getFilter(pair.first().getSelf()).claim();
143             assertThat(actualFilter, anyOf(is(pair.first()), is(pair.second())));
144         }
145     }
146 
147     @Test
148     public void testGetFilterById() throws Exception {
149         final List<Pair<Filter, Filter>> expectedFilters = ImmutableList.of(
150                 Pair.of(FILTER_10000_OLD, FILTER_10000_NEW),
151                 Pair.of(FILTER_10001_OLD, FILTER_10001_NEW),
152                 Pair.of(FILTER_10002_OLD, FILTER_10002_NEW),
153                 Pair.of(FILTER_10003_OLD, FILTER_10003_NEW));
154         for (Pair<Filter, Filter> pair : expectedFilters) {
155             final Filter actualFilter = client.getSearchClient().getFilter(pair.first().getId()).claim();
156             assertThat(actualFilter, anyOf(is(pair.first()), is(pair.second())));
157         }
158     }
159 
160 
161     @Test
162     public void testGetNotExistent() throws Exception {
163         final ErrorCollection.Builder ecb = ErrorCollection.builder();
164         ecb.errorMessage("The selected filter is not available to you, perhaps it has been deleted or had its permissions changed.")
165                 .status(400);
166 
167         TestUtil.assertExpectedErrorCollection(ImmutableList.of(ecb.build()), new Runnable() {
168             @Override
169             public void run() {
170                 client.getSearchClient().getFilter(resolveURI("rest/api/latest/filter/999999")).claim();
171             }
172         });
173     }
174 
175     @Test
176     public void testGetNotExistentById() throws Exception {
177         final ErrorCollection.Builder ecb = ErrorCollection.builder();
178         ecb.errorMessage("The selected filter is not available to you, perhaps it has been deleted or had its permissions changed.")
179                 .status(400);
180 
181         TestUtil.assertExpectedErrorCollection(ImmutableList.of(ecb.build()), new Runnable() {
182             @Override
183             public void run() {
184                 client.getSearchClient().getFilter(999999).claim();
185             }
186         });
187 
188     }
189 
190 }