Search in sources :

Example 71 with Attachment

use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.

the class RemoteAttachmentDownloaderTest method testIntegration.

@Test
public void testIntegration() throws Exception {
    AttachmentContent attachmentContent = saveRemoteAttachment(url);
    assertThat(retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout), is(1));
    assertThat(attachmentConnector.getAttachmentStream(attachmentContent, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentContent.getId())))), hasLength(greaterThan(0l)));
    assertThat(retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout), is(0));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) Test(org.junit.Test)

Example 72 with Attachment

use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.

the class PortletUtils method updateAttachmentsFromRequest.

/**
 * Returns a set of updated attachments from the given request.
 *
 * This function will also take a set of existing attachments that will be
 * updated according to the request. A new set with the updated attachments will
 * be returned.
 *
 * If some of the attachments in the given set are not present in the request,
 * they will not be part of the returned set.
 *
 * Note: the given set will not be changed. However, the containing attachments
 * are changed during update.
 *
 * @param request
 *            request to parse for attachments
 * @param documentAttachments
 *            existing attachments
 *
 * @return set of updated attachments present in the request
 */
public static Set<Attachment> updateAttachmentsFromRequest(PortletRequest request, Set<Attachment> documentAttachments) {
    Set<Attachment> attachments = Sets.newHashSet();
    User user = UserCacheHolder.getUserFromRequest(request);
    String[] ids = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.ATTACHMENT_CONTENT_ID.toString());
    String[] fileNames = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.FILENAME.toString());
    String[] types = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.ATTACHMENT_TYPE.toString());
    String[] createdComments = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.CREATED_COMMENT.toString());
    String[] checkStatuses = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.CHECK_STATUS.toString());
    String[] checkedComments = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.CHECKED_COMMENT.toString());
    if (ids == null || ids.length == 0) {
        LOGGER.info("No ids transmitted. All attachments will be deleted.");
        return attachments;
    } else if (CommonUtils.oneIsNull(fileNames, types, createdComments, checkStatuses, checkedComments)) {
        LOGGER.error("Invalid request content. One of the attachment parameters is null. No attachments will be saved or deleted.");
        return documentAttachments;
    } else if (!CommonUtils.allHaveSameLength(ids, fileNames, types, createdComments, checkStatuses, checkedComments)) {
        LOGGER.error("Not all of the attachment parameter arrays have the same length! No attachments will be saved or deleted.");
        return documentAttachments;
    }
    Map<String, Attachment> documentAttachmentMap = CommonUtils.nullToEmptySet(documentAttachments).stream().collect(Collectors.toMap(Attachment::getAttachmentContentId, Function.identity()));
    for (int i = 0; i < ids.length; ++i) {
        String id = ids[i];
        Attachment attachment = documentAttachmentMap.get(id);
        if (attachment == null) {
            // the sha1 checksum is not computed here, but in the backend, when updating the
            // component in the database
            attachment = CommonUtils.getNewAttachment(user, id, fileNames[i]);
            documentAttachmentMap.put(attachment.getAttachmentContentId(), attachment);
        }
        // Filename is not overwritten. Unknown reason.
        attachment.setAttachmentType(getAttachmentTypefromString(types[i]));
        attachment.setCreatedComment(createdComments[i]);
        if (getCheckStatusfromString(checkStatuses[i]) != CheckStatus.NOTCHECKED) {
            if (attachment.checkStatus != getCheckStatusfromString(checkStatuses[i]) || !checkedComments[i].equals(attachment.checkedComment)) {
                attachment.setCheckedOn(SW360Utils.getCreatedOn());
                attachment.setCheckedBy(UserCacheHolder.getUserFromRequest(request).getEmail());
                attachment.setCheckedTeam(UserCacheHolder.getUserFromRequest(request).getDepartment());
                attachment.setCheckedComment(checkedComments[i]);
            }
        } else {
            attachment.setCheckedOn(null);
            attachment.setCheckedBy(null);
            attachment.setCheckedTeam(null);
            attachment.setCheckedComment("");
        }
        attachment.setCheckStatus(getCheckStatusfromString(checkStatuses[i]));
        // add attachments to list of added/modified attachments. This way deleted
        // attachments are automatically not in the set
        attachments.add(attachment);
    }
    return attachments;
}
Also used : User(org.eclipse.sw360.datahandler.thrift.users.User) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

Example 73 with Attachment

use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.

the class CombinedCLIParserTest method testIsApplicableToFailsOnIncorrectRootElement.

@Test
public void testIsApplicableToFailsOnIncorrectRootElement() throws Exception {
    AttachmentContent content = new AttachmentContent().setId("A1").setFilename("a.xml").setContentType("application/xml");
    when(connector.getAttachmentStream(content, new User(), new Project())).thenReturn(new ReaderInputStream(new StringReader("<wrong-root/>")));
    assertFalse(parser.isApplicableTo(attachment, new User(), new Project()));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) User(org.eclipse.sw360.datahandler.thrift.users.User) StringReader(java.io.StringReader) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Test(org.junit.Test)

Example 74 with Attachment

use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.

the class CombinedCLIParserTest method setUp.

@Before
public void setUp() throws Exception {
    cliTestfile = IOUtils.toString(makeAttachmentContentStream(TEST_XML_FILENAME));
    attachment = new Attachment("A1", "a.xml").setAttachmentType(AttachmentType.COMPONENT_LICENSE_INFO_COMBINED);
    content = new AttachmentContent().setId("A1").setFilename("a.xml").setContentType("application/xml");
    parser = spy(new CombinedCLIParser(connector, attachment -> content, componentDatabaseHandler));
    doReturn("external-correlation-id").when(parser).getCorrelationKey();
    Release r1 = new Release().setId("id1").setName("r1").setVersion("1.0").setVendor(new Vendor().setFullname("VendorA Fullname").setShortname("VendorA")).setExternalIds(ImmutableMap.of(parser.getCorrelationKey(), "1234"));
    Release r2 = new Release().setId("id2").setName("r2").setVersion("2.0").setVendor(new Vendor().setFullname("VendorB Fullname").setShortname("VendorB")).setExternalIds(ImmutableMap.of(parser.getCorrelationKey(), "4321"));
    Release r3 = new Release().setId("id3").setName("r3").setVersion("3.0").setVendor(new Vendor().setFullname("VendorC Fullname").setShortname("VendorC"));
    Release r4 = new Release().setId("id4").setName("r4").setVersion("4.0").setVendor(new Vendor().setFullname("VendorD Fullname").setShortname("VendorD")).setExternalIds(ImmutableMap.of("some_external_id", "1234"));
    when(componentDatabaseHandler.getAllReleasesIdMap()).thenReturn(ImmutableMap.of(r1.getId(), r1, r2.getId(), r2, r3.getId(), r3, r4.getId(), r4));
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) Vendor(org.eclipse.sw360.datahandler.thrift.vendors.Vendor) Release(org.eclipse.sw360.datahandler.thrift.components.Release) Before(org.junit.Before)

Example 75 with Attachment

use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.

the class SPDXParserTest method testGetLicenseInfo.

@Test
@UseDataProvider("dataProviderAdd")
public void testGetLicenseInfo(String exampleFile, List<String> expectedLicenses, int numberOfCoyprights, String exampleCopyright) throws Exception {
    Attachment attachment = makeAttachment(exampleFile, Arrays.stream(AttachmentType.values()).filter(SW360Constants.LICENSE_INFO_ATTACHMENT_TYPES::contains).findAny().get());
    LicenseInfoParsingResult result = parser.getLicenseInfos(attachment, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachment.getAttachmentContentId())))).stream().findFirst().orElseThrow(() -> new RuntimeException("Parser returned empty LisenceInfoParsingResult list"));
    assertLicenseInfoParsingResult(result);
    assertIsResultOfExample(result.getLicenseInfo(), exampleFile, expectedLicenses, numberOfCoyprights, exampleCopyright);
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) SW360Constants(org.eclipse.sw360.datahandler.common.SW360Constants) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)65 Test (org.junit.Test)40 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)38 User (org.eclipse.sw360.datahandler.thrift.users.User)27 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)22 InputStream (java.io.InputStream)19 Release (org.eclipse.sw360.datahandler.thrift.components.Release)15 IOException (java.io.IOException)13 PortletRequest (javax.portlet.PortletRequest)11 TestUserCacheHolder (org.eclipse.sw360.portal.TestUserCacheHolder)9 StringReader (java.io.StringReader)8 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)8 TException (org.apache.thrift.TException)8 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)8 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)7 OutputStream (java.io.OutputStream)5 AttachmentInputStream (org.ektorp.AttachmentInputStream)5 ResponseEntity (org.springframework.http.ResponseEntity)5 ComponentService (org.eclipse.sw360.datahandler.thrift.components.ComponentService)4 Maps (com.google.common.collect.Maps)3