// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.acceptance.rest.config;

import static com.google.common.truth.Truth.assertThat;

import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.server.config.ListTasks.TaskInfo;
import com.google.gson.reflect.TypeToken;
import java.util.List;
import org.junit.Test;

public class KillTaskIT extends AbstractDaemonTest {

  private void killTask() throws Exception {
    RestResponse r = adminRestSession.get("/config/server/tasks/");
    List<TaskInfo> result =
        newGson().fromJson(r.getReader(), new TypeToken<List<TaskInfo>>() {}.getType());
    r.consume();
    int taskCount = result.size();
    assertThat(taskCount).isGreaterThan(0);

    r = adminRestSession.delete("/config/server/tasks/" + result.get(0).id);
    r.assertNoContent();
    r.consume();

    r = adminRestSession.get("/config/server/tasks/");
    result = newGson().fromJson(r.getReader(), new TypeToken<List<TaskInfo>>() {}.getType());
    r.consume();
    assertThat(result).hasSize(taskCount - 1);
  }

  private void killTask_NotFound() throws Exception {
    RestResponse r = adminRestSession.get("/config/server/tasks/");
    List<TaskInfo> result =
        newGson().fromJson(r.getReader(), new TypeToken<List<TaskInfo>>() {}.getType());
    r.consume();
    assertThat(result.size()).isGreaterThan(0);

    userRestSession.delete("/config/server/tasks/" + result.get(0).id).assertNotFound();
  }

  @Test
  public void killTaskTests_inOrder() throws Exception {
    // As killTask() changes the state of the server, we want to test it last
    killTask_NotFound();
    killTask();
  }
}

@@ -708,16 +708,65 @@
 ertThat;
+%0Aimport static java.util.stream.Collectors.toSet;
 %0A%0Aimport
@@ -985,24 +985,73 @@
 .util.List;%0A
+import java.util.Optional;%0Aimport java.util.Set;%0A
 import org.j
@@ -1367,84 +1367,230 @@
 ();%0A
+%0A
     
-int taskCount = result.size();%0A    assertThat(taskCount).isGreaterThan(0
+Optional%3CString%3E id =%0A        result%0A            .stream()%0A            .filter(t -%3E %22Log File Compressor%22.equals(t.command))%0A            .map(t -%3E t.id)%0A            .findFirst();%0A    assertThat(id.isPresent()).isTrue(
 );%0A%0A
@@ -1647,32 +1647,24 @@
 sks/%22 + 
-result.get(0).id
+id.get()
 );%0A    r
@@ -1875,48 +1875,118 @@
     
-assertThat(result).hasSize(task
+Set%3CString%3E ids = result.stream().map(t -%3E t.id).collect(toSet());%0A    assertThat(ids).doesNot
 Co
-u
 nt
- - 1
+ain(id.get()
 );%0A 

Try to fix flaky test KillTaskIT#killTask()