|
| 1 | +package io.swagger; |
| 2 | + |
| 3 | +import com.openpojo.reflection.PojoClass; |
| 4 | +import com.openpojo.reflection.filters.FilterPackageInfo; |
| 5 | +import com.openpojo.reflection.impl.PojoClassFactory; |
| 6 | +import com.openpojo.validation.Validator; |
| 7 | +import com.openpojo.validation.ValidatorBuilder; |
| 8 | +import com.openpojo.validation.test.impl.GetterTester; |
| 9 | +import com.openpojo.validation.test.impl.SetterTester; |
| 10 | +import io.swagger.models.ComposedModel; |
| 11 | +import io.swagger.models.License; |
| 12 | +import io.swagger.models.ModelImpl; |
| 13 | +import io.swagger.models.Operation; |
| 14 | +import io.swagger.models.RefModel; |
| 15 | +import io.swagger.models.RefResponse; |
| 16 | +import io.swagger.models.Swagger; |
| 17 | +import io.swagger.models.Tag; |
| 18 | +import io.swagger.models.auth.ApiKeyAuthDefinition; |
| 19 | +import io.swagger.models.auth.In; |
| 20 | +import io.swagger.models.parameters.BodyParameter; |
| 21 | +import io.swagger.models.parameters.CookieParameter; |
| 22 | +import io.swagger.models.properties.ArrayProperty; |
| 23 | +import io.swagger.models.properties.BaseIntegerProperty; |
| 24 | +import io.swagger.models.properties.DateProperty; |
| 25 | +import io.swagger.models.properties.DateTimeProperty; |
| 26 | +import io.swagger.models.properties.DoubleProperty; |
| 27 | +import io.swagger.models.properties.FloatProperty; |
| 28 | +import io.swagger.models.properties.IntegerProperty; |
| 29 | +import io.swagger.models.properties.LongProperty; |
| 30 | +import io.swagger.models.properties.ObjectProperty; |
| 31 | +import io.swagger.models.properties.PropertyBuilder; |
| 32 | +import io.swagger.models.refs.RefFormat; |
| 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; |
| 46 | + |
| 47 | +/* |
| 48 | + * This class is written in order to test all the getters and setters in this module. |
| 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. |
| 50 | + */ |
| 51 | +@PrepareForTest({In.class, RefFormat.class, RefType.class}) |
| 52 | +public class PojosTest extends PowerMockTestCase { |
| 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 | + } |
| 130 | +} |
0 commit comments