Skip to content

Commit

Permalink
Fix Quality flaws
Browse files Browse the repository at this point in the history
mainly by removing unused code
  • Loading branch information
Simon Brandhof authored and SonarTech committed May 10, 2018
1 parent 02befe2 commit e582be9
Show file tree
Hide file tree
Showing 28 changed files with 31 additions and 210 deletions.
Expand Up @@ -119,16 +119,6 @@ public void submit_returns_task_without_component_info_when_submit_has_none() {
verifyCeTask(taskSubmit, task, null);
}

@Test
public void submit_fails_with_ISE_if_paused() {
underTest.pauseSubmit();

expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Compute Engine does not currently accept new tasks");

submit(CeTaskTypes.REPORT, "PROJECT_1");
}

@Test
public void massSubmit_returns_tasks_for_each_CeTaskSubmit_populated_from_CeTaskSubmit_and_creates_CeQueue_row_for_each() {
CeTaskSubmit taskSubmit1 = createTaskSubmit(CeTaskTypes.REPORT, "PROJECT_1", "rob");
Expand Down Expand Up @@ -688,15 +678,6 @@ private CeQueueDto insertCeQueueDto(String uuid, CeQueueDto.Status status, int e
return dto;
}

@Test
public void pause_and_resume_submits() {
assertThat(underTest.isSubmitPaused()).isFalse();
underTest.pauseSubmit();
assertThat(underTest.isSubmitPaused()).isTrue();
underTest.resumeSubmit();
assertThat(underTest.isSubmitPaused()).isFalse();
}

private void verifyCeTask(CeTaskSubmit taskSubmit, CeTask task, @Nullable ComponentDto componentDto) {
if (componentDto == null) {
assertThat(task.getOrganizationUuid()).isEqualTo(defaultOrganizationProvider.get().getUuid());
Expand Down
Expand Up @@ -19,7 +19,11 @@
*/
package org.sonar.db;

public class DaoDatabaseUtils extends DatabaseUtils {
public class DaoUtils {

private DaoUtils() {
// prevent new instances
}

/**
* Returns an escaped value in parameter, with the desired wildcards. Suitable to be used in a like sql query<br />
Expand Down
Expand Up @@ -114,13 +114,6 @@ public void deleteByUuid(DbSession session, String uuid) {
mapper(session).deleteByUuid(uuid);
}

/**
* Update all rows with: STATUS='PENDING', STARTED_AT=NULL, UPDATED_AT={now}
*/
public void resetAllToPendingStatus(DbSession session) {
mapper(session).resetAllToPendingStatus(system2.now());
}

/**
* Update all tasks for the specified worker uuid which are not PENDING to:
* STATUS='PENDING', STARTED_AT=NULL, UPDATED_AT={now}.
Expand Down Expand Up @@ -179,7 +172,7 @@ private Optional<CeQueueDto> tryToPeek(DbSession session, EligibleTaskDto eligib

CeQueueDto result = mapper(session).selectByUuid(eligible.getUuid());
session.commit();
return Optional.of(result);
return Optional.ofNullable(result);
}

private static CeQueueMapper mapper(DbSession session) {
Expand Down
Expand Up @@ -63,8 +63,6 @@ public interface CeQueueMapper {

void insert(CeQueueDto dto);

void resetAllToPendingStatus(@Param("updatedAt") long updatedAt);

int resetToPendingForWorker(@Param("workerUuid") String workerUuid, @Param("updatedAt") long updatedAt);

int updateIf(@Param("uuid") String uuid,
Expand Down
Expand Up @@ -21,7 +21,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -37,11 +36,10 @@
import org.sonar.db.DbSession;

import static com.google.common.base.Preconditions.checkArgument;
import static java.nio.charset.StandardCharsets.UTF_8;

public class CeScannerContextDao implements Dao {

private static final Charset UTF_8 = Charset.forName("UTF-8");

private final System2 system;

public CeScannerContextDao(System2 system) {
Expand Down
Expand Up @@ -34,10 +34,6 @@ public void insert(DbSession dbSession, Collection<CeTaskCharacteristicDto> char
}
}

public List<CeTaskCharacteristicDto> selectByTaskUuid(DbSession dbSession, String taskUuid) {
return mapper(dbSession).selectByTaskUuid(taskUuid);
}

public List<CeTaskCharacteristicDto> selectByTaskUuids(DbSession dbSession, List<String> taskUuids) {
return executeLargeInputs(taskUuids, uuid -> mapper(dbSession).selectByTaskUuids(uuid));
}
Expand Down
Expand Up @@ -24,8 +24,6 @@

public interface CeTaskCharacteristicMapper {

List<CeTaskCharacteristicDto> selectByTaskUuid(@Param("taskUuid") String taskUuid);

List<CeTaskCharacteristicDto> selectByTaskUuids(@Param("taskUuids") List<String> taskUuids);

void insert(CeTaskCharacteristicDto taskCharacteristic);
Expand Down
Expand Up @@ -44,7 +44,7 @@
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.DatabaseUtils.checkThatNotTooManyConditions;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;
import static org.sonar.db.DatabaseUtils.executeLargeInputsIntoSet;
Expand Down
Expand Up @@ -34,7 +34,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import static org.apache.commons.lang.StringUtils.substringBeforeLast;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.component.ComponentValidator.checkComponentKey;
import static org.sonar.db.component.ComponentValidator.checkComponentLongName;
import static org.sonar.db.component.ComponentValidator.checkComponentName;
Expand Down
Expand Up @@ -27,7 +27,7 @@
import org.sonar.db.WildcardPosition;

import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;

public class ComponentQuery {
private final String nameOrKeyQuery;
Expand Down
Expand Up @@ -28,7 +28,7 @@

import static com.google.common.collect.Lists.newArrayList;
import static java.util.Objects.requireNonNull;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;

public class ComponentTreeQuery {
Expand Down
Expand Up @@ -30,7 +30,7 @@
import org.sonar.db.WildcardPosition;
import org.sonar.db.component.ComponentDto;

import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;

public class IssueDao implements Dao {
Expand Down
Expand Up @@ -28,7 +28,7 @@

import static com.google.common.collect.Lists.newArrayList;
import static java.util.Objects.requireNonNull;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;

public class MeasureTreeQuery {
Expand Down
Expand Up @@ -30,7 +30,7 @@
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.defaultIfBlank;
import static org.sonar.api.utils.Paging.offset;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;

/**
* Query used to get users and groups permissions
Expand Down
Expand Up @@ -26,7 +26,7 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.sonar.db.DaoDatabaseUtils;
import org.sonar.db.DaoUtils;
import org.sonar.db.WildcardPosition;

public class ProjectQgateAssociationQuery {
Expand Down Expand Up @@ -62,7 +62,7 @@ private ProjectQgateAssociationQuery(Builder builder) {
if (this.projectSearch == null) {
this.projectSearchUpperLikeSql = null;
} else {
this.projectSearchUpperLikeSql = DaoDatabaseUtils.buildLikeValue(projectSearch.toUpperCase(Locale.ENGLISH), WildcardPosition.BEFORE_AND_AFTER);
this.projectSearchUpperLikeSql = DaoUtils.buildLikeValue(projectSearch.toUpperCase(Locale.ENGLISH), WildcardPosition.BEFORE_AND_AFTER);
}

this.pageSize = builder.pageSize;
Expand Down
Expand Up @@ -29,7 +29,7 @@

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;

public class SearchGroupsQuery {
Expand Down
Expand Up @@ -30,7 +30,7 @@
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;

public class SearchUsersQuery {
Expand Down
Expand Up @@ -30,7 +30,7 @@
import org.apache.ibatis.session.RowBounds;
import org.sonar.api.utils.System2;
import org.sonar.db.Dao;
import org.sonar.db.DaoDatabaseUtils;
import org.sonar.db.DaoUtils;
import org.sonar.db.DbSession;
import org.sonar.db.WildcardPosition;

Expand Down Expand Up @@ -108,7 +108,7 @@ private static String groupSearchToSql(@Nullable String query) {
}

String upperCasedNameQuery = StringUtils.upperCase(query, Locale.ENGLISH);
return DaoDatabaseUtils.buildLikeValue(upperCasedNameQuery, WildcardPosition.BEFORE_AND_AFTER);
return DaoUtils.buildLikeValue(upperCasedNameQuery, WildcardPosition.BEFORE_AND_AFTER);
}

public List<GroupDto> selectByOrganizationUuid(DbSession dbSession, String organizationUuid) {
Expand Down
Expand Up @@ -29,7 +29,7 @@
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;

public class UserMembershipQuery {
Expand Down
Expand Up @@ -228,15 +228,6 @@
)
</insert>

<update id="resetAllToPendingStatus" parameterType="map">
update ce_queue set
status='PENDING',
started_at=NULL,
updated_at=#{updatedAt,jdbcType=BIGINT}
where
status &lt;&gt; 'PENDING'
</update>

<update id="resetToPendingForWorker">
update ce_queue set
status='PENDING',
Expand Down
Expand Up @@ -10,15 +10,6 @@
c.text_value as "value"
</sql>

<select id="selectByTaskUuid" parameterType="map" resultType="CeTaskCharacteristic">
SELECT
<include refid="columns"/>
FROM
ce_task_characteristics c
WHERE
c.task_uuid = #{taskUuid,jdbcType=VARCHAR}
</select>

<select id="selectByTaskUuids" parameterType="map" resultType="CeTaskCharacteristic">
SELECT
<include refid="columns"/>
Expand Down
Expand Up @@ -22,12 +22,12 @@
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.DaoDatabaseUtils.buildLikeValue;
import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.WildcardPosition.AFTER;
import static org.sonar.db.WildcardPosition.BEFORE;
import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;

public class DaoDatabaseUtilsTest {
public class DaoUtilsTest {

@Test
public void buildLikeValue_with_special_characters() {
Expand Down
Expand Up @@ -201,63 +201,6 @@ public void test_delete() {
assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_1).isPresent()).isFalse();
}

@Test
public void test_resetAllToPendingStatus() {
insert(TASK_UUID_1, COMPONENT_UUID_1, PENDING);
insert(TASK_UUID_2, COMPONENT_UUID_1, IN_PROGRESS);
insert(TASK_UUID_3, COMPONENT_UUID_1, IN_PROGRESS);
verifyCeQueueStatuses(TASK_UUID_1, PENDING, TASK_UUID_2, IN_PROGRESS, TASK_UUID_3, IN_PROGRESS);

underTest.resetAllToPendingStatus(db.getSession());
db.getSession().commit();

verifyCeQueueStatuses(TASK_UUID_1, PENDING, TASK_UUID_2, PENDING, TASK_UUID_3, PENDING);
}

@Test
public void resetAllToPendingStatus_updates_updatedAt() {
long now = 1_334_333L;
insert(TASK_UUID_1, COMPONENT_UUID_1, IN_PROGRESS);
insert(TASK_UUID_2, COMPONENT_UUID_1, IN_PROGRESS);
mockSystem2ForSingleCall(now);

underTestWithSystem2Mock.resetAllToPendingStatus(db.getSession());

assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_1).get().getUpdatedAt()).isEqualTo(now);
assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_2).get().getUpdatedAt()).isEqualTo(now);
}

@Test
public void resetAllToPendingStatus_resets_startedAt() {
assertThat(insert(TASK_UUID_1, COMPONENT_UUID_1, PENDING).getStartedAt()).isNull();
assertThat(underTest.peek(db.getSession(), WORKER_UUID_1, MAX_EXECUTION_COUNT).get().getUuid()).isEqualTo(TASK_UUID_1);
assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_1).get().getStartedAt()).isNotNull();

underTest.resetAllToPendingStatus(db.getSession());

assertThat(underTest.selectByUuid(db.getSession(), TASK_UUID_1).get().getStartedAt()).isNull();
}

@Test
public void resetAllToPendingStatus_does_not_reset_workerUuid_nor_executionCount() {
CeQueueDto dto = new CeQueueDto()
.setUuid(TASK_UUID_1)
.setTaskType(CeTaskTypes.REPORT)
.setComponentUuid(COMPONENT_UUID_1)
.setStatus(IN_PROGRESS)
.setSubmitterLogin(SUBMITTER_LOGIN)
.setWorkerUuid(WORKER_UUID_1)
.setExecutionCount(EXECUTION_COUNT);
underTest.insert(db.getSession(), dto);
db.commit();

underTest.resetAllToPendingStatus(db.getSession());

CeQueueDto saved = underTest.selectByUuid(db.getSession(), TASK_UUID_1).get();
assertThat(saved.getWorkerUuid()).isEqualTo(WORKER_UUID_1);
assertThat(saved.getExecutionCount()).isEqualTo(EXECUTION_COUNT);
}

@Test
public void resetToPendingForWorker_resets_status_of_non_pending_tasks_only_for_specified_workerUuid() {
long startedAt = 2_099_888L;
Expand Down
Expand Up @@ -38,29 +38,6 @@ public class CeTaskCharacteristicDaoTest {

private CeTaskCharacteristicDao underTest = new CeTaskCharacteristicDao();

@Test
public void selectByTaskUuid() {
CeTaskCharacteristicDto dto1 = new CeTaskCharacteristicDto()
.setKey("key1")
.setValue("value1")
.setUuid("uuid1")
.setTaskUuid("task");
CeTaskCharacteristicDto dto2 = new CeTaskCharacteristicDto()
.setKey("key2")
.setValue("value2")
.setUuid("uuid2")
.setTaskUuid("task");
underTest.insert(dbTester.getSession(), asList(dto1, dto2));
dbTester.getSession().commit();

assertThat(underTest.selectByTaskUuid(dbTester.getSession(), "task"))
.extracting(CeTaskCharacteristicDto::getTaskUuid, CeTaskCharacteristicDto::getUuid, CeTaskCharacteristicDto::getKey, CeTaskCharacteristicDto::getValue)
.containsOnly(
tuple("task", "uuid1", "key1", "value1"),
tuple("task", "uuid2", "key2", "value2"));
assertThat(underTest.selectByTaskUuid(dbTester.getSession(), "unknown")).isEmpty();
}

@Test
public void selectByTaskUuids() {
CeTaskCharacteristicDto dto1 = new CeTaskCharacteristicDto()
Expand Down

0 comments on commit e582be9

Please sign in to comment.