View Javadoc

1   /*
2    * Copyright (C) 2010-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 com.atlassian.jira.rest.client.internal.async;
18  
19  import com.atlassian.jira.rest.client.api.domain.util.ErrorCollection;
20  import com.atlassian.jira.rest.client.internal.json.ResourceUtil;
21  import com.google.common.collect.Iterators;
22  import org.codehaus.jettison.json.JSONException;
23  import org.hamcrest.collection.IsIterableContainingInAnyOrder;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  import java.util.Collection;
28  
29  public class AbstractAsynchronousRestClientTest {
30  
31      private static final int BAD_REQUEST = 400;
32  
33      @Test
34      public void testExtractErrors() throws JSONException {
35          final String str = ResourceUtil.getStringFromResource("/json/error/valid.json");
36          final Collection<ErrorCollection> errors = AbstractAsynchronousRestClient.extractErrors(BAD_REQUEST, str);
37          final ErrorCollection errorCollection = Iterators.getOnlyElement(errors.iterator());
38          Assert.assertThat(errorCollection.getErrors().values(), IsIterableContainingInAnyOrder.containsInAnyOrder("abcfsd"));
39      }
40  
41      @Test
42      public void testExtractErrors2() throws JSONException {
43          final String str = ResourceUtil.getStringFromResource("/json/error/valid2.json");
44          final Collection<ErrorCollection> errors = AbstractAsynchronousRestClient.extractErrors(BAD_REQUEST, str);
45          final ErrorCollection errorCollection = Iterators.getOnlyElement(errors.iterator());
46          Assert.assertThat(errorCollection.getErrorMessages(), IsIterableContainingInAnyOrder.containsInAnyOrder("a", "b", "xxx"));
47      }
48  
49      @Test
50      public void testExtractErrors3() throws JSONException {
51          final String str = ResourceUtil.getStringFromResource("/json/error/valid3.json");
52          final Collection<ErrorCollection> errors = AbstractAsynchronousRestClient.extractErrors(BAD_REQUEST, str);
53          final ErrorCollection errorCollection = Iterators.getOnlyElement(errors.iterator());
54          Assert.assertThat(errorCollection.getErrors().values(), IsIterableContainingInAnyOrder.containsInAnyOrder("aa", "bb"));
55      }
56  
57      @Test
58      public void testExtractErrors4() throws JSONException {
59          final String str = ResourceUtil.getStringFromResource("/json/error/valid4.json");
60          final Collection<ErrorCollection> errors = AbstractAsynchronousRestClient.extractErrors(BAD_REQUEST, str);
61          final ErrorCollection errorCollection = Iterators.getOnlyElement(errors.iterator());
62  
63          Assert.assertThat(errorCollection.getErrorMessages(), IsIterableContainingInAnyOrder.containsInAnyOrder("a", "b"));
64          Assert.assertEquals(errorCollection.getErrors().get("a"), "y");
65          Assert.assertEquals(errorCollection.getErrors().get("c"), "z");
66      }
67  
68  }