|
25 | 25 | import io.swagger.models.properties.RefProperty; |
26 | 26 | import io.swagger.models.properties.StringProperty; |
27 | 27 | import io.swagger.util.Json; |
| 28 | +import org.dbunit.database.DatabaseConnection; |
28 | 29 | import org.junit.Assert; |
29 | 30 | import org.junit.Before; |
30 | 31 | import org.junit.Test; |
|
45 | 46 | import java.lang.reflect.Field; |
46 | 47 | import java.lang.reflect.InvocationTargetException; |
47 | 48 | import java.lang.reflect.Method; |
| 49 | +import java.sql.Connection; |
| 50 | +import java.sql.DatabaseMetaData; |
| 51 | +import java.sql.ResultSet; |
48 | 52 | import java.util.ArrayList; |
| 53 | +import java.util.Arrays; |
| 54 | +import java.util.HashMap; |
| 55 | +import java.util.HashSet; |
49 | 56 | import java.util.List; |
50 | 57 | import java.util.Map; |
| 58 | +import java.util.Set; |
51 | 59 |
|
52 | 60 | import static junit.framework.TestCase.assertTrue; |
53 | 61 | import static org.junit.Assert.assertEquals; |
54 | 62 | import static org.junit.Assert.assertFalse; |
55 | 63 | import static org.junit.Assert.assertNotNull; |
56 | 64 |
|
57 | 65 | public class SwaggerSpecificationCreatorTest extends BaseModuleWebContextSensitiveTest { |
| 66 | + |
58 | 67 | private SwaggerSpecificationCreator swaggerCreator; |
59 | 68 |
|
| 69 | + Map<String, Integer> beforeCounts; |
| 70 | + |
60 | 71 | @Before |
61 | | - public void setUp() { |
| 72 | + public void setUp() throws Exception { |
62 | 73 | Context.getService(RestService.class).initialize(); |
63 | 74 | Context.getAdministrationService().saveGlobalProperty( |
64 | 75 | new GlobalProperty(RestConstants.SWAGGER_QUIET_DOCS_GLOBAL_PROPERTY_NAME, "true")); |
65 | 76 | Context.flushSession(); |
66 | 77 | 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; |
67 | 103 | } |
68 | 104 |
|
69 | 105 | @Test |
@@ -233,4 +269,20 @@ public void generateGETModelPatient_shouldReturnAnArrayPropertyWithRefPropertyWh |
233 | 269 | RefProperty refProperty = (RefProperty) arrayProperty.getItems(); |
234 | 270 | assertEquals("#/definitions/PatientIdentifierGet", refProperty.get$ref()); |
235 | 271 | } |
| 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 | + } |
236 | 288 | } |
0 commit comments