/** * Delete the non root components (ie. sub-view, application or project copy) from the specified collection of {@link ComponentDto} * and data from their child tables. * <p> * This method has no effect when passed an empty collection or only root components. * </p> */ public void deleteNonRootComponentsInView(DbSession dbSession, Collection<ComponentDto> components) { Set<ComponentDto> nonRootComponents = components.stream().filter(PurgeDao::isNotRoot).collect(MoreCollectors.toSet()); if (nonRootComponents.isEmpty()) { return; } PurgeProfiler profiler = new PurgeProfiler(); PurgeCommands purgeCommands = new PurgeCommands(dbSession, profiler); deleteNonRootComponentsInView(nonRootComponents, purgeCommands); }
private void verifyNoEffect(ComponentDto firstRoot, ComponentDto... otherRoots) { DbSession dbSession = mock(DbSession.class); List<ComponentDto> componentDtos = Stream.concat(Stream.of(firstRoot), Arrays.stream(otherRoots)).collect(Collectors.toList()); Collections.shuffle(componentDtos); // order of collection must not matter underTest.deleteNonRootComponentsInView(dbSession, componentDtos); verifyZeroInteractions(dbSession); }
@Test public void deleteNonRootComponents_has_no_effect_when_parameter_is_empty() { DbSession dbSession = mock(DbSession.class); underTest.deleteNonRootComponentsInView(dbSession, Collections.emptyList()); verifyZeroInteractions(dbSession); }
@Test public void deleteNonRootComponents_deletes_measures_of_any_non_root_component_of_a_view() { ComponentDto view = db.components().insertView(); ComponentDto subview = db.components().insertComponent(ComponentTesting.newSubView(view)); ComponentDto pc = db.components().insertComponent(newProjectCopy("a", db.components().insertPrivateProject(), view)); insertMeasureFor(view, subview, pc); assertThat(getComponentUuidsOfMeasures()).containsOnly(view.uuid(), subview.uuid(), pc.uuid()); underTest.deleteNonRootComponentsInView(dbSession, singletonList(pc)); assertThat(getComponentUuidsOfMeasures()) .containsOnly(view.uuid(), subview.uuid()); underTest.deleteNonRootComponentsInView(dbSession, singletonList(subview)); assertThat(getComponentUuidsOfMeasures()) .containsOnly(view.uuid()); }
@Test public void deleteNonRootComponents_deletes_properties_of_subviews_of_a_view() { ComponentDto view = db.components().insertView(); ComponentDto subview1 = db.components().insertComponent(ComponentTesting.newSubView(view)); ComponentDto subview2 = db.components().insertComponent(ComponentTesting.newSubView(subview1)); ComponentDto subview3 = db.components().insertComponent(ComponentTesting.newSubView(view)); ComponentDto pc = db.components().insertComponent(newProjectCopy("a", db.components().insertPrivateProject(), view)); insertPropertyFor(view, subview1, subview2, subview3, pc); assertThat(getResourceIdOfProperties()).containsOnly(view.getId(), subview1.getId(), subview2.getId(), subview3.getId(), pc.getId()); underTest.deleteNonRootComponentsInView(dbSession, singletonList(subview1)); assertThat(getResourceIdOfProperties()) .containsOnly(view.getId(), subview2.getId(), subview3.getId(), pc.getId()); underTest.deleteNonRootComponentsInView(dbSession, asList(subview2, subview3, pc)); assertThat(getResourceIdOfProperties()) .containsOnly(view.getId(), pc.getId()); }
@Test public void deleteNonRootComponentsInView_deletes_manual_measures_of_subviews_of_a_view() { ComponentDto view = db.components().insertView(); ComponentDto subview1 = db.components().insertComponent(ComponentTesting.newSubView(view)); ComponentDto subview2 = db.components().insertComponent(ComponentTesting.newSubView(subview1)); ComponentDto subview3 = db.components().insertComponent(ComponentTesting.newSubView(view)); ComponentDto pc = db.components().insertComponent(newProjectCopy("a", db.components().insertPrivateProject(), view)); insertManualMeasureFor(view, subview1, subview2, subview3, pc); assertThat(getComponentUuidsOfManualMeasures()).containsOnly(view.uuid(), subview1.uuid(), subview2.uuid(), subview3.uuid(), pc.uuid()); underTest.deleteNonRootComponentsInView(dbSession, singletonList(subview1)); assertThat(getComponentUuidsOfManualMeasures()) .containsOnly(view.uuid(), subview2.uuid(), subview3.uuid(), pc.uuid()); underTest.deleteNonRootComponentsInView(dbSession, asList(subview2, subview3, pc)); assertThat(getComponentUuidsOfManualMeasures()) .containsOnly(view.uuid(), pc.uuid()); }
@Test public void deleteNonRootComponents_deletes_only_specified_non_root_components_of_a_project_from_table_PROJECTS() { ComponentDto project = new Random().nextBoolean() ? db.components().insertPublicProject() : db.components().insertPrivateProject(); ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)); ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(module1)); ComponentDto dir1 = db.components().insertComponent(newDirectory(module1, "A/B")); ComponentDto dir2 = db.components().insertComponent(newDirectory(module2, "A/C")); ComponentDto file1 = db.components().insertComponent(newFileDto(dir1)); ComponentDto file2 = db.components().insertComponent(newFileDto(module2)); ComponentDto file3 = db.components().insertComponent(newFileDto(project)); underTest.deleteNonRootComponentsInView(dbSession, singletonList(file3)); assertThat(uuidsIn("projects")) .containsOnly(project.uuid(), module1.uuid(), module2.uuid(), dir1.uuid(), dir2.uuid(), file1.uuid(), file2.uuid()); underTest.deleteNonRootComponentsInView(dbSession, asList(module1, dir2, file1)); assertThat(uuidsIn("projects")) .containsOnly(project.uuid(), module2.uuid(), dir1.uuid(), file2.uuid()); }
@Test public void deleteNonRootComponents_deletes_only_non_root_components_of_a_project_from_table_PROJECTS() { ComponentDto project = new Random().nextBoolean() ? db.components().insertPublicProject() : db.components().insertPrivateProject(); ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)); ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(module1)); ComponentDto dir1 = db.components().insertComponent(newDirectory(module1, "A/B")); List<ComponentDto> components = asList( project, module1, module2, dir1, db.components().insertComponent(newDirectory(module2, "A/C")), db.components().insertComponent(newFileDto(dir1)), db.components().insertComponent(newFileDto(module2)), db.components().insertComponent(newFileDto(project))); Collections.shuffle(components); underTest.deleteNonRootComponentsInView(dbSession, components); assertThat(uuidsIn("projects")) .containsOnly(project.uuid()); }
@Test public void deleteNonRootComponents_deletes_only_specified_non_root_components_of_a_view_from_table_PROJECTS() { ComponentDto[] projects = { db.components().insertPrivateProject(), db.components().insertPrivateProject(), db.components().insertPrivateProject() }; ComponentDto view = db.components().insertView(); ComponentDto subview1 = db.components().insertComponent(ComponentTesting.newSubView(view)); ComponentDto subview2 = db.components().insertComponent(ComponentTesting.newSubView(subview1)); ComponentDto pc1 = db.components().insertComponent(newProjectCopy("a", projects[0], view)); ComponentDto pc2 = db.components().insertComponent(newProjectCopy("b", projects[1], subview1)); ComponentDto pc3 = db.components().insertComponent(newProjectCopy("c", projects[2], subview2)); underTest.deleteNonRootComponentsInView(dbSession, singletonList(pc3)); assertThat(uuidsIn("projects")) .containsOnly(view.uuid(), projects[0].uuid(), projects[1].uuid(), projects[2].uuid(), subview1.uuid(), subview2.uuid(), pc1.uuid(), pc2.uuid()); underTest.deleteNonRootComponentsInView(dbSession, asList(subview1, pc2)); assertThat(uuidsIn("projects")) .containsOnly(view.uuid(), projects[0].uuid(), projects[1].uuid(), projects[2].uuid(), subview2.uuid(), pc1.uuid()); }
@Test public void deleteNonRootComponents_deletes_only_non_root_components_of_a_view_from_table_PROJECTS() { ComponentDto[] projects = { db.components().insertPrivateProject(), db.components().insertPrivateProject(), db.components().insertPrivateProject() }; ComponentDto view = db.components().insertView(); ComponentDto subview1 = db.components().insertComponent(ComponentTesting.newSubView(view)); ComponentDto subview2 = db.components().insertComponent(ComponentTesting.newSubView(subview1)); List<ComponentDto> components = asList( view, subview1, subview2, db.components().insertComponent(newProjectCopy("a", projects[0], view)), db.components().insertComponent(newProjectCopy("b", projects[1], subview1)), db.components().insertComponent(newProjectCopy("c", projects[2], subview2))); Collections.shuffle(components); underTest.deleteNonRootComponentsInView(dbSession, components); assertThat(uuidsIn("projects")) .containsOnly(view.uuid(), projects[0].uuid(), projects[1].uuid(), projects[2].uuid()); }