Skip to content

Commit

Permalink
fix(cloudant): fix attachment creation
Browse files Browse the repository at this point in the history
While creating attachment with repository.add(), there are additional
steps needed to create the attachment in DB. Not used by API.

Signed-off-by: Gaurav Mishra <mishra.gaurav@siemens.com>
  • Loading branch information
GMishx authored and smrutis1 committed Oct 28, 2024
1 parent 5bdef6d commit 9452b2b
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public AttachmentConnector getAttachmentConnector(){
return attachmentConnector;
}

public AttachmentContent add(AttachmentContent attachmentContent){
public AttachmentContent add(AttachmentContent attachmentContent) throws SW360Exception {
attachmentContentRepository.add(attachmentContent);
return attachmentContent;
}
Expand Down Expand Up @@ -138,7 +138,7 @@ public void deleteAttachmentUsagesByUsageDataTypes(Source usedBy, Set<UsageData.
}
}

public AttachmentUsage makeAttachmentUsage(AttachmentUsage attachmentUsage) {
public AttachmentUsage makeAttachmentUsage(AttachmentUsage attachmentUsage) throws SW360Exception {
attachmentUsageRepository.add(attachmentUsage);
return attachmentUsage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,14 @@ private static <T extends TBase> Runnable prepareChangeLogRunnable(T newDocVersi
referenceDocLogList.stream().forEach(referenceDocLog -> {
referenceDocLog.setDocumentId(changeLogParentId);
changelog.debug(convertObjectToJson(referenceDocLog));
changeLogRepository.add(referenceDocLog);
try {
changeLogRepository.add(referenceDocLog);
} catch (SW360Exception e) {
log.error("Error occurred while adding change log", e);
}
});
} catch (Exception exp) {
log.error("Error occured while creating Change Logs", exp);
log.error("Error occurred while creating Change Logs", exp);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,12 @@ public List<ProjectVulnerabilityRating> getProjectVulnerabilityRatingByProjectId
public RequestStatus updateProjectVulnerabilityRating(ProjectVulnerabilityRating link) {
if( ! link.isSetId()){
link.setId(SW360Constants.PROJECT_VULNERABILITY_RATING_ID_PREFIX + link.getProjectId());
pvrRepository.add(link);
try {
pvrRepository.add(link);
} catch (SW360Exception e) {
log.error("Unable to update project vulnerability rating.", e);
return RequestStatus.FAILURE;
}
} else {
pvrRepository.update(link);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ public void checkDeletedReleaseListInLoop(int loopCount, List<Release> deletedRe
assertTrue(CommonUtils.isNullOrEmptyMap(relationShip_c1));
}

private void createTestRecords001() {
private void createTestRecords001() throws SW360Exception {

List<Component> components = new ArrayList<Component>();
Component component_dr_A = new Component().setId(COMPONENT_ID_A).setName("DR_A").setDescription("DR Component A").setCreatedBy(USER_EMAIL1).setMainLicenseIds(new HashSet<>(Arrays.asList("lic1"))).setCreatedOn("2022-07-20");
Expand Down Expand Up @@ -1208,7 +1208,7 @@ private void createTestRecords001() {
}


private void createTestRecords002() {
private void createTestRecords002() throws SW360Exception {

List<Component> components = new ArrayList<Component>();
Component component_dr_A = new Component().setId(COMPONENT_ID_A).setName("DR_A").setDescription("DR Component A").setCreatedBy(USER_EMAIL1).setMainLicenseIds(new HashSet<>(Arrays.asList("lic1"))).setCreatedOn("2022-07-20");
Expand Down Expand Up @@ -1300,7 +1300,7 @@ private void createTestRecords002() {
}
}

private void createTestRecords002(int maxLink, int depth, List<String> releaseIdList, List<String> componentIdList) {
private void createTestRecords002(int maxLink, int depth, List<String> releaseIdList, List<String> componentIdList) throws SW360Exception {
//create root node
String componentId = String.format("dr_%08x", treeNodeCreateReleaseCounter);
treeNodeCreateReleaseCounter++;
Expand All @@ -1323,7 +1323,7 @@ private void createTestRecords002(int maxLink, int depth, List<String> releaseId
createReleaseTree(releaseId, 0, releaseIdList, componentIdList);
}

private void createReleaseTree(String parentId, int level, List<String> outReleaseIdList, List< String> outComponentIdList) {
private void createReleaseTree(String parentId, int level, List<String> outReleaseIdList, List< String> outComponentIdList) throws SW360Exception {
//create a compoent
String componentId = String.format("dr_%08x", treeNodeCreateReleaseCounter);
treeNodeCreateReleaseCounter++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -42,7 +43,7 @@ public class FossologyRestConfig {
private boolean outdated;

@Autowired
public FossologyRestConfig(ConfigContainerRepository repository) {
public FossologyRestConfig(ConfigContainerRepository repository) throws SW360Exception {
this.repository = repository;
// eager loading (or initial insert)
get();
Expand All @@ -67,7 +68,12 @@ public String getFolderId() {
}

private String getFirstValue(String key) {
return get().getConfigKeyToValues().getOrDefault(key, new HashSet<>()).stream().findFirst().orElse(null);
try {
return get().getConfigKeyToValues().getOrDefault(key, new HashSet<>()).stream().findFirst().orElse(null);
} catch (SW360Exception e) {
log.error(e);
return null;
}
}

public ConfigContainer update(ConfigContainer newConfig) {
Expand Down Expand Up @@ -97,7 +103,13 @@ public ConfigContainer update(ConfigContainer newConfig) {
throw new IllegalStateException("The new FOSSology REST configuration does not contain a valid folder id.");
}

ConfigContainer current = get();
ConfigContainer current;
try {
current = get();
} catch (SW360Exception e) {
log.error(e);
throw new IllegalStateException("Unable to get container config.");
}
current.setConfigKeyToValues(newConfig.getConfigKeyToValues());

repository.update(current);
Expand All @@ -108,7 +120,7 @@ public ConfigContainer update(ConfigContainer newConfig) {
return current;
}

public ConfigContainer get() {
public ConfigContainer get() throws SW360Exception {
if (config == null || outdated) {
try {
config = repository.getByConfigFor(ConfigFor.FOSSOLOGY_REST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,12 @@ public RequestStatus addOrUpdateCustomProperties(CustomProperties customProperti
if(customProperties.isSetId()){
customPropertiesRepository.update(customProperties);
} else {
customPropertiesRepository.add(customProperties);
try {
customPropertiesRepository.add(customProperties);
} catch (SW360Exception e) {
log.error("Unable to add or update custom license property.", e);
return RequestStatus.FAILURE;
}
}
return RequestStatus.SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.junit.Before;
import org.junit.Test;

import java.net.MalformedURLException;
import java.util.*;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -167,7 +166,7 @@ public void testUpdateLicenseIdNotMatch() throws Exception {
handler.updateLicense(invalidLicense, user, user);
}

public void createTestEntries() throws MalformedURLException {
public void createTestEntries() throws SW360Exception {
// List of test objects
licenses = new HashMap<>();
obligs = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public List<ModerationRequest> getRequestByDocumentId(String documentId) {
return requests;
}

public String createClearingRequest(ClearingRequest request, User user) {
public String createClearingRequest(ClearingRequest request, User user) throws SW360Exception {
request.setTimestamp(System.currentTimeMillis());
long id = 1;
synchronized (ModerationDatabaseHandler.class) {
Expand Down Expand Up @@ -441,7 +441,12 @@ public RequestStatus createRequest(Component component, User user, Boolean isDel
if(component.isSetComponentType()) {
request.setComponentType(component.getComponentType());
}
addOrUpdate(request, user);
try {
addOrUpdate(request, user);
} catch (SW360Exception e) {
log.error("Unable to update request.", e);
return RequestStatus.FAILURE;
}
return RequestStatus.SENT_TO_MODERATOR;
}

Expand Down Expand Up @@ -476,7 +481,12 @@ public RequestStatus createRequest(Release release, User user, Boolean isDeleteR
} catch (SW360Exception e) {
log.error("Could not retrieve parent component type of release with ID=" + release.getId());
}
addOrUpdate(request, user);
try {
addOrUpdate(request, user);
} catch (SW360Exception e) {
log.error("Unable to update request.", e);
return RequestStatus.FAILURE;
}
return RequestStatus.SENT_TO_MODERATOR;
}

Expand Down Expand Up @@ -551,7 +561,12 @@ public RequestStatus createRequest(Project project, User user, Boolean isDeleteR
// Fill the request
ModerationRequestGenerator generator = new ProjectModerationRequestGenerator();
request = generator.setAdditionsAndDeletions(request, project, dbproject);
addOrUpdate(request, user);
try {
addOrUpdate(request, user);
} catch (SW360Exception e) {
log.error("Unable to create request.", e);
return RequestStatus.FAILURE;
}
return RequestStatus.SENT_TO_MODERATOR;
}

Expand Down Expand Up @@ -617,7 +632,12 @@ public RequestStatus createRequest(License license, User user) {
// Fill the request
ModerationRequestGenerator generator = new LicenseModerationRequestGenerator();
request = generator.setAdditionsAndDeletions(request, license, dblicense);
addOrUpdate(request, user);
try {
addOrUpdate(request, user);
} catch (SW360Exception e) {
log.error("Unable to create request.", e);
return RequestStatus.FAILURE;
}
return RequestStatus.SENT_TO_MODERATOR;
}

Expand All @@ -633,7 +653,11 @@ public void createRequest(User user) {
// Set the object
request.setUser(user);

addOrUpdate(request, user);
try {
addOrUpdate(request, user);
} catch (SW360Exception e) {
log.error("Unable to create request.", e);
}
}

private Set<String> getSPDXDocumentModerators(String department, SPDXDocument dbSpdx) {
Expand Down Expand Up @@ -685,7 +709,12 @@ public RequestStatus createRequest(SPDXDocument spdx, User user, Boolean isDelet
// Fill the request
ModerationRequestGenerator generator = new SpdxDocumentModerationRequestGenerator();
request = generator.setAdditionsAndDeletions(request, spdx, dbSpdx);
addOrUpdate(request, user);
try {
addOrUpdate(request, user);
} catch (SW360Exception e) {
log.error("Unable to create request.", e);
return RequestStatus.FAILURE;
}
return RequestStatus.SENT_TO_MODERATOR;
}

Expand All @@ -706,7 +735,12 @@ public RequestStatus createRequest(DocumentCreationInformation documentCreationI
// Fill the request
ModerationRequestGenerator generator = new SpdxDocumentCreationInfoModerationRequestGenerator();
request = generator.setAdditionsAndDeletions(request, documentCreationInfo, dbDocumentCreationInfo);
addOrUpdate(request, user);
try {
addOrUpdate(request, user);
} catch (SW360Exception e) {
log.error("Unable to create request.", e);
return RequestStatus.FAILURE;
}
return RequestStatus.SENT_TO_MODERATOR;
}

Expand All @@ -727,7 +761,12 @@ public RequestStatus createRequest(PackageInformation packageInfo, User user, Bo
// Fill the request
ModerationRequestGenerator generator = new SpdxPackageInfoModerationRequestGenerator();
request = generator.setAdditionsAndDeletions(request, packageInfo, dbPackageInfo);
addOrUpdate(request, user);
try {
addOrUpdate(request, user);
} catch (SW360Exception e) {
log.error("Unable to create request.", e);
return RequestStatus.FAILURE;
}
return RequestStatus.SENT_TO_MODERATOR;
}

Expand Down Expand Up @@ -846,10 +885,10 @@ private List<User> getAllSW360Users() {
return sw360users;
}

public void addOrUpdate(ModerationRequest request, User user) {
public void addOrUpdate(ModerationRequest request, User user) throws SW360Exception {
addOrUpdate(request, user.getEmail());
}
public void addOrUpdate(ModerationRequest request, String userEmail) {
public void addOrUpdate(ModerationRequest request, String userEmail) throws SW360Exception {
if (request.isSetId()) {
repository.update(request);
sendMailNotificationsForUpdatedRequest(request, userEmail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.common.DatabaseSettingsTest;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.eclipse.sw360.datahandler.thrift.moderation.DocumentType;
import org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
Expand All @@ -24,7 +25,7 @@
public class DatabaseTestSetup {


public static void main(String[] args) throws MalformedURLException {
public static void main(String[] args) throws MalformedURLException, SW360Exception {

DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(DatabaseSettingsTest.getConfiguredClient(), DatabaseSettingsTest.COUCH_DB_DATABASE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void testWithBrokenURL() throws Exception {
}
}

private AttachmentContent saveRemoteAttachment(String remoteUrl) {
private AttachmentContent saveRemoteAttachment(String remoteUrl) throws SW360Exception {
AttachmentContent attachmentContent = new AttachmentContent()
.setFilename("testfile")
.setContentType("text")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ public AddDocumentRequestSummary addVendor(Vendor vendor) {
log.error("Error creating the vendor: " + e.why);
return new AddDocumentRequestSummary().setRequestStatus(AddDocumentRequestStatus.FAILURE).setMessage(e.why);
}
repository.add(vendor);
try {
repository.add(vendor);
} catch (SW360Exception e) {
log.error("Error adding vendor", e);
return new AddDocumentRequestSummary().setRequestStatus(AddDocumentRequestStatus.FAILURE).setMessage(e.why);
}
return new AddDocumentRequestSummary().setRequestStatus(AddDocumentRequestStatus.SUCCESS).setId(vendor.getId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.common.DatabaseSettingsTest;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.eclipse.sw360.datahandler.thrift.vendors.VendorService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

/**
Expand All @@ -30,15 +29,15 @@
public class TestVendorClient {

@SuppressWarnings("unused")
public static void InitDatabase() throws MalformedURLException {
public static void InitDatabase() throws SW360Exception {
DatabaseConnectorCloudant databaseConnector = new DatabaseConnectorCloudant(DatabaseSettingsTest.getConfiguredClient(), DatabaseSettingsTest.COUCH_DB_DATABASE);

databaseConnector.add(new Vendor().setShortname("Microsoft").setFullname("Microsoft Corporation").setUrl("http://www.microsoft.com"));
databaseConnector.add(new Vendor().setShortname("Apache").setFullname("The Apache Software Foundation").setUrl("http://www.apache.org"));
databaseConnector.add(new Vendor().setShortname("Oracle").setFullname("Oracle Corporation Inc").setUrl("http://www.oracle.com"));
}

public static void main(String[] args) throws TException, IOException {
public static void main(String[] args) throws TException {
THttpClient thriftClient = new THttpClient("http://127.0.0.1:8080/vendorservice/thrift");
TProtocol protocol = new TCompactProtocol(thriftClient);
VendorService.Iface client = new VendorService.Client(protocol);
Expand Down
Loading

0 comments on commit 9452b2b

Please sign in to comment.