@Test public void isUnhealthyIfTransactionFails() throws Exception { MockDataProvider mockDataProvider = new MockDataProvider() { @Override public MockResult[] execute(MockExecuteContext ctx) throws SQLException { throw new SQLException("BOOM"); } }; try { runHealthCheck(mockDataProvider); assert_().fail(); } catch (DataAccessException e) { assertThat(e.getMessage()).contains(validationQuery); assertThat(e.getMessage()).contains("BOOM"); } }
@Override public MockResult[] execute(MockExecuteContext ctx) throws SQLException { MockResult[] mock = new MockResult[1]; // The execute context contains SQL string(s), bind values, and other meta-data String sql = ctx.sql(); // Exceptions are propagated through the JDBC and jOOQ APIs if (sql.equals("SELECT 1")) { mock[0] = new MockResult(1, DSL.using(SQLDialect.HSQLDB).newResult()); } else { throw new DataAccessException("Incorrect validation query"); } return mock; }
private void loadStatements(MockExecuteContext ctx) { Collection<Object> bindings = new ArrayList<>(Arrays.asList(ctx.bindings())); int start, next = 0; StatementVerifier previous = null; for (StatementVerifier statement : statements) { StatementType type1 = statement.getType(); String name = type1.name(); start = next; String sql = ctx.sql(); next = sql.toUpperCase().indexOf(name, start); if (next > start && previous != null) { previous.bind(sql.substring(start, next), bindings); } else if (next == -1) { fail(name + " not found"); } previous = statement; } if (previous != null) { previous.bind(ctx.sql().substring(next), bindings); } }
@Test public void isHealthyIfNoExceptionIsThrown() throws Exception { MockDataProvider mockDataProvider = new MockDataProvider() { @Override public MockResult[] execute(MockExecuteContext ctx) throws SQLException { return new MockResult[0]; } }; HealthCheck.Result result = runHealthCheck(mockDataProvider); assertThat(result.isHealthy()).named("is healthy").isTrue(); }
@Override public MockResult[] execute(MockExecuteContext ctx) throws SQLException { // You might need a DSLContext to create org.jooq.Result and org.jooq.Record objects DSLContext create = DSL.using(SQLDialect.MYSQL); MockResult[] mock = new MockResult[1]; // The execute context contains SQL string(s), bind values, and other meta-data String sql = ctx.sql(); System.out.println("SQL Query: " + sql); // Exceptions are propagated through the JDBC and jOOQ APIs if (sql.toUpperCase().startsWith("DROP")) { throw new SQLException("Statement not supported: " + sql); } // You decide, whether any given statement returns results, and how many else if (sql.toUpperCase().startsWith("SELECT")) { // Always return one author record Result<UserRecord> result = create.newResult(USER); result.add(create.newRecord(USER)); result.get(0).setValue(USER.ID, 1); result.get(0).setValue(USER.USERNAME, "Orwell"); mock[0] = new MockResult(1, result); } else if (sql.toUpperCase().startsWith("INSERT")) { mock[0] = new MockResult(1, create.newResult(USER)); } else if (sql.toUpperCase().startsWith("UPDATE")) { mock[0] = new MockResult(1, create.newResult(USER)); } else if (sql.toUpperCase().startsWith("DELETE")) { mock[0] = new MockResult(1, create.newResult(USER)); } // You can detect batch statements easily else if (ctx.batch()) { // [...] } return mock; }
public MockDataProviderBuilder verify(Consumer<MockExecuteContext> r) { verifications.add(r); return this; }
private void assertWhere(String id, MockExecuteContext ctx) { Object[] bindings = ctx.bindings(); assertEquals(id, new String((byte[]) bindings[bindings.length-1])); }