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