View Javadoc

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