@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; }
@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(); }
public MockDataProviderBuilder reply(Function<DSLContext, Result<?>> fn) { new PersistenceBroker<Void>(mockConnector(ctx->null)){ @Override protected void persist(Void thing, DSLContext create) { } }.execute((create)-> results.add(new MockResult(-1, fn.apply(create)))); return this; }
@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 updateCount(int rows) { results.add(new MockResult(rows, null)); return this; }
public MockDataProvider build() { return ctx -> { verifications.forEach(c -> c.accept(ctx)); return results.toArray(new MockResult[results.size()]); }; }
@Test public void findProgram() { ClubManager clubManager = new ClubManager(); String id = UUID.randomUUID().toString(); Curriculum curriculum = Programs.AWANA.get(); String orgName = "An Org"; String locale = "en_US"; DSLContext create = DSL.using(SQLDialect.HSQLDB); MockDataProvider provider = ctx -> { assertWhere(id, ctx); // TODO: assert the join // TODO: refactor this mess Result<Record7<byte[], String, String, byte[], byte[], byte[], String>> result = create.newResult( ORGANIZATION.ID, ORGANIZATION.ORGANIZATIONNAME, ORGANIZATION.LOCALE, ORGANIZATION.CLUB_ID, CLUB.ID, CLUB.PARENT_CLUB_ID, CLUB.CURRICULUM); Record7<byte[], String, String, byte[], byte[], byte[], String> record = create.newRecord( ORGANIZATION.ID, ORGANIZATION.ORGANIZATIONNAME, ORGANIZATION.LOCALE, ORGANIZATION.CLUB_ID, CLUB.ID, CLUB.PARENT_CLUB_ID, CLUB.CURRICULUM); result.add(record); record.setValue(ORGANIZATION.ID, id.getBytes()); record.setValue(ORGANIZATION.CLUB_ID, id.getBytes()); record.setValue(ORGANIZATION.ORGANIZATIONNAME, orgName); record.setValue(ORGANIZATION.LOCALE, locale); record.setValue(CLUB.ID, id.getBytes()); record.setValue(CLUB.PARENT_CLUB_ID, null); record.setValue(CLUB.CURRICULUM, curriculum.getId()); return new MockResult[] {new MockResult(1, result)}; }; Program program = setup(provider).find(id, clubManager).get(); assertEquals(id, program.getId()); assertFalse(program.getParentGroup().isPresent()); assertEquals(curriculum, program.getCurriculum()); assertEquals(locale, program.getLocale().toString()); assertEquals(orgName, program.getShortCode()); }
private MockResult[] emptyResults() { MockResult[] results = new MockResult[1]; results[0] = new MockResult(0, null); return results; }