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.v1.controller; 24 25 import jakarta.ws.rs.core.HttpHeaders; 26 import jakarta.ws.rs.core.Response; 27 import org.opengrok.web.api.ApiTask; 28 import org.opengrok.web.api.ApiTaskManager; 29 30 import java.net.URI; 31 import java.util.concurrent.TimeUnit; 32 33 import static org.awaitility.Awaitility.await; 34 import static org.junit.jupiter.api.Assertions.assertNotNull; 35 import static org.junit.jupiter.api.Assertions.assertTrue; 36 37 public class ApiUtils { ApiUtils()38 private ApiUtils() { 39 // utility class 40 } 41 42 /** 43 * Busy-waits the status of asynchronous API call, mimicking 44 * {@link org.opengrok.indexer.web.ApiUtils#waitForAsyncApi(Response)}, 45 * however side-steps status API check by going to the {@link ApiTaskManager} directly in order to avoid 46 * going through the {@link StatusController} as it might not be deployed in the unit tests. 47 * The method will return right away if the status of the response object parameter is not 48 * {@code Response.Status.ACCEPTED}. 49 * @param response API Response object 50 * @return the response object 51 */ waitForTask(Response response)52 protected static Response waitForTask(Response response) { 53 if (response.getStatus() != Response.Status.ACCEPTED.getStatusCode()) { 54 return response; 55 } 56 57 String location = response.getHeaderString(HttpHeaders.LOCATION); 58 String locationUri = URI.create(location).getPath(); 59 final String apiPrefix = "api/v1/status/"; 60 assertTrue(locationUri.contains(apiPrefix)); 61 int idx = locationUri.indexOf(apiPrefix); 62 assertTrue(idx > 0); 63 String uuid = locationUri.substring(idx + apiPrefix.length()); 64 ApiTask apiTask = ApiTaskManager.getInstance().getApiTask(uuid); 65 assertNotNull(apiTask); 66 await().atMost(16, TimeUnit.SECONDS).until(apiTask::isDone); 67 68 if (!apiTask.isDone()) { 69 return response; 70 } else { 71 return apiTask.getResponse(); 72 } 73 } 74 } 75