View Javadoc

1   package com.atlassian.plugins.rest.common.expand.resolver;
2   
3   import com.atlassian.plugins.rest.common.expand.EntityExpander;
4   import com.google.common.collect.Lists;
5   
6   import static org.junit.Assert.*;
7   
8   import org.junit.Before;
9   import org.junit.Test;
10  
11  import static org.mockito.Mockito.*;
12  
13  import java.util.Collections;
14  
15  public class ChainingEntityExpanderResolverTest {
16      private ChainingEntityExpanderResolver resolver;
17      private EntityExpanderResolver resolverItem1;
18      private EntityExpanderResolver resolverItem2;
19  
20      @Before
21      public void setUp() throws Exception {
22          resolverItem1 = mock(EntityExpanderResolver.class);
23          resolverItem2 = mock(EntityExpanderResolver.class);
24  
25          resolver = new ChainingEntityExpanderResolver(Lists.newArrayList(resolverItem1, resolverItem2));
26      }
27  
28      @Test
29      public void testConstructorWithNullList() {
30          try {
31              new ChainingEntityExpanderResolver(null);
32              fail();
33          } catch (NullPointerException e) {
34              // expected
35          }
36      }
37  
38      @Test
39      public void testConstructorWithListWithNullItem() {
40          try {
41              new ChainingEntityExpanderResolver(Collections.<EntityExpanderResolver>singletonList(null));
42              fail();
43          } catch (NullPointerException e) {
44              // expected
45          }
46      }
47  
48      @Test
49      public void testHasExpanderWithNullClass() {
50          try {
51              final Class clazz = null;
52              resolver.hasExpander(clazz);
53              fail();
54          } catch (NullPointerException e) {
55              // expected
56          }
57      }
58  
59      @Test
60      public void testHasExpanderWithClassAndNoResolverHasExpander() {
61          when(resolverItem1.hasExpander(Object.class)).thenReturn(false);
62          when(resolverItem2.hasExpander(Object.class)).thenReturn(false);
63  
64          assertFalse(resolver.hasExpander(Object.class));
65      }
66  
67      @Test
68      public void testHasExpanderWithClassAndResolver2HasExpander() {
69          when(resolverItem1.hasExpander(Object.class)).thenReturn(false);
70          when(resolverItem2.hasExpander(Object.class)).thenReturn(true);
71  
72          assertTrue(resolver.hasExpander(Object.class));
73      }
74  
75      @Test
76      public void testHasExpanderWithClassAndResolver1HasExpander() {
77          when(resolverItem1.hasExpander(Object.class)).thenReturn(true);
78          verify(resolverItem2, never()).hasExpander(Object.class);
79  
80          assertTrue(resolver.hasExpander(Object.class));
81      }
82  
83      @Test
84      public void testGetExpanderWithClassAndNoResolverHasExpander() {
85          when(resolverItem1.getExpander(Object.class)).thenReturn(null);
86          when(resolverItem2.getExpander(Object.class)).thenReturn(null);
87  
88          assertNull(resolver.getExpander(Object.class));
89      }
90  
91      @Test
92      public void testGetExpanderWithClassAndResolver2HasExpander() {
93          final EntityExpander<Object> entityExpander = mock(EntityExpander.class);
94          when(resolverItem1.getExpander(Object.class)).thenReturn(null);
95          when(resolverItem2.getExpander(Object.class)).thenReturn(entityExpander);
96  
97          assertEquals(entityExpander, resolver.getExpander(Object.class));
98      }
99  
100     @Test
101     public void testGetExpanderWithClassAndResolver1HasExpander() {
102         final EntityExpander<Object> entityExpander = mock(EntityExpander.class);
103         when(resolverItem1.getExpander(Object.class)).thenReturn(entityExpander);
104         verify(resolverItem2, never()).getExpander(Object.class);
105 
106         assertEquals(entityExpander, resolver.getExpander(Object.class));
107     }
108 }