Skip to content

Commit

Permalink
remove usage of Guava Optional from ComponentDao
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb authored and SonarTech committed Oct 4, 2018
1 parent 2e59950 commit e80c0f3
Show file tree
Hide file tree
Showing 37 changed files with 91 additions and 94 deletions.
Expand Up @@ -95,7 +95,7 @@ private java.util.Optional<CeTask> submit(CeTaskSubmit submission, EnumSet<Submi
ComponentDto component = null;
String componentUuid = taskDto.getComponentUuid();
if (componentUuid != null) {
component = dbClient.componentDao().selectByUuid(dbSession, componentUuid).orNull();
component = dbClient.componentDao().selectByUuid(dbSession, componentUuid).orElse(null);
}
CeTask task = convertToTask(taskDto, submission.getCharacteristics(), component);
return java.util.Optional.of(task);
Expand Down
Expand Up @@ -20,16 +20,17 @@
package org.sonar.ce.task.projectanalysis.component;

import java.util.Date;
import java.util.Optional;
import javax.annotation.Nullable;
import org.sonar.api.utils.System2;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.analysis.Branch;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.BranchDto;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.protobuf.DbProjectBranches;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.analysis.Branch;

import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
Expand All @@ -51,15 +52,15 @@ public void persist(DbSession dbSession) {
Branch branch = analysisMetadataHolder.getBranch();
String branchUuid = treeRootHolder.getRoot().getUuid();

com.google.common.base.Optional<ComponentDto> branchComponentDtoOpt = dbClient.componentDao().selectByUuid(dbSession, branchUuid);
Optional<ComponentDto> branchComponentDtoOpt = dbClient.componentDao().selectByUuid(dbSession, branchUuid);

ComponentDto branchComponentDto;
if (branch.isMain()) {
checkState(branchComponentDtoOpt.isPresent(), "Project has been deleted by end-user during analysis");
branchComponentDto = branchComponentDtoOpt.get();
} else {
// inserts new row in table projects if it's the first time branch is analyzed
branchComponentDto = branchComponentDtoOpt.or(() -> insertIntoProjectsTable(dbSession, branchUuid));
branchComponentDto = branchComponentDtoOpt.orElseGet(() -> insertIntoProjectsTable(dbSession, branchUuid));
}

// insert or update in table project_branches
Expand Down
Expand Up @@ -216,7 +216,7 @@ private Organization toOrganization(DbSession dbSession, String organizationUuid

private ComponentDto toProject(String projectKey) {
try (DbSession dbSession = dbClient.openSession(false)) {
com.google.common.base.Optional<ComponentDto> opt = dbClient.componentDao().selectByKey(dbSession, projectKey);
Optional<ComponentDto> opt = dbClient.componentDao().selectByKey(dbSession, projectKey);
checkState(opt.isPresent(), "Project with key '%s' can't be found", projectKey);
return opt.get();
}
Expand Down
Expand Up @@ -20,11 +20,11 @@
package org.sonar.ce.task.projectanalysis.step;

import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.sonar.api.utils.MessageException;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
Expand Down Expand Up @@ -119,7 +119,7 @@ public void visitProject(Component rawProject) {

private void validateAnalysisDate(Optional<ComponentDto> baseProject) {
if (baseProject.isPresent()) {
java.util.Optional<SnapshotDto> snapshotDto = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(session, baseProject.get().uuid());
Optional<SnapshotDto> snapshotDto = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(session, baseProject.get().uuid());
long currentAnalysisDate = analysisMetadataHolder.getAnalysisDate();
Long lastAnalysisDate = snapshotDto.map(SnapshotDto::getCreatedAt).orElse(null);
if (lastAnalysisDate != null && currentAnalysisDate <= lastAnalysisDate) {
Expand Down
Expand Up @@ -571,7 +571,7 @@ private String[] readLines(File filename) throws IOException {
@CheckForNull
private FileSourceDto insertContentOfFileInDb(String key, @Nullable String[] content) {
return dbTester.getDbClient().componentDao().selectByKey(dbTester.getSession(), key)
.transform(file -> {
.map(file -> {
SourceLineHashesComputer linesHashesComputer = new SourceLineHashesComputer();
if (content != null) {
stream(content).forEach(linesHashesComputer::addLine);
Expand All @@ -584,7 +584,7 @@ private FileSourceDto insertContentOfFileInDb(String key, @Nullable String[] con
dbTester.getDbClient().fileSourceDao().insert(dbTester.getSession(), fileSourceDto);
dbTester.commit();
return fileSourceDto;
}).orNull();
}).orElse(null);
}

private void setFilesInReport(Component... files) {
Expand Down
Expand Up @@ -19,9 +19,9 @@
*/
package org.sonar.ce.task.projectanalysis.step;

import com.google.common.base.Optional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -54,7 +54,6 @@
import static org.apache.commons.lang.StringUtils.isEmpty;
import static org.apache.commons.lang.StringUtils.trimToNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down
Expand Up @@ -93,7 +93,7 @@ public Optional<CeTask> peek(String workerUuid) {
ComponentDto component = null;
String componentUuid = taskDto.getComponentUuid();
if (componentUuid != null) {
component = dbClient.componentDao().selectByUuid(dbSession, componentUuid).orNull();
component = dbClient.componentDao().selectByUuid(dbSession, componentUuid).orElse(null);
}
Map<String, String> characteristics = dbClient.ceTaskCharacteristicsDao().selectByTaskUuids(dbSession, singletonList(taskDto.getUuid())).stream()
.collect(uniqueIndex(CeTaskCharacteristicDto::getKey, CeTaskCharacteristicDto::getValue));
Expand Down
Expand Up @@ -19,15 +19,15 @@
*/
package org.sonar.db.component;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -86,11 +86,11 @@ private static ComponentMapper mapper(DbSession session) {
}

public Optional<ComponentDto> selectById(DbSession session, long id) {
return Optional.fromNullable(mapper(session).selectById(id));
return Optional.ofNullable(mapper(session).selectById(id));
}

public Optional<ComponentDto> selectByUuid(DbSession session, String uuid) {
return Optional.fromNullable(mapper(session).selectByUuid(uuid));
return Optional.ofNullable(mapper(session).selectByUuid(uuid));
}

public ComponentDto selectOrFailByUuid(DbSession session, String uuid) {
Expand Down Expand Up @@ -258,15 +258,15 @@ public ComponentDto selectOrFailByKey(DbSession session, String key) {
}

public Optional<ComponentDto> selectByKey(DbSession session, String key) {
return Optional.fromNullable(mapper(session).selectByKey(key));
return Optional.ofNullable(mapper(session).selectByKey(key));
}

public java.util.Optional<ComponentDto> selectByKeyAndBranch(DbSession session, String key, String branch) {
return java.util.Optional.ofNullable(mapper(session).selectByKeyAndBranchKey(key, generateBranchKey(key, branch), branch));
public Optional<ComponentDto> selectByKeyAndBranch(DbSession session, String key, String branch) {
return Optional.ofNullable(mapper(session).selectByKeyAndBranchKey(key, generateBranchKey(key, branch), branch));
}

public java.util.Optional<ComponentDto> selectByKeyAndPullRequest(DbSession session, String key, String pullRequestId) {
return java.util.Optional.ofNullable(mapper(session).selectByKeyAndBranchKey(key, generatePullRequestKey(key, pullRequestId), pullRequestId));
public Optional<ComponentDto> selectByKeyAndPullRequest(DbSession session, String key, String pullRequestId) {
return Optional.ofNullable(mapper(session).selectByKeyAndBranchKey(key, generatePullRequestKey(key, pullRequestId), pullRequestId));
}

public List<UuidWithProjectUuidDto> selectAllViewsAndSubViews(DbSession session) {
Expand Down Expand Up @@ -366,13 +366,15 @@ public void insert(DbSession session, ComponentDto item) {
}

public void insert(DbSession session, Collection<ComponentDto> items) {
for (ComponentDto item : items) {
insert(session, item);
}
insert(session, items.stream());
}

private void insert(DbSession session, Stream<ComponentDto> items) {
items.forEach(item -> insert(session, item));
}

public void insert(DbSession session, ComponentDto item, ComponentDto... others) {
insert(session, Lists.asList(item, others));
insert(session, Stream.concat(Stream.of(item), Arrays.stream(others)));
}

public void update(DbSession session, ComponentUpdateDto component) {
Expand Down
Expand Up @@ -19,7 +19,6 @@
*/
package org.sonar.db.component;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
Expand All @@ -29,6 +28,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.function.Consumer;
Expand Down Expand Up @@ -68,7 +68,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.guava.api.Assertions.assertThat;
import static org.sonar.api.resources.Qualifiers.APP;
import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.api.utils.DateUtils.parseDate;
Expand Down Expand Up @@ -136,7 +135,7 @@ public void get_by_uuid() {
assertThat(result.getCopyResourceUuid()).isNull();
assertThat(result.isPrivate()).isTrue();

assertThat(underTest.selectByUuid(dbSession, "UNKNOWN")).isAbsent();
assertThat(underTest.selectByUuid(dbSession, "UNKNOWN")).isEmpty();
}

@Test
Expand Down Expand Up @@ -211,7 +210,7 @@ public void selectByKey() {
assertThat(result.language()).isEqualTo("java");
assertThat(result.projectUuid()).isEqualTo(project.uuid());

assertThat(underTest.selectByKey(dbSession, "unknown")).isAbsent();
assertThat(underTest.selectByKey(dbSession, "unknown")).isEmpty();
}

@Test
Expand Down Expand Up @@ -393,7 +392,7 @@ public void get_nullable_by_id() {
ComponentDto project = db.components().insertPrivateProject();

assertThat(underTest.selectById(dbSession, project.getId())).isPresent();
assertThat(underTest.selectById(dbSession, 0L)).isAbsent();
assertThat(underTest.selectById(dbSession, 0L)).isEmpty();
}

@Test
Expand Down Expand Up @@ -1437,7 +1436,7 @@ public void delete() {
underTest.delete(dbSession, project1.getId());
dbSession.commit();

assertThat(underTest.selectByKey(dbSession, "PROJECT_1")).isAbsent();
assertThat(underTest.selectByKey(dbSession, "PROJECT_1")).isEmpty();
assertThat(underTest.selectByKey(dbSession, "PROJECT_2")).isPresent();
}

Expand Down
Expand Up @@ -66,11 +66,11 @@ public boolean isEnabled(ComponentDto projectDto) {
private Stream<WebhookDto> readWebHooksFrom(String projectUuid) {
try (DbSession dbSession = dbClient.openSession(false)) {

Optional<ComponentDto> optionalComponentDto = ofNullable(dbClient.componentDao().selectByUuid(dbSession, projectUuid).orNull());
Optional<ComponentDto> optionalComponentDto = ofNullable(dbClient.componentDao().selectByUuid(dbSession, projectUuid).orElse(null));
ComponentDto componentDto = checkStateWithOptional(optionalComponentDto, "the requested project '%s' was not found", projectUuid);

if (componentDto.getMainBranchProjectUuid() != null && !componentDto.uuid().equals(componentDto.getMainBranchProjectUuid())) {
Optional<ComponentDto> mainBranchComponentDto = ofNullable(dbClient.componentDao().selectByUuid(dbSession, componentDto.getMainBranchProjectUuid()).orNull());
Optional<ComponentDto> mainBranchComponentDto = ofNullable(dbClient.componentDao().selectByUuid(dbSession, componentDto.getMainBranchProjectUuid()).orElse(null));
componentDto = checkStateWithOptional(mainBranchComponentDto, "the requested project '%s' was not found", projectUuid);
}

Expand Down
Expand Up @@ -19,11 +19,11 @@
*/
package org.sonar.server.ce.queue;

import com.google.common.base.Optional;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;
Expand Down Expand Up @@ -82,7 +82,7 @@ public CeTask submit(String organizationKey, String projectKey, @Nullable String
Optional<ComponentDto> component = dbClient.componentDao().selectByKey(dbSession, effectiveProjectKey);
validateProject(dbSession, component, projectKey);
ensureOrganizationIsConsistent(component, organizationDto);
ComponentDto project = component.or(() -> createProject(dbSession, organizationDto, projectKey, deprecatedBranch, projectName));
ComponentDto project = component.orElseGet(() -> createProject(dbSession, organizationDto, projectKey, deprecatedBranch, projectName));
checkScanPermission(project);
return submitReport(dbSession, reportInput, project, characteristics);
}
Expand Down
Expand Up @@ -92,7 +92,7 @@ private void checkPermission(DbSession dbSession, CeQueueDto ceQueueDto) {
if (componentUuid == null) {
throw insufficientPrivilegesException();
}
com.google.common.base.Optional<ComponentDto> component = dbClient.componentDao().selectByUuid(dbSession, componentUuid);
Optional<ComponentDto> component = dbClient.componentDao().selectByUuid(dbSession, componentUuid);
if (!component.isPresent()) {
throw insufficientPrivilegesException();
}
Expand Down
Expand Up @@ -95,12 +95,12 @@ public void handle(Request wsRequest, Response wsResponse) throws Exception {
Ce.TaskResponse.Builder wsTaskResponse = Ce.TaskResponse.newBuilder();
Optional<CeQueueDto> queueDto = dbClient.ceQueueDao().selectByUuid(dbSession, taskUuid);
if (queueDto.isPresent()) {
com.google.common.base.Optional<ComponentDto> component = loadComponent(dbSession, queueDto.get().getComponentUuid());
Optional<ComponentDto> component = loadComponent(dbSession, queueDto.get().getComponentUuid());
checkPermission(component);
wsTaskResponse.setTask(wsTaskFormatter.formatQueue(dbSession, queueDto.get()));
} else {
CeActivityDto ceActivityDto = WsUtils.checkFoundWithOptional(dbClient.ceActivityDao().selectByUuid(dbSession, taskUuid), "No activity found for task '%s'", taskUuid);
com.google.common.base.Optional<ComponentDto> component = loadComponent(dbSession, ceActivityDto.getComponentUuid());
Optional<ComponentDto> component = loadComponent(dbSession, ceActivityDto.getComponentUuid());
checkPermission(component);
Set<AdditionalField> additionalFields = AdditionalField.getFromRequest(wsRequest);
maskErrorStacktrace(ceActivityDto, additionalFields);
Expand All @@ -111,14 +111,14 @@ public void handle(Request wsRequest, Response wsResponse) throws Exception {
}
}

private com.google.common.base.Optional<ComponentDto> loadComponent(DbSession dbSession, @Nullable String projectUuid) {
private Optional<ComponentDto> loadComponent(DbSession dbSession, @Nullable String projectUuid) {
if (projectUuid == null) {
return com.google.common.base.Optional.absent();
return Optional.empty();
}
return dbClient.componentDao().selectByUuid(dbSession, projectUuid);
}

private void checkPermission(com.google.common.base.Optional<ComponentDto> component) {
private void checkPermission(Optional<ComponentDto> component) {
if (component.isPresent()) {
String orgUuid = component.get().getOrganizationUuid();
if (!userSession.hasPermission(OrganizationPermission.ADMINISTER, orgUuid) &&
Expand Down
Expand Up @@ -19,8 +19,8 @@
*/
package org.sonar.server.component;

import com.google.common.base.Optional;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.resources.Qualifiers;
Expand Down Expand Up @@ -129,23 +129,23 @@ private static Set<String> getRootQualifiers(ResourceTypes resourceTypes) {

public OrganizationDto getOrganization(DbSession dbSession, ComponentDto component) {
String organizationUuid = component.getOrganizationUuid();
java.util.Optional<OrganizationDto> organizationDto = dbClient.organizationDao().selectByUuid(dbSession, organizationUuid);
Optional<OrganizationDto> organizationDto = dbClient.organizationDao().selectByUuid(dbSession, organizationUuid);
return checkFoundWithOptional(organizationDto, "Organization with uuid '%s' not found", organizationUuid);
}

/**
* Components of the main branch won't be found
*/
public ComponentDto getByKeyAndBranch(DbSession dbSession, String key, String branch) {
java.util.Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch);
Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch);
if (componentDto.isPresent() && componentDto.get().isEnabled()) {
return componentDto.get();
}
throw new NotFoundException(format("Component '%s' on branch '%s' not found", key, branch));
}

public ComponentDto getByKeyAndPullRequest(DbSession dbSession, String key, String pullRequest) {
java.util.Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndPullRequest(dbSession, key, pullRequest);
Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndPullRequest(dbSession, key, pullRequest);
if (componentDto.isPresent() && componentDto.get().isEnabled()) {
return componentDto.get();
}
Expand Down
Expand Up @@ -20,13 +20,13 @@
package org.sonar.server.duplication.ws;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import java.io.Serializable;
import java.io.StringReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.xml.stream.XMLInputFactory;
Expand Down Expand Up @@ -90,9 +90,9 @@ private Duplication createDuplication(Map<String, ComponentDto> componentsByKey,
if (component == null) {
Optional<ComponentDto> componentDtoOptional;
if (branch != null) {
componentDtoOptional = Optional.fromNullable(componentDao.selectByKeyAndBranch(session, componentKey, branch).orElseGet(null));
componentDtoOptional = Optional.ofNullable(componentDao.selectByKeyAndBranch(session, componentKey, branch).orElseGet(null));
} else if (pullRequest != null) {
componentDtoOptional = Optional.fromNullable(componentDao.selectByKeyAndPullRequest(session, componentKey, pullRequest).orElseGet(null));
componentDtoOptional = Optional.ofNullable(componentDao.selectByKeyAndPullRequest(session, componentKey, pullRequest).orElseGet(null));
} else {
componentDtoOptional = componentDao.selectByKey(session, componentKey);
}
Expand Down

0 comments on commit e80c0f3

Please sign in to comment.