import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ProjectLinkDto;
+import org.sonar.db.project.ProjectDto;
import org.sonar.db.project.ProjectExportMapper;
import static org.assertj.core.api.Assertions.assertThat;
public LogTester logTester = new LogTester();
private final FakeDumpWriter dumpWriter = new FakeDumpWriter();
- private final ComponentRepository componentRepository = mock(ComponentRepository.class);
private final ProjectHolder projectHolder = mock(ProjectHolder.class);
- private final ExportLinksStep underTest = new ExportLinksStep(db.getDbClient(), componentRepository, projectHolder, dumpWriter);
+ private final ExportLinksStep underTest = new ExportLinksStep(db.getDbClient(), projectHolder, dumpWriter);
+ private ProjectDto project;
@Before
public void setUp() {
logTester.setLevel(Level.DEBUG);
- ComponentDto project = db.components().insertPublicProject(PROJECT).getMainBranchComponent();
- when(projectHolder.projectDto()).thenReturn(db.components().getProjectDtoByMainBranch(project));
- when(componentRepository.getRef(PROJECT_UUID)).thenReturn(1L);
+ project = db.components().insertPublicProject(PROJECT).getProjectDto();
+ when(projectHolder.projectDto()).thenReturn(project);
}
@Test
@Test
public void export_links() {
- ProjectLinkDto link1 = db.componentLinks().insertCustomLink(PROJECT);
- ProjectLinkDto link2 = db.componentLinks().insertProvidedLink(PROJECT);
- db.componentLinks().insertCustomLink(db.components().insertPrivateProject().getMainBranchComponent());
+ ProjectLinkDto link1 = db.componentLinks().insertCustomLink(project);
+ ProjectLinkDto link2 = db.componentLinks().insertProvidedLink(project);
+ db.componentLinks().insertCustomLink(db.components().insertPrivateProject().getProjectDto());
underTest.execute(new TestComputationStepContext());
@Test
public void throws_ISE_if_error() {
- db.componentLinks().insertCustomLink(PROJECT);
- db.componentLinks().insertProvidedLink(PROJECT);
- db.componentLinks().insertProvidedLink(PROJECT);
- db.componentLinks().insertCustomLink(db.components().insertPrivateProject().getMainBranchComponent());
+ db.componentLinks().insertCustomLink(project);
+ db.componentLinks().insertProvidedLink(project);
+ db.componentLinks().insertProvidedLink(project);
+ db.componentLinks().insertCustomLink(db.components().insertPrivateProject().getProjectDto());
dumpWriter.failIfMoreThan(2, DumpElement.LINKS);
@Test
public void test_all_fields() {
- ProjectLinkDto link = db.componentLinks().insertCustomLink(PROJECT, l -> l.setName("name").setHref("href").setType("type"));
+ ProjectLinkDto link = db.componentLinks().insertCustomLink(project, l -> l.setName("name").setHref("href").setType("type"));
underTest.execute(new TestComputationStepContext());
public class ExportLinksStep implements ComputationStep {
private final DbClient dbClient;
- private final ComponentRepository componentRepository;
private final ProjectHolder projectHolder;
private final DumpWriter dumpWriter;
- public ExportLinksStep(DbClient dbClient, ComponentRepository componentRepository, ProjectHolder projectHolder, DumpWriter dumpWriter) {
+ public ExportLinksStep(DbClient dbClient, ProjectHolder projectHolder, DumpWriter dumpWriter) {
this.dbClient = dbClient;
- this.componentRepository = componentRepository;
this.projectHolder = projectHolder;
this.dumpWriter = dumpWriter;
}
.setUuid(link.getUuid())
.setName(defaultString(link.getName()))
.setHref(defaultString(link.getHref()))
- .setType(defaultString(link.getType()))
- .setComponentRef(componentRepository.getRef(link.getProjectUuid()));
+ .setType(defaultString(link.getType()));
linksWriter.write(builder.build());
++count;
}
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.project.ProjectDto;
import static org.sonar.db.component.ProjectLinkTesting.newCustomLinkDto;
import static org.sonar.db.component.ProjectLinkTesting.newProvidedLinkDto;
db.commit();
return componentLink;
}
+
+ @SafeVarargs
+ public final ProjectLinkDto insertProvidedLink(ProjectDto project, Consumer<ProjectLinkDto>... dtoPopulators) {
+ return insertLink(project, newProvidedLinkDto(), dtoPopulators);
+ }
+
+ @SafeVarargs
+ public final ProjectLinkDto insertCustomLink(ProjectDto project, Consumer<ProjectLinkDto>... dtoPopulators) {
+ return insertLink(project, newCustomLinkDto(), dtoPopulators);
+ }
+
+ @SafeVarargs
+ private final ProjectLinkDto insertLink(ProjectDto project, ProjectLinkDto componentLink, Consumer<ProjectLinkDto>... dtoPopulators) {
+ Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(componentLink));
+ dbClient.projectLinkDao().insert(dbSession, componentLink.setProjectUuid(project.getUuid()));
+ db.commit();
+ return componentLink;
+ }
+
}