1   package com.atlassian.plugins.rest.module.expand.resolver;
2   
3   import com.atlassian.plugin.osgi.factory.OsgiPlugin;
4   import com.atlassian.plugins.rest.common.expand.EntityCrawler;
5   import com.atlassian.plugins.rest.common.expand.EntityExpander;
6   import com.atlassian.plugins.rest.common.expand.ExpandContext;
7   import com.atlassian.plugins.rest.common.expand.Expander;
8   import com.atlassian.plugins.rest.common.expand.resolver.EntityExpanderResolver;
9   import static org.junit.Assert.*;
10  import org.junit.Before;
11  import org.junit.Test;
12  import static org.mockito.Mockito.mock;
13  import static org.mockito.Mockito.when;
14  
15  /**
16   * Testing {@link OsgiPluginEntityExpanderResolver}
17   */
18  public class OsgiPluginEntityExpanderResolverTest
19  {
20      private OsgiPluginEntityExpanderResolver resolver;
21      private EntityExpander entityExpander;
22  
23      @Before
24      public void setUp() throws Exception
25      {
26          entityExpander = mock(AnnotatedClassEntityExpander.class);
27          OsgiPlugin osgiPlugin = mock(OsgiPlugin.class);
28          when(osgiPlugin.autowire(AnnotatedClassEntityExpander.class)).thenReturn((AnnotatedClassEntityExpander) entityExpander);
29  
30          resolver = new OsgiPluginEntityExpanderResolver(osgiPlugin);
31      }
32  
33      @Test
34      public void testConstructorWithNullPlugin()
35      {
36          try
37          {
38              new OsgiPluginEntityExpanderResolver(null);
39              fail();
40          }
41          catch (NullPointerException e)
42          {
43              // expected
44          }
45      }
46  
47      @Test
48      public void testHasExpanderWithNullClass()
49      {
50          try
51          {
52              final Class clazz = null;
53              resolver.hasExpander(clazz);
54              fail();
55          }
56          catch (NullPointerException e)
57          {
58              // expected
59          }
60      }
61  
62      @Test
63      public void testHasExpanderWithAnnotatedClass()
64      {
65          assertTrue(resolver.hasExpander(AnnotatedClass.class));
66      }
67  
68      @Test
69      public void testHasExpanderWithNonAnnotatedClass()
70      {
71          assertFalse(resolver.hasExpander(Object.class));
72      }
73  
74      @Test
75      public void testGetExpanderWithAnnotatedClass()
76      {
77          assertEquals(entityExpander, resolver.getExpander(AnnotatedClass.class));
78      }
79  
80      @Test
81      public void testGetExpanderWithNonAnnotatedClass()
82      {
83          assertNull(resolver.getExpander(Object.class));
84      }
85  
86      @Expander(AnnotatedClassEntityExpander.class)
87      private static class AnnotatedClass
88      {
89      }
90  
91      private static class AnnotatedClassEntityExpander implements EntityExpander<AnnotatedClass>
92      {
93          public AnnotatedClass expand(ExpandContext expandContext, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler)
94          {
95              return null;
96          }
97      }
98  }