1818
1919import org .apache .commons .io .IOUtils ;
2020import org .apache .knox .gateway .database .AbstractDataSourceFactory ;
21+ import org .junit .After ;
2122import org .junit .AfterClass ;
2223import org .junit .BeforeClass ;
2324import org .junit .Test ;
@@ -46,31 +47,36 @@ public class DelegationPolicySchemaTest {
4647 private static final String H2_DB = "delegationpolicies" ;
4748 // H2 is the OOTB embedded backend; DB_CLOSE_DELAY=-1 keeps the in-memory database alive for the class.
4849 private static final String H2_URL = "jdbc:h2:mem:" + H2_DB + ";DB_CLOSE_DELAY=-1" ;
49- private static final String HSQL_URL = "jdbc:hsqldb:mem:delegationschema;ifexists=false" ;
50- private static final String HSQL_USER = "SA" ;
51- private static final String HSQL_PASSWORD = "" ;
50+
51+ // Child-first so the foreign keys don't block the truncation between tests.
52+ private static final String [] TABLES_CHILD_FIRST = {
53+ "DELEGATION_POLICY_RESOURCE_SCOPES" , "DELEGATION_POLICY_RESOURCES" ,
54+ "DELEGATION_POLICY_GROUPS" , "DELEGATION_POLICY_USERS" , "DELEGATION_POLICIES" };
5255
5356 private static Connection h2Conn ;
54- private static Connection hsqlConn ;
5557
5658 @ BeforeClass
5759 public static void setUp () throws Exception {
5860 h2Conn = DriverManager .getConnection (H2_URL );
59- hsqlConn = DriverManager .getConnection (HSQL_URL , HSQL_USER , HSQL_PASSWORD );
6061 // H2 (the OOTB embedded backend) uses the standard DDL script; IF NOT EXISTS makes it idempotent.
6162 runScript (h2Conn , loadSql (AbstractDataSourceFactory .KNOXIDF_DELEGATION_POLICY_TABLES_SQL ));
62- // Run standard DDL on HSQLDB
63- runScript (hsqlConn , loadSql (AbstractDataSourceFactory .KNOXIDF_DELEGATION_POLICY_TABLES_SQL ));
6463 // Verify Oracle DDL script is present on the classpath (execution requires an Oracle-backed test)
6564 loadSql (AbstractDataSourceFactory .ORACLE_KNOXIDF_DELEGATION_POLICY_TABLES_SQL );
6665 }
6766
67+ @ After
68+ public void clearTables () throws Exception {
69+ // Reset mutable state so each test starts clean regardless of JUnit method order (notably the
70+ // "empty after DDL" assertion in testAllFiveTablesQueryable).
71+ try (Statement stmt = h2Conn .createStatement ()) {
72+ for (String table : TABLES_CHILD_FIRST ) {
73+ stmt .execute ("DELETE FROM " + table );
74+ }
75+ }
76+ }
77+
6878 @ AfterClass
6979 public static void tearDown () throws Exception {
70- try (Connection conn = DriverManager .getConnection (HSQL_URL , HSQL_USER , HSQL_PASSWORD );
71- Statement stmt = conn .createStatement ()) {
72- stmt .execute ("SHUTDOWN" );
73- }
7480 if (h2Conn != null && !h2Conn .isClosed ()) {
7581 try (Statement stmt = h2Conn .createStatement ()) {
7682 stmt .execute ("DROP ALL OBJECTS" );
@@ -80,7 +86,7 @@ public static void tearDown() throws Exception {
8086 }
8187
8288 @ Test
83- public void testH2AllFiveTablesQueryable () throws Exception {
89+ public void testAllFiveTablesQueryable () throws Exception {
8490 for (String table : new String []{
8591 "DELEGATION_POLICIES" , "DELEGATION_POLICY_USERS" , "DELEGATION_POLICY_GROUPS" ,
8692 "DELEGATION_POLICY_RESOURCES" , "DELEGATION_POLICY_RESOURCE_SCOPES" }) {
@@ -93,28 +99,15 @@ public void testH2AllFiveTablesQueryable() throws Exception {
9399 }
94100
95101 @ Test
96- public void testHsqlAllFiveTablesQueryable () throws Exception {
97- for (String table : new String []{
98- "DELEGATION_POLICIES" , "DELEGATION_POLICY_USERS" , "DELEGATION_POLICY_GROUPS" ,
99- "DELEGATION_POLICY_RESOURCES" , "DELEGATION_POLICY_RESOURCE_SCOPES" }) {
100- try (Statement stmt = hsqlConn .createStatement ();
101- ResultSet rs = stmt .executeQuery ("SELECT COUNT(*) FROM " + table )) {
102- assertTrue ("Table must be queryable: " + table , rs .next ());
103- assertEquals ("Table must be empty after DDL: " + table , 0 , rs .getInt (1 ));
104- }
105- }
106- }
107-
108- @ Test
109- public void testHsqlStandardSqlIdempotent () throws Exception {
102+ public void testStandardSqlIdempotent () throws Exception {
110103 // Running the script twice must not throw due to IF NOT EXISTS
111- runScript (hsqlConn , loadSql (AbstractDataSourceFactory .KNOXIDF_DELEGATION_POLICY_TABLES_SQL ));
104+ runScript (h2Conn , loadSql (AbstractDataSourceFactory .KNOXIDF_DELEGATION_POLICY_TABLES_SQL ));
112105 }
113106
114107 @ Test
115108 public void testDefaultValues () throws Exception {
116109 final String id = UUID .randomUUID ().toString ();
117- try (Statement stmt = hsqlConn .createStatement ()) {
110+ try (Statement stmt = h2Conn .createStatement ()) {
118111 stmt .execute ("INSERT INTO DELEGATION_POLICIES "
119112 + "(registration_id, actor_authority, actor_id, created_at, updated_at) "
120113 + "VALUES ('" + id + "', 'oidc', 'actor-id', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" );
@@ -131,21 +124,21 @@ public void testDefaultValues() throws Exception {
131124
132125 @ Test
133126 public void testNotNullViolationActorAuthority () throws Exception {
134- expectConstraintViolation (hsqlConn ,
127+ expectConstraintViolation (h2Conn ,
135128 "INSERT INTO DELEGATION_POLICIES (registration_id, actor_authority, actor_id, created_at, updated_at) "
136129 + "VALUES ('" + UUID .randomUUID () + "', NULL, 'actor-id', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" );
137130 }
138131
139132 @ Test
140133 public void testNotNullViolationActorId () throws Exception {
141- expectConstraintViolation (hsqlConn ,
134+ expectConstraintViolation (h2Conn ,
142135 "INSERT INTO DELEGATION_POLICIES (registration_id, actor_authority, actor_id, created_at, updated_at) "
143136 + "VALUES ('" + UUID .randomUUID () + "', 'oidc', NULL, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" );
144137 }
145138
146139 @ Test
147140 public void testNotNullViolationCreatedAt () throws Exception {
148- expectConstraintViolation (hsqlConn ,
141+ expectConstraintViolation (h2Conn ,
149142 "INSERT INTO DELEGATION_POLICIES (registration_id, actor_authority, actor_id, created_at, updated_at) "
150143 + "VALUES ('" + UUID .randomUUID () + "', 'oidc', 'actor-id', NULL, CURRENT_TIMESTAMP)" );
151144 }
@@ -154,36 +147,36 @@ public void testNotNullViolationCreatedAt() throws Exception {
154147 public void testUniqueConstraintOnActorAuthorityAndId () throws Exception {
155148 final String id1 = UUID .randomUUID ().toString ();
156149 final String id2 = UUID .randomUUID ().toString ();
157- try (Statement stmt = hsqlConn .createStatement ()) {
150+ try (Statement stmt = h2Conn .createStatement ()) {
158151 stmt .execute ("INSERT INTO DELEGATION_POLICIES (registration_id, actor_authority, actor_id, created_at, updated_at) "
159152 + "VALUES ('" + id1 + "', 'oidc', 'duplicateactor', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" );
160- expectConstraintViolation (hsqlConn ,
153+ expectConstraintViolation (h2Conn ,
161154 "INSERT INTO DELEGATION_POLICIES (registration_id, actor_authority, actor_id, created_at, updated_at) "
162155 + "VALUES ('" + id2 + "', 'oidc', 'duplicateactor', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" );
163156 }
164157 }
165158
166159 @ Test
167160 public void testFkViolationUsers () throws Exception {
168- expectConstraintViolation (hsqlConn ,
161+ expectConstraintViolation (h2Conn ,
169162 "INSERT INTO DELEGATION_POLICY_USERS (registration_id, username) VALUES ('" + UUID .randomUUID () + "', 'alice')" );
170163 }
171164
172165 @ Test
173166 public void testFkViolationGroups () throws Exception {
174- expectConstraintViolation (hsqlConn ,
167+ expectConstraintViolation (h2Conn ,
175168 "INSERT INTO DELEGATION_POLICY_GROUPS (registration_id, group_name) VALUES ('" + UUID .randomUUID () + "', 'admins')" );
176169 }
177170
178171 @ Test
179172 public void testFkViolationResources () throws Exception {
180- expectConstraintViolation (hsqlConn ,
173+ expectConstraintViolation (h2Conn ,
181174 "INSERT INTO DELEGATION_POLICY_RESOURCES (registration_id, resource_uri) VALUES ('" + UUID .randomUUID () + "', '/api/v1')" );
182175 }
183176
184177 @ Test
185178 public void testFkViolationResourceScopes () throws Exception {
186- expectConstraintViolation (hsqlConn ,
179+ expectConstraintViolation (h2Conn ,
187180 "INSERT INTO DELEGATION_POLICY_RESOURCE_SCOPES (registration_id, resource_uri, scope) "
188181 + "VALUES ('" + UUID .randomUUID () + "', '/api/v1', 'read')" );
189182 }
0 commit comments