Skip to content

Commit

Permalink
Merge pull request #2212 from siemens/fix/fossologyReServerConnection
Browse files Browse the repository at this point in the history
feat(rest): create new endpoint to check fossology connection.

Recviewed by: [email protected]
Tested by: [email protected]
  • Loading branch information
ag4ums authored Dec 19, 2023
2 parents 47d14b1 + 2fe147f commit 21151a2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
14 changes: 12 additions & 2 deletions rest/resource-server/src/docs/asciidoc/fossology.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//
// Copyright Siemens AG, 2023. Part of the SW360 Portal Project.
//
// This program and the accompanying materials are made
Expand All @@ -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[]
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[]
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -64,6 +64,17 @@ public ResponseEntity<?> saveConfigration(@RequestBody Map<String, String> 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);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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");
}
Expand All @@ -89,4 +110,32 @@ private static void setConfigValues(Map<String, Set<String>> configKeyToValues,
values.add(value);
}

private void setKeyValuePair(Map<String, Set<String>> 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.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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());
}
}

0 comments on commit 21151a2

Please sign in to comment.