xref: /OpenGrok/opengrok-web/src/test/java/org/opengrok/web/api/ApiTaskTest.java (revision 2f93c05f7383b8c5fc9e10bf9d6c390e686694a8)
11c258122SVladimir Kotal /*
21c258122SVladimir Kotal  * CDDL HEADER START
31c258122SVladimir Kotal  *
41c258122SVladimir Kotal  * The contents of this file are subject to the terms of the
51c258122SVladimir Kotal  * Common Development and Distribution License (the "License").
61c258122SVladimir Kotal  * You may not use this file except in compliance with the License.
71c258122SVladimir Kotal  *
81c258122SVladimir Kotal  * See LICENSE.txt included in this distribution for the specific
91c258122SVladimir Kotal  * language governing permissions and limitations under the License.
101c258122SVladimir Kotal  *
111c258122SVladimir Kotal  * When distributing Covered Code, include this CDDL HEADER in each
121c258122SVladimir Kotal  * file and include the License file at LICENSE.txt.
131c258122SVladimir Kotal  * If applicable, add the following below this CDDL HEADER, with the
141c258122SVladimir Kotal  * fields enclosed by brackets "[]" replaced with your own identifying
151c258122SVladimir Kotal  * information: Portions Copyright [yyyy] [name of copyright owner]
161c258122SVladimir Kotal  *
171c258122SVladimir Kotal  * CDDL HEADER END
181c258122SVladimir Kotal  */
191c258122SVladimir Kotal 
201c258122SVladimir Kotal /*
211c258122SVladimir Kotal  * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
221c258122SVladimir Kotal  */
231c258122SVladimir Kotal package org.opengrok.web.api;
241c258122SVladimir Kotal 
251c258122SVladimir Kotal import jakarta.ws.rs.core.Response;
261c258122SVladimir Kotal import org.junit.jupiter.api.Test;
271c258122SVladimir Kotal 
281c258122SVladimir Kotal import static org.junit.jupiter.api.Assertions.assertEquals;
291c258122SVladimir Kotal import static org.junit.jupiter.api.Assertions.assertFalse;
301c258122SVladimir Kotal import static org.junit.jupiter.api.Assertions.assertThrows;
311c258122SVladimir Kotal import static org.junit.jupiter.api.Assertions.assertTrue;
321c258122SVladimir Kotal 
331c258122SVladimir Kotal class ApiTaskTest {
341c258122SVladimir Kotal 
doNothing()35*2f93c05fSVladimir Kotal     private Object doNothing() {
36*2f93c05fSVladimir Kotal         return null;
371c258122SVladimir Kotal     }
381c258122SVladimir Kotal 
391c258122SVladimir Kotal     @Test
testConstructorBasic()401c258122SVladimir Kotal     void testConstructorBasic() {
411c258122SVladimir Kotal         ApiTask apiTask = new ApiTask("foo", this::doNothing);
421c258122SVladimir Kotal         assertFalse(apiTask.isCompleted());
431c258122SVladimir Kotal     }
441c258122SVladimir Kotal 
451c258122SVladimir Kotal     @Test
testConstructorResponseStatus()461c258122SVladimir Kotal     void testConstructorResponseStatus() {
471c258122SVladimir Kotal         Response.Status status = Response.Status.INTERNAL_SERVER_ERROR;
481c258122SVladimir Kotal         ApiTask apiTask = new ApiTask("bar", this::doNothing, status);
491c258122SVladimir Kotal         assertEquals(status, apiTask.getResponseStatus());
501c258122SVladimir Kotal     }
511c258122SVladimir Kotal 
521c258122SVladimir Kotal     private static class Task {
531c258122SVladimir Kotal         private int value;
541c258122SVladimir Kotal 
Task()551c258122SVladimir Kotal         Task() {
561c258122SVladimir Kotal             value = 1;
571c258122SVladimir Kotal         }
581c258122SVladimir Kotal 
setValue(int value)591c258122SVladimir Kotal         public void setValue(int value) {
601c258122SVladimir Kotal             this.value = value;
611c258122SVladimir Kotal         }
621c258122SVladimir Kotal 
getValue()631c258122SVladimir Kotal         public int getValue() {
641c258122SVladimir Kotal             return value;
651c258122SVladimir Kotal         }
661c258122SVladimir Kotal     }
671c258122SVladimir Kotal 
681c258122SVladimir Kotal     @Test
testCallable()69*2f93c05fSVladimir Kotal     void testCallable() throws Exception {
701c258122SVladimir Kotal         Task task = new Task();
711c258122SVladimir Kotal         int newValue = task.getValue() ^ 1;
72*2f93c05fSVladimir Kotal         ApiTask apiTask = new ApiTask("foo",
73*2f93c05fSVladimir Kotal                 () -> {
74*2f93c05fSVladimir Kotal                     task.setValue(newValue);
75*2f93c05fSVladimir Kotal                     return newValue;
76*2f93c05fSVladimir Kotal                 });
771c258122SVladimir Kotal         assertFalse(apiTask.isCompleted());
78*2f93c05fSVladimir Kotal         assertFalse(apiTask.isDone());
79*2f93c05fSVladimir Kotal         apiTask.getCallable().call();
801c258122SVladimir Kotal         assertEquals(newValue, task.getValue());
811c258122SVladimir Kotal         assertTrue(apiTask.isCompleted());
821c258122SVladimir Kotal     }
831c258122SVladimir Kotal 
841c258122SVladimir Kotal     @Test
testEarlyGetResponse()85*2f93c05fSVladimir Kotal     void testEarlyGetResponse() {
86*2f93c05fSVladimir Kotal         ApiTask apiTask = new ApiTask("early", () -> null);
87*2f93c05fSVladimir Kotal         assertThrows(IllegalStateException.class, apiTask::getResponse);
88*2f93c05fSVladimir Kotal     }
89*2f93c05fSVladimir Kotal 
90*2f93c05fSVladimir Kotal     @Test
testAlreadySubmitted()911c258122SVladimir Kotal     void testAlreadySubmitted() {
921c258122SVladimir Kotal         ApiTask apiTask = new ApiTask("foo", this::doNothing);
931c258122SVladimir Kotal         apiTask.setSubmitted();
94*2f93c05fSVladimir Kotal         assertThrows(IllegalStateException.class, apiTask::getCallable);
951c258122SVladimir Kotal     }
961c258122SVladimir Kotal }
97