From 79a18ff7dde8a5edb400fa78908b63b102f7863d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radovan=20Tom=C3=A1=C5=A1ik?= Date: Mon, 14 Nov 2022 14:33:55 +0100 Subject: [PATCH] API endpoint for connection testing (#158) * basic test endpoint * tests for test --- .../bbmri/negotiator/rest/Directory.java | 77 ++++++++++++------- .../negotiator/rest/DirectoryAPITest.java | 13 +++- 2 files changed, 61 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/samply/bbmri/negotiator/rest/Directory.java b/src/main/java/de/samply/bbmri/negotiator/rest/Directory.java index 225d596d..1528b8a9 100644 --- a/src/main/java/de/samply/bbmri/negotiator/rest/Directory.java +++ b/src/main/java/de/samply/bbmri/negotiator/rest/Directory.java @@ -26,49 +26,47 @@ package de.samply.bbmri.negotiator.rest; -import java.net.URI; -import java.net.URISyntaxException; -import java.sql.SQLException; -import java.util.HashSet; -import java.util.List; -import java.util.UUID; - -import javax.faces.context.ExternalContext; -import javax.faces.context.FacesContext; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.*; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - +import de.samply.bbmri.negotiator.Config; +import de.samply.bbmri.negotiator.ConfigFactory; +import de.samply.bbmri.negotiator.NegotiatorConfig; import de.samply.bbmri.negotiator.ServletUtil; +import de.samply.bbmri.negotiator.config.Negotiator; +import de.samply.bbmri.negotiator.db.util.DbUtil; +import de.samply.bbmri.negotiator.jooq.Tables; import de.samply.bbmri.negotiator.jooq.tables.records.CollectionRecord; +import de.samply.bbmri.negotiator.jooq.tables.records.JsonQueryRecord; import de.samply.bbmri.negotiator.jooq.tables.records.ListOfDirectoriesRecord; +import de.samply.bbmri.negotiator.jooq.tables.records.QueryRecord; +import de.samply.bbmri.negotiator.rest.dto.CreateQueryResultDTO; import de.samply.bbmri.negotiator.rest.dto.QuerySearchDTO; +import de.samply.bbmri.negotiator.rest.dto.QuerySearchDTOHelper; import de.samply.bbmri.negotiator.util.JsonCollectionUpdateHelper; -import eu.bbmri.eric.csit.service.negotiator.util.NToken; import eu.bbmri.eric.csit.service.negotiator.lifecycle.CollectionLifeCycleStatus; import eu.bbmri.eric.csit.service.negotiator.lifecycle.RequestLifeCycleStatus; import eu.bbmri.eric.csit.service.negotiator.lifecycle.util.LifeCycleRequestStatusStatus; import eu.bbmri.eric.csit.service.negotiator.lifecycle.util.LifeCycleRequestStatusType; +import eu.bbmri.eric.csit.service.negotiator.util.NToken; import eu.bbmri.eric.csit.service.negotiator.util.QueryJsonStringManipulator; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; -import de.samply.bbmri.negotiator.rest.dto.QuerySearchDTOHelper; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import de.samply.bbmri.negotiator.Config; -import de.samply.bbmri.negotiator.ConfigFactory; -import de.samply.bbmri.negotiator.NegotiatorConfig; -import de.samply.bbmri.negotiator.config.Negotiator; -import de.samply.bbmri.negotiator.db.util.DbUtil; -import de.samply.bbmri.negotiator.jooq.Tables; -import de.samply.bbmri.negotiator.jooq.tables.records.JsonQueryRecord; -import de.samply.bbmri.negotiator.jooq.tables.records.QueryRecord; -import de.samply.bbmri.negotiator.rest.dto.CreateQueryResultDTO; +import javax.faces.context.ExternalContext; +import javax.faces.context.FacesContext; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.net.URI; +import java.net.URISyntaxException; +import java.sql.SQLException; +import java.util.HashSet; +import java.util.List; +import java.util.UUID; /** * REST endpoints for the directory @@ -133,6 +131,29 @@ public Response createQuery(String queryString, @Context HttpServletRequest requ throw new ServerErrorException(Response.Status.INTERNAL_SERVER_ERROR); } } + @OPTIONS + @Path("/test") + @Consumes(MediaType.APPLICATION_JSON) + public Response testConnectionOptions(String jsonString, @Context HttpServletRequest request) { + return Response + .status(200) + .header("Access-Control-Allow-Origin", "*") + .header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization") + .header("Access-Control-Allow-Credentials", "true") + .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD") + .build(); + } + @POST + @Path("/test") + @Consumes(MediaType.APPLICATION_JSON) + public Response testConnection(String jsonString, @Context HttpServletRequest request){ + String apiCallId = UUID.randomUUID().toString(); + logger.info(apiCallId + " API call via create_query API."); + checkAuthentication(request); + return Response + .status(200) + .build(); + } private void checkAuthentication(HttpServletRequest request) throws ForbiddenException { Negotiator negotiator = getNegotiatorConfig(); diff --git a/src/test/java/de/samply/bbmri/negotiator/rest/DirectoryAPITest.java b/src/test/java/de/samply/bbmri/negotiator/rest/DirectoryAPITest.java index b342fa06..1e7d9746 100644 --- a/src/test/java/de/samply/bbmri/negotiator/rest/DirectoryAPITest.java +++ b/src/test/java/de/samply/bbmri/negotiator/rest/DirectoryAPITest.java @@ -90,9 +90,20 @@ void testCreateQuery_withToken() throws SQLException { assertEquals(202, response.getStatus()); } + @Test + void testConnection() { + Mockito.when(negotiatorConfig.getMolgenisUsername()).thenReturn("username"); + Mockito.when(negotiatorConfig.getMolgenisPassword()).thenReturn("password"); + Mockito.when(request.getHeader("Authorization")).thenReturn("Basic dXNlcm5hbWU6cGFzc3dvcmQ="); + Directory directorySpy = Mockito.spy(directory); + Mockito.doReturn(negotiatorConfig).when(directorySpy).getNegotiatorConfig(); + Response response = directorySpy.testConnection("msg : idk", request); + assertEquals(200, response.getStatus()); + } + private String finderJson = "{\n \"query_start_timestamp\":\"2021-08-10T08:04:51.019\",\n \"access_duration\":{\n \"end_date\":\"\",\n \"start_date\":\"\"\n },\n \"query_id\":\"RQ-37d68fc0-4d67-480a-9419-2580335cabc7\",\n \"user_email\":\"anni.ahonen-bishopp@bcplatforms.com\",\n \"user_id\":\"115715892511411738289\",\n \"description\":\"my description of the cohort\",\n \"cohort\":{\n \"result\":{\n \"counts\":[\n {\n \"rquest_id\":\"RQ-CC-e7c7d4d0-2a81-4833-b5b4-ac2a0c1a3e6f\",\n \"count\":\"1030\"\n }\n ]\n },\n \"input\":{\n \"collections\":[\n {\n \"rquest_id\":\"RQ-CC-e7c7d4d0-2a81-4833-b5b4-ac2a0c1a3e6f\",\n \"external_id\":\"Graz\"\n }\n ],\n \"cohorts\":[\n {\n \"name\":\"cases\",\n \"groups\":[\n {\n \"rules\":[\n {\n \"type\":\"ALT\",\n \"varname\":\"SAMPLES\",\n \"value\":\"1\"\n }\n ],\n \"rules_oper\":\"AND\"\n }\n ],\n \"groups_oper\":\"OR\"\n }\n ],\n \"time_window\":\"2018-01-01|\"\n },\n \"application\":\"bcrquest_server\",\n \"searched_codes\":{\n \n },\n \"query_url\":\"https://rquest-dev2.bcplatforms.cloud/bcrquest/#!search-results/RQ-37d68fc0-4d67-480a-9419-2580335cabc7\"\n },\n \"request_id\":\"How many subjects with samples\",\n \"items\":[\n \n ]\n}"; private String queryJsonNoNToken = "{\n \"collections\":[\n {\n \"biobankId\":\"bbmri-eric:ID:UK_GBR-1-19\",\n \"collectionId\":\"bbmri-eric:ID:UK_GBR-1-19:collection:542\"\n }\n ],\n \"humanReadable\":\"test\",\n \"URL\":\"http://d1.ref.development.bibbox.org\"\n}"; private String queryJsonNToken = "{\n \"nToken\":\"79ad527ff0bb448783461fd35b9c21d5__search__1ec374ef3a16475aa479a2c61d11fa48\", \"collections\":[\n {\n \"biobankId\":\"bbmri-eric:ID:UK_GBR-1-19\",\n \"collectionId\":\"bbmri-eric:ID:UK_GBR-1-19:collection:542\"\n }\n ],\n \"humanReadable\":\"test\",\n \"URL\":\"http://d1.ref.development.bibbox.org\"\n}"; private String recordJsonWithMultipleQueries = "{\"searchQueries\":[{\"humanReadable\":\"test\", \"URL\":\"http://d1.ref.development.bibbox.org\", \"token\":\"1ec374ef3a16475aa479a2c61d11fa48\", \"collections\":[ {\"biobankId\":\"bbmri-eric:ID:UK_GBR-1-19\",\"collectionId\":\"bbmri-eric:ID:UK_GBR-1-19:collection:542\"}]}, {\"humanReadable\":\"test2\", \"URL\":\"http://d1.ref.development.bibbox.org\", \"token\":\"2ec374ef3a16475aa479a2c61d11fa48\", \"collections\":[ {\"biobankId\":\"bbmri-eric:ID:UK_GBR-1-20\",\"collectionId\":\"bbmri-eric:ID:UK_GBR-1-20:collection:543\"}]} ]}"; -} \ No newline at end of file +}