protected IPResource retrieveOrCreateResource(IPResourceService resourceService, ChangesContext changes, IPResource resource) { // Search in current changes Optional<IPResource> foundOptional = changes.getResourcesToAdd().stream() // .filter(it -> resourceService.resourceEqualsPk(resource, it)) // .findAny(); if (foundOptional.isPresent()) { return foundOptional.get(); } foundOptional = changes.getResourcesToUpdate().stream() // .filter(it -> resourceService.resourceEqualsPk(resource, it.getB())) // .map(it -> it.getB()) // .findAny(); if (foundOptional.isPresent()) { return foundOptional.get(); } // Search in repository foundOptional = resourceService.resourceFindByPk(resource); if (foundOptional.isPresent()) { return foundOptional.get(); } // Create new changes.resourceAdd(resource); return resource; }
@SuppressWarnings({ "unchecked" }) protected <T extends IPResource> T retrieveOrCreateResource(IPResourceService resourceService, ChangesContext changes, T resource, Class<T> resourceClass) { // Search in current changes Optional<T> foundOptional = changes.getResourcesToAdd().stream() // .filter(it -> resourceService.resourceEqualsPk(resource, it)) // .map(it -> (T) it) // .findAny(); if (foundOptional.isPresent()) { return foundOptional.get(); } foundOptional = changes.getResourcesToUpdate().stream() // .filter(it -> resourceService.resourceEqualsPk(resource, it.getB())) // .map(it -> (T) it.getB()) // .findAny(); if (foundOptional.isPresent()) { return foundOptional.get(); } // Search in repository foundOptional = resourceService.resourceFindByPk(resource); if (foundOptional.isPresent()) { return foundOptional.get(); } // Create new changes.resourceAdd(resource); return resource; }
protected IPResource retrieveAndUpdateOrCreateResource(IPResourceService resourceService, ChangesContext changes, IPResource resource) { // Search in current changes Optional<IPResource> foundOptional = changes.getResourcesToAdd().stream() // .filter(it -> resourceService.resourceEqualsPk(resource, it)) // .findAny(); if (foundOptional.isPresent()) { updateResourceIfDifferent(resource, foundOptional.get()); return foundOptional.get(); } foundOptional = changes.getResourcesToUpdate().stream() // .filter(it -> resourceService.resourceEqualsPk(resource, it.getB())) // .map(it -> it.getB()) // .findAny(); if (foundOptional.isPresent()) { updateResourceIfDifferent(resource, foundOptional.get()); return foundOptional.get(); } // Search in repository foundOptional = resourceService.resourceFindByPk(resource); if (foundOptional.isPresent()) { IPResource foundResource = foundOptional.get(); if (updateResourceIfDifferent(resource, foundResource)) { changes.resourceUpdate(foundResource, foundResource); } return foundResource; } // Create new changes.resourceAdd(resource); return resource; }
@Test public void testQueryEditorName() { IPResourceService resourceService = getCommonServicesContext().getResourceService(); InternalChangeService internalChangeService = getInternalServicesContext().getInternalChangeService(); // Get the initial id Optional<JunitResource> junitResourceOptional = resourceService.resourceFind(resourceService.createResourceQuery(JunitResource.class) // .propertyEquals(JunitResource.PROPERTY_SET_TEXTS, "two")// ); Assert.assertTrue(junitResourceOptional.isPresent()); long expectedId = junitResourceOptional.get().getInternalId(); // Modify its editor JunitResource resource = junitResourceOptional.get(); ChangesContext changes = new ChangesContext(); resource.setResourceEditorName("junit"); changes.getResourcesToUpdate().add(new Tuple2<>(expectedId, resource)); internalChangeService.changesExecute(changes); // Get the resource junitResourceOptional = resourceService.resourceFind(resourceService.createResourceQuery(JunitResource.class) // .addEditorEquals("junit") // ); Assert.assertTrue(junitResourceOptional.isPresent()); Assert.assertEquals((Long) expectedId, junitResourceOptional.get().getInternalId()); // Don't get the resource if wrong editor junitResourceOptional = resourceService.resourceFind(resourceService.createResourceQuery(JunitResource.class) // .addEditorEquals("not junit") // ); Assert.assertFalse(junitResourceOptional.isPresent()); }
changes.getResourcesToUpdate().size(), changes.getResourcesToDelete().size(), changes.getResourcesToAdd().size(), changes.getResourcesToRefresh().size(), // Set<Long> toRefreshFarIdsSeconds = new HashSet<>(); for (Tuple2<Long, IPResource> update : changes.getResourcesToUpdate()) {
@Test public void testDuplicatePkSameResource_update() { // Common JunitResource resource; ChangesContext changes = new ChangesContext(); InternalChangeService internalChangeService = getInternalServicesContext().getInternalChangeService(); IPResourceService resourceService = getCommonServicesContext().getResourceService(); // Create 2 items resource = new JunitResource("t1", JunitResourceEnum.A, 1); resource.setLongNumber(10L); changes.getResourcesToAdd().add(resource); resource = new JunitResource("t2", JunitResourceEnum.A, 2); resource.setLongNumber(10L); changes.getResourcesToAdd().add(resource); internalChangeService.changesExecute(changes); // Rename second item to same pk as first thrown.expect(ResourcePrimaryKeyCollisionException.class); resource = new JunitResource("t1", JunitResourceEnum.A, 1); resource.setLongNumber(20L); changes.getResourcesToUpdate().add(new Tuple2<>(resourceService.resourceFindByPk(new JunitResource("t2", JunitResourceEnum.A, 2)).get().getInternalId(), resource)); internalChangeService.changesExecute(changes); }
changes.getResourcesToUpdate().add(new Tuple2<>(machine.getInternalId(), machine)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(machine.getInternalId(), machine)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(machine.getInternalId(), machine)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(machine.getInternalId(), machine)); try { getInternalServicesContext().getInternalChangeService().changesExecute(changes);
website.getDomainNames().clear(); website.getDomainNames().add("d2.example.com"); changes.getResourcesToUpdate().add(new Tuple2<>(website.getInternalId(), website)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(website.getInternalId(), website)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(website.getInternalId(), website)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(website.getInternalId(), website)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); website.getDomainNames().clear(); website.getDomainNames().add("d3.example.com"); changes.getResourcesToUpdate().add(new Tuple2<>(website.getInternalId(), website)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); website.getDomainNames().clear(); website.getDomainNames().add("d4.example.com"); changes.getResourcesToUpdate().add(new Tuple2<>(website.getInternalId(), website)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); websiteCertificate.setStart(DateTools.parseDateOnly("2001-07-01")); websiteCertificate.setEnd(DateTools.parseDateOnly("2001-08-01")); changes.getResourcesToUpdate().add(new Tuple2<>(websiteCertificate.getInternalId(), websiteCertificate)); getInternalServicesContext().getInternalChangeService().changesExecute(changes);
changes.getResourcesToUpdate().add(new Tuple2<>(resource.getInternalId(), new JunitResource("example2.com", JunitResourceEnum.A, 2))); internalChangeService.changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(resource.getInternalId(), new JunitResource("example2.com", JunitResourceEnum.A, 5))); internalChangeService.changesExecute(changes); Assert.fail("Didn't get an exception");
changes.getResourcesToUpdate().add(new Tuple2<>(m2.getInternalId(), new Machine("m3.node.example.com", m2Ip1))); try { getInternalServicesContext().getInternalChangeService().changesExecute(changes);
changes.getResourcesToUpdate().add(new Tuple2<>(m2.getInternalId(), m2)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(m2.getInternalId(), m2)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(dp.getInternalId(), new DnsPointer("pointer2.example.com"))); getInternalServicesContext().getInternalChangeService().changesExecute(changes); dp = resourceService.resourceFindByPk(new DnsPointer("pointer2.example.com")).get();
changes.getResourcesToUpdate().add(new Tuple2<>(a1.getInternalId(), a1)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); Assert.fail("Expecting exception");
changes.getResourcesToUpdate().add(new Tuple2<>(c1.getInternalId(), c2)); getInternalServicesContext().getInternalChangeService().changesExecute(changes);
changes.getResourcesToUpdate().add(new Tuple2<>(m1.getInternalId(), new Machine(machineName1, m1Ip2))); changes.getResourcesToUpdate().add(new Tuple2<>(m2.getInternalId(), new Machine(machineName2, m2Ip2))); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(m2.getInternalId(), new Machine(machineName2, null))); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.getResourcesToUpdate().add(new Tuple2<>(m2.getInternalId(), new Machine("anotherName.node.example.com"))); getInternalServicesContext().getInternalChangeService().changesExecute(changes); Assert.fail("Expecting an exception");