View Javadoc

1   package com.atlassian.plugins.rest.doclet.generators.schema;
2   
3   import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.ArrayBean;
4   import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.DateTimeBean;
5   import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.DefinitionBean;
6   import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.InterfaceBean;
7   import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.NestedGenericType;
8   import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.RawBean;
9   import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.SimpleBean;
10  import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.SimpleListBean;
11  import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.StaticFieldsBean;
12  import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.SubclassedGeneric;
13  import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.TestAbstractBean;
14  import com.atlassian.plugins.rest.doclet.generators.schema.beans.artificial.UriBean;
15  import com.atlassian.plugins.rest.doclet.generators.schema.beans.attachment.AttachmentBean;
16  import com.atlassian.plugins.rest.doclet.generators.schema.beans.issue.IssueBean;
17  import com.atlassian.plugins.rest.doclet.generators.schema.beans.jira.PageBean;
18  import com.atlassian.plugins.rest.doclet.generators.schema.beans.permissions.PermissionsJsonBean;
19  import com.atlassian.plugins.rest.doclet.generators.schema.beans.permissionscheme.PermissionSchemeBean;
20  import com.atlassian.rest.annotation.RestProperty;
21  import org.codehaus.jackson.JsonNode;
22  import org.codehaus.jackson.map.ObjectMapper;
23  import org.junit.Test;
24  
25  import java.io.IOException;
26  import java.util.List;
27  import java.util.Map;
28  
29  import static com.atlassian.plugins.rest.doclet.generators.resourcedoc.JsonOperations.toJson;
30  import static org.hamcrest.Matchers.equalTo;
31  import static org.junit.Assert.assertThat;
32  
33  public class SchemaGeneratorTest
34  {
35      public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
36  
37      @Test
38      public void testRequestScopes()
39      {
40          testCase(PermissionSchemeBean.class, RestProperty.Scope.REQUEST, "permission-scheme-request.json");
41          testCase(PermissionSchemeBean.class, RestProperty.Scope.RESPONSE, "permission-scheme-response.json");
42      }
43  
44      @Test
45      public void testSuperclassFieldsEnumsAndPatternProperties()
46      {
47          testCase(PermissionsJsonBean.class, RestProperty.Scope.REQUEST, "permissions.json");
48      }
49  
50      @Test
51      public void testAdvancedGenericTypeResolution()
52      {
53          testCase(AttachmentBean.class, RestProperty.Scope.RESPONSE, "attachment.json");
54      }
55  
56      @Test
57      public void testSimpleGenericList()
58      {
59          testCase(SimpleListBean.class, RestProperty.Scope.RESPONSE, "simple-list.json");
60      }
61  
62      @Test
63      public void testNestedGenericTypes()
64      {
65          testCase(NestedGenericType.class, RestProperty.Scope.RESPONSE, "nested-generic-type.json");
66      }
67  
68      @Test
69      public void staticFieldsShouldBeIgnored()
70      {
71          testCase(StaticFieldsBean.class, RestProperty.Scope.REQUEST, "static-fields.json");
72      }
73  
74      @Test
75      public void fieldsFromInterfacesAreGathered()
76      {
77          testCase(InterfaceBean.class, RestProperty.Scope.REQUEST, "interface.json");
78      }
79  
80      @Test
81      public void datesHaveStringFormat()
82      {
83          testCase(DateTimeBean.class, RestProperty.Scope.REQUEST, "date-time.json");
84      }
85  
86      @Test
87      public void reusedBeansArePutInDefinitions()
88      {
89          testCase(DefinitionBean.class, RestProperty.Scope.REQUEST, "definition.json");
90      }
91  
92      @Test
93      public void testWrappingInList()
94      {
95          testCase(List.class, RestProperty.Scope.REQUEST, "simple-wrapped-in-list.json", SimpleBean.class);
96      }
97  
98      @Test
99      public void testWrappingInMap()
100     {
101         testCase(Map.class, RestProperty.Scope.REQUEST, "simple-wrapped-in-map.json", String.class, SimpleBean.class);
102     }
103 
104     @Test
105     public void testSubclassedGeneric()
106     {
107         testCase(SubclassedGeneric.class, RestProperty.Scope.REQUEST, "subclassed-generic.json");
108     }
109 
110     @Test
111     public void ultimateTest()
112     {
113         testCase(IssueBean.class, RestProperty.Scope.RESPONSE, "issue.json");
114     }
115 
116     @Test
117     public void testStringWrappedInList()
118     {
119         testCase(List.class, RestProperty.Scope.REQUEST, "list-of-strings.json", String.class);
120     }
121 
122     @Test
123     public void testArrays()
124     {
125         testCase(ArrayBean.class, RestProperty.Scope.REQUEST, "array.json");
126     }
127 
128     @Test
129     public void testWrappingInPageBean()
130     {
131         testCase(PageBean.class, RestProperty.Scope.REQUEST, "paged-simple.json", SimpleBean.class);
132     }
133 
134     @Test
135     public void testAbstractClasses()
136     {
137         testCase(TestAbstractBean.class, RestProperty.Scope.REQUEST, "test-abstract.json");
138     }
139 
140     @Test
141     public void testJsonRawValue()
142     {
143         testCase(RawBean.class, RestProperty.Scope.REQUEST, "raw.json");
144     }
145 
146     @Test
147     public void uriHasProperFormatSet()
148     {
149         testCase(UriBean.class, RestProperty.Scope.REQUEST, "uri.json");
150     }
151 
152     private void testCase(Class<?> bean, RestProperty.Scope scope, String fileWithExpectedValue, Class<?>... genericTypes)
153     {
154         RichClass model = RichClass.of(bean, genericTypes);
155         Schema schema = SchemaGenerator.generateSchema(model, scope);
156 
157         String actual = formatJson(toJson(schema));
158         String expected = getJsonFromFile(fileWithExpectedValue);
159 
160         assertThat(actual, equalTo(expected));
161     }
162 
163     private String getJsonFromFile(final String fileWithExpectedValue)
164     {
165         try
166         {
167             JsonNode jsonNode = OBJECT_MAPPER.readTree(getClass().getClassLoader().getResourceAsStream(fileWithExpectedValue));
168             return jsonNode.toString();
169         }
170         catch (IOException e)
171         {
172             return "";
173         }
174     }
175 
176     private String formatJson(String json)
177     {
178         try
179         {
180             return OBJECT_MAPPER.readTree(json).toString();
181         }
182         catch (IOException e)
183         {
184             throw new RuntimeException(e);
185         }
186     }
187 }