1 package org.kuali.student.sonar.database;
2
3 import org.junit.Before;
4 import org.junit.After;
5 import org.junit.Test;
6 import org.kuali.student.sonar.database.exception.FKConstraintException;
7 import org.kuali.student.sonar.database.exception.FieldMappingException;
8 import org.kuali.student.sonar.database.exception.InvalidConstraintException;
9 import org.kuali.student.sonar.database.plugin.DatabseIntegrityRulesRepository;
10 import org.kuali.student.sonar.database.plugin.ForeignKeyConstraint;
11 import org.kuali.student.sonar.database.utility.FKConstraintReport;
12 import org.kuali.student.sonar.database.utility.FKConstraintValidator;
13 import org.sonar.api.resources.Resource;
14 import org.sonar.api.rules.Rule;
15 import org.sonar.api.rules.Violation;
16 import org.kuali.student.sonar.database.utility.ForeignKeyValidationContext;
17
18
19 import java.sql.*;
20 import java.util.Properties;
21 import static org.junit.Assert.fail;
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class TestDatabaseIntegrityScript {
36
37 private FKConstraintValidator validator;
38
39 @Before
40 public void init() throws SQLException {
41 validator = new FKConstraintValidator();
42
43 ForeignKeyValidationContext context = new ForeignKeyValidationContext();
44 validator.setContext(context);
45 context.setSkip(false);
46 context.setQueryFileName("missing_FK_query.sql");
47 context.setQueryFilePath("sql/");
48
49
50 try {
51 Class.forName("oracle.jdbc.driver.OracleDriver");
52 } catch (ClassNotFoundException e) {
53 throw new RuntimeException("Unable to find DB Driver Class", e);
54 }
55
56
57 Properties props = new Properties();
58 props.setProperty("user", "KSBUNDLED");
59 props.setProperty("password", "KSBUNDLED");
60
61
62
63 context.setConnection(DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", props));
64 }
65
66 @Test
67 public void testFKSQL() throws SQLException {
68
69 FKConstraintReport report = validator.runFKSQL(Thread.currentThread().getContextClassLoader());
70
71 System.out.println("\n**** Done Adding constraints and Detecting Orphaned Data *****\n");
72
73 for (FKConstraintException exception : report.getFieldMappingIssues()) {
74 System.out.println(exception.getMessage());
75 }
76
77 for (FKConstraintException exception : report.getTableMappingIssues()) {
78 System.out.println(exception.getMessage());
79 }
80
81 for (FKConstraintException exception : report.getColumnTypeIncompatabilityIssues()) {
82 System.out.println(exception.getMessage());
83 }
84
85 for (FKConstraintException exception : report.getOrphanedDataIssues()) {
86 System.out.println(exception.getMessage());
87 }
88
89 for (FKConstraintException exception : report.getOtherIssues()) {
90 System.out.println(exception.getMessage());
91 }
92
93 System.out.println("\nSUMMARY");
94 if (report.getFieldMappingIssues().size()>0) {
95 System.out.println(report.getFieldMappingIssues().size() + " Field Mapping Issues");
96 }
97 if (report.getTableMappingIssues().size()>0) {
98 System.out.println(report.getTableMappingIssues().size() + " Table Mapping Issues");
99 }
100 if (report.getColumnTypeIncompatabilityIssues().size()>0) {
101 System.out.println(report.getColumnTypeIncompatabilityIssues().size() + " Column Type Issues");
102 }
103 if (report.getOrphanedDataIssues().size()>0) {
104 System.out.println(report.getOrphanedDataIssues().size() + " Orphaned Data Issues");
105 }
106 if (report.getOtherIssues().size()>0) {
107 System.out.println(report.getOtherIssues().size() + " Other Issues");
108 }
109 }
110
111 @Test
112 public void testCreateViolations() {
113 ForeignKeyConstraint constraint = new ForeignKeyConstraint("localTable", "localColumn", "foreignTable", "foreignColumn", "Test Constraint");
114 Violation violation = Violation.create(Rule.create(
115 DatabseIntegrityRulesRepository.REPOSITORY_KEY,
116 DatabseIntegrityRulesRepository.PARENT_KEY_MISSING_RULE_KEY,
117 "RuleTest"),
118 (Resource)constraint);
119 constraint = new ForeignKeyConstraint("localTable2", "localColumn2", "foreignTable2", "foreignColumn2", "Test Constraint2");
120 Violation violation2 = Violation.create(Rule.create(
121 DatabseIntegrityRulesRepository.REPOSITORY_KEY,
122 DatabseIntegrityRulesRepository.FIELD_MAPPING_RULE_KEY,
123 "RuleTest"),
124 (Resource)constraint);
125 }
126
127 @After
128 public void cleanup() throws InvalidConstraintException, SQLException {
129 validator.revert();
130 }
131
132 }