@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"); } }
private HealthCheck.Result runHealthCheck(MockDataProvider provider) throws Exception { MockConnection mockConnection = new MockConnection(provider) { @Override public Savepoint setSavepoint() throws SQLException { return new Savepoint() { @Override public int getSavepointId() throws SQLException { return 0; } @Override public String getSavepointName() throws SQLException { return "savepoint"; } }; } }; Configuration configuration = new DefaultConfiguration().set(mockConnection); configuration.settings().setExecuteLogging(false); JooqHealthCheck healthCheck = new JooqHealthCheck(configuration, validationQuery); return healthCheck.check(); }
@Test public void findListener() { PersonManager personManager = new PersonManager(); Person person = personManager.createPerson(); ClubManager clubManager = new ClubManager(); Club club = clubManager.createClub(null, Programs.AWANA.get()); MockDataProvider provider = selectOne((s) -> { s.assertUUID(person.getId(), LISTENER.ID); }, LISTENER, r -> { r.setId(person.getId().getBytes()); r.setClubId(club.getId().getBytes()); }); Listener listener = setup(provider).find(person.getId(), personManager, clubManager).get(); assertListener(person, listener); }
@Test public void findClubListeners() { PersonManager personManager = new PersonManager(); Person person1 = personManager.createPerson(); Person person2 = personManager.createPerson(); ClubManager clubManager = new ClubManager(); Club club = clubManager.createClub(null, Programs.AWANA.get()); MockDataProvider provider = select((s) -> { s.assertUUID(club.getId(), LISTENER.CLUB_ID); }, (create) -> { Result<ListenerRecord> result = create.newResult(LISTENER); addClubListenerRecord(person1, club, create, result); addClubListenerRecord(person2, club, create, result); return result; }); Set<Listener> listeners = setup(provider).find(club, personManager); Iterator<Listener> iterator = listeners.iterator(); assertListener(person1, iterator.next()); assertListener(person2, iterator.next()); assertFalse(iterator.hasNext()); }
@Test public void findLeader() { PersonManager personManager = new PersonManager(); Person person = personManager.createPerson(); ClubManager clubManager = new ClubManager(); Club club = clubManager.createClub(null, Programs.AWANA.get()); MockDataProvider provider = selectOne((s) -> { s.assertUUID(person.getId(), LEADER.ID); }, LEADER, r -> { r.setId(person.getId().getBytes()); r.setClubId(club.getId().getBytes()); r.setRole(ClubLeader.LeadershipRole.DIRECTOR.name()); }); ClubLeader clubLeader = setup(provider).find(person.getId(), personManager, clubManager).get(); assertEquals(clubLeader, person.asClubLeader().get()); assertEquals(person.getId(), clubLeader.getId()); assertEquals(ClubLeader.LeadershipRole.DIRECTOR, clubLeader.getLeadershipRole()); }
@Test public void findClub() { String clubId = UUID.randomUUID().toString(); ClubManager manager = new ClubManager(); Club parent = manager.createClub(null, Programs.AWANA.get()); String parentClubId = parent.getId(); Curriculum curriculum = Programs.find("TnT").get(); String curriculumId = curriculum.getId(); MockDataProvider provider = selectOne((s) -> { s.assertUUID(clubId, CLUB.ID); }, CLUB, (r)-> { r.setId(clubId.getBytes()); r.setParentClubId(parentClubId.getBytes()); r.setCurriculum(curriculumId); }); Club club = setup(provider).find(clubId, manager).get(); assertEquals(clubId, club.getId()); assertEquals(parent, club.getParentGroup().get()); assertEquals(curriculum, club.getCurriculum()); }
static Connector mockConnector(MockDataProvider provider) { // Put your provider into a MockConnection and use that connection // in your application. In this case, with a jOOQ DSLContext: Connection connection = new MockConnection(provider); return new Connector() { @Override public Connection connect() throws SQLException { return connection; } @Override public SQLDialect getDialect() { return SQLDialect.HSQLDB; } @Override public Settings getSettings() { return new Settings(); } }; }
private User assertFindUser(Integer auth) { PersonManager manager = new PersonManager(); String providerId = "Someone"; String uniqueID = UUID.randomUUID().toString(); String id = manager.createPerson().getId(); MockDataProvider provider = selectOne((s) -> { s.assertFieldEquals(providerId, LOGIN.PROVIDER_ID); s.assertFieldEquals(uniqueID, LOGIN.UNIQUE_ID); }, LOGIN, (r)-> { r.setId(id.getBytes()); r.setAuth(auth); }); User user = setup(provider).find(providerId, uniqueID, manager).get(); assertEquals(id, user.getId()); assertEquals(providerId, user.getLoginInformation().getProviderID()); assertEquals(uniqueID, user.getLoginInformation().getUniqueID()); return user; }
@Test public void findAddress() { String id = UUID.randomUUID().toString(); MockDataProvider provider = selectOne((s) -> { s.assertUUID(id, ADDRESS.ID); }, ADDRESS, (r) -> { r.setId(id.getBytes()); r.setLine1("Line 1"); r.setLine2("Line 2"); r.setCity("A City"); r.setPostalCode("12345"); r.setTerritory("NE"); r.setCountry("US"); }); Address address = setup(provider).find(id).get(); assertEquals(id, address.getId()); assertEquals("Line 1", address.getLine1()); assertEquals("Line 2", address.getLine2()); assertEquals("A City", address.getCity()); assertEquals("12345", address.getPostalCode()); assertEquals("US", address.getCountry().getValue()); }
@Test public void findCeremony() { String ceremonyId = UUID.randomUUID().toString(); LocalDate now = LocalDate.now(); MockDataProvider provider = selectOne((s) -> s.assertUUID(ceremonyId, CEREMONY.ID), CEREMONY, (r) -> { r.setId(ceremonyId.getBytes()); r.setName("Foobar"); r.setPresentationDate(UtilityMethods.toSqlDate(now)); }); Optional<Ceremony> ceremony = setup(provider).find(ceremonyId); assertTrue(ceremony.isPresent()); assertEquals("Foobar", ceremony.get().getName()); assertEquals(ceremonyId, ceremony.get().getId()); assertEquals(now, ceremony.get().presentationDate()); }
@Test public void findClubber() { PersonManager personManager = new PersonManager(); Person person = personManager.createPerson(); String familyId = UUID.randomUUID().toString(); ClubManager clubManager = new ClubManager(); Club club = clubManager.createClub(null, Programs.AWANA.get()); MockDataProvider provider = selectOne((s) -> { s.assertUUID(person.getId(), CLUBBER.ID); }, CLUBBER, (r)-> { r.setId(person.getId().getBytes()); r.setFamilyId(familyId.getBytes()); r.setClubId(club.getId().getBytes()); r.setAgeGroup(AgeGroup.DefaultAgeGroup.THIRD_GRADE.name()); }); Clubber clubber = setup(provider, personManager, clubManager).find(person.getId()).get(); assertEquals(person.getId(), clubber.getId()); assertEquals(AgeGroup.DefaultAgeGroup.THIRD_GRADE, person.asClubber().get().getCurrentAgeGroup()); assertEquals(familyId, person.getFamily().get().getId()); }
@Test public void findClubberNoFamily() { PersonManager personManager = new PersonManager(); Person person = personManager.createPerson(); ClubManager clubManager = new ClubManager(); Club club = clubManager.createClub(null, Programs.AWANA.get()); MockDataProvider provider = selectOne((s) -> { s.assertUUID(person.getId(), CLUBBER.ID); }, CLUBBER, (r)-> { r.setId(person.getId().getBytes()); r.setFamilyId(null); r.setClubId(club.getId().getBytes()); r.setAgeGroup(AgeGroup.DefaultAgeGroup.THIRD_GRADE.name()); }); Clubber clubber = setup(provider, personManager, clubManager).find(person.getId()).get(); assertEquals(person.getId(), clubber.getId()); assertEquals(AgeGroup.DefaultAgeGroup.THIRD_GRADE, person.asClubber().get().getCurrentAgeGroup()); assertFalse(clubber.getFamily().isPresent()); }
@Test public void findClubberNoClub() { PersonManager personManager = new PersonManager(); Person person = personManager.createPerson(); ClubManager clubManager = new ClubManager(); MockDataProvider provider = selectOne((s) -> { s.assertUUID(person.getId(), CLUBBER.ID); }, CLUBBER, (r)-> { r.setId(person.getId().getBytes()); r.setFamilyId(null); r.setClubId(null); r.setAgeGroup(AgeGroup.DefaultAgeGroup.THIRD_GRADE.name()); }); Clubber clubber = setup(provider, personManager, clubManager).find(person.getId()).get(); assertEquals(person.getId(), clubber.getId()); assertFalse(clubber.getClub().isPresent()); }
@Test public void testFindsFamilyIdById() { String id = UUID.randomUUID().toString(); String familyId = UUID.randomUUID().toString(); MockDataProvider provider = selectOne( (s) -> s.assertUUID(id, PARENT.ID), PARENT, record->{ record.setId(id.getBytes()); record.setFamilyId(familyId.getBytes()); }); String result = setup(provider).findFamily(id).get(); assertEquals(familyId, result); }
@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 void testUpdatesNothing() throws Exception { final Listener listener = newListener(); MockDataProvider provider = new MockDataProviderBuilder() .updateCount(0) .build(); setup(provider).persist(listener); }
@Test public void testPersistsCorrectValues() throws Exception { final Listener listener = newListener(); MockDataProvider provider = mergeProvider(assertUUID(listener)); setup(provider).persist(listener); }
@Test public void findNoListener() { PersonManager personManager = new PersonManager(); Person person = personManager.createPerson(); ClubManager clubManager = new ClubManager(); MockDataProvider provider = select((s) -> { s.assertUUID(person.getId(), LISTENER.ID); }, (create) -> create.newResult(LISTENER)); assertFalse(setup(provider).find(person.getId(), personManager, clubManager).isPresent()); assertFalse(person.asClubLeader().isPresent()); }
@Test(expected = DataAccessException.class) public void testUpdatesNothing() throws Exception { final ClubLeader leader = newLeader(); MockDataProvider provider = new MockDataProviderBuilder() .updateCount(0) .build(); setup(provider).persist(leader); }
@Test public void testPersistsCorrectValues() throws Exception { final ClubLeader leader = newLeader(); MockDataProvider provider = mergeProvider(assertUUID(leader), assertFields(leader)); setup(provider).persist(leader); }
@Test public void findNoLeader() { PersonManager personManager = new PersonManager(); Person person = personManager.createPerson(); ClubManager clubManager = new ClubManager(); MockDataProvider provider = select((s) -> { s.assertUUID(person.getId(), LEADER.ID); }, (create)->create.newResult(LEADER)); assertFalse(setup(provider).find(person.getId(), personManager, clubManager).isPresent()); assertFalse(person.asClubLeader().isPresent()); }
@Test public void testPersist() { final MockClub club = newClub(); MockDataProvider provider = mergeProvider(assertUUID(club), assertFields(club)); setup(provider).persist(club); }
@Test public void testPersistProgramAsClub() { final Program program = newClub().getProgram(); MockDataProvider provider = mergeProvider(assertUUID(program), assertFields(program)); setup(provider).persist(program); }
@Test(expected = DataAccessException.class) public void testUpdatesNothing() throws Exception { final MockClub club = newClub(); MockDataProvider provider = new MockDataProviderBuilder() .updateCount(0) .build(); setup(provider).persist(club); }
@Test public void findNoClub() { String clubId = UUID.randomUUID().toString(); ClubManager manager = new ClubManager(); MockDataProvider provider = select((s) -> { s.assertUUID(clubId, CLUB.ID); }, (r) -> r.newResult(CLUB)); assertFalse(setup(provider).find(clubId, manager).isPresent()); }
@Test(expected = DataAccessException.class) public void testUpdatesNothing() throws Exception { final Family family = newFamily(); MockDataProvider provider = new MockDataProviderBuilder() .updateCount(0) .build(); setup(provider).persist(family); }
@Test(expected = DataAccessException.class) public void testUpdatesNothing() throws Exception { final Provider authProvider = newProvider(); MockDataProvider provider = new MockDataProviderBuilder() .updateCount(0) .build(); setup(provider).persist(authProvider); }
@Test public void testPersistsCorrectValues() throws Exception { final Provider authProvider = newProvider(); MockDataProvider provider = mergeProvider(assertKey(authProvider), assertFields(authProvider)); setup(provider).persist(authProvider); }
@Test public void findNoProviders() { MockDataProvider provider = select((s) -> { // TODO: find a way to assert empty where clause }, (r) -> r.newResult(PROVIDER)); assertTrue(setup(provider).find().isEmpty()); }
@Test public void findAllProviders() { List<ProviderRecord> allRecords = Arrays.asList(new ProviderRecord( "providerId", "site", "name", "image", "clientKey", "secret" )); MockDataProvider provider = select((s) -> { // TODO: find a way to assert empty where clause }, (r) -> { Result<ProviderRecord> result = r.newResult(PROVIDER); result.addAll(allRecords); return result; }); List<Provider> providers = setup(provider).find(); assertEquals(1, providers.size()); int i=0; for(Provider p: providers) { ProviderRecord record = allRecords.get(i++); assertEquals(record.getClientKey(), p.getClientKey()); assertEquals(record.getImage(), p.getImage()); assertEquals(record.getName(), p.getName()); assertEquals(record.getProviderId(), p.getId()); assertEquals(record.getSite(), p.getSite()); assertEquals(record.getSecret(), p.getSecret()); } }
static MockDataProvider mergeProvider(Consumer<StatementVerifier> idFn, Consumer<StatementVerifier> fieldsFn) { return new MockDataProviderBuilder() .updateCount(1) .statement(StatementType.MERGE, idFn) .statement(StatementType.UPDATE, fieldsFn) .statement(StatementType.INSERT, (s) -> { idFn.accept(s); fieldsFn.accept(s); }) .build(); }
static MockDataProvider mergeProvider(Consumer<StatementVerifier> idFn) { return new MockDataProviderBuilder() .updateCount(1) .statement(StatementType.MERGE, idFn) .statement(StatementType.INSERT, idFn) .build(); }
static <R extends UpdatableRecord<R>> MockDataProvider selectOne( Consumer<StatementVerifier> assertWhereFn, Table<R> table, Consumer<R> fn) { return select(assertWhereFn, (create) -> { Result<R> result = create.newResult(table); R record = create.newRecord(table); result.add(record); fn.accept(record); return result; }); }
@Test public void testPersistMerges() throws Exception { final User person = newUser(); MockDataProvider provider = mergeProvider(assertUUID(person), assertNullFields()); setup(provider).persist(person.getLoginInformation()); }
@Test(expected = DataAccessException.class) public void testUpdatesNothing() throws Exception { final User person = newUser(); MockDataProvider provider = new MockDataProviderBuilder() .updateCount(0) .build(); setup(provider).persist(person.getLoginInformation()); }
@Test public void testPersistsCorrectValues() throws Exception { final User person = newUser(); person.resetAuth(); MockDataProvider provider = mergeProvider(assertUUID(person), assertFields(person)); setup(provider).persist(person.getLoginInformation()); }
@Test public void testPersistProgramAsOrganization() { final Program program = newProgram(); MockDataProvider provider = mergeProvider(assertUUID(program), assertFields(program)); setup(provider).persist(program); }
@Test public void testPersistEmptyProgram() { final Program program = new ProgramAdapter(null, null, (String)null) { String id = UUID.randomUUID().toString(); @Override public String getId() { return id; } }; MockDataProvider provider = mergeProvider(assertUUID(program), assertNullFields(program)); setup(provider).persist(program); }
@Test(expected = DataAccessException.class) public void testUpdatesNothing() throws Exception { final Program club = newProgram(); MockDataProvider provider = new MockDataProviderBuilder() .updateCount(0) .build(); setup(provider).persist(club); }