@Test public void select_by_id() { ComponentDto project = db.components().insertPrivateProject(); ProjectLinkDto link = db.componentLinks().insertProvidedLink(project, c -> c .setUuid("ABCD") .setName("Home") .setType("homepage") .setHref("http://www.struts.org")); ProjectLinkDto reloaded = underTest.selectByUuid(db.getSession(), link.getUuid()); assertThat(reloaded.getUuid()).isEqualTo("ABCD"); assertThat(reloaded.getProjectUuid()).isEqualTo(project.uuid()); assertThat(reloaded.getType()).isEqualTo("homepage"); assertThat(reloaded.getName()).isEqualTo("Home"); assertThat(reloaded.getHref()).isEqualTo("http://www.struts.org"); assertThat(reloaded.getCreatedAt()).isEqualTo(NOW); assertThat(reloaded.getUpdatedAt()).isEqualTo(NOW); }
private void mergeLinks(DbSession session, String componentUuid, List<ScannerReport.ComponentLink> links, List<ProjectLinkDto> previousLinks) { Set<String> linkType = new HashSet<>(); links.forEach( link -> { String type = convertType(link.getType()); checkArgument(!linkType.contains(type), "Link of type '%s' has already been declared on component '%s'", type, componentUuid); linkType.add(type); Optional<ProjectLinkDto> previousLink = previousLinks.stream() .filter(input -> input != null && input.getType().equals(convertType(link.getType()))) .findFirst(); if (previousLink.isPresent()) { previousLink.get().setHref(link.getHref()); dbClient.projectLinkDao().update(session, previousLink.get()); } else { dbClient.projectLinkDao().insert(session, new ProjectLinkDto() .setUuid(uuidFactory.create()) .setProjectUuid(componentUuid) .setType(type) .setHref(link.getHref())); } }); previousLinks.stream() .filter(dto -> !linkType.contains(dto.getType())) .filter(dto -> ProjectLinkDto.PROVIDED_TYPES.contains(dto.getType())) .forEach(dto -> dbClient.projectLinkDao().delete(session, dto.getUuid())); }
private CreateWsResponse doHandle(CreateRequest createWsRequest) { String name = createWsRequest.getName(); String url = createWsRequest.getUrl(); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto component = checkProject(getComponentByUuidOrKey(dbSession, createWsRequest)); userSession.checkComponentPermission(UserRole.ADMIN, component); ProjectLinkDto link = new ProjectLinkDto() .setUuid(uuidFactory.create()) .setProjectUuid(component.uuid()) .setName(name) .setHref(url) .setType(nameToType(name)); dbClient.projectLinkDao().insert(dbSession, link); dbSession.commit(); return buildResponse(link); } }
@Test public void delete_link() { mockBranch(true); ComponentDto project = db.components().insertPrivateProject(p -> p.setUuid("ABCD")); db.componentLinks().insertProvidedLink(project, l -> l.setType("homepage").setName("Home").setHref("http://www.sonar.org")); treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build()); reportReader.putComponent(ScannerReport.Component.newBuilder() .setRef(1) .setType(ComponentType.PROJECT) .build()); underTest.execute(new TestComputationStepContext()); assertThat(db.countRowsOfTable("project_links")).isZero(); }
private static ProjectLinkDto newCommonLinkDto() { return new ProjectLinkDto() .setUuid(Uuids.createFast()) .setProjectUuid(Uuids.createFast()) .setHref(randomAlphanumeric(128)) .setCreatedAt(System.currentTimeMillis()) .setUpdatedAt(System.currentTimeMillis()); }
@Test public void example() { ComponentDto project = db.components().insertPrivateProject(); db.componentLinks().insertProvidedLink(project, l -> l.setUuid("1").setType("homepage").setName("Homepage").setHref("http://example.org")); db.componentLinks().insertCustomLink(project, l -> l.setUuid("2").setType("custom").setName("Custom").setHref("http://example.org/custom")); logInAsProjectAdministrator(project); String result = ws.newRequest() .setParam(PARAM_PROJECT_KEY, project.getKey()) .execute().getInput(); assertJson(result).isSimilarTo(getClass().getResource("search-example.json")); }
@Test public void request_by_project_key() { ComponentDto project = db.components().insertPrivateProject(); ProjectLinkDto link = db.componentLinks().insertCustomLink(project); logInAsProjectAdministrator(project); SearchWsResponse response = callByKey(project.getKey()); assertThat(response.getLinksList()) .extracting(Link::getId, Link::getName) .containsExactlyInAnyOrder(tuple(link.getUuid(), link.getName())); }
@Test public void select_by_project_uuids() { ComponentDto project1 = db.components().insertPrivateProject(); ProjectLinkDto link1 = db.componentLinks().insertProvidedLink(project1); ProjectLinkDto link2 = db.componentLinks().insertProvidedLink(project1); ComponentDto project2 = db.components().insertPrivateProject(); ProjectLinkDto link3 = db.componentLinks().insertProvidedLink(project2); assertThat(underTest.selectByProjectUuids(db.getSession(), asList(project1.uuid(), project2.uuid()))) .extracting(ProjectLinkDto::getUuid) .containsOnly(link1.getUuid(), link2.getUuid(), link3.getUuid()); assertThat(underTest.selectByProjectUuids(db.getSession(), singletonList(project1.uuid()))) .extracting(ProjectLinkDto::getUuid) .containsOnly(link1.getUuid(), link2.getUuid()); assertThat(underTest.selectByProjectUuids(db.getSession(), Collections.emptyList())).isEmpty(); }
private static void checkNotProvided(ProjectLinkDto link) { String type = link.getType(); boolean isProvided = type != null && PROVIDED_TYPES.contains(type); WsUtils.checkRequest(!isProvided, "Provided link cannot be deleted."); }
@SafeVarargs private final ProjectLinkDto insertLink(ComponentDto project, ProjectLinkDto componentLink, Consumer<ProjectLinkDto>... dtoPopulators) { Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(componentLink)); dbClient.projectLinkDao().insert(dbSession, componentLink.setProjectUuid(project.uuid())); db.commit(); return componentLink; } }
private CreateWsResponse doHandle(CreateRequest createWsRequest) { String name = createWsRequest.getName(); String url = createWsRequest.getUrl(); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto component = checkProject(getComponentByUuidOrKey(dbSession, createWsRequest)); userSession.checkComponentPermission(UserRole.ADMIN, component); ProjectLinkDto link = new ProjectLinkDto() .setUuid(uuidFactory.create()) .setProjectUuid(component.uuid()) .setName(name) .setHref(url) .setType(nameToType(name)); dbClient.projectLinkDao().insert(dbSession, link); dbSession.commit(); return buildResponse(link); } }
@Test public void search_json_example() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto jdk7 = insertJdk7(organizationDto); ComponentDto cLang = insertClang(organizationDto); db.componentLinks().insertProvidedLink(jdk7, l -> l.setHref("http://www.oracle.com").setType(ProjectLinkDto.TYPE_HOME_PAGE).setName("Home")); db.componentLinks().insertProvidedLink(jdk7, l -> l.setHref("http://download.java.net/openjdk/jdk8/").setType(ProjectLinkDto.TYPE_SOURCES).setName("Sources")); long oneTime = DateUtils.parseDateTime("2016-06-10T13:17:53+0000").getTime(); long anotherTime = DateUtils.parseDateTime("2016-06-11T14:25:53+0000").getTime(); SnapshotDto jdk7Snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(jdk7).setCreatedAt(oneTime)); SnapshotDto cLangSnapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(cLang).setCreatedAt(anotherTime)); dbClient.liveMeasureDao().insert(dbSession, newLiveMeasure(jdk7, alertStatusMetric).setData(Level.ERROR.name())); dbClient.liveMeasureDao().insert(dbSession, newLiveMeasure(cLang, alertStatusMetric).setData(Level.OK.name())); db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, jdk7); db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, cLang); db.commit(); System.setProperty("user.timezone", "UTC"); String result = ws.newRequest().execute().getInput(); assertJson(result).isSimilarTo(getClass().getResource("search_my_projects-example.json")); }
private void createAndTest(ComponentDto project, String name, String url, String type) { ProjectLinks.CreateWsResponse response = ws.newRequest() .setMethod("POST") .setParam(PARAM_PROJECT_KEY, project.getDbKey()) .setParam(PARAM_NAME, name) .setParam(PARAM_URL, url) .executeProtobuf(ProjectLinks.CreateWsResponse.class); String newId = response.getLink().getId(); ProjectLinkDto link = dbClient.projectLinkDao().selectByUuid(dbSession, newId); assertThat(link.getName()).isEqualTo(name); assertThat(link.getHref()).isEqualTo(url); assertThat(link.getType()).isEqualTo(type); }
@Test public void request_by_project_id() { ComponentDto project = db.components().insertPrivateProject(); ProjectLinkDto link = db.componentLinks().insertCustomLink(project); logInAsProjectAdministrator(project); SearchWsResponse response = callByUuid(project.uuid()); assertThat(response.getLinksList()) .extracting(Link::getId, Link::getName) .containsExactlyInAnyOrder(tuple(link.getUuid(), link.getName())); }
private TestResponse deleteLink(ProjectLinkDto link) { return ws.newRequest() .setMethod("POST") .setParam(PARAM_ID, link.getUuid()) .execute(); }