From 78bd75f5f186c89b3e87fef7aa1863a78dc51747 Mon Sep 17 00:00:00 2001 From: Oleh Astappiev Date: Mon, 25 Sep 2023 15:20:06 +0200 Subject: [PATCH] feat: add some tests and bugfixes --- .../java/de/l3s/interweb/core/Results.java | 3 + interweb-server/pom.xml | 10 ++ .../server/search/SearchResource.java | 3 +- .../interweb/server/GreetingResourceIT.java | 8 -- .../interweb/server/RequestTokenAuthTest.java | 106 ++++++++++++++++++ .../interweb/server/WelcomeResourceTest.java | 5 +- pom.xml | 6 +- 7 files changed, 127 insertions(+), 14 deletions(-) delete mode 100644 interweb-server/src/test/java/de/l3s/interweb/server/GreetingResourceIT.java create mode 100644 interweb-server/src/test/java/de/l3s/interweb/server/RequestTokenAuthTest.java diff --git a/interweb-core/src/main/java/de/l3s/interweb/core/Results.java b/interweb-core/src/main/java/de/l3s/interweb/core/Results.java index f1c69c68..d201603d 100644 --- a/interweb-core/src/main/java/de/l3s/interweb/core/Results.java +++ b/interweb-core/src/main/java/de/l3s/interweb/core/Results.java @@ -4,8 +4,10 @@ import java.util.LinkedList; import java.util.List; +import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +@JsonInclude(JsonInclude.Include.NON_NULL) public class Results { /** * Time in ms that the request took to complete. @@ -15,6 +17,7 @@ public class Results { /** * The list of results from different connectors. */ + @JsonProperty("results") private final List results = new LinkedList<>(); public Results() { diff --git a/interweb-server/pom.xml b/interweb-server/pom.xml index a9b79572..622a78e7 100644 --- a/interweb-server/pom.xml +++ b/interweb-server/pom.xml @@ -121,6 +121,16 @@ quarkus-junit5 test + + io.quarkus + quarkus-junit5-mockito + test + + + io.quarkus + quarkus-panache-mock + test + io.rest-assured rest-assured diff --git a/interweb-server/src/main/java/de/l3s/interweb/server/search/SearchResource.java b/interweb-server/src/main/java/de/l3s/interweb/server/search/SearchResource.java index 5f3081f7..d94ba0e5 100644 --- a/interweb-server/src/main/java/de/l3s/interweb/server/search/SearchResource.java +++ b/interweb-server/src/main/java/de/l3s/interweb/server/search/SearchResource.java @@ -8,6 +8,7 @@ import jakarta.validation.Valid; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; import jakarta.ws.rs.GET; import jakarta.ws.rs.POST; import jakarta.ws.rs.Path; @@ -61,7 +62,7 @@ public Uni search(@Parameter(description = "The search query", ex @POST @Authenticated - public Uni search(@Valid SearchQuery query) { + public Uni search(@NotNull @Valid SearchQuery query) { long start = System.currentTimeMillis(); return searchService.search(query).map(results -> { results.setElapsedTime(System.currentTimeMillis() - start); diff --git a/interweb-server/src/test/java/de/l3s/interweb/server/GreetingResourceIT.java b/interweb-server/src/test/java/de/l3s/interweb/server/GreetingResourceIT.java deleted file mode 100644 index b92bc743..00000000 --- a/interweb-server/src/test/java/de/l3s/interweb/server/GreetingResourceIT.java +++ /dev/null @@ -1,8 +0,0 @@ -package de.l3s.interweb.server; - -import io.quarkus.test.junit.QuarkusIntegrationTest; - -@QuarkusIntegrationTest -public class GreetingResourceIT extends WelcomeResourceTest { - // Execute the same tests but in packaged mode. -} diff --git a/interweb-server/src/test/java/de/l3s/interweb/server/RequestTokenAuthTest.java b/interweb-server/src/test/java/de/l3s/interweb/server/RequestTokenAuthTest.java new file mode 100644 index 00000000..01507dd0 --- /dev/null +++ b/interweb-server/src/test/java/de/l3s/interweb/server/RequestTokenAuthTest.java @@ -0,0 +1,106 @@ +package de.l3s.interweb.server; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.containsString; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import io.quarkus.panache.mock.PanacheMock; +import io.quarkus.test.InjectMock; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import io.smallrye.mutiny.Uni; +import org.apache.http.HttpStatus; +import org.hamcrest.Matchers; +import org.jose4j.json.internal.json_simple.JSONObject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import de.l3s.interweb.core.search.SearchQuery; +import de.l3s.interweb.core.search.SearchResults; +import de.l3s.interweb.server.principal.Consumer; +import de.l3s.interweb.server.principal.Principal; +import de.l3s.interweb.server.search.SearchService; + +@QuarkusTest +class RequestTokenAuthTest { + + @InjectMock + SearchService searchService; + + @BeforeEach + public void setup() { + PanacheMock.mock(Consumer.class); + Consumer testConsumer = Mockito.mock(Consumer.class); + Mockito.when(testConsumer.principal).thenReturn(Mockito.mock(Principal.class)); + Mockito.when(Consumer.findByApiKey("testkey")).thenReturn(Uni.createFrom().item(testConsumer)); + + SearchQuery searchQuery = new SearchQuery(); + searchQuery.setQuery("hello world"); + searchQuery.setContentTypes(de.l3s.interweb.core.search.ContentType.image); + Mockito.when(searchService.search(searchQuery)).thenReturn(Uni.createFrom().item(new SearchResults())); + } + + @Test + void unauthorized() { + given() + .when() + .post("/search") + .then() + .statusCode(HttpStatus.SC_UNAUTHORIZED) + .body(Matchers.emptyString()); + } + + @Test + void wrongKey() { + JSONObject requestParams = new JSONObject(); + + given() + .body(requestParams) + .header("Api-Key", "testkey2") + .when() + .post("/search") + .then() + .statusCode(HttpStatus.SC_UNAUTHORIZED) + .body(Matchers.emptyString()); + } + + @Test + void goodKeyBadRequest() { + Map query = new HashMap<>(); + query.put("query", "hello world"); + + given() + .header("Api-Key", "testkey") + .contentType(ContentType.JSON) + .body(query) + .when() + .post("/search") + .then() + .statusCode(HttpStatus.SC_BAD_REQUEST) + .body(containsString("Constraint Violation")); + } + + @Test + void goodKeyGoodRequest() { + Map query = new HashMap<>(); + query.put("query", "hello world"); + query.put("content_types", List.of("image")); + + given() + .header("Api-Key", "testkey") + .contentType(ContentType.JSON) + .body(query) + .when() + .post("/search") + .then() + .statusCode(200) + .body( + "elapsed_time", Matchers.greaterThan(0), + "results", Matchers.notNullValue() + ); + } +} \ No newline at end of file diff --git a/interweb-server/src/test/java/de/l3s/interweb/server/WelcomeResourceTest.java b/interweb-server/src/test/java/de/l3s/interweb/server/WelcomeResourceTest.java index 90ec4372..9b059526 100644 --- a/interweb-server/src/test/java/de/l3s/interweb/server/WelcomeResourceTest.java +++ b/interweb-server/src/test/java/de/l3s/interweb/server/WelcomeResourceTest.java @@ -4,6 +4,7 @@ import static org.hamcrest.CoreMatchers.*; import io.quarkus.test.junit.QuarkusTest; +import org.apache.http.HttpStatus; import org.junit.jupiter.api.Test; @QuarkusTest @@ -12,9 +13,9 @@ public class WelcomeResourceTest { @Test public void testHelloEndpoint() { given() - .when().get("/q/health") + .when().get("/q/health/live") .then() - .statusCode(200) + .statusCode(HttpStatus.SC_OK) .body(containsString("\"status\": \"UP\"")); } diff --git a/pom.xml b/pom.xml index 15cf6f0c..c6e61a3d 100644 --- a/pom.xml +++ b/pom.xml @@ -27,10 +27,10 @@ 3.11.0 3.3.0 3.3.1 - 3.5.0 - 3.1.3 + 3.5.1 + 3.1.5 3.3.0 - 4.7.3.5 + 4.7.3.6