Skip to content

Commit b3adfb9

Browse files
author
Michel Chomnoue
committed
Add unit tests for swagger models module
1 parent 1352dba commit b3adfb9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3623
-126
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Code Coverage Report generation
2+
3+
To generate the code coverage report, execute the following command:
4+
> mvn clean verify
5+
6+
This will generate code coverage report in each of the modules. In order to view the same, open the following file in your browser.
7+
> target/site/jacoco/index.html

modules/swagger-models/pom.xml

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
1+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
34
<parent>
45
<groupId>io.swagger</groupId>
56
<artifactId>swagger-project</artifactId>
@@ -68,6 +69,53 @@
6869
<artifactId>testng</artifactId>
6970
<scope>test</scope>
7071
</dependency>
72+
<dependency>
73+
<groupId>com.openpojo</groupId>
74+
<artifactId>openpojo</artifactId>
75+
<version>0.8.0</version>
76+
<scope>test</scope>
77+
<exclusions>
78+
<exclusion>
79+
<groupId>junit</groupId>
80+
<artifactId>junit</artifactId>
81+
</exclusion>
82+
</exclusions>
83+
</dependency>
84+
<dependency>
85+
<groupId>org.powermock</groupId>
86+
<artifactId>powermock-module-testng</artifactId>
87+
<version>${powermock.version}</version>
88+
<scope>test</scope>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.powermock</groupId>
92+
<artifactId>powermock-api-mockito</artifactId>
93+
<version>${powermock.version}</version>
94+
<exclusions>
95+
<exclusion>
96+
<groupId>junit</groupId>
97+
<artifactId>junit</artifactId>
98+
</exclusion>
99+
<exclusion>
100+
<groupId>org.hamcrest</groupId>
101+
<artifactId>hamcrest-core</artifactId>
102+
</exclusion>
103+
</exclusions>
104+
<scope>test</scope>
105+
</dependency>
106+
<dependency>
107+
<groupId>org.mockito</groupId>
108+
<artifactId>mockito-core</artifactId>
109+
<version>${mockito-version}</version>
110+
<scope>test</scope>
111+
</dependency>
112+
<dependency>
113+
<groupId>asm</groupId>
114+
<artifactId>asm</artifactId>
115+
<version>3.3.1</version>
116+
<scope>test</scope>
117+
</dependency>
118+
71119
</dependencies>
72120
<properties>
73121
<!-- TODO increase coverage -->
@@ -76,5 +124,7 @@
76124
<coverage.line.minimum>0.0</coverage.line.minimum>
77125
<!-- Setting this to a really high number since this module has almost no coverage -->
78126
<coverage.missed.classes>41</coverage.missed.classes>
127+
<powermock.version>1.6.4</powermock.version>
128+
<mockito-version>1.10.19</mockito-version>
79129
</properties>
80130
</project>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package io.swagger;
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+
import com.openpojo.reflection.PojoClass;
17+
import com.openpojo.reflection.filters.FilterPackageInfo;
18+
19+
import com.openpojo.reflection.impl.PojoClassFactory;
20+
import com.openpojo.validation.Validator;
21+
import com.openpojo.validation.ValidatorBuilder;
22+
import com.openpojo.validation.test.impl.GetterTester;
23+
import com.openpojo.validation.test.impl.SetterTester;
24+
25+
import io.swagger.models.ComposedModel;
26+
import io.swagger.models.License;
27+
import io.swagger.models.ModelImpl;
28+
import io.swagger.models.Operation;
29+
import io.swagger.models.RefModel;
30+
import io.swagger.models.RefResponse;
31+
import io.swagger.models.Swagger;
32+
import io.swagger.models.Tag;
33+
import io.swagger.models.auth.ApiKeyAuthDefinition;
34+
import io.swagger.models.auth.In;
35+
import io.swagger.models.parameters.BodyParameter;
36+
import io.swagger.models.parameters.CookieParameter;
37+
import io.swagger.models.properties.ArrayProperty;
38+
import io.swagger.models.properties.BaseIntegerProperty;
39+
import io.swagger.models.properties.DateProperty;
40+
import io.swagger.models.properties.DateTimeProperty;
41+
import io.swagger.models.properties.DoubleProperty;
42+
import io.swagger.models.properties.FloatProperty;
43+
import io.swagger.models.properties.IntegerProperty;
44+
import io.swagger.models.properties.LongProperty;
45+
import io.swagger.models.properties.ObjectProperty;
46+
import io.swagger.models.properties.PropertyBuilder;
47+
import io.swagger.models.refs.RefFormat;
48+
import io.swagger.models.refs.RefType;
49+
50+
/*
51+
* This class is written in order to test all the getters and setters in this module.
52+
* 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+
*/
54+
@PrepareForTest({ In.class, RefFormat.class, RefType.class })
55+
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+
}
133+
}

0 commit comments

Comments
 (0)