@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 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; }
protected void createUnixUserIfMissing(IPResourceService resourceService, ChangesContext changes) { Optional<UnixUser> unixUserOptional = resourceService.resourceFind(resourceService.createResourceQuery(UnixUser.class).propertyEquals(UnixUser.PROPERTY_NAME, UNIX_USER_REDIRECTION_NAME)); if (!unixUserOptional.isPresent()) { logger.info("Could not find the unix user {}. Will create it", UNIX_USER_REDIRECTION_NAME); UnixUser unixUser = new UnixUser(UnixUserAvailableIdHelper.getNextAvailableId(), UNIX_USER_REDIRECTION_NAME, "/home/" + UNIX_USER_REDIRECTION_NAME, null, null); changes.resourceAdd(unixUser); } }
changes.resourceAdd(createWithSets( // "sets_0.0", // changes.resourceAdd(createWithSets( // "sets_1.1", // Sets.newHashSet(DateTools.parseDateOnly("2000-01-01")), // Sets.newHashSet("1") // )); changes.resourceAdd(createWithSets( // "sets_1.2", // Sets.newHashSet(DateTools.parseDateOnly("2000-01-02")), // Sets.newHashSet("2") // )); changes.resourceAdd(createWithSets( // "sets_2.1", // Sets.newHashSet(DateTools.parseDateOnly("2000-01-01"), DateTools.parseDateOnly("2000-02-01")), // Sets.newHashSet("1", "2") // )); changes.resourceAdd(createWithSets( // "sets_2.2", // Sets.newHashSet(DateTools.parseDateOnly("2000-01-02"), DateTools.parseDateOnly("2000-02-02")), //
public static void createFakeData(CommonServicesContext commonCtx, InternalServicesContext internalCtx) { // JunitResource ChangesContext changes = new ChangesContext(commonCtx.getResourceService()); JunitResource junitResource = new JunitResource("www.example.com", JunitResourceEnum.A, 1); changes.resourceAdd(junitResource); changes.tagAdd(junitResource, "tag1"); changes.tagAdd(junitResource, "asite"); junitResource = new JunitResource("www.example.com", JunitResourceEnum.A, 2); changes.resourceAdd(junitResource); changes.tagAdd(junitResource, "asite"); changes.resourceAdd(new JunitResource("example.com", JunitResourceEnum.B, 3)); changes.resourceAdd(new JunitResource("t1_aaa", JunitResourceEnum.A, DateTools.parseFull("2000-01-01 00:00:00"), 1, 1L, 1.0, 1.0f, true, "one", "two")); changes.resourceAdd(new JunitResource("t2_aaa", JunitResourceEnum.C, DateTools.parseFull("2000-06-01 00:00:00"), 5, 8L, 1.5, 7.3f, false, "one", "three")); changes.resourceAdd(new JunitResource("zz", JunitResourceEnum.B, DateTools.parseFull("2000-04-01 00:00:00"), 80, 4L, 77.6, 3.1f, true)); internalCtx.getInternalChangeService().changesExecute(changes); }
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; }
private void common(CommonServicesContext services, ChangesContext changes, JunitResource resource) { if (!alwaysUpdate) { return; } List<JunitResource> junitResources = services.getResourceService().resourceFindAll(services.getResourceService().createResourceQuery(JunitResource.class)); if (junitResources.size() < 3) { for (int i = 0; i < 5; ++i) { changes.resourceAdd(new JunitResource(SecureRandomTools.randomHexString(10))); } } else { JunitResource update = junitResources.get(0); JunitResource refresh = junitResources.get(1); JunitResource delete = junitResources.get(2); update.setDate(new Date()); changes.resourceUpdate(update); changes.resourceRefresh(refresh); changes.resourceDelete(delete); } }
@Test public void testChanges_reduntantLinksAndTags() { ChangesContext changes = new ChangesContext(getCommonServicesContext().getResourceService()); JunitResource r1 = new JunitResource("reduntant_1"); JunitResource r2 = new JunitResource("reduntant_2"); changes.resourceAdd(r1); changes.resourceAdd(r2); changes.linkAdd(r1, "link1", r2); changes.linkAdd(r1, "link1", r2); changes.linkAdd(r1, "link2", r2); changes.tagAdd(r1, "tag1"); changes.tagAdd(r1, "tag1"); changes.tagAdd(r1, "tag2"); getInternalServicesContext().getInternalChangeService().changesExecute(changes); // Check IPResourceService resourceService = getCommonServicesContext().getResourceService(); r1 = resourceService.resourceFindByPk(r1).get(); r2 = resourceService.resourceFindByPk(r2).get(); Assert.assertEquals(Arrays.asList("link1", "link2"), resourceService.linkFindAllByFromResource(r1).stream().map(it -> it.getA()).sorted().collect(Collectors.toList())); Assert.assertEquals(Arrays.asList("tag1", "tag2"), resourceService.tagFindAllByResource(r1).stream().sorted().collect(Collectors.toList())); }
@Test public void testChanges_linksAndTagsAreNotKeptWhenDeleted() { // Create ChangesContext changes = new ChangesContext(getCommonServicesContext().getResourceService()); JunitResource r1 = new JunitResource("1"); JunitResource r2 = new JunitResource("2"); changes.resourceAdd(r1); changes.resourceAdd(r2); changes.linkAdd(r1, "link1", r2); changes.tagAdd(r1, "tag1"); getInternalServicesContext().getInternalChangeService().changesExecute(changes); IPResourceService resourceService = getCommonServicesContext().getResourceService(); r1 = resourceService.resourceFindByPk(r1).get(); r2 = resourceService.resourceFindByPk(r2).get(); Assert.assertEquals(Arrays.asList("link1"), resourceService.linkFindAllByFromResource(r1).stream().map(it -> it.getA()).sorted().collect(Collectors.toList())); Assert.assertEquals(Arrays.asList("tag1"), resourceService.tagFindAllByResource(r1).stream().sorted().collect(Collectors.toList())); // Delete it changes.resourceDelete(resourceService.resourceFindByPk(r1).get().getInternalId()); getInternalServicesContext().getInternalChangeService().changesExecute(changes); Assert.assertEquals(0, resourceService.linkFindAllByFromResource(r1).size()); Assert.assertEquals(0, resourceService.tagFindAllByResource(r1).size()); // Recreate it changes.resourceAdd(r1); getInternalServicesContext().getInternalChangeService().changesExecute(changes); // Check no links and tags Assert.assertEquals(0, resourceService.linkFindAllByFromResource(r1).size()); Assert.assertEquals(0, resourceService.tagFindAllByResource(r1).size()); }
resourcesByTypeAndName.put(resourceType + "/" + resourceName, resource); changes.resourceAdd(resource);
@Test public void testDuplicatePkSameResource_create() { // Common JunitResource resource; ChangesContext changes = new ChangesContext(getCommonServicesContext().getResourceService()); InternalChangeService internalChangeService = getInternalServicesContext().getInternalChangeService(); // Create 1 item resource = new JunitResource("t1", JunitResourceEnum.A, 1); resource.setLongNumber(10L); changes.resourceAdd(resource); internalChangeService.changesExecute(changes); // Create same. Not fine thrown.expect(ResourcePrimaryKeyCollisionException.class); resource = new JunitResource("t1", JunitResourceEnum.A, 1); resource.setLongNumber(30L); changes.resourceAdd(resource); internalChangeService.changesExecute(changes); }
changesContext.resourceAdd(resource); } else { changesContext.resourceUpdate(internalId, resource);
@Test public void testDuplicatePkSameResource_update() { // Common JunitResource resource; ChangesContext changes = new ChangesContext(getCommonServicesContext().getResourceService()); InternalChangeService internalChangeService = getInternalServicesContext().getInternalChangeService(); IPResourceService resourceService = getCommonServicesContext().getResourceService(); // Create 2 items resource = new JunitResource("t1", JunitResourceEnum.A, 1); resource.setLongNumber(10L); changes.resourceAdd(resource); resource = new JunitResource("t2", JunitResourceEnum.A, 2); resource.setLongNumber(10L); changes.resourceAdd(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.resourceUpdate(resourceService.resourceFindByPk(new JunitResource("t2", JunitResourceEnum.A, 2)).get().getInternalId(), resource); internalChangeService.changesExecute(changes); }
@Test(timeout = 30000) public void testInfiniteLoop() { thrown.expect(InfiniteUpdateLoop.class); TestingControllerPluginDefinitionProvider.getInstance().getTestingControllerInfiniteLoopUpdateHander().setAlwaysUpdate(true); ChangesContext changes = new ChangesContext(getCommonServicesContext().getResourceService()); JunitResource resource = new JunitResource("OneToGetStarted"); changes.resourceAdd(resource); getInternalServicesContext().getInternalChangeService().changesExecute(changes); }
ConcreteLevel2 cl22 = new ConcreteLevel2("L2-2", "PB", "AB", "BBB"); JunitResource j = new JunitResource("one"); changes.resourceAdd(cl11); changes.resourceAdd(cl12); changes.resourceAdd(cl21); changes.resourceAdd(cl22); changes.resourceAdd(j);
@Test public void testChanges_refresh() { // Create resource JunitResource junitResource = new JunitResource("testChanges_refresh"); ChangesContext changes = new ChangesContext(getCommonServicesContext().getResourceService()); changes.resourceAdd(junitResource); getInternalServicesContext().getInternalChangeService().changesExecute(changes); // Reset TestingControllerMockUpdateHander testingControllerMockUpdateHander = TestingControllerPluginDefinitionProvider.getInstance().getTestingControllerMockUpdateHander(); testingControllerMockUpdateHander.clear(); Assert.assertEquals(0, testingControllerMockUpdateHander.getChecked().size()); // Refresh resource changes.resourceRefresh(junitResource); getInternalServicesContext().getInternalChangeService().changesExecute(changes); // Assert Assert.assertEquals(1, testingControllerMockUpdateHander.getChecked().size()); }
Machine m2 = new Machine("m2.example.com", "199.141.1.201"); ChangesContext changes = new ChangesContext(getCommonServicesContext().getResourceService()); changes.resourceAdd(m1); changes.resourceAdd(m2); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.resourceAdd(dp); getInternalServicesContext().getInternalChangeService().changesExecute(changes);
c1.setThumbprint("my_thumb"); ChangesContext changes = new ChangesContext(getCommonServicesContext().getResourceService()); changes.resourceAdd(c1); getInternalServicesContext().getInternalChangeService().changesExecute(changes); c1 = resourceService.resourceFindByPk(c1).get();
changes.resourceAdd(machine); getInternalServicesContext().getInternalChangeService().changesExecute(changes);
changes.resourceAdd(new Machine(machineName1)); changes.resourceAdd(new Machine(machineName2, m2Ip1)); getInternalServicesContext().getInternalChangeService().changesExecute(changes);