*/
package org.sonar.db.rule;
-import com.google.common.base.Optional;
import java.util.Collection;
import java.util.List;
+import java.util.Optional;
import javax.annotation.Nullable;
import org.apache.ibatis.session.ResultHandler;
import org.sonar.api.rule.RuleKey;
public Optional<RuleDto> selectByKey(DbSession session, String organizationUuid, RuleKey key) {
RuleDto res = mapper(session).selectByKey(organizationUuid, key);
ensureOrganizationIsSet(organizationUuid, res);
- return Optional.fromNullable(res);
+ return Optional.ofNullable(res);
}
public Optional<RuleDefinitionDto> selectDefinitionByKey(DbSession session, RuleKey key) {
- return Optional.fromNullable(mapper(session).selectDefinitionByKey(key));
+ return Optional.ofNullable(mapper(session).selectDefinitionByKey(key));
}
public java.util.Optional<RuleMetadataDto> selectMetadataByKey(DbSession session, RuleKey key, OrganizationDto organization) {
public Optional<RuleDto> selectById(long id, String organizationUuid, DbSession session) {
RuleDto res = mapper(session).selectById(organizationUuid, id);
ensureOrganizationIsSet(organizationUuid, res);
- return Optional.fromNullable(res);
+ return Optional.ofNullable(res);
}
public Optional<RuleDefinitionDto> selectDefinitionById(long id, DbSession session) {
- return Optional.fromNullable(mapper(session).selectDefinitionById(id));
+ return Optional.ofNullable(mapper(session).selectDefinitionById(id));
}
public List<RuleDto> selectByIds(DbSession session, String organizationUuid, List<Integer> ids) {
*/
package org.sonar.db.rule;
-import com.google.common.base.Optional;
import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
import org.apache.ibatis.session.ResultHandler;
import org.junit.Rule;
import org.junit.Test;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.guava.api.Assertions.assertThat;
public class RuleDaoTest {
dbTester.prepareDbUnit(getClass(), "shared.xml");
String organizationUuid = "org-1";
- assertThat(underTest.selectById(55l, organizationUuid, dbTester.getSession())).isAbsent();
+ assertThat(underTest.selectById(55l, organizationUuid, dbTester.getSession())).isEmpty();
Optional<RuleDto> ruleDtoOptional = underTest.selectById(1l, organizationUuid, dbTester.getSession());
assertThat(ruleDtoOptional).isPresent();
assertThat(ruleDtoOptional.get().getId()).isEqualTo(1);
public void selectDefinitionById() {
dbTester.prepareDbUnit(getClass(), "shared.xml");
- assertThat(underTest.selectDefinitionById(55l, dbTester.getSession())).isAbsent();
+ assertThat(underTest.selectDefinitionById(55l, dbTester.getSession())).isEmpty();
Optional<RuleDefinitionDto> ruleDtoOptional = underTest.selectDefinitionById(1l, dbTester.getSession());
assertThat(ruleDtoOptional).isPresent();
assertThat(ruleDtoOptional.get().getId()).isEqualTo(1);
String organizationUuid = "org-1";
assertThat(underTest.selectByQuery(dbTester.getSession(), organizationUuid, RuleQuery.create()))
- .extracting(RuleDto::getOrganizationUuid)
- .containsExactly(organizationUuid, organizationUuid);
+ .extracting(RuleDto::getOrganizationUuid)
+ .containsExactly(organizationUuid, organizationUuid);
}
@Test
String organizationUuid = "org-1";
OrganizationDto organization = OrganizationTesting.newOrganizationDto().setUuid(organizationUuid);
RuleMetadataDto metadataV1 = new RuleMetadataDto()
- .setRuleId(1)
- .setOrganizationUuid(organizationUuid)
- .setCreatedAt(3_500_000_000_000L)
- .setUpdatedAt(4_000_000_000_000L);
+ .setRuleId(1)
+ .setOrganizationUuid(organizationUuid)
+ .setCreatedAt(3_500_000_000_000L)
+ .setUpdatedAt(4_000_000_000_000L);
RuleMetadataDto metadataV2 = new RuleMetadataDto()
- .setRuleId(1)
- .setOrganizationUuid(organizationUuid)
- .setNoteData("My note")
- .setNoteUserLogin("admin")
- .setNoteCreatedAt(DateUtils.parseDate("2013-12-19").getTime())
- .setNoteUpdatedAt(DateUtils.parseDate("2013-12-20").getTime())
- .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
- .setRemediationGapMultiplier("1h")
- .setRemediationBaseEffort("5min")
- .setTags(newHashSet("tag1", "tag2"))
- .setCreatedAt(6_500_000_000_000L)
- .setUpdatedAt(7_000_000_000_000L);
+ .setRuleId(1)
+ .setOrganizationUuid(organizationUuid)
+ .setNoteData("My note")
+ .setNoteUserLogin("admin")
+ .setNoteCreatedAt(DateUtils.parseDate("2013-12-19").getTime())
+ .setNoteUpdatedAt(DateUtils.parseDate("2013-12-20").getTime())
+ .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
+ .setRemediationGapMultiplier("1h")
+ .setRemediationBaseEffort("5min")
+ .setTags(newHashSet("tag1", "tag2"))
+ .setCreatedAt(6_500_000_000_000L)
+ .setUpdatedAt(7_000_000_000_000L);
underTest.insertOrUpdate(dbTester.getSession(), metadataV1);
dbTester.commit();
}
private Optional<RuleDefinitionDto> getRuleByKey(DbSession session, RuleKey ruleKey) {
- Optional<RuleDefinitionDto> rule = Optional.ofNullable(dbClient.ruleDao().selectDefinitionByKey(session, ruleKey).orNull());
+ Optional<RuleDefinitionDto> rule = Optional.ofNullable(dbClient.ruleDao().selectDefinitionByKey(session, ruleKey).orElse(null));
return (rule.isPresent() && rule.get().getStatus() != RuleStatus.REMOVED) ? rule : Optional.empty();
}
}
}
Optional<RuleDefinitionDto> getRule(DbSession dbSession, RuleKey ruleKey) {
- return Optional.ofNullable(db.ruleDao().selectDefinitionByKey(dbSession, ruleKey).orNull());
+ return Optional.ofNullable(db.ruleDao().selectDefinitionByKey(dbSession, ruleKey).orElse(null));
}
Optional<ActiveRuleDto> getActiveRule(DbSession session, ActiveRuleKey key) {
package org.sonar.server.rule;
import com.google.common.base.Function;
-import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableListMultimap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
+import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.sonar.api.rule.RuleKey;
*/
package org.sonar.server.rule;
-import com.google.common.base.Optional;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.apache.commons.lang.ObjectUtils;
}
private Optional<RuleDefinitionDto> loadRule(RuleKey ruleKey, DbSession dbSession) {
- return dbClient.ruleDao().selectDefinitionByKey(dbSession, ruleKey).transform(Optional::of).or(Optional::empty);
+ return dbClient.ruleDao().selectDefinitionByKey(dbSession, ruleKey);
}
private RuleKey createCustomRule(RuleKey ruleKey, NewCustomRule newRule, RuleDto templateRuleDto, DbSession dbSession) {
package org.sonar.server.rule;
import com.google.common.base.Function;
-import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
*/
package org.sonar.server.rule.ws;
-import com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
private Rules.CreateResponse createResponse(DbSession dbSession, RuleKey ruleKey) {
RuleDefinitionDto rule = dbClient.ruleDao().selectDefinitionByKey(dbSession, ruleKey)
- .transform(java.util.Optional::of).or(java.util.Optional::empty)
.orElseThrow(() -> new IllegalStateException(String.format("Cannot load rule, that has just been created '%s'", ruleKey)));
List<RuleDefinitionDto> templateRules = new ArrayList<>();
if (rule.getTemplateId() != null) {
*/
package org.sonar.server.rule.ws;
-import com.google.common.base.Optional;
import com.google.common.io.Resources;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
-import java.util.Optional;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
private RuleUpdate createRuleUpdate(DbSession dbSession, RuleKey key, OrganizationDto organization) {
RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization, key)
- .transform(Optional::of).or(Optional::empty)
.orElseThrow(() -> new NotFoundException(format("This rule does not exist: %s", key)));
RuleUpdate ruleUpdate = ofNullable(rule.getTemplateId())
.map(x -> RuleUpdate.createForCustomRule(key))
private UpdateResponse buildResponse(DbSession dbSession, RuleKey key, OrganizationDto organization) {
RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization, key)
- .transform(Optional::of).or(Optional::empty)
.orElseThrow(() -> new NotFoundException(format("Rule not found: %s", key)));
List<RuleDefinitionDto> templateRules = new ArrayList<>(1);
if (rule.getTemplateId() != null) {
dbClient.ruleDao().selectDefinitionById(rule.getTemplateId(), dbSession)
- .transform(Optional::of).or(Optional::empty)
.ifPresent(templateRules::add);
}
List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, singletonList(rule.getId()));