Skip to content

Commit 4199191

Browse files
committed
more tests
1 parent e97de79 commit 4199191

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

omod-2.0/src/test/java/org/openmrs/module/webservices/rest/doc/SwaggerSpecificationCreatorTest.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.swagger.models.properties.RefProperty;
2626
import io.swagger.models.properties.StringProperty;
2727
import io.swagger.util.Json;
28+
import org.dbunit.database.DatabaseConnection;
2829
import org.junit.Assert;
2930
import org.junit.Before;
3031
import org.junit.Test;
@@ -45,25 +46,60 @@
4546
import java.lang.reflect.Field;
4647
import java.lang.reflect.InvocationTargetException;
4748
import java.lang.reflect.Method;
49+
import java.sql.Connection;
50+
import java.sql.DatabaseMetaData;
51+
import java.sql.ResultSet;
4852
import java.util.ArrayList;
53+
import java.util.Arrays;
54+
import java.util.HashMap;
55+
import java.util.HashSet;
4956
import java.util.List;
5057
import java.util.Map;
58+
import java.util.Set;
5159

5260
import static junit.framework.TestCase.assertTrue;
5361
import static org.junit.Assert.assertEquals;
5462
import static org.junit.Assert.assertFalse;
5563
import static org.junit.Assert.assertNotNull;
5664

5765
public class SwaggerSpecificationCreatorTest extends BaseModuleWebContextSensitiveTest {
66+
5867
private SwaggerSpecificationCreator swaggerCreator;
5968

69+
Map<String, Integer> beforeCounts;
70+
6071
@Before
61-
public void setUp() {
72+
public void setUp() throws Exception {
6273
Context.getService(RestService.class).initialize();
6374
Context.getAdministrationService().saveGlobalProperty(
6475
new GlobalProperty(RestConstants.SWAGGER_QUIET_DOCS_GLOBAL_PROPERTY_NAME, "true"));
6576
Context.flushSession();
6677
swaggerCreator = new SwaggerSpecificationCreator();
78+
beforeCounts = getRowCounts();
79+
}
80+
81+
@Test
82+
public void checkNoDatabaseChanges() throws Exception {
83+
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
84+
ssc.getJSON();
85+
86+
Map<String, Integer> afterCounts = getRowCounts();
87+
88+
Assert.assertEquals("Ensure no tables are created or destroyed", beforeCounts.size(), afterCounts.size());
89+
Assert.assertTrue("Ensure that no data was added or removed from any tables",
90+
ensureCountsEqual(beforeCounts, afterCounts));
91+
}
92+
93+
private boolean ensureCountsEqual(Map<String, Integer> beforeCounts, Map<String, Integer> afterCounts) {
94+
for (String key : beforeCounts.keySet()) {
95+
if (beforeCounts.get(key) != afterCounts.get(key)) {
96+
System.err.println("The " + key + " table has a different number of rows (" + beforeCounts.get(key)
97+
+ " before, " + afterCounts.get(key) + " after).");
98+
99+
return false;
100+
}
101+
}
102+
return true;
67103
}
68104

69105
@Test
@@ -233,4 +269,20 @@ public void generateGETModelPatient_shouldReturnAnArrayPropertyWithRefPropertyWh
233269
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
234270
assertEquals("#/definitions/PatientIdentifierGet", refProperty.get$ref());
235271
}
272+
273+
public Map<String, Integer> getRowCounts() throws Exception {
274+
Map<String, Integer> ret = new HashMap<>();
275+
276+
Connection con = this.getConnection();
277+
DatabaseMetaData metaData = con.getMetaData();
278+
DatabaseConnection databaseConnection = new DatabaseConnection(con);
279+
280+
ResultSet rs = metaData.getTables(null, "PUBLIC", "%", null);
281+
while (rs.next()) {
282+
String tableName = rs.getString(3);
283+
284+
ret.put(tableName, databaseConnection.getRowCount(tableName));
285+
}
286+
return ret;
287+
}
236288
}

0 commit comments

Comments
 (0)