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      private ListWrapperEntityExpanderResolver resolver;
32  
33      @Before
34      public void setUp() throws Exception {
35          resolver = new ListWrapperEntityExpanderResolver();
36      }
37  
38      @Test
39      public void testHasExpanderWithNullClass() {
40          try {
41              final Class clazz = null;
42              resolver.hasExpander(clazz);
43              fail();
44          } catch (NullPointerException e) {
45              // expected
46          }
47      }
48  
49      @Test
50      public void testGetExpanderWithNullClass() {
51          try {
52              final Class clazz = null;
53              resolver.getExpander(clazz);
54              fail();
55          } catch (NullPointerException e) {
56              // expected
57          }
58      }
59  
60      @Test
61      public void testHasExpanderWithClassForNonListWrapper() {
62          assertFalse(resolver.hasExpander(Object.class));
63      }
64  
65      @Test
66      public void testGetExpanderWithClassForNonListWrapper() {
67          assertNull(resolver.getExpander(Object.class));
68      }
69  
70      @Test
71      public void testHasExpanderWithClassForListWrapper() {
72          assertTrue(resolver.hasExpander(ListWrapper.class));
73      }
74  
75      @Test
76      public void testGetExpanderWithClassForListWrapper() {
77          assertEquals(ListWrapperEntityExpanderResolver.EXPANDER, resolver.getExpander(ListWrapper.class));
78      }
79  
80      @Test
81      @SuppressWarnings("unchecked")
82      public void testExpandCollection() throws Exception {
83          final List<String> strings = Arrays.asList("one", "two");
84          final ClassWithExpandableList<String> listWrapper = new ClassWithExpandableList<String>(strings);
85  
86          Expandable expandable = expandable("list");
87  
88          EntityCrawler crawler = Mockito.mock(EntityCrawler.class);
89          ExpandContext<ListWrapper<String>> context = new DefaultExpandContext<ListWrapper<String>>(listWrapper, expandable, new DefaultExpandParameter(singleton("list.whatever")));
90  
91          // run the test
92          new ListWrapperEntityExpanderResolver.ListWrapperEntityExpander().expand(context, resolver, crawler);
93  
94          // check that the list was expanded
95          assertTrue(listWrapper.items != null);
96          assertEquals(listWrapper.items.size(), strings.size());
97      }
98  
99      @Test
100     @SuppressWarnings("unchecked")
101     public void testExpandInheritedCollection() throws Exception {
102         final List<String> strings = Arrays.asList("one", "two");
103         final ClassWithExpandableList<String> listWrapper = new ClassWithInheritedExpandableList<String>(strings);
104 
105         Expandable expandable = expandable("list");
106 
107         EntityCrawler crawler = Mockito.mock(EntityCrawler.class);
108         ExpandContext<ListWrapper<String>> context = new DefaultExpandContext<ListWrapper<String>>(listWrapper, expandable, new DefaultExpandParameter(singleton("list.whatever")));
109 
110         // run the test
111         new ListWrapperEntityExpanderResolver.ListWrapperEntityExpander().expand(context, resolver, crawler);
112 
113         // check that the list was expanded
114         assertTrue(listWrapper.items != null);
115         assertEquals(listWrapper.items.size(), strings.size());
116     }
117 
118     static class ClassWithExpandableList<T> extends AbstractPagedListWrapper<T> {
119         List<T> items = null;
120         final ListWrapperCallback<T> cb;
121 
122         ClassWithExpandableList(List<T> wrappedList) {
123             super(wrappedList.size(), Integer.MAX_VALUE);
124             cb = ListWrapperCallBacks.ofList(wrappedList);
125         }
126 
127         @Override
128         public ListWrapperCallback<T> getPagingCallback() {
129             return cb;
130         }
131     }
132 
133     static class ClassWithInheritedExpandableList<T> extends ClassWithExpandableList<T> {
134 
135         ClassWithInheritedExpandableList(List<T> wrappedList) {
136             super(wrappedList);
137         }
138     }
139 
140     static Expandable expandable(final String value) {
141         return new Expandable() {
142             public String value() {
143                 return value;
144             }
145 
146             public Class<? extends Annotation> annotationType() {
147                 return Expandable.class;
148             }
149         };
150     }
151 }