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