public <T extends IPResource> T assertResourceExists(boolean expectedExists, T resourcePk, Class<T> resourceClass) { IPResourceService resourceService = getCommonServicesContext().getResourceService(); Optional<T> resourceFindOne = resourceService.resourceFindByPk(resourcePk); Assert.assertEquals(expectedExists, resourceFindOne.isPresent()); if (resourceFindOne.isPresent()) { return resourceFindOne.get(); } else { return null; } }
public <T extends IPResource> T assertResourceExists(boolean expectedExists, T resourcePk, Class<T> resourceClass) { IPResourceService resourceService = getCommonServicesContext().getResourceService(); Optional<T> resourceFindOne = resourceService.resourceFindByPk(resourcePk); Assert.assertEquals(expectedExists, resourceFindOne.isPresent()); if (resourceFindOne.isPresent()) { return resourceFindOne.get(); } else { return null; } }
public void resourceRefresh(IPResource resource) { Long internalId = resource.getInternalId(); if (internalId == null) { Optional<IPResource> found = resourceService.resourceFindByPk(resource); if (found.isPresent()) { internalId = found.get().getInternalId(); } } resourceRefresh(internalId); }
public void resourceDelete(IPResource resource) { Long internalId = resource.getInternalId(); if (internalId == null) { Optional<IPResource> found = resourceService.resourceFindByPk(resource); if (found.isPresent()) { internalId = found.get().getInternalId(); } } resourceDelete(internalId); }
@Test public void testQueryPK_none() { IPResourceService resourceService = getCommonServicesContext().getResourceService(); // Missing detail Optional<JunitResource> one = resourceService.resourceFindByPk(new JunitResource("www.example.com", JunitResourceEnum.A, null)); Assert.assertFalse(one.isPresent()); // Wrong details one = resourceService.resourceFindByPk(new JunitResource("www.example.com", JunitResourceEnum.A, 6)); Assert.assertFalse(one.isPresent()); }
@Test public void testQueryPK_none() { IPResourceService resourceService = getCommonServicesContext().getResourceService(); // Missing detail Optional<JunitResource> one = resourceService.resourceFindByPk(new JunitResource("www.example.com", JunitResourceEnum.A, null)); Assert.assertFalse(one.isPresent()); // Wrong details one = resourceService.resourceFindByPk(new JunitResource("www.example.com", JunitResourceEnum.A, 6)); Assert.assertFalse(one.isPresent()); }
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; }
private void removeApplicationFor(CommonServicesContext services, ChangesContext changes, String machineName) { IPResourceService resourceService = services.getResourceService(); // Application Application backupApplication = new Application(); backupApplication.setDescription("Backup to Google Cloud for " + machineName); backupApplication.setName("backup_google-" + machineName.replaceAll("\\.", "_")); Optional<Application> existingApplicationOptional = resourceService.resourceFindByPk(backupApplication); if (existingApplicationOptional.isPresent()) { Application existingApplication = existingApplicationOptional.get(); logger.info("Removing application {}", existingApplication.getName()); changes.resourceDelete(existingApplication); } }
@Test public void testQueryOne() { IPResourceService resourceService = getCommonServicesContext().getResourceService(); resourceService.resourceFindByPk(new JunitResource("www.example.com", JunitResourceEnum.A, 2)).get(); }
@Test public void testQueryOne() { IPResourceService resourceService = getCommonServicesContext().getResourceService(); resourceService.resourceFindByPk(new JunitResource("www.example.com", JunitResourceEnum.A, 2)).get(); }
@Test public void testQueryPK_one() { IPResourceService resourceService = getCommonServicesContext().getResourceService(); Optional<JunitResource> one = resourceService.resourceFindByPk(new JunitResource("www.example.com", JunitResourceEnum.A, 2)); Assert.assertTrue(one.isPresent()); Set<String> tags = resourceService.tagFindAllByResource(one.get()); Assert.assertEquals(1, tags.size()); Assert.assertTrue(tags.contains("asite")); }
@Test public void testQueryPK_one() { IPResourceService resourceService = getCommonServicesContext().getResourceService(); Optional<JunitResource> one = resourceService.resourceFindByPk(new JunitResource("www.example.com", JunitResourceEnum.A, 2)); Assert.assertTrue(one.isPresent()); Set<String> tags = resourceService.tagFindAllByResource(one.get()); Assert.assertEquals(1, tags.size()); Assert.assertTrue(tags.contains("asite")); }
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 testChanges_reduntantLinksAndTags() { ChangesContext changes = new ChangesContext(); JunitResource r1 = new JunitResource("reduntant_1"); JunitResource r2 = new JunitResource("reduntant_2"); changes.getResourcesToAdd().add(r1); changes.getResourcesToAdd().add(r2); changes.getLinksToAdd().add(new Tuple3<>(r1, "link1", r2)); changes.getLinksToAdd().add(new Tuple3<>(r1, "link1", r2)); changes.getLinksToAdd().add(new Tuple3<>(r1, "link2", r2)); changes.getTagsToAdd().add(new Tuple2<>(r1, "tag1")); changes.getTagsToAdd().add(new Tuple2<>(r1, "tag1")); changes.getTagsToAdd().add(new Tuple2<>(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_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(); JunitResource r1 = new JunitResource("1"); JunitResource r2 = new JunitResource("2"); changes.getResourcesToAdd().add(r1); changes.getResourcesToAdd().add(r2); changes.getLinksToAdd().add(new Tuple3<>(r1, "link1", r2)); changes.getTagsToAdd().add(new Tuple2<>(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.getResourcesToDelete().add(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.getResourcesToAdd().add(r1); getInternalServicesContext().getInternalChangeService().changesExecute(changes); // Check no links and tags Assert.assertEquals(0, resourceService.linkFindAllByFromResource(r1).size()); Assert.assertEquals(0, resourceService.tagFindAllByResource(r1).size()); }
@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()); }
@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); }
@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); }