*/
package org.sonar.ce.queue;
-import com.google.common.collect.ImmutableSet;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Arrays.asList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.ce.container.ComputeEngineStatus.Status.STARTED;
+import static org.sonar.ce.container.ComputeEngineStatus.Status.STOPPING;
+
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Optional;
import java.util.Random;
+
import javax.annotation.Nullable;
+
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Rule;
import org.sonar.server.organization.DefaultOrganization;
import org.sonar.server.organization.DefaultOrganizationProvider;
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.util.Arrays.asList;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.sonar.ce.container.ComputeEngineStatus.Status.STARTED;
-import static org.sonar.ce.container.ComputeEngineStatus.Status.STOPPING;
+import com.google.common.collect.ImmutableSet;
public class InternalCeQueueImplTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
- public DbTester dbTester = DbTester.create(system2);
+ public DbTester db = DbTester.create(system2);
- private DbSession session = dbTester.getSession();
+ private DbSession session = db.getSession();
private UuidFactory uuidFactory = UuidFactoryImpl.INSTANCE;
- private CEQueueStatus queueStatus = new CEQueueStatusImpl(dbTester.getDbClient());
+ private CEQueueStatus queueStatus = new CEQueueStatusImpl(db.getDbClient());
private DefaultOrganizationProvider defaultOrganizationProvider = mock(DefaultOrganizationProvider.class);
private ComputeEngineStatus computeEngineStatus = mock(ComputeEngineStatus.class);
- private InternalCeQueue underTest = new InternalCeQueueImpl(system2, dbTester.getDbClient(), uuidFactory, queueStatus, defaultOrganizationProvider, computeEngineStatus);
+ private InternalCeQueue underTest = new InternalCeQueueImpl(system2, db.getDbClient(), uuidFactory, queueStatus, defaultOrganizationProvider, computeEngineStatus);
@Before
public void setUp() throws Exception {
- OrganizationDto defaultOrganization = dbTester.getDefaultOrganization();
+ OrganizationDto defaultOrganization = db.getDefaultOrganization();
when(defaultOrganizationProvider.get()).thenReturn(DefaultOrganization.newBuilder()
.setUuid(defaultOrganization.getUuid())
.setKey(defaultOrganization.getKey())
underTest.remove(peek.get(), CeActivityDto.Status.SUCCESS, null, null);
// queue is empty
- assertThat(dbTester.getDbClient().ceQueueDao().selectByUuid(dbTester.getSession(), task.getUuid()).isPresent()).isFalse();
+ assertThat(db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).isPresent()).isFalse();
assertThat(underTest.peek(WORKER_UUID_2).isPresent()).isFalse();
// available in history
- Optional<CeActivityDto> history = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), task.getUuid());
+ Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
assertThat(history.isPresent()).isTrue();
assertThat(history.get().getStatus()).isEqualTo(CeActivityDto.Status.SUCCESS);
assertThat(history.get().getIsLast()).isTrue();
underTest.remove(peek.get(), CeActivityDto.Status.SUCCESS, newTaskResult(null), null);
// available in history
- Optional<CeActivityDto> history = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), task.getUuid());
+ Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
assertThat(history.isPresent()).isTrue();
assertThat(history.get().getAnalysisUuid()).isNull();
}
underTest.remove(peek.get(), CeActivityDto.Status.SUCCESS, newTaskResult(AN_ANALYSIS_UUID), null);
// available in history
- Optional<CeActivityDto> history = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), task.getUuid());
+ Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
assertThat(history.isPresent()).isTrue();
assertThat(history.get().getAnalysisUuid()).isEqualTo("U1");
}
Optional<CeTask> peek = underTest.peek(WORKER_UUID_1);
underTest.remove(peek.get(), CeActivityDto.Status.FAILED, null, error);
- Optional<CeActivityDto> activityDto = dbTester.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid());
+ Optional<CeActivityDto> activityDto = db.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid());
assertThat(activityDto).isPresent();
assertThat(activityDto.get().getErrorMessage()).isEqualTo(error.getMessage());
Optional<CeTask> peek = underTest.peek(WORKER_UUID_1);
underTest.remove(peek.get(), CeActivityDto.Status.FAILED, null, error);
- CeActivityDto activityDto = dbTester.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid()).get();
+ CeActivityDto activityDto = db.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid()).get();
assertThat(activityDto.getErrorType()).isEqualTo("aType");
assertThat(activityDto.getErrorMessage()).isEqualTo("aMessage");
assertThat(activityDto.getErrorStacktrace()).isEqualToIgnoringWhitespace(stacktraceToString(error));
@Test
public void remove_copies_executionCount_and_workerUuid() {
- dbTester.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
+ db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setWorkerUuid("Dustin")
.setExecutionCount(2));
- dbTester.commit();
+ db.commit();
underTest.remove(new CeTask.Builder()
.setOrganizationUuid("foo")
.setType("bar")
.build(), CeActivityDto.Status.SUCCESS, null, null);
- CeActivityDto dto = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "uuid").get();
+ CeActivityDto dto = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), "uuid").get();
assertThat(dto.getExecutionCount()).isEqualTo(2);
assertThat(dto.getWorkerUuid()).isEqualTo("Dustin");
}
@Test
public void peek_overrides_workerUuid_to_argument() {
- dbTester.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
+ db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setWorkerUuid("must be overriden"));
- dbTester.commit();
+ db.commit();
underTest.peek(WORKER_UUID_1);
- CeQueueDto ceQueueDto = dbTester.getDbClient().ceQueueDao().selectByUuid(session, "uuid").get();
+ CeQueueDto ceQueueDto = db.getDbClient().ceQueueDao().selectByUuid(session, "uuid").get();
assertThat(ceQueueDto.getWorkerUuid()).isEqualTo(WORKER_UUID_1);
}
@Test
public void peek_peeks_pending_tasks_with_executionCount_equal_to_0_and_increases_it() {
- dbTester.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
+ db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setExecutionCount(0));
- dbTester.commit();
+ db.commit();
assertThat(underTest.peek(WORKER_UUID_1).get().getUuid()).isEqualTo("uuid");
- assertThat(dbTester.getDbClient().ceQueueDao().selectByUuid(session, "uuid").get().getExecutionCount()).isEqualTo(1);
+ assertThat(db.getDbClient().ceQueueDao().selectByUuid(session, "uuid").get().getExecutionCount()).isEqualTo(1);
}
@Test
public void peek_peeks_pending_tasks_with_executionCount_equal_to_1_and_increases_it() {
- dbTester.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
+ db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setExecutionCount(1));
- dbTester.commit();
+ db.commit();
assertThat(underTest.peek(WORKER_UUID_1).get().getUuid()).isEqualTo("uuid");
- assertThat(dbTester.getDbClient().ceQueueDao().selectByUuid(session, "uuid").get().getExecutionCount()).isEqualTo(2);
+ assertThat(db.getDbClient().ceQueueDao().selectByUuid(session, "uuid").get().getExecutionCount()).isEqualTo(2);
}
@Test
public void peek_ignores_pending_tasks_with_executionCount_equal_to_2() {
- dbTester.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
+ db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setExecutionCount(2));
- dbTester.commit();
+ db.commit();
assertThat(underTest.peek(WORKER_UUID_1).isPresent()).isFalse();
}
@Test
public void peek_ignores_pending_tasks_with_executionCount_greater_than_2() {
- dbTester.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
+ db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setExecutionCount(2 + Math.abs(new Random().nextInt(100))));
- dbTester.commit();
+ db.commit();
assertThat(underTest.peek(WORKER_UUID_1).isPresent()).isFalse();
}
}
private void verifyResetTask(CeQueueDto originalDto) {
- CeQueueDto dto = dbTester.getDbClient().ceQueueDao().selectByUuid(session, originalDto.getUuid()).get();
+ CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(session, originalDto.getUuid()).get();
assertThat(dto.getStatus()).isEqualTo(CeQueueDto.Status.PENDING);
assertThat(dto.getExecutionCount()).isEqualTo(originalDto.getExecutionCount());
assertThat(dto.getCreatedAt()).isEqualTo(originalDto.getCreatedAt());
}
private void verifyUnmodifiedTask(CeQueueDto originalDto) {
- CeQueueDto dto = dbTester.getDbClient().ceQueueDao().selectByUuid(session, originalDto.getUuid()).get();
+ CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(session, originalDto.getUuid()).get();
assertThat(dto.getStatus()).isEqualTo(originalDto.getStatus());
assertThat(dto.getExecutionCount()).isEqualTo(originalDto.getExecutionCount());
assertThat(dto.getCreatedAt()).isEqualTo(originalDto.getCreatedAt());
.setStatus(CeQueueDto.Status.IN_PROGRESS)
.setWorkerUuid(workerUuid)
.setExecutionCount(executionCount);
- dbTester.getDbClient().ceQueueDao().insert(session, dto);
- dbTester.commit();
+ db.getDbClient().ceQueueDao().insert(session, dto);
+ db.commit();
return dto;
}
.setStatus(CeQueueDto.Status.PENDING)
.setWorkerUuid(workerUuid)
.setExecutionCount(executionCount);
- dbTester.getDbClient().ceQueueDao().insert(session, dto);
- dbTester.commit();
+ db.getDbClient().ceQueueDao().insert(session, dto);
+ db.commit();
return dto;
}
@Test
public void cancel_pending() throws Exception {
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
+ CeQueueDto queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get();
- // ignore
- boolean canceled = underTest.cancel("UNKNOWN");
- assertThat(canceled).isFalse();
+ underTest.cancel(db.getSession(), queueDto);
- canceled = underTest.cancel(task.getUuid());
- assertThat(canceled).isTrue();
- Optional<CeActivityDto> activity = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), task.getUuid());
+ Optional<CeActivityDto> activity = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
assertThat(activity.isPresent()).isTrue();
assertThat(activity.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
}
@Test
public void cancel_copies_executionCount_and_workerUuid() {
- dbTester.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
+ CeQueueDto ceQueueDto = db.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
.setUuid("uuid")
.setTaskType("foo")
.setStatus(CeQueueDto.Status.PENDING)
.setWorkerUuid("Dustin")
.setExecutionCount(2));
- dbTester.commit();
+ db.commit();
- underTest.cancel("uuid");
+ underTest.cancel(db.getSession(), ceQueueDto);
- CeActivityDto dto = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "uuid").get();
+ CeActivityDto dto = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), "uuid").get();
assertThat(dto.getExecutionCount()).isEqualTo(2);
assertThat(dto.getWorkerUuid()).isEqualTo("Dustin");
}
@Test
public void fail_to_cancel_if_in_progress() throws Exception {
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage(Matchers.startsWith("Task is in progress and can't be canceled"));
-
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
underTest.peek(WORKER_UUID_2);
+ CeQueueDto queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get();
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage(Matchers.startsWith("Task is in progress and can't be canceled"));
- underTest.cancel(task.getUuid());
+ underTest.cancel(db.getSession(), queueDto);
}
@Test
int canceledCount = underTest.cancelAll();
assertThat(canceledCount).isEqualTo(2);
- Optional<CeActivityDto> history = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), pendingTask1.getUuid());
+ Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), pendingTask1.getUuid());
assertThat(history.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
- history = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), pendingTask2.getUuid());
+ history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), pendingTask2.getUuid());
assertThat(history.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
- history = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), inProgressTask.getUuid());
+ history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), inProgressTask.getUuid());
assertThat(history.isPresent()).isFalse();
}
}
private void verifyReset(CeQueueDto original) {
- CeQueueDto dto = dbTester.getDbClient().ceQueueDao().selectByUuid(dbTester.getSession(), original.getUuid()).get();
+ CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), original.getUuid()).get();
// We do not touch ExecutionCount nor CreatedAt
assertThat(dto.getExecutionCount()).isEqualTo(original.getExecutionCount());
assertThat(dto.getCreatedAt()).isEqualTo(original.getCreatedAt());
}
private void verifyUnmodified(CeQueueDto original) {
- CeQueueDto dto = dbTester.getDbClient().ceQueueDao().selectByUuid(dbTester.getSession(), original.getUuid()).get();
+ CeQueueDto dto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), original.getUuid()).get();
assertThat(dto.getStatus()).isEqualTo(original.getStatus());
assertThat(dto.getExecutionCount()).isEqualTo(original.getExecutionCount());
assertThat(dto.getCreatedAt()).isEqualTo(original.getCreatedAt());
}
private void verifyCanceled(CeQueueDto original) {
- assertThat(dbTester.getDbClient().ceQueueDao().selectByUuid(dbTester.getSession(), original.getUuid())).isEmpty();
- CeActivityDto dto = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), original.getUuid()).get();
+ assertThat(db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), original.getUuid())).isEmpty();
+ CeActivityDto dto = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), original.getUuid()).get();
assertThat(dto.getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
assertThat(dto.getExecutionCount()).isEqualTo(original.getExecutionCount());
assertThat(dto.getWorkerUuid()).isEqualTo(original.getWorkerUuid());
.setStatus(status)
.setExecutionCount(executionCount)
.setWorkerUuid(workerUuid);
- dbTester.getDbClient().ceQueueDao().insert(dbTester.getSession(), dto);
- dbTester.commit();
+ db.getDbClient().ceQueueDao().insert(db.getSession(), dto);
+ db.commit();
return dto;
}
}
private void verifyCeQueueDtoForTaskSubmit(CeTaskSubmit taskSubmit) {
- Optional<CeQueueDto> queueDto = dbTester.getDbClient().ceQueueDao().selectByUuid(dbTester.getSession(), taskSubmit.getUuid());
+ Optional<CeQueueDto> queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), taskSubmit.getUuid());
assertThat(queueDto.isPresent()).isTrue();
CeQueueDto dto = queueDto.get();
assertThat(dto.getTaskType()).isEqualTo(taskSubmit.getType());
}
private ComponentDto newComponentDto(String uuid) {
- return ComponentTesting.newPublicProjectDto(dbTester.getDefaultOrganization(), uuid).setName("name_" + uuid).setDbKey("key_" + uuid);
+ return ComponentTesting.newPublicProjectDto(db.getDefaultOrganization(), uuid).setName("name_" + uuid).setDbKey("key_" + uuid);
}
private CeTask submit(String reportType, String componentUuid) {
}
private ComponentDto insertComponent(ComponentDto componentDto) {
- dbTester.getDbClient().componentDao().insert(session, componentDto);
+ db.getDbClient().componentDao().insert(session, componentDto);
session.commit();
return componentDto;
}
*/
package org.sonar.ce.queue;
+import static java.util.Arrays.asList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.Matchers.startsWith;
+
import java.util.List;
import java.util.Optional;
+
import javax.annotation.Nullable;
+
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import static java.util.Arrays.asList;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.hamcrest.Matchers.startsWith;
-
public class CeQueueImplTest {
private static final String WORKER_UUID = "workerUuid";
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Rule
- public DbTester dbTester = DbTester.create(system2);
+ public DbTester db = DbTester.create(system2);
- private DbSession session = dbTester.getSession();
+ private DbSession session = db.getSession();
private UuidFactory uuidFactory = UuidFactoryImpl.INSTANCE;
- private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester);
+ private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
- private CeQueue underTest = new CeQueueImpl(dbTester.getDbClient(), uuidFactory, defaultOrganizationProvider);
+ private CeQueue underTest = new CeQueueImpl(db.getDbClient(), uuidFactory, defaultOrganizationProvider);
@Test
public void submit_returns_task_populated_from_CeTaskSubmit_and_creates_CeQueue_row() {
CeTaskSubmit taskSubmit = createTaskSubmit(CeTaskTypes.REPORT, "PROJECT_1", "rob");
+
CeTask task = underTest.submit(taskSubmit);
verifyCeTask(taskSubmit, task, null);
@Test
public void submit_populates_component_name_and_key_of_CeTask_if_component_exists() {
- ComponentDto componentDto = insertComponent(ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert(), "PROJECT_1"));
+ ComponentDto componentDto = insertComponent(ComponentTesting.newPrivateProjectDto(db.organizations().insert(), "PROJECT_1"));
CeTaskSubmit taskSubmit = createTaskSubmit(CeTaskTypes.REPORT, componentDto.uuid(), null);
CeTask task = underTest.submit(taskSubmit);
@Test
public void massSubmit_populates_component_name_and_key_of_CeTask_if_component_exists() {
- ComponentDto componentDto1 = insertComponent(ComponentTesting.newPrivateProjectDto(dbTester.getDefaultOrganization(), "PROJECT_1"));
+ ComponentDto componentDto1 = insertComponent(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization(), "PROJECT_1"));
CeTaskSubmit taskSubmit1 = createTaskSubmit(CeTaskTypes.REPORT, componentDto1.uuid(), null);
CeTaskSubmit taskSubmit2 = createTaskSubmit("something", "non existing component uuid", null);
@Test
public void cancel_pending() throws Exception {
CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
+ CeQueueDto queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get();
- // ignore
- boolean canceled = underTest.cancel("UNKNOWN");
- assertThat(canceled).isFalse();
+ underTest.cancel(db.getSession(), queueDto);
- canceled = underTest.cancel(task.getUuid());
- assertThat(canceled).isTrue();
- Optional<CeActivityDto> activity = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), task.getUuid());
+ Optional<CeActivityDto> activity = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
assertThat(activity.isPresent()).isTrue();
assertThat(activity.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
}
@Test
public void fail_to_cancel_if_in_progress() throws Exception {
+ submit(CeTaskTypes.REPORT, "PROJECT_1");
+ CeQueueDto ceQueueDto = db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, MAX_EXECUTION_COUNT).get();
+
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(startsWith("Task is in progress and can't be canceled"));
- CeTask task = submit(CeTaskTypes.REPORT, "PROJECT_1");
-
- dbTester.getDbClient().ceQueueDao().peek(session, WORKER_UUID, MAX_EXECUTION_COUNT);
-
- underTest.cancel(task.getUuid());
+ underTest.cancel(db.getSession(), ceQueueDto);
}
@Test
CeTask pendingTask1 = submit(CeTaskTypes.REPORT, "PROJECT_2");
CeTask pendingTask2 = submit(CeTaskTypes.REPORT, "PROJECT_3");
- dbTester.getDbClient().ceQueueDao().peek(session, WORKER_UUID, MAX_EXECUTION_COUNT);
+ db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, MAX_EXECUTION_COUNT);
int canceledCount = underTest.cancelAll();
assertThat(canceledCount).isEqualTo(2);
- Optional<CeActivityDto> history = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), pendingTask1.getUuid());
+ Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), pendingTask1.getUuid());
assertThat(history.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
- history = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), pendingTask2.getUuid());
+ history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), pendingTask2.getUuid());
assertThat(history.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
- history = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), inProgressTask.getUuid());
+ history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), inProgressTask.getUuid());
assertThat(history.isPresent()).isFalse();
}
}
private void verifyCeQueueDtoForTaskSubmit(CeTaskSubmit taskSubmit) {
- Optional<CeQueueDto> queueDto = dbTester.getDbClient().ceQueueDao().selectByUuid(dbTester.getSession(), taskSubmit.getUuid());
+ Optional<CeQueueDto> queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), taskSubmit.getUuid());
assertThat(queueDto.isPresent()).isTrue();
assertThat(queueDto.get().getTaskType()).isEqualTo(taskSubmit.getType());
assertThat(queueDto.get().getComponentUuid()).isEqualTo(taskSubmit.getComponentUuid());
}
private ComponentDto insertComponent(ComponentDto componentDto) {
- dbTester.getDbClient().componentDao().insert(session, componentDto);
+ db.getDbClient().componentDao().insert(session, componentDto);
session.commit();
return componentDto;
}
*/
package org.sonar.server.ce.ws;
+import static org.assertj.core.api.Java6Assertions.assertThat;
+
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.ce.queue.CeQueue;
+import org.sonar.ce.queue.CeQueueImpl;
+import org.sonar.ce.queue.CeTask;
+import org.sonar.ce.queue.CeTaskSubmit;
+import org.sonar.core.util.UuidFactoryFast;
+import org.sonar.db.DbTester;
+import org.sonar.db.ce.CeActivityDto;
+import org.sonar.db.ce.CeQueueDto;
+import org.sonar.db.ce.CeTaskTypes;
+import org.sonar.db.component.ComponentDto;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-
public class CancelActionTest {
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public DbTester db = DbTester.create();
+
+ private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
+ private CeQueue queue = new CeQueueImpl(db.getDbClient(), UuidFactoryFast.getInstance(), defaultOrganizationProvider);
- private CeQueue queue = mock(CeQueue.class);
- private CancelAction underTest = new CancelAction(userSession, queue);
+ private CancelAction underTest = new CancelAction(userSession, db.getDbClient(), queue);
private WsActionTester tester = new WsActionTester(underTest);
@Test
public void cancel_pending_task() {
logInAsSystemAdministrator();
+ ComponentDto project = db.components().insertPrivateProject();
+ CeQueueDto queue = createTaskSubmit(project);
tester.newRequest()
- .setParam("id", "T1")
+ .setParam("id", queue.getUuid())
.execute();
- verify(queue).cancel("T1");
+ assertThat(db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), queue.getUuid()).get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
+ }
+
+ @Test
+ public void does_not_fail_on_unknown_task() {
+ logInAsSystemAdministrator();
+
+ tester.newRequest()
+ .setParam("id", "UNKNOWN")
+ .execute();
}
@Test
expectedException.expectMessage("The 'id' parameter is missing");
tester.newRequest().execute();
-
- verifyZeroInteractions(queue);
}
@Test
tester.newRequest()
.setParam("id", "T1")
.execute();
-
- verifyZeroInteractions(queue);
}
private void logInAsSystemAdministrator() {
userSession.logIn().setSystemAdministrator();
}
+
+ private CeQueueDto createTaskSubmit(ComponentDto component) {
+ CeTaskSubmit.Builder submission = queue.prepareSubmit();
+ submission.setType(CeTaskTypes.REPORT);
+ submission.setComponentUuid(component.uuid());
+ submission.setSubmitterLogin(null);
+ CeTask task = queue.submit(submission.build());
+ return db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get();
+ }
}