Skip to content

Commit 43c7f2a

Browse files
committed
Merge pull request #1585 from DevFactory/df-unit-tests-swagger-models
Unit tests for swagger-models module to increase code coverage from 15% to 99%
2 parents 6fc3a8a + 262f8db commit 43c7f2a

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

+3506
-48
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: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)