package edu.kit.ipd.crowdcontrol.workerservice.command;

import com.google.protobuf.util.JsonFormat;
import edu.kit.ipd.crowdcontrol.workerservice.proto.Answer;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import spark.Response;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * @author LeanderK
 * @version 1.0
 */
public class CommandsIntegrationTest {
    private final JsonFormat.Printer printer = JsonFormat.printer();
    private final CommandsTest commandsTest = new CommandsTest();

    @Rule
    public final ExpectedException exception = ExpectedException.none();

    @Test
    public void testSubmitFullAnswer() throws Exception {
        String answer =  "http://placehold.it/350x150";
        int workerID = 1;
        int experiment = 2;
        int answerID = 3;
        submitAnswerHelper(answer, "image", printer.print(Answer.newBuilder()
                .setExperiment(experiment)
                .setAnswer(answer)
                .build()),
                experiment, workerID, answerID, response -> verify(response, times(2)).status(201));
    }

    private Object submitAnswerHelper(String answer, String description, String answerRequest, int task, int workerID, int answerID, Consumer<Response> responseVerifier) {
        return commandsTest.submit(task, description,
                communication -> {
                    when(communication.submitAnswer(answer, task, workerID))
                            .thenReturn(CompletableFuture.completedFuture(answerID));
                },
                request -> {
                    when(request.params("workerID")).thenReturn(String.valueOf(workerID));
                    when(request.body()).thenReturn(answerRequest);
                    when(request.contentType()).thenReturn("application/json");
                },
                Commands::submitAnswer,
                responseVerifier
        );
    }
}
@@ -94,16 +94,92 @@
 Format;%0A
+import edu.kit.ipd.crowdcontrol.workerservice.InternalServerErrorException;%0A
 import e
@@ -1348,16 +1348,569 @@
 %0A    %7D%0A%0A
+    @Test(expected = InternalServerErrorException.class)%0A    public void testSubmitFalseAnswer() throws Exception %7B%0A        String answer =  %22www.google.de%22;%0A        int workerID = 1;%0A        int experiment = 2;%0A        int answerID = 3;%0A        submitAnswerHelper(answer, %22image%22, printer.print(Answer.newBuilder()%0A                        .setExperiment(experiment)%0A                        .setAnswer(answer)%0A                        .build()),%0A                experiment, workerID, answerID, response -%3E verify(response, times(2)).status(201));%0A    %7D%0A%0A
     priv

improved commands test