Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rest): Update release with attachment info #2092

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,13 @@ public Set<AttachmentDTO> getAttachmentDTOs(Set<Attachment> attachments, Map<Att
});
return attachmentDTOS;
}

public boolean isAttachmentExist(String id) {
try {
Attachment attachment = getAttachmentForId(id);
return attachment != null;
} catch (ResourceNotFoundException | TException notFoundException) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
Expand Down Expand Up @@ -829,7 +830,16 @@ private Release setBackwardCompatibleFieldsInRelease(Map<String, Object> reqBody
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(sw360Module);

Set<Attachment> attachments = getAttachmentsFromRequest(reqBodyMap.get("attachments"), mapper);
if (null != reqBodyMap.get("attachments")) {
reqBodyMap.remove("attachments");
}
Release release = mapper.convertValue(reqBodyMap, Release.class);
if (null != attachments) {
release.setAttachments(attachments);
}

mapOfBackwardCompatible_Field_OldFieldNames_NewFieldNames.entrySet().stream().forEach(entry -> {
Release._Fields field = entry.getKey();
String oldFieldName = entry.getValue()[0];
Expand All @@ -841,6 +851,24 @@ private Release setBackwardCompatibleFieldsInRelease(Map<String, Object> reqBody

return release;
}

private Set<Attachment> getAttachmentsFromRequest(Object attachmentData, ObjectMapper mapper) {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
if (null == attachmentData) {
return null;
}
Set<AttachmentDTO> attachmentDTOs = mapper.convertValue(attachmentData,
mapper.getTypeFactory().constructCollectionType(Set.class, AttachmentDTO.class));
return attachmentDTOs.stream()
.map(attachmentDTO -> {
boolean isAttachmentExist = attachmentService.isAttachmentExist(attachmentDTO.getAttachmentContentId());
if (!isAttachmentExist) {
throw new ResourceNotFoundException("Attachment " + attachmentDTO.getAttachmentContentId() + " not found.");
}
return restControllerHelper.convertToAttachment(attachmentDTO, sw360User);
})
.collect(Collectors.toSet());
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public void before() throws TException, IOException {
given(this.attachmentServiceMock.filterAttachmentsToRemove(any(), any(), any())).willReturn(Collections.singleton(attachment));
given(this.attachmentServiceMock.updateAttachment(any(), any(), any(), any())).willReturn(att2);
given(this.sw360VendorService.getVendorById(any())).willReturn(new Vendor("TV", "Test Vendor", "http://testvendor.com"));
given(this.attachmentServiceMock.isAttachmentExist(eq("1231231254"))).willReturn(true);

Map<String, Set<String>> externalIds = new HashMap<>();
externalIds.put("mainline-id-component", new HashSet<>(Arrays.asList("1432", "4876")));
Expand Down Expand Up @@ -751,9 +752,23 @@ public void should_document_get_usedbyresource_for_release() throws Exception {

@Test
public void should_document_update_release() throws Exception {
Release updateRelease = new Release();
release.setName("Updated release");
release.setComponentType(ComponentType.OSS);
Map<String, Object> updateRelease = new HashMap<>();
updateRelease.put("name", "Updated release");
updateRelease.put("componentType", ComponentType.OSS.toString());

Map<String, String> attachmentData = new HashMap<>();
attachmentData.put("sha1", "da373e491d3863477568896089ee9457bc316783");
attachmentData.put("attachmentType",AttachmentType.BINARY_SELF.toString());
attachmentData.put("attachmentContentId", "1231231254");
attachmentData.put("createdTeam", "Clearing Team 1");
attachmentData.put("createdComment", "please check asap");
attachmentData.put("createdOn", "2022-08-19");
attachmentData.put("createdBy", "[email protected]");
attachmentData.put("checkedComment", "everything looks good");
attachmentData.put("checkedTeam", "Clearing Team 2");
attachmentData.put("checkedOn", "2016-12-18");
updateRelease.put("attachments", Collections.singletonList(attachmentData));

String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(patch("/api/releases/" + releaseId)
.contentType(MediaTypes.HAL_JSON)
Expand Down
Loading