leletan commented on code in PR #20852:
URL: https://github.com/apache/flink/pull/20852#discussion_r992626495


##########
flink-runtime/src/test/java/org/apache/flink/runtime/rest/handler/job/checkpoints/CheckpointTriggerHandlersTest.java:
##########
@@ -0,0 +1,349 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.flink.runtime.rest.handler.job.checkpoints;
+
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.api.common.time.Time;
+import org.apache.flink.core.execution.CheckpointType;
+import org.apache.flink.runtime.dispatcher.UnknownOperationKeyException;
+import org.apache.flink.runtime.messages.Acknowledge;
+import org.apache.flink.runtime.rest.RestMatchers;
+import org.apache.flink.runtime.rest.handler.HandlerRequest;
+import org.apache.flink.runtime.rest.handler.HandlerRequestException;
+import org.apache.flink.runtime.rest.handler.RestHandlerException;
+import org.apache.flink.runtime.rest.handler.async.AsynchronousOperationResult;
+import org.apache.flink.runtime.rest.handler.async.OperationResult;
+import org.apache.flink.runtime.rest.handler.job.AsynchronousJobOperationKey;
+import org.apache.flink.runtime.rest.messages.EmptyRequestBody;
+import org.apache.flink.runtime.rest.messages.JobIDPathParameter;
+import org.apache.flink.runtime.rest.messages.TriggerId;
+import org.apache.flink.runtime.rest.messages.TriggerIdPathParameter;
+import 
org.apache.flink.runtime.rest.messages.checkpoints.CheckpointTriggerInfo;
+import 
org.apache.flink.runtime.rest.messages.checkpoints.CheckpointTriggerMessageParameters;
+import 
org.apache.flink.runtime.rest.messages.checkpoints.CheckpointTriggerRequestBody;
+import 
org.apache.flink.runtime.rest.messages.checkpoints.CheckpointTriggerStatusMessageParameters;
+import org.apache.flink.runtime.rest.messages.queue.QueueStatus;
+import org.apache.flink.runtime.webmonitor.RestfulGateway;
+import org.apache.flink.runtime.webmonitor.TestingRestfulGateway;
+import org.apache.flink.runtime.webmonitor.retriever.GatewayRetriever;
+import org.apache.flink.util.TestLogger;
+import org.apache.flink.util.concurrent.FutureUtils;
+
+import 
org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/** Test for {@link CheckpointTriggerHandlers}. */
+public class CheckpointTriggerHandlersTest extends TestLogger {
+
+    private static final Time TIMEOUT = Time.seconds(10);
+
+    private static final JobID JOB_ID = new JobID();
+
+    private static final Long COMPLETED_CHECKPOINT_ID = 123456L;
+
+    private static CheckpointTriggerHandlers.CheckpointTriggerHandler 
checkpointTriggerHandler;
+
+    private static CheckpointTriggerHandlers.CheckpointTriggerStatusHandler
+            checkpointTriggerStatusHandler;
+
+    @BeforeAll
+    static void setUp() throws Exception {
+        GatewayRetriever<RestfulGateway> leaderRetriever =
+                () -> CompletableFuture.completedFuture(null);
+
+        checkpointTriggerHandler =
+                new CheckpointTriggerHandlers.CheckpointTriggerHandler(
+                        leaderRetriever, TIMEOUT, Collections.emptyMap());
+
+        checkpointTriggerStatusHandler =
+                new CheckpointTriggerHandlers.CheckpointTriggerStatusHandler(
+                        leaderRetriever, TIMEOUT, Collections.emptyMap());
+    }
+
+    @Test
+    public void testCheckpointTriggerCompletedSuccessfully() throws Exception {
+        final OperationResult<Long> successfulResult =
+                OperationResult.success(COMPLETED_CHECKPOINT_ID);
+        final CompletableFuture<CheckpointType> checkpointPropertiesFuture =
+                new CompletableFuture<>();
+
+        final AtomicReference<AsynchronousJobOperationKey> keyReference = new 
AtomicReference<>();
+        final TestingRestfulGateway testingRestfulGateway =
+                new TestingRestfulGateway.Builder()
+                        .setTriggerCheckpointFunction(
+                                (AsynchronousJobOperationKey key,
+                                        CheckpointType checkpointType) -> {
+                                    keyReference.set(key);
+                                    
checkpointPropertiesFuture.complete(checkpointType);
+                                    return 
CompletableFuture.completedFuture(Acknowledge.get());
+                                })
+                        .setGetCheckpointStatusFunction(
+                                (AsynchronousJobOperationKey operationKey) -> {
+                                    if 
(operationKey.equals(keyReference.get())) {
+                                        return 
CompletableFuture.completedFuture(successfulResult);
+                                    }
+                                    throw new RuntimeException(
+                                            "Expected operation key "
+                                                    + keyReference.get()
+                                                    + ", but received "
+                                                    + operationKey);
+                                })
+                        .build();
+
+        final CheckpointType checkpointType = CheckpointType.FULL;
+
+        final TriggerId triggerId =
+                checkpointTriggerHandler
+                        .handleRequest(
+                                triggerCheckpointRequest(checkpointType, null),
+                                testingRestfulGateway)
+                        .get()
+                        .getTriggerId();
+
+        final AsynchronousOperationResult<CheckpointTriggerInfo> 
checkpointTriggerResponseBody =
+                checkpointTriggerStatusHandler
+                        .handleRequest(
+                                checkpointTriggerStatusRequest(triggerId), 
testingRestfulGateway)
+                        .get();
+
+        assertEquals(checkpointTriggerResponseBody.queueStatus().getId(), 
QueueStatus.Id.COMPLETED);
+        assertNotNull(checkpointTriggerResponseBody.resource());
+        assertEquals(
+                checkpointTriggerResponseBody.resource().getCheckpointId(),
+                COMPLETED_CHECKPOINT_ID);
+        assertEquals(checkpointPropertiesFuture.get(), CheckpointType.FULL);
+    }
+
+    @Test
+    public void testTriggerCheckpointNoCheckpointType() throws Exception {
+        final OperationResult<Long> successfulResult =
+                OperationResult.success(COMPLETED_CHECKPOINT_ID);
+        final CompletableFuture<CheckpointType> checkpointTypeFuture = new 
CompletableFuture<>();
+
+        final AtomicReference<AsynchronousJobOperationKey> keyReference = new 
AtomicReference<>();
+        final TestingRestfulGateway testingRestfulGateway =
+                new TestingRestfulGateway.Builder()
+                        .setTriggerCheckpointFunction(
+                                (AsynchronousJobOperationKey key,
+                                        CheckpointType checkpointType) -> {
+                                    keyReference.set(key);
+                                    
checkpointTypeFuture.complete(checkpointType);
+                                    return 
CompletableFuture.completedFuture(Acknowledge.get());
+                                })
+                        .setGetCheckpointStatusFunction(
+                                (AsynchronousJobOperationKey operationKey) -> {
+                                    if 
(operationKey.equals(keyReference.get())) {
+                                        return 
CompletableFuture.completedFuture(successfulResult);
+                                    }
+                                    throw new RuntimeException(
+                                            "Expected operation key "
+                                                    + keyReference.get()
+                                                    + ", but received "
+                                                    + operationKey);
+                                })
+                        .build();
+
+        final TriggerId triggerId =
+                checkpointTriggerHandler
+                        .handleRequest(triggerCheckpointRequest(null, null), 
testingRestfulGateway)
+                        .get()
+                        .getTriggerId();
+
+        AsynchronousOperationResult<CheckpointTriggerInfo> 
checkpointTriggerResponseBody;
+        checkpointTriggerResponseBody =
+                checkpointTriggerStatusHandler
+                        .handleRequest(
+                                checkpointTriggerStatusRequest(triggerId), 
testingRestfulGateway)
+                        .get();
+
+        assertEquals(checkpointTriggerResponseBody.queueStatus().getId(), 
QueueStatus.Id.COMPLETED);
+        assertNotNull(checkpointTriggerResponseBody.resource());
+        assertEquals(
+                checkpointTriggerResponseBody.resource().getCheckpointId(),
+                COMPLETED_CHECKPOINT_ID);
+        assertEquals(checkpointTypeFuture.get(), CheckpointType.DEFAULT);
+    }
+
+    @Test
+    public void testCheckpointCompletedWithException() throws Exception {
+        final OperationResult<Long> failedResult =
+                OperationResult.failure(new RuntimeException("expected"));
+
+        final AtomicReference<AsynchronousJobOperationKey> keyReference = new 
AtomicReference<>();
+        final TestingRestfulGateway testingRestfulGateway =
+                new TestingRestfulGateway.Builder()
+                        .setTriggerCheckpointFunction(
+                                (AsynchronousJobOperationKey key,
+                                        CheckpointType checkpointType) -> {
+                                    keyReference.set(key);
+                                    return 
CompletableFuture.completedFuture(Acknowledge.get());
+                                })
+                        .setGetCheckpointStatusFunction(
+                                (AsynchronousJobOperationKey operationKey) -> {
+                                    if 
(operationKey.equals(keyReference.get())) {
+                                        return 
CompletableFuture.completedFuture(failedResult);
+                                    }
+                                    throw new RuntimeException(
+                                            "Expected operation key "
+                                                    + keyReference.get()
+                                                    + ", but received "
+                                                    + operationKey);
+                                })
+                        .build();
+
+        final TriggerId triggerId =
+                checkpointTriggerHandler
+                        .handleRequest(triggerCheckpointRequest(null, null), 
testingRestfulGateway)
+                        .get()
+                        .getTriggerId();
+
+        AsynchronousOperationResult<CheckpointTriggerInfo> 
checkpointTriggerResponseBody;
+        checkpointTriggerResponseBody =
+                checkpointTriggerStatusHandler
+                        .handleRequest(
+                                checkpointTriggerStatusRequest(triggerId), 
testingRestfulGateway)
+                        .get();
+
+        assertEquals(checkpointTriggerResponseBody.queueStatus().getId(), 
QueueStatus.Id.COMPLETED);
+        assertNotNull(checkpointTriggerResponseBody.resource());
+        
assertNotNull(checkpointTriggerResponseBody.resource().getFailureCause());
+
+        final Throwable checkpointError =
+                checkpointTriggerResponseBody
+                        .resource()
+                        .getFailureCause()
+                        .deserializeError(ClassLoader.getSystemClassLoader());
+        assertThat(checkpointError.getMessage()).matches("expected");
+        assertInstanceOf(RuntimeException.class, checkpointError);
+    }
+
+    @Test
+    public void testProvidedTriggerId() throws Exception {
+        final OperationResult<Long> successfulResult =
+                OperationResult.success(COMPLETED_CHECKPOINT_ID);
+        final AtomicReference<AsynchronousJobOperationKey> keyReference = new 
AtomicReference<>();
+        final TestingRestfulGateway testingRestfulGateway =
+                new TestingRestfulGateway.Builder()
+                        .setTriggerCheckpointFunction(
+                                (AsynchronousJobOperationKey key,
+                                        CheckpointType checkpointType) -> {
+                                    keyReference.set(key);
+                                    return 
CompletableFuture.completedFuture(Acknowledge.get());
+                                })
+                        .setGetCheckpointStatusFunction(
+                                (AsynchronousJobOperationKey operationKey) -> {
+                                    if 
(operationKey.equals(keyReference.get())) {
+                                        return 
CompletableFuture.completedFuture(successfulResult);
+                                    }
+                                    throw new RuntimeException(
+                                            "Expected operation key "
+                                                    + keyReference.get()
+                                                    + ", but received "
+                                                    + operationKey);
+                                })
+                        .build();
+
+        final TriggerId providedTriggerId = new TriggerId();
+
+        final TriggerId returnedTriggerId =
+                checkpointTriggerHandler
+                        .handleRequest(
+                                triggerCheckpointRequest(CheckpointType.FULL, 
providedTriggerId),
+                                testingRestfulGateway)
+                        .get()
+                        .getTriggerId();
+
+        assertEquals(providedTriggerId, returnedTriggerId);
+
+        AsynchronousOperationResult<CheckpointTriggerInfo> 
checkpointTriggerResponseBody;
+        checkpointTriggerResponseBody =
+                checkpointTriggerStatusHandler
+                        .handleRequest(
+                                
checkpointTriggerStatusRequest(providedTriggerId),
+                                testingRestfulGateway)
+                        .get();
+
+        assertEquals(checkpointTriggerResponseBody.queueStatus().getId(), 
QueueStatus.Id.COMPLETED);
+        assertNotNull(checkpointTriggerResponseBody.resource());

Review Comment:
   I see what you mean by assertj now. Will fix.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to