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