Skip to content

Commit

Permalink
feat(rest): Update release with attachment info
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangnt2 committed Aug 21, 2023
1 parent 165d56d commit 7a785ca
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
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,23 @@ private Release setBackwardCompatibleFieldsInRelease(Map<String, Object> reqBody

return release;
}

private Set<Attachment> getAttachmentsFromRequest(Object attachmentData, ObjectMapper mapper) {
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);
})
.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

0 comments on commit 7a785ca

Please sign in to comment.