View Javadoc

1   package com.atlassian.plugins.rest.common.expand.resolver;
2   
3   import com.atlassian.plugins.rest.common.expand.DefaultExpandContext;
4   import com.atlassian.plugins.rest.common.expand.EntityCrawler;
5   import com.atlassian.plugins.rest.common.expand.ExpandContext;
6   import com.atlassian.plugins.rest.common.expand.Expandable;
7   import com.atlassian.plugins.rest.common.expand.entity.AbstractPagedListWrapper;
8   import com.atlassian.plugins.rest.common.expand.entity.ListWrapper;
9   import com.atlassian.plugins.rest.common.expand.entity.ListWrapperCallBacks;
10  import com.atlassian.plugins.rest.common.expand.entity.ListWrapperCallback;
11  import com.atlassian.plugins.rest.common.expand.parameter.DefaultExpandParameter;
12  import org.junit.Before;
13  import org.junit.Test;
14  import org.mockito.Mockito;
15  
16  import java.lang.annotation.Annotation;
17  import java.util.Arrays;
18  import java.util.List;
19  
20  import static java.util.Collections.singleton;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  /**
28   * Testing {@link ListWrapperEntityExpanderResolver}
29   */
30  public class ListWrapperEntityExpanderResolverTest
31  {
32      private ListWrapperEntityExpanderResolver resolver;
33  
34      @Before
35      public void setUp() throws Exception
36      {
37          resolver = new ListWrapperEntityExpanderResolver();
38      }
39  
40      @Test
41      public void testHasExpanderWithNullClass()
42      {
43          try
44          {
45              final Class clazz = null;
46              resolver.hasExpander(clazz);
47              fail();
48          }
49          catch (NullPointerException e)
50          {
51              // expected
52          }
53      }
54  
55      @Test
56      public void testGetExpanderWithNullClass()
57      {
58          try
59          {
60              final Class clazz = null;
61              resolver.getExpander(clazz);
62              fail();
63          }
64          catch (NullPointerException e)
65          {
66              // expected
67          }
68      }
69  
70      @Test
71      public void testHasExpanderWithClassForNonListWrapper()
72      {
73          assertFalse(resolver.hasExpander(Object.class));
74      }
75  
76      @Test
77      public void testGetExpanderWithClassForNonListWrapper()
78      {
79          assertNull(resolver.getExpander(Object.class));
80      }
81  
82      @Test
83      public void testHasExpanderWithClassForListWrapper()
84      {
85          assertTrue(resolver.hasExpander(ListWrapper.class));
86      }
87  
88      @Test
89      public void testGetExpanderWithClassForListWrapper()
90      {
91          assertEquals(ListWrapperEntityExpanderResolver.EXPANDER, resolver.getExpander(ListWrapper.class));
92      }
93  
94      @Test
95      @SuppressWarnings ("unchecked")
96      public void testExpandCollection() throws Exception
97      {
98          final List<String> strings = Arrays.asList("one", "two");
99          final ClassWithExpandableList<String> listWrapper = new ClassWithExpandableList<String>(strings);
100 
101         Expandable expandable = expandable("list");
102 
103         EntityCrawler crawler = Mockito.mock(EntityCrawler.class);
104         ExpandContext<ListWrapper<String>> context = new DefaultExpandContext<ListWrapper<String>>(listWrapper, expandable, new DefaultExpandParameter(singleton("list.whatever")));
105 
106         // run the test
107         new ListWrapperEntityExpanderResolver.ListWrapperEntityExpander().expand(context, resolver, crawler);
108 
109         // check that the list was expanded
110         assertTrue(listWrapper.items != null);
111         assertEquals(listWrapper.items.size(), strings.size());
112     }
113 
114     @Test
115     @SuppressWarnings ("unchecked")
116     public void testExpandInheritedCollection() throws Exception
117     {
118         final List<String> strings = Arrays.asList("one", "two");
119         final ClassWithExpandableList<String> listWrapper = new ClassWithInheritedExpandableList<String>(strings);
120 
121         Expandable expandable = expandable("list");
122 
123         EntityCrawler crawler = Mockito.mock(EntityCrawler.class);
124         ExpandContext<ListWrapper<String>> context = new DefaultExpandContext<ListWrapper<String>>(listWrapper, expandable, new DefaultExpandParameter(singleton("list.whatever")));
125 
126         // run the test
127         new ListWrapperEntityExpanderResolver.ListWrapperEntityExpander().expand(context, resolver, crawler);
128 
129         // check that the list was expanded
130         assertTrue(listWrapper.items != null);
131         assertEquals(listWrapper.items.size(), strings.size());
132     }
133 
134     static class ClassWithExpandableList<T> extends AbstractPagedListWrapper<T>
135     {
136         List<T> items = null;
137         final ListWrapperCallback<T> cb;
138 
139         ClassWithExpandableList(List<T> wrappedList)
140         {
141             super(wrappedList.size(), Integer.MAX_VALUE);
142             cb = ListWrapperCallBacks.ofList(wrappedList);
143         }
144 
145         @Override
146         public ListWrapperCallback<T> getPagingCallback()
147         {
148             return cb;
149         }
150     }
151 
152     static class ClassWithInheritedExpandableList<T> extends ClassWithExpandableList<T>
153     {
154 
155         ClassWithInheritedExpandableList(List<T> wrappedList)
156         {
157             super(wrappedList);
158         }
159     }
160 
161     static Expandable expandable(final String value)
162     {
163         return new Expandable()
164         {
165             public String value()
166             {
167                 return value;
168             }
169 
170             public Class<? extends Annotation> annotationType()
171             {
172                 return Expandable.class;
173             }
174         };
175     }
176 }