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.HttpHeaders; 26 import jakarta.ws.rs.core.Response; 27 import org.junit.jupiter.api.Test; 28 29 import java.util.Map; 30 import java.util.concurrent.RejectedExecutionException; 31 import java.util.concurrent.TimeUnit; 32 33 import static org.awaitility.Awaitility.await; 34 import static org.junit.jupiter.api.Assertions.assertEquals; 35 import static org.junit.jupiter.api.Assertions.assertNotNull; 36 import static org.junit.jupiter.api.Assertions.assertNull; 37 import static org.junit.jupiter.api.Assertions.assertSame; 38 import static org.junit.jupiter.api.Assertions.assertThrows; 39 import static org.junit.jupiter.api.Assertions.assertTrue; 40 41 class ApiTaskManagerTest { 42 @Test testSingleton()43 void testSingleton() { 44 assertNotNull(ApiTaskManager.getInstance()); 45 } 46 47 @Test testQueueName()48 void testQueueName() { 49 String name = "foo"; 50 assertEquals(name, ApiTaskManager.getQueueName(name)); 51 name = "/foo"; 52 assertEquals(name.substring(1), ApiTaskManager.getQueueName(name)); 53 } 54 doNothing()55 private Object doNothing() { 56 return null; 57 } 58 59 @Test testTaskInvalidQueue()60 void testTaskInvalidQueue() { 61 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 62 ApiTask apiTask = new ApiTask("foo", this::doNothing); 63 assertEquals(Response.Status.BAD_REQUEST, 64 apiTaskManager.submitApiTask("/nonexistent", apiTask).getStatusInfo()); 65 } 66 67 @Test testTaskSubmitDelete()68 void testTaskSubmitDelete() { 69 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 70 String name = "foo"; 71 apiTaskManager.addPool(name, 1); 72 ApiTask apiTask = new ApiTask("foo", this::doNothing); 73 Response response = apiTaskManager.submitApiTask(name, apiTask); 74 assertEquals(Response.Status.ACCEPTED, response.getStatusInfo()); 75 String location = response.getHeaderString(HttpHeaders.LOCATION); 76 assertNotNull(location); 77 String uuidString = apiTask.getUuid().toString(); 78 assertTrue(location.contains(uuidString)); 79 assertSame(apiTask, apiTaskManager.getApiTask(uuidString)); 80 apiTaskManager.deleteApiTask(uuidString); 81 assertNull(apiTaskManager.getApiTask(uuidString)); 82 } 83 84 @Test taskSubmitCallableWithException()85 void taskSubmitCallableWithException() { 86 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 87 String name = "exception"; 88 apiTaskManager.addPool(name, 1); 89 ApiTask apiTask = new ApiTask("foo", 90 () -> { 91 throw new Exception("foo"); 92 }); 93 apiTaskManager.submitApiTask(name, apiTask); 94 await().atMost(3, TimeUnit.SECONDS).until(apiTask::isDone); 95 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), apiTask.getResponse().getStatus()); 96 } 97 98 @Test taskSubmitCallableWithExceptionMapping()99 void taskSubmitCallableWithExceptionMapping() { 100 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 101 String name = "exceptionMap"; 102 apiTaskManager.addPool(name, 1); 103 final String exceptionText = "exception text"; 104 ApiTask apiTask = new ApiTask("foo", 105 () -> { 106 throw new IllegalStateException(exceptionText); 107 }, 108 Response.Status.NO_CONTENT, 109 Map.of(IllegalStateException.class, Response.Status.NOT_ACCEPTABLE)); 110 apiTaskManager.submitApiTask(name, apiTask); 111 await().atMost(3, TimeUnit.SECONDS).until(apiTask::isDone); 112 Response response = apiTask.getResponse(); 113 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatus()); 114 assertTrue(response.getEntity().toString().contains(exceptionText)); 115 } 116 117 @Test testCallable()118 void testCallable() { 119 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 120 String name = "payload"; 121 apiTaskManager.addPool(name, 1); 122 final String payloadText = "payload text"; 123 ApiTask apiTask = new ApiTask("payload", () -> payloadText); 124 apiTaskManager.submitApiTask(name, apiTask); 125 await().atMost(3, TimeUnit.SECONDS).until(apiTask::isDone); 126 Response response = apiTask.getResponse(); 127 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); 128 assertNotNull(response.getEntity()); 129 assertTrue(response.getEntity().toString().contains(payloadText)); 130 } 131 132 @Test testTaskInvalidUuid()133 void testTaskInvalidUuid() { 134 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 135 assertThrows(IllegalArgumentException.class, () -> apiTaskManager.getApiTask("foo")); 136 } 137 138 @Test testDuplicateQueueAdd()139 void testDuplicateQueueAdd() { 140 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 141 String name = "myQueue"; 142 apiTaskManager.addPool(name, 1); 143 assertThrows(IllegalStateException.class, () -> apiTaskManager.addPool(name, 1)); 144 } 145 146 @Test testShutdown()147 void testShutdown() throws InterruptedException { 148 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 149 String name = "bar"; 150 apiTaskManager.addPool(name, 1); 151 apiTaskManager.shutdown(); 152 ApiTask apiTask = new ApiTask("nada", this::doNothing); 153 assertThrows(RejectedExecutionException.class, () -> apiTaskManager.submitApiTask(name, apiTask)); 154 } 155 } 156