1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package org.opengrok.web.api; 24 25 import jakarta.ws.rs.core.Response; 26 import org.junit.jupiter.api.Test; 27 28 import static org.junit.jupiter.api.Assertions.assertEquals; 29 import static org.junit.jupiter.api.Assertions.assertFalse; 30 import static org.junit.jupiter.api.Assertions.assertThrows; 31 import static org.junit.jupiter.api.Assertions.assertTrue; 32 33 class ApiTaskTest { 34 doNothing()35 private Object doNothing() { 36 return null; 37 } 38 39 @Test testConstructorBasic()40 void testConstructorBasic() { 41 ApiTask apiTask = new ApiTask("foo", this::doNothing); 42 assertFalse(apiTask.isCompleted()); 43 } 44 45 @Test testConstructorResponseStatus()46 void testConstructorResponseStatus() { 47 Response.Status status = Response.Status.INTERNAL_SERVER_ERROR; 48 ApiTask apiTask = new ApiTask("bar", this::doNothing, status); 49 assertEquals(status, apiTask.getResponseStatus()); 50 } 51 52 private static class Task { 53 private int value; 54 Task()55 Task() { 56 value = 1; 57 } 58 setValue(int value)59 public void setValue(int value) { 60 this.value = value; 61 } 62 getValue()63 public int getValue() { 64 return value; 65 } 66 } 67 68 @Test testCallable()69 void testCallable() throws Exception { 70 Task task = new Task(); 71 int newValue = task.getValue() ^ 1; 72 ApiTask apiTask = new ApiTask("foo", 73 () -> { 74 task.setValue(newValue); 75 return newValue; 76 }); 77 assertFalse(apiTask.isCompleted()); 78 assertFalse(apiTask.isDone()); 79 apiTask.getCallable().call(); 80 assertEquals(newValue, task.getValue()); 81 assertTrue(apiTask.isCompleted()); 82 } 83 84 @Test testEarlyGetResponse()85 void testEarlyGetResponse() { 86 ApiTask apiTask = new ApiTask("early", () -> null); 87 assertThrows(IllegalStateException.class, apiTask::getResponse); 88 } 89 90 @Test testAlreadySubmitted()91 void testAlreadySubmitted() { 92 ApiTask apiTask = new ApiTask("foo", this::doNothing); 93 apiTask.setSubmitted(); 94 assertThrows(IllegalStateException.class, apiTask::getCallable); 95 } 96 } 97