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.Response; 26 import org.glassfish.jersey.servlet.ServletContainer; 27 import org.glassfish.jersey.test.DeploymentContext; 28 import org.glassfish.jersey.test.ServletDeploymentContext; 29 import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; 30 import org.glassfish.jersey.test.spi.TestContainerException; 31 import org.glassfish.jersey.test.spi.TestContainerFactory; 32 import org.junit.jupiter.api.AfterAll; 33 import org.junit.jupiter.api.Test; 34 import org.opengrok.web.api.ApiTask; 35 import org.opengrok.web.api.ApiTaskManager; 36 import org.opengrok.web.api.v1.RestApp; 37 38 import java.util.UUID; 39 40 import static org.junit.jupiter.api.Assertions.assertEquals; 41 42 class StatusControllerTest extends OGKJerseyTest { 43 @Override configureDeployment()44 protected DeploymentContext configureDeployment() { 45 return ServletDeploymentContext.forServlet(new ServletContainer(new RestApp())).build(); 46 } 47 48 @Override getTestContainerFactory()49 protected TestContainerFactory getTestContainerFactory() throws TestContainerException { 50 return new GrizzlyWebTestContainerFactory(); 51 } 52 53 @AfterAll cleanup()54 static void cleanup() throws InterruptedException { 55 ApiTaskManager.getInstance().shutdown(); 56 } 57 doNothing()58 private Object doNothing() { 59 return null; 60 } 61 62 @Test testGetNoUuid()63 void testGetNoUuid() { 64 Response response = target(StatusController.PATH) 65 .path(UUID.randomUUID().toString()) 66 .request() 67 .get(); 68 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); 69 } 70 71 @Test testDeleteNoUuid()72 void testDeleteNoUuid() { 73 Response response = target(StatusController.PATH) 74 .path(UUID.randomUUID().toString()) 75 .request() 76 .delete(); 77 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); 78 } 79 80 @Test testGet()81 void testGet() throws InterruptedException { 82 int sleepTime = 3000; 83 ApiTask apiTask = new ApiTask("foo", () -> { 84 try { 85 Thread.sleep(sleepTime); 86 } catch (InterruptedException e) { 87 e.printStackTrace(); 88 } 89 return null; 90 }, Response.Status.CREATED); 91 String uuidString = apiTask.getUuid().toString(); 92 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 93 String poolName = "foo"; 94 apiTaskManager.addPool(poolName, 1); 95 apiTaskManager.submitApiTask(poolName, apiTask); 96 Response response = target(StatusController.PATH) 97 .path(uuidString) 98 .request() 99 .get(); 100 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus()); 101 102 Thread.sleep(sleepTime); 103 response = target(StatusController.PATH) 104 .path(uuidString) 105 .request() 106 .get(); 107 assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); 108 } 109 110 @Test testDeleteNotCompleted()111 void testDeleteNotCompleted() { 112 int sleepTime = 3000; 113 ApiTask apiTask = new ApiTask("foo", () -> { 114 try { 115 Thread.sleep(sleepTime); 116 } catch (InterruptedException e) { 117 e.printStackTrace(); 118 } 119 return null; 120 }); 121 String uuidString = apiTask.getUuid().toString(); 122 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 123 String poolName = "deleteNotCompleted"; 124 apiTaskManager.addPool(poolName, 1); 125 apiTaskManager.submitApiTask(poolName, apiTask); 126 Response response = target(StatusController.PATH) 127 .path(uuidString) 128 .request() 129 .delete(); 130 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); 131 } 132 133 @Test testDelete()134 void testDelete() throws InterruptedException { 135 ApiTask apiTask = new ApiTask("foo", this::doNothing); 136 String uuidString = apiTask.getUuid().toString(); 137 ApiTaskManager apiTaskManager = ApiTaskManager.getInstance(); 138 String poolName = "deleteCompleted"; 139 apiTaskManager.addPool(poolName, 1); 140 apiTaskManager.submitApiTask(poolName, apiTask); 141 Thread.sleep(1000); 142 Response response = target(StatusController.PATH) 143 .path(uuidString) 144 .request() 145 .delete(); 146 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); 147 } 148 } 149