1 package com.atlassian.plugins.rest.common.expand;
2
3 import com.atlassian.plugins.rest.common.expand.parameter.DefaultExpandParameter;
4 import com.atlassian.plugins.rest.common.expand.resolver.EntityExpanderResolver;
5 import com.google.common.collect.Lists;
6 import com.google.common.collect.Sets;
7 import static org.junit.Assert.*;
8 import org.junit.Before;
9 import org.junit.Test;
10
11 import javax.xml.bind.annotation.XmlAttribute;
12 import javax.xml.bind.annotation.XmlElement;
13 import java.lang.reflect.Field;
14 import java.util.Set;
15
16
17
18
19 public class EntityCrawlerTest
20 {
21 private EntityCrawler entityCrawler;
22
23 @Before
24 public void setUp()
25 {
26 entityCrawler = new EntityCrawler();
27 }
28
29 @Test
30 public void testGetExpandableWithNullField()
31 {
32 assertNull(entityCrawler.getExpandable(null));
33 }
34
35 @Test
36 public void testGetExpandableWithNoExpandableAnnotation() throws Exception
37 {
38 final Field field = SomeClass.class.getField("field1");
39 assertNull(entityCrawler.getExpandable(field));
40 }
41
42 @Test
43 public void testGetExpandableWithValuedExpandable() throws Exception
44 {
45 final Field field = SomeClass.class.getField("field2");
46 assertEquals("field2Value", entityCrawler.getExpandable(field).value());
47 }
48
49 @Test
50 public void testGetExpandableWithNamedXmlElement() throws Exception
51 {
52 final Field field = SomeClass.class.getField("field3");
53 assertEquals("field3Value", entityCrawler.getExpandable(field).value());
54 }
55
56 @Test
57 public void testGetExpandableWithUnNamedXmlElement() throws Exception
58 {
59 final Field field = SomeClass.class.getField("field4");
60 assertEquals("field4", entityCrawler.getExpandable(field).value());
61 }
62
63 @Test
64 public void testGetExpandableWithNoXmlElement() throws Exception
65 {
66 final Field field = SomeClass.class.getField("field5");
67 assertEquals("field5", entityCrawler.getExpandable(field).value());
68 }
69
70 @Test
71 public void testCrawlGetsFieldsFromSuperClass() throws Exception
72 {
73 final Set<String> expectedFields = Sets.newHashSet("field2Value", "field3Value", "field4", "field5");
74
75 entityCrawler.crawl(new SomeDerivedClass(), new DefaultExpandParameter(Lists.newArrayList("*")),
76 new EntityExpanderResolver()
77 {
78 public boolean hasExpander(Class<?> type)
79 {
80 return true;
81 }
82
83 public <T> EntityExpander<T> getExpander(Class<? extends T> type)
84 {
85 return new EntityExpander<T>()
86 {
87 public T expand(ExpandContext<T> tExpandContext, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler)
88 {
89 assertTrue(expectedFields.remove(tExpandContext.getExpandable().value()));
90 return null;
91 }
92 };
93 }
94 });
95
96 assertTrue(expectedFields.isEmpty());
97 }
98
99 private static class SomeClass
100 {
101 public Object field1 = new Object();
102
103 @Expandable ("field2Value")
104 public Object field2 = new Object();
105
106 @Expandable
107 @XmlElement (name = "field3Value")
108 public Object field3 = new Object();
109
110 @Expandable
111 @XmlElement
112 public Object field4 = new Object();
113
114 @Expandable
115 public Object field5 = new Object();
116
117 @Expandable
118 public Object ignoredField;
119 }
120
121 private static class SomeDerivedClass extends SomeClass
122 {
123 @XmlAttribute
124 private String expand;
125 }
126 }