ContactInformationMaintainer extends ControllerMaintainer { @Override protected boolean maintain() { TenantController tenants = controller().tenants(); boolean success = true; for (Tenant tenant : tenants.asList()) { log.log(INFO, "Updating contact information for " + tenant); try { switch (tenant.type()) { case athenz: tenants.lockIfPresent(tenant.name(), LockedTenant.Athenz.class, lockedTenant -> { Contact contact = contactRetriever.getContact(lockedTenant.get().propertyId()); log.log(INFO, "Contact found for " + tenant + " was " + (Optional.of(contact).equals(tenant.contact()) ? "un" : "") + "changed"); tenants.store(lockedTenant.with(contact)); }); break; case cloud: break; default: throw new IllegalArgumentException("Unexpected tenant type '" + tenant.type() + "'."); } } catch (Exception e) { success = false; log.log(Level.WARNING, "Failed to update contact information for " + tenant + ": " + Exceptions.toMessageString(e) + ". Retrying in " + interval()); } } return success; } ContactInformationMaintainer(Controller controller, Duration interval);   }
@Test public void updates_contact_information() { PropertyId propertyId1 = new PropertyId("1"); PropertyId propertyId2 = new PropertyId("2"); TenantName name1 = tester.createTenant("tenant1", "domain1", 1L); TenantName name2 = tester.createTenant("zenant1", "domain2", 2L); Supplier<AthenzTenant> tenant1 = () -> (AthenzTenant) tester.controller().tenants().require(name1); Supplier<AthenzTenant> tenant2 = () -> (AthenzTenant) tester.controller().tenants().require(name2); assertFalse("No contact information initially", tenant1.get().contact().isPresent()); assertFalse("No contact information initially", tenant2.get().contact().isPresent()); Contact contact = testContact(); tester.serviceRegistry().contactRetriever().addContact(propertyId1, () -> { throw new RuntimeException("ERROR"); }); tester.serviceRegistry().contactRetriever().addContact(propertyId2, () -> contact); maintainer.maintain(); assertEquals("No contact information added due to error", Optional.empty(), tenant1.get().contact()); assertEquals("Contact information added", Optional.of(contact), tenant2.get().contact()); }