diff options
author | Feideus <erwan.ulrich@gmail.com> | 2018-05-24 17:14:06 +0200 |
---|---|---|
committer | Feideus <erwan.ulrich@gmail.com> | 2018-05-24 17:14:06 +0200 |
commit | c9d6584c23738eb438176a1046308e516a03309f (patch) | |
tree | 30228d6ef016b2c77ca5c2cc75204f1a0c4ca2fe /src | |
parent | fe3178b32f62958b03bc67638a9d631095674239 (diff) | |
download | schemafuzz-c9d6584c23738eb438176a1046308e516a03309f.tar.gz schemafuzz-c9d6584c23738eb438176a1046308e516a03309f.tar.bz2 schemafuzz-c9d6584c23738eb438176a1046308e516a03309f.zip |
Inject,Undo and Compare tests implemented and passing.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/schemaspy/DBFuzzer.java | 6 | ||||
-rw-r--r-- | src/main/java/org/schemaspy/model/GenericTreeNode.java | 41 | ||||
-rw-r--r-- | src/main/java/org/schemaspy/model/Row.java | 18 | ||||
-rw-r--r-- | src/main/java/org/schemaspy/model/SingleChange.java | 5 | ||||
-rw-r--r-- | src/main/java/org/schemaspy/model/Table.java | 4 | ||||
-rw-r--r-- | src/main/java/org/schemaspy/model/TableColumn.java | 3 | ||||
-rw-r--r-- | src/test/java/org/schemaspy/model/GenericTreeNodeTest.java | 166 |
7 files changed, 199 insertions, 44 deletions
diff --git a/src/main/java/org/schemaspy/DBFuzzer.java b/src/main/java/org/schemaspy/DBFuzzer.java index da6bfea..63a7842 100644 --- a/src/main/java/org/schemaspy/DBFuzzer.java +++ b/src/main/java/org/schemaspy/DBFuzzer.java @@ -36,7 +36,7 @@ public class DBFuzzer { if(rootMutation.getChosenChange() != null) { - resQuery = rootMutation.inject(analyzer,false); + resQuery = rootMutation.inject(analyzer.getSqlService(),analyzer.getDb(),false); if(resQuery) { LOGGER.info("GenericTreeNode was sucessfull"); @@ -127,7 +127,7 @@ public class DBFuzzer { if(currentMutation.getChosenChange() != null) { - resQuery = currentMutation.inject(analyzer,false); + resQuery = currentMutation.inject(analyzer.getSqlService(),analyzer.getDb(),false); if(resQuery) { LOGGER.info("GenericTreeNode was sucessfull"); @@ -323,7 +323,7 @@ public class DBFuzzer else if (markingDiff == 0 || markingDiff < 0) { SingleChange tmp = mutationTree.getRoot().singleChangeBasedOnWeight(); - nextMut = new GenericTreeNode(tmp.getattachedToMutation().getPost_change_row(), nextId(), mutationTree.getRoot(), tmp.getattachedToMutation()); + nextMut = new GenericTreeNode(tmp.getAttachedToMutation().getPost_change_row(), nextId(), mutationTree.getRoot(), tmp.getAttachedToMutation()); nextMut.setChosenChange(tmp); nextMut.initPostChangeRow(); } diff --git a/src/main/java/org/schemaspy/model/GenericTreeNode.java b/src/main/java/org/schemaspy/model/GenericTreeNode.java index bf65f0e..0e8adae 100644 --- a/src/main/java/org/schemaspy/model/GenericTreeNode.java +++ b/src/main/java/org/schemaspy/model/GenericTreeNode.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; import org.schemaspy.*; +import org.schemaspy.service.SqlService; public class GenericTreeNode { @@ -28,7 +29,7 @@ public class GenericTreeNode { /** * Default GenericTreeNode constructor */ - public GenericTreeNode(Row initial_state_row, int id) { // used only for rootMutation + public GenericTreeNode(Row initial_state_row, int id) { // used only for rootMutation and Tests this.cascadingFK = false; this.subTreeWeight = 0; this.parent = null; @@ -39,6 +40,18 @@ public class GenericTreeNode { this.potential_changes = discoverMutationPossibilities(this); } + public GenericTreeNode(Row initial_state_row,int id, SingleChange sg) { // used only for Tests + this.cascadingFK = false; + this.subTreeWeight = 0; + this.parent = null; + this.weight = 1; + this.depth = 0; + this.id = id; + this.initial_state_row = initial_state_row; + this.chosenChange = sg; + } + + public GenericTreeNode(Row initial_state_row, int id, GenericTreeNode rootMutation, GenericTreeNode parentMutation) { this.parent = parentMutation; this.cascadingFK = false; @@ -226,8 +239,6 @@ public class GenericTreeNode { ArrayList<SingleChange> oneChange = new ArrayList<SingleChange>(); - System.out.println(tableColumn); - String typeName = tableColumn.getTypeName(); @@ -282,7 +293,7 @@ public class GenericTreeNode { return oneChange; } - public boolean inject(SchemaAnalyzer analyzer, boolean undo) + public boolean inject(SqlService sqlService,Database db, boolean undo) { if (undo) @@ -293,7 +304,7 @@ public class GenericTreeNode { String theQuery = updateQueryBuilder(undo); try { - PreparedStatement stmt = analyzer.getSqlService().prepareStatement(theQuery, analyzer.getDb(), null); + PreparedStatement stmt = sqlService.prepareStatement(theQuery, db, null); stmt.execute(); return true; } @@ -310,11 +321,11 @@ public class GenericTreeNode { this.post_change_row.setValueOfColumn(chosenChange.getParentTableColumn().getName(), chosenChange.getNewValue()); } - public boolean undo(SchemaAnalyzer analyzer) + public boolean undo(SqlService sqlService,Database db) { try { - return this.inject(analyzer, true); + return this.inject(sqlService,db, true); } catch(Exception e) { @@ -423,14 +434,13 @@ public class GenericTreeNode { public boolean compare(GenericTreeNode genericTreeNode) { - boolean res = false; if (this.getId() == genericTreeNode.getId()) - res = true; + return true; if (this.initial_state_row.compare(genericTreeNode.getInitial_state_row()) && this.chosenChange.compare(genericTreeNode.getChosenChange())) - res = true; + return true; - return res; + return false; } public boolean undoToMutation(GenericTreeNode target, SchemaAnalyzer analyzer) @@ -440,12 +450,12 @@ public class GenericTreeNode { for(GenericTreeNode node : goingUp ) { - node.undo(analyzer); + node.undo(analyzer.getSqlService(),analyzer.getDb()); } for(GenericTreeNode node : goingDown ) { - node.inject(analyzer, false); + node.inject(analyzer.getSqlService(),analyzer.getDb(), false); } return true; @@ -469,11 +479,6 @@ public class GenericTreeNode { public void setChildren(ArrayList<GenericTreeNode> children) { - for (GenericTreeNode child : children) - { - child.parent = this; - } - this.children = children; } diff --git a/src/main/java/org/schemaspy/model/Row.java b/src/main/java/org/schemaspy/model/Row.java index f05b628..59890e3 100644 --- a/src/main/java/org/schemaspy/model/Row.java +++ b/src/main/java/org/schemaspy/model/Row.java @@ -40,11 +40,12 @@ public class Row this.content = new HashMap<String,String>(); } - public Row(Table parentTable, HashMap<String,String> content, Integer nbKeys) { - this.parentTable = parentTable; - this.content = new HashMap<String,String>(); - this.content = content; - this.nbKeys = nbKeys; + public Row(Table parentTable, HashMap<String,String> content, Integer nbKeys) + { + this.parentTable = parentTable; + this.content = new HashMap<String,String>(); + this.content = content; + this.nbKeys = nbKeys; } public Table getParentTable() @@ -95,12 +96,9 @@ public class Row { if(!initial_state_row.getContent().containsKey(entry.getKey())) return false; - else - { - if(!initial_state_row.getContent().get(entry.getKey()).equals(entry.getValue())) - return false; - } + if(!initial_state_row.getContent().get(entry.getKey()).equals(entry.getValue())) + return false; } return true; } diff --git a/src/main/java/org/schemaspy/model/SingleChange.java b/src/main/java/org/schemaspy/model/SingleChange.java index 460eba1..1c6e0ff 100644 --- a/src/main/java/org/schemaspy/model/SingleChange.java +++ b/src/main/java/org/schemaspy/model/SingleChange.java @@ -42,7 +42,7 @@ public class SingleChange @Override public String toString() { - return "\n[SG - attachedToMutation : "+this.getattachedToMutation().getId()+"| OV :"+oldValue+" | NV :"+newValue+" ]\n"; + return "\n[SG - attachedToMutation : "+this.getAttachedToMutation().getId()+"| OV :"+oldValue+" | NV :"+newValue+" ]\n"; } public String getOldValue() @@ -62,6 +62,7 @@ public class SingleChange public boolean compare(SingleChange chosenChange) { + if(!chosenChange.getParentTableColumn().getTable().getName().equals(this.getParentTableColumn().getTable().getName())) return false; @@ -74,7 +75,7 @@ public class SingleChange return true; } - public GenericTreeNode getattachedToMutation() + public GenericTreeNode getAttachedToMutation() { return this.attachedToMutation; } diff --git a/src/main/java/org/schemaspy/model/Table.java b/src/main/java/org/schemaspy/model/Table.java index b44424b..774bb48 100644 --- a/src/main/java/org/schemaspy/model/Table.java +++ b/src/main/java/org/schemaspy/model/Table.java @@ -76,7 +76,7 @@ public class Table implements Comparable<Table> { setComments(comments); } - public Table(String name, CaseInsensitiveMap<TableColumn> tableColumns) // Test Purposes + public Table(String name) // Test Purposes { this.catalog = null; this.schema = null; @@ -84,8 +84,6 @@ public class Table implements Comparable<Table> { this.container = null; this.db = null; this.name = name; - this.columns = tableColumns; - System.out.println(columns); } /** diff --git a/src/main/java/org/schemaspy/model/TableColumn.java b/src/main/java/org/schemaspy/model/TableColumn.java index a13a061..ebd96da 100644 --- a/src/main/java/org/schemaspy/model/TableColumn.java +++ b/src/main/java/org/schemaspy/model/TableColumn.java @@ -57,10 +57,11 @@ public class TableColumn { } - public TableColumn(String tableColumnName, String typeName) //Test purposes + public TableColumn(String tableColumnName, String typeName, String parentTableName) //Test purposes { this.name = tableColumnName; this.typeName = typeName; + this.table = new Table(parentTableName); } /** * Create a column associated with a table. diff --git a/src/test/java/org/schemaspy/model/GenericTreeNodeTest.java b/src/test/java/org/schemaspy/model/GenericTreeNodeTest.java index 42dc0de..9e329c1 100644 --- a/src/test/java/org/schemaspy/model/GenericTreeNodeTest.java +++ b/src/test/java/org/schemaspy/model/GenericTreeNodeTest.java @@ -2,20 +2,44 @@ package org.schemaspy.model; import nl.jqno.equalsverifier.internal.exceptions.AssertionException; import org.junit.*; +import org.junit.rules.ExternalResource; +import org.junit.runner.RunWith; +import org.mockito.Mock; import org.schemaspy.Config; +import org.schemaspy.cli.CommandLineArguments; +import org.schemaspy.service.DatabaseService; import org.schemaspy.service.SqlService; import org.schemaspy.util.CaseInsensitiveMap; import org.springframework.beans.factory.annotation.Autowired; - +import org.junit.Test; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit4.SpringRunner; import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.util.ArrayList; import java.util.HashMap; import java.util.Random; + +@RunWith(SpringRunner.class) +@SpringBootTest public class GenericTreeNodeTest { @Autowired private SqlService sqlService; + @Autowired + private DatabaseService databaseService; + private Database database; + @Mock + private ProgressListener progressListener; + @MockBean + private CommandLineArguments arguments; + @MockBean + private CommandLineRunner commandLineRunner; + @Test @@ -85,7 +109,7 @@ public class GenericTreeNodeTest { gtn1.setChosenChange(sg1); - Assert.assertEquals("Testing singleChange Attached Mutation consistency",gtn1.getChosenChange().getattachedToMutation().getId(),gtn1.getId()); + Assert.assertEquals("Testing singleChange Attached Mutation consistency",gtn1.getChosenChange().getAttachedToMutation().getId(),gtn1.getId()); } @@ -117,21 +141,149 @@ public class GenericTreeNodeTest { // Assert.assertFalse("No null in a node possibilities",gtn1.getPotential_changes().contains("null")); // } + @Test + public void NoNullMutationPossibilitiesTest() throws Exception + { + + String[] args = { + "-t", "pgsql", + "-db","sample_database2", + "-hostOptionalPort","127.0.0.1", + "-o", "target/integrationtesting/databaseServiceIT", + "-dp","postgresql-42.2.2.jar", + "-u", "feideus", + "-p", "feideus" + }; + + Config config = new Config(args); + DatabaseMetaData databaseMetaData = sqlService.connect(config); + String schema = sqlService.getConnection().getSchema(); + String catalog = sqlService.getConnection().getCatalog(); + database = new Database( + databaseMetaData, + "DatabaseServiceIT", + catalog, + schema, + null + ); + databaseService.gatheringSchemaDetails(config, database, progressListener); + + PreparedStatement stmt = sqlService.prepareStatement("SELECT * FROM test_table", database, null); + ResultSet rs = stmt.executeQuery(); + QueryResponseParser parser = new QueryResponseParser(); + + QueryResponse response = parser.parse(rs,database.getTablesMap().get("test_table")); + GenericTreeNode tmpMutation = new GenericTreeNode(response.getRows().get(0),1); + Assert.assertFalse(tmpMutation.discoverMutationPossibilities(tmpMutation).contains("null")); + + } + + @Test + public void injectAndUndoConsistencyTest() throws Exception + { + String[] args = { + "-t", "pgsql", + "-db","sample_database2", + "-hostOptionalPort","127.0.0.1", + "-o", "target/integrationtesting/databaseServiceIT", + "-dp","postgresql-42.2.2.jar", + "-u", "feideus", + "-p", "feideus" + }; + + Config config = new Config(args); + DatabaseMetaData databaseMetaData = sqlService.connect(config); + String schema = sqlService.getConnection().getSchema(); + String catalog = sqlService.getConnection().getCatalog(); + Database database = new Database( + databaseMetaData, + "DatabaseServiceIT", + catalog, + schema, + null + ); + databaseService.gatheringSchemaDetails(config, database, progressListener); + + + PreparedStatement stmt = sqlService.prepareStatement("SELECT * FROM test_table", database, null); + ResultSet rs = stmt.executeQuery(); + QueryResponseParser parser = new QueryResponseParser(); + QueryResponse response = parser.parse(rs,database.getTablesMap().get("test_table")); + + Row row = response.getRows().get(0); + GenericTreeNode tmpMutation = new GenericTreeNode(row,1); + tmpMutation.setChosenChange(tmpMutation.getPotential_changes().get(0)); + tmpMutation.initPostChangeRow(); + + + Assert.assertTrue(tmpMutation.inject(sqlService,database,false)); //Test + + rs = stmt.executeQuery(); + response = parser.parse(rs,database.getTablesMap().get("test_table")); + + Assert.assertTrue(response.getRows().get(0).compare(tmpMutation.getPost_change_row())); + + Assert.assertTrue(tmpMutation.undo(sqlService,database)); //Test + + rs = stmt.executeQuery(); + response = parser.parse(rs,database.getTablesMap().get("test_table")); + + Assert.assertTrue(response.getRows().get(0).compare(tmpMutation.getInitial_state_row())); + + } @Test - public void discoverMutationPossibilitiesTest() throws Exception + public void compareTest() throws Exception { String[] args = { - "-t", "src/test/resources/integrationTesting/dbTypes/h2memory", - "-db", "sample_database2", - "-s", "DATABASESERVICEIT", + "-t", "pgsql", + "-db","sample_database2", + "-hostOptionalPort","127.0.0.1", "-o", "target/integrationtesting/databaseServiceIT", - "-u", "feideus" + "-dp","postgresql-42.2.2.jar", + "-u", "feideus", "-p", "feideus" }; Config config = new Config(args); DatabaseMetaData databaseMetaData = sqlService.connect(config); + String schema = sqlService.getConnection().getSchema(); + String catalog = sqlService.getConnection().getCatalog(); + Database database = new Database( + databaseMetaData, + "DatabaseServiceIT", + catalog, + schema, + null + ); + databaseService.gatheringSchemaDetails(config, database, progressListener); + + + PreparedStatement stmt = sqlService.prepareStatement("SELECT * FROM test_table", database, null); + ResultSet rs = stmt.executeQuery(); + QueryResponseParser parser = new QueryResponseParser(); + QueryResponse response = parser.parse(rs,database.getTablesMap().get("test_table")); + + Row row = response.getRows().get(0); + Row row2 = row.clone(); + + GenericTreeNode tmpMutation = new GenericTreeNode(row,1); + tmpMutation.setChosenChange(tmpMutation.getPotential_changes().get(0)); + + GenericTreeNode tmpMutation2 = new GenericTreeNode(row2,2); + tmpMutation2.setChosenChange(tmpMutation.getPotential_changes().get(0)); // taking potential change fron mut1 just to be sure + + + Assert.assertTrue(tmpMutation.compare(tmpMutation2)); + + tmpMutation.getInitial_state_row().getContent().replace("id","-20"); + + Assert.assertFalse(tmpMutation.compare(tmpMutation2)); + + tmpMutation.setChosenChange(tmpMutation.getPotential_changes().get(1)); + + Assert.assertFalse(tmpMutation.compare(tmpMutation2)); + } } |