From 2fe147f09118a55a31f8953dc33ceb3352ca7c89 Mon Sep 17 00:00:00 2001 From: Nikesh kumar Date: Fri, 17 Nov 2023 14:13:54 +0530 Subject: [PATCH] fix(rest): create new enpoint to check server connection. Signed-off-by: Nikesh kumar --- .../src/docs/asciidoc/fossology.adoc | 14 +++++- .../fossology/FossologyAdminController.java | 15 +++++- .../Sw360FossologyAdminServices.java | 49 +++++++++++++++++++ .../restdocs/FossologySpecTest.java | 10 ++++ 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/rest/resource-server/src/docs/asciidoc/fossology.adoc b/rest/resource-server/src/docs/asciidoc/fossology.adoc index a62a99dbec..6e2b809e97 100644 --- a/rest/resource-server/src/docs/asciidoc/fossology.adoc +++ b/rest/resource-server/src/docs/asciidoc/fossology.adoc @@ -1,4 +1,3 @@ -// // Copyright Siemens AG, 2023. Part of the SW360 Portal Project. // // This program and the accompanying materials are made @@ -22,4 +21,15 @@ A `POST` request will save the configuration. include::{snippets}/should_document_save_configuration/curl-request.adoc[] ===== Example response -include::{snippets}/should_document_save_configuration/http-response.adoc[] \ No newline at end of file +include::{snippets}/should_document_save_configuration/http-response.adoc[] + +[[re-server-configuration]] +==== check the server configuration. + +A `GET` request will save the configuration. + +===== Example request +include::{snippets}/should_document_check_server_configuration/curl-request.adoc[] + +===== Example response +include::{snippets}/should_document_check_server_configuration/http-response.adoc[] diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/admin/fossology/FossologyAdminController.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/admin/fossology/FossologyAdminController.java index 9f3261bf33..1978a4ac23 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/admin/fossology/FossologyAdminController.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/admin/fossology/FossologyAdminController.java @@ -16,8 +16,6 @@ import java.util.Map; import org.apache.thrift.TException; -import org.eclipse.sw360.datahandler.thrift.ConfigContainer; -import org.eclipse.sw360.datahandler.thrift.RequestSummary; import org.eclipse.sw360.datahandler.thrift.fossology.FossologyService; import org.eclipse.sw360.datahandler.thrift.users.User; import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper; @@ -30,6 +28,8 @@ import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import io.swagger.v3.oas.annotations.Parameter; import lombok.NonNull; @@ -64,6 +64,17 @@ public ResponseEntity saveConfigration(@RequestBody Map reque throw new TException(e.getMessage()); } return ResponseEntity.ok(Series.SUCCESSFUL); + } + + @RequestMapping(value = FOSSOLOGY_URL + "/reServerConnection", method = RequestMethod.GET) + public ResponseEntity checkServerConnection()throws TException { + try { + User sw360User = restControllerHelper.getSw360UserFromAuthentication(); + sw360FossologyAdminServices.serverConnection(sw360User); + } catch (Exception e) { + throw new TException(e.getMessage()); + } + return ResponseEntity.ok(Series.SUCCESSFUL); } } diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/admin/fossology/Sw360FossologyAdminServices.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/admin/fossology/Sw360FossologyAdminServices.java index 0901b1a40e..b0463f1f99 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/admin/fossology/Sw360FossologyAdminServices.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/admin/fossology/Sw360FossologyAdminServices.java @@ -49,6 +49,25 @@ public class Sw360FossologyAdminServices { public static Sw360FossologyAdminServices instance; private boolean fossologyConnectionEnabled; + String key; + + public RequestStatus checkFossologyConnection() throws TException { + + RequestStatus checkConnection = null; + try { + checkConnection = new ThriftClients().makeFossologyClient().checkConnection(); + } catch (SW360Exception exp) { + if (exp.getErrorCode() == 404) { + throw new ResourceNotFoundException(exp.getWhy()); + } else { + throw new RuntimeException(exp.getWhy()); + } + } + fossologyConnectionEnabled = checkConnection.equals(RequestStatus.SUCCESS); + return checkConnection; + + } + public void saveConfig(User sw360User, String url, String folderId, String token) throws TException { FossologyService.Iface client = getThriftFossologyClient(); ConfigContainer fossologyConfig = client.getFossologyConfig(); @@ -64,6 +83,8 @@ public void saveConfig(User sw360User, String url, String folderId, String token } else { throw new HttpMessageNotReadableException("fossologyConfig value is null."); } + setKeyValuePair(configKeyToValues, key, url, folderId, token); + fossologyConfig.setConfigKeyToValues(configKeyToValues); } else { throw new HttpMessageNotReadableException("Unable to save the details. User is not admin"); } @@ -89,4 +110,32 @@ private static void setConfigValues(Map> configKeyToValues, values.add(value); } + private void setKeyValuePair(Map> map, String key, String url, String folderId, + String token) { + map.computeIfAbsent(key, k -> new HashSet<>()).addAll(Set.of(url, folderId, token)); + } + + public void serverConnection(User sw360User) throws TException{ + if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) { + serveCheckConnection(); + } else { + throw new HttpMessageNotReadableException("User is not admin"); + } + + } + + private void serveCheckConnection() throws TException{ + FossologyService.Iface sw360FossologyClient = getThriftFossologyClient(); + RequestStatus checkConnection = null; + try { + checkConnection = sw360FossologyClient.checkConnection(); + } catch (TException exp) { + throw new RuntimeException("Connection to Fossology server Failed."); + } + + if (checkConnection == RequestStatus.FAILURE) { + throw new RuntimeException("Connection to Fossology server Failed."); + } + } + } diff --git a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/FossologySpecTest.java b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/FossologySpecTest.java index 8de172ffa7..fd816d70fa 100644 --- a/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/FossologySpecTest.java +++ b/rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/FossologySpecTest.java @@ -16,6 +16,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.io.IOException; @@ -73,6 +74,7 @@ public void before() throws TException, IOException,TTransportException { when(fossologyAdminServices.getThriftFossologyClient()).thenReturn(fossologyClient); when(fossologyClient.getFossologyConfig()).thenReturn(fossologyConfig); Mockito.doNothing().when(fossologyAdminServices).saveConfig(any(), any(), any(), any()); + Mockito.doNothing().when(fossologyAdminServices).serverConnection(any()); } @Test @@ -90,4 +92,12 @@ public void should_document_save_configuration() throws Exception { .andExpect(status().isOk()); } + @Test + public void should_document_check_server_configuration() throws Exception { + String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword); + mockMvc.perform(get("/api/fossology/reServerConnection") + .contentType(MediaTypes.HAL_JSON) + .header("Authorization", "Bearer " + accessToken)) + .andExpect(status().isOk()); + } }