Applications { public Applications(CuratorDatabaseClient db) { this.db = db; for (ApplicationId id : ids()) { try (Mutex lock = db.lock(id)) { try (Mutex innerLock = db.configLock(id)) { get(id).ifPresent(application -> put(application, lock)); } } } } Applications(CuratorDatabaseClient db); List<ApplicationId> ids(); Optional<Application> get(ApplicationId id); void put(Application application, Mutex applicationLock); void put(Application application, NestedTransaction transaction, Mutex applicationLock); void remove(ApplicationId application, NestedTransaction transaction, Mutex applicationLock);  }
@Test public void testApplications() { NodeRepositoryTester tester = new NodeRepositoryTester(); Applications applications = new NodeRepositoryTester().nodeRepository().applications(); ApplicationId app1 = ApplicationId.from("t1", "a1", "i1"); ApplicationId app2 = ApplicationId.from("t1", "a2", "i1"); ApplicationId app3 = ApplicationId.from("t1", "a2", "default"); assertTrue(applications.get(app1).isEmpty()); assertEquals(List.of(), applications.ids()); applications.put(new Application(app1), tester.nodeRepository().lock(app1)); assertEquals(app1, applications.get(app1).get().id()); assertEquals(List.of(app1), applications.ids()); NestedTransaction t = new NestedTransaction(); applications.remove(app1, t, tester.nodeRepository().lock(app1)); t.commit(); assertTrue(applications.get(app1).isEmpty()); assertEquals(List.of(), applications.ids()); applications.put(new Application(app1), tester.nodeRepository().lock(app1)); applications.put(new Application(app2), tester.nodeRepository().lock(app1)); t = new NestedTransaction(); applications.put(new Application(app3), t, tester.nodeRepository().lock(app1)); assertEquals(List.of(app1, app2), applications.ids()); t.commit(); assertEquals(List.of(app1, app2, app3), applications.ids()); t = new NestedTransaction(); applications.remove(app1, t, tester.nodeRepository().lock(app1)); applications.remove(app2, t, tester.nodeRepository().lock(app2)); applications.remove(app3, t, tester.nodeRepository().lock(app3)); assertEquals(List.of(app1, app2, app3), applications.ids()); t.commit(); assertTrue(applications.get(app1).isEmpty()); assertEquals(List.of(), applications.ids()); }