|
1 | 1 | package io.swagger; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | | -import java.util.Arrays; |
5 | | -import java.util.HashMap; |
6 | | -import java.util.HashSet; |
7 | | -import java.util.List; |
8 | | -import java.util.Map; |
9 | | -import java.util.Set; |
10 | | - |
11 | | -import org.powermock.core.classloader.annotations.PrepareForTest; |
12 | | -import org.powermock.modules.testng.PowerMockTestCase; |
13 | | -import org.testng.annotations.BeforeMethod; |
14 | | -import org.testng.annotations.Test; |
15 | | - |
16 | 3 | import com.openpojo.reflection.PojoClass; |
17 | 4 | import com.openpojo.reflection.filters.FilterPackageInfo; |
18 | | - |
19 | 5 | import com.openpojo.reflection.impl.PojoClassFactory; |
20 | 6 | import com.openpojo.validation.Validator; |
21 | 7 | import com.openpojo.validation.ValidatorBuilder; |
22 | 8 | import com.openpojo.validation.test.impl.GetterTester; |
23 | 9 | import com.openpojo.validation.test.impl.SetterTester; |
24 | | - |
25 | 10 | import io.swagger.models.ComposedModel; |
26 | 11 | import io.swagger.models.License; |
27 | 12 | import io.swagger.models.ModelImpl; |
|
46 | 31 | import io.swagger.models.properties.PropertyBuilder; |
47 | 32 | import io.swagger.models.refs.RefFormat; |
48 | 33 | import io.swagger.models.refs.RefType; |
| 34 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 35 | +import org.powermock.modules.testng.PowerMockTestCase; |
| 36 | +import org.testng.annotations.BeforeMethod; |
| 37 | +import org.testng.annotations.Test; |
| 38 | + |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.Arrays; |
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.HashSet; |
| 43 | +import java.util.List; |
| 44 | +import java.util.Map; |
| 45 | +import java.util.Set; |
49 | 46 |
|
50 | 47 | /* |
51 | 48 | * This class is written in order to test all the getters and setters in this module. |
52 | 49 | * In order to test these we just need to pass a list of classes for which the getter and setter tests should be run. |
53 | 50 | */ |
54 | | -@PrepareForTest({ In.class, RefFormat.class, RefType.class }) |
| 51 | +@PrepareForTest({In.class, RefFormat.class, RefType.class}) |
55 | 52 | public class PojosTest extends PowerMockTestCase { |
56 | | - private static final String[] POJO_PACKAGES = { "io.swagger.models", "io.swagger.models.auth", |
57 | | - "io.swagger.models.parameters", "io.swagger.models.properties", "io.swagger.models.refs" }; |
58 | | - |
59 | | - private ArrayList<PojoClass> pojoClasses; |
60 | | - |
61 | | - @BeforeMethod |
62 | | - public void setup() { |
63 | | - pojoClasses = new ArrayList<PojoClass>(); |
64 | | - for (String pojoPackage : POJO_PACKAGES) { |
65 | | - List<PojoClass> packagePojoClasses = PojoClassFactory.getPojoClasses(pojoPackage, new FilterPackageInfo()); |
66 | | - for (PojoClass clazz : packagePojoClasses) { |
67 | | - if (clazz.getName().contains("$") || clazz.isAbstract() || clazz.isInterface() || clazz.isEnum() |
68 | | - || clazz.getName().endsWith("Test")) |
69 | | - continue; |
70 | | - pojoClasses.add(clazz); |
71 | | - } |
72 | | - |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - @Test |
77 | | - public void testOpenPojo() { |
78 | | - Validator validator = ValidatorBuilder.create().with(new SetterTester()).with(new GetterTester()).build(); |
79 | | - for (PojoClass clazz : pojoClasses) { |
80 | | - try { |
81 | | - validator.validate(clazz); |
82 | | - } catch (AssertionError ex) { |
83 | | - continue; |
84 | | - } |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - @Test |
89 | | - public void testEqualsAndHashcodes() throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
90 | | - Map<Class<?>, Set<String>> classesExclusions = new HashMap<Class<?>, Set<String>>(); |
91 | | - |
92 | | - classesExclusions.put(BodyParameter.class, new HashSet<String>(Arrays.asList("examples"))); |
93 | | - classesExclusions.put(ComposedModel.class, new HashSet<String>(Arrays.asList("reference"))); |
94 | | - classesExclusions.put(DoubleProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
95 | | - classesExclusions.put(DateProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
96 | | - classesExclusions.put(DateTimeProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
97 | | - classesExclusions.put(FloatProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
98 | | - classesExclusions.put(IntegerProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
99 | | - classesExclusions.put(License.class, new HashSet<String>(Arrays.asList("vendorExtensions"))); |
100 | | - classesExclusions.put(LongProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
101 | | - classesExclusions.put(ModelImpl.class, new HashSet<String>(Arrays.asList("_enum"))); |
102 | | - classesExclusions.put(ObjectProperty.class, new HashSet<String>(Arrays.asList("properties"))); |
103 | | - classesExclusions.put(RefModel.class, new HashSet<String>(Arrays.asList("title"))); |
104 | | - classesExclusions.put(RefResponse.class, |
105 | | - new HashSet<String>(Arrays.asList("headers", "schema", "vendorExtensions"))); |
106 | | - classesExclusions.put(Swagger.class, new HashSet<String>(Arrays.asList("vendorExtensions", "responses"))); |
107 | | - classesExclusions.put(Tag.class, new HashSet<String>(Arrays.asList("vendorExtensions"))); |
108 | | - |
109 | | - Set<Class<?>> classesUsingInheritedFields = new HashSet<Class<?>>(Arrays.asList(ApiKeyAuthDefinition.class, |
110 | | - BodyParameter.class, ArrayProperty.class, BaseIntegerProperty.class, CookieParameter.class)); |
111 | | - Set<Class<?>> excludedClasses = new HashSet<Class<?>>(Arrays.asList(PropertyBuilder.class)); |
112 | | - for (PojoClass clazz : pojoClasses) { |
113 | | - if (excludedClasses.contains(clazz.getClazz())) |
114 | | - continue; |
115 | | - Set<String> exclusions = classesExclusions.get(clazz.getClazz()); |
116 | | - TestUtils.testEquals(clazz.getClazz(), exclusions, classesUsingInheritedFields.contains(clazz.getClazz())); |
117 | | - } |
118 | | - } |
119 | | - |
120 | | - @Test |
121 | | - public void testBuildersAndCommonMethods() throws Exception { |
122 | | - Map<Class<?>, Set<String>> classesExclusions = new HashMap<Class<?>, Set<String>>(); |
123 | | - |
124 | | - classesExclusions.put(Operation.class, new HashSet<String>(Arrays.asList("deprecated", "vendorExtensions"))); |
125 | | - classesExclusions.put(Swagger.class, new HashSet<String>(Arrays.asList("vendorExtensions"))); |
126 | | - |
127 | | - for (PojoClass clazz : pojoClasses) { |
128 | | - Set<String> exclusions = classesExclusions.get(clazz.getClazz()); |
129 | | - TestUtils.testBuilders(clazz.getClazz(), exclusions); |
130 | | - TestUtils.testCommonMethods(clazz.getClazz(), exclusions); |
131 | | - } |
132 | | - } |
| 53 | + private static final String[] POJO_PACKAGES = {"io.swagger.models", "io.swagger.models.auth", |
| 54 | + "io.swagger.models.parameters", "io.swagger.models.properties", "io.swagger.models.refs"}; |
| 55 | + |
| 56 | + private ArrayList<PojoClass> pojoClasses; |
| 57 | + |
| 58 | + @BeforeMethod |
| 59 | + public void setup() { |
| 60 | + pojoClasses = new ArrayList<PojoClass>(); |
| 61 | + for (String pojoPackage : POJO_PACKAGES) { |
| 62 | + List<PojoClass> packagePojoClasses = PojoClassFactory.getPojoClasses(pojoPackage, new FilterPackageInfo()); |
| 63 | + for (PojoClass clazz : packagePojoClasses) { |
| 64 | + if (clazz.getName().contains("$") || clazz.isAbstract() || clazz.isInterface() || clazz.isEnum() |
| 65 | + || clazz.getName().endsWith("Test")) |
| 66 | + continue; |
| 67 | + pojoClasses.add(clazz); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testOpenPojo() { |
| 75 | + Validator validator = ValidatorBuilder.create().with(new SetterTester()).with(new GetterTester()).build(); |
| 76 | + for (PojoClass clazz : pojoClasses) { |
| 77 | + try { |
| 78 | + validator.validate(clazz); |
| 79 | + } catch (AssertionError ex) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testEqualsAndHashcodes() throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
| 87 | + Map<Class<?>, Set<String>> classesExclusions = new HashMap<Class<?>, Set<String>>(); |
| 88 | + |
| 89 | + classesExclusions.put(BodyParameter.class, new HashSet<String>(Arrays.asList("examples"))); |
| 90 | + classesExclusions.put(ComposedModel.class, new HashSet<String>(Arrays.asList("reference"))); |
| 91 | + classesExclusions.put(DoubleProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
| 92 | + classesExclusions.put(DateProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
| 93 | + classesExclusions.put(DateTimeProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
| 94 | + classesExclusions.put(FloatProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
| 95 | + classesExclusions.put(IntegerProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
| 96 | + classesExclusions.put(License.class, new HashSet<String>(Arrays.asList("vendorExtensions"))); |
| 97 | + classesExclusions.put(LongProperty.class, new HashSet<String>(Arrays.asList("_enum"))); |
| 98 | + classesExclusions.put(ModelImpl.class, new HashSet<String>(Arrays.asList("_enum"))); |
| 99 | + classesExclusions.put(ObjectProperty.class, new HashSet<String>(Arrays.asList("properties"))); |
| 100 | + classesExclusions.put(RefModel.class, new HashSet<String>(Arrays.asList("title"))); |
| 101 | + classesExclusions.put(RefResponse.class, |
| 102 | + new HashSet<String>(Arrays.asList("headers", "schema", "vendorExtensions"))); |
| 103 | + classesExclusions.put(Swagger.class, new HashSet<String>(Arrays.asList("vendorExtensions", "responses"))); |
| 104 | + classesExclusions.put(Tag.class, new HashSet<String>(Arrays.asList("vendorExtensions"))); |
| 105 | + |
| 106 | + Set<Class<?>> classesUsingInheritedFields = new HashSet<Class<?>>(Arrays.asList(ApiKeyAuthDefinition.class, |
| 107 | + BodyParameter.class, ArrayProperty.class, BaseIntegerProperty.class, CookieParameter.class)); |
| 108 | + Set<Class<?>> excludedClasses = new HashSet<Class<?>>(Arrays.asList(PropertyBuilder.class)); |
| 109 | + for (PojoClass clazz : pojoClasses) { |
| 110 | + if (excludedClasses.contains(clazz.getClazz())) |
| 111 | + continue; |
| 112 | + Set<String> exclusions = classesExclusions.get(clazz.getClazz()); |
| 113 | + TestUtils.testEquals(clazz.getClazz(), exclusions, classesUsingInheritedFields.contains(clazz.getClazz())); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testBuildersAndCommonMethods() throws Exception { |
| 119 | + Map<Class<?>, Set<String>> classesExclusions = new HashMap<Class<?>, Set<String>>(); |
| 120 | + |
| 121 | + classesExclusions.put(Operation.class, new HashSet<String>(Arrays.asList("deprecated", "vendorExtensions"))); |
| 122 | + classesExclusions.put(Swagger.class, new HashSet<String>(Arrays.asList("vendorExtensions"))); |
| 123 | + |
| 124 | + for (PojoClass clazz : pojoClasses) { |
| 125 | + Set<String> exclusions = classesExclusions.get(clazz.getClazz()); |
| 126 | + TestUtils.testBuilders(clazz.getClazz(), exclusions); |
| 127 | + TestUtils.testCommonMethods(clazz.getClazz(), exclusions); |
| 128 | + } |
| 129 | + } |
133 | 130 | } |
0 commit comments