Skip to content

Commit

Permalink
[DURACOM-328] fix error in check for Patch request
Browse files Browse the repository at this point in the history
  • Loading branch information
steph-ieffam committed Feb 26, 2025
1 parent ee7a4e6 commit f787e0b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
13 changes: 12 additions & 1 deletion dspace-api/src/test/data/dspaceFolder/config/item-submission.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<name-map collection-handle="123456789/collection-test" submission-name="collectiontest"/>
<name-map collection-entity-type="CustomEntityType" submission-name="entitytypetest"/>
<name-map collection-handle="123456789/test-duplicate-detection" submission-name="test-duplicate-detection"/>
<name-map collection-handle="123456789/collection-test-patch" submission-name="publicationTestPatch"/>
</submission-map>


Expand Down Expand Up @@ -194,7 +195,11 @@
<processing-class>org.dspace.app.rest.submit.step.NotifyStep</processing-class>
<type>coarnotify</type>
</step-definition>

<step-definition id="upload-no-required-metadata" mandatory="true">
<heading>submit.progressbar.upload-no-required-metadata</heading>
<processing-class>org.dspace.app.rest.submit.step.UploadStep</processing-class>
<type>upload</type>
</step-definition>
</step-definitions>

<!-- The submission-definitions map lays out the detailed definition of -->
Expand Down Expand Up @@ -300,6 +305,12 @@
<step id="traditionalpageone"/>
</submission-process>

<submission-process name="publicationTestPatch">
<step id="collection" />
<step id="traditionalpageone" />
<step id="upload-no-required-metadata" />
<step id="license" />
</submission-process>
</submission-definitions>

</item-submission>
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<property name="map">
<map>
<entry key="upload" value-ref="uploadConfigurationDefault" />
<entry key="upload-no-required-metadata" value-ref="uploadConfigurationPublicationTestPatch" />
</map>
</property>
</bean>
Expand Down Expand Up @@ -116,4 +117,16 @@
</property>
</bean>

<bean id="uploadConfigurationPublicationTestPatch" class="org.dspace.submit.model.UploadConfiguration">
<property name="name" value="upload-no-required-metadata"/>
<property name="metadata" value="bitstream-metadata" />
<property name="options">
<list>
<ref bean="administrator"/>
<ref bean="openAccess"/>
<ref bean="embargoed" />
</list>
</property>
</bean>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -48,6 +49,13 @@ public class UploadStep extends AbstractProcessingStep

private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(UploadStep.class);

private static final Pattern UPDATE_METADATA_PATTERN =
Pattern.compile("^/sections/[^/]+/files/[^/]+/metadata/[^/]+(/[^/]+)?$");
private static final Pattern PRIMARY_FLAG_PATTERN =
Pattern.compile("^/sections/[^/]+/primary$");
private static final Pattern ACCESS_CONDITION_PATTERN =
Pattern.compile("^/sections/[^/]+/files/[^/]+/accessConditions(/[^/]+)?$");

@Override
public DataUpload getData(SubmissionService submissionService, InProgressSubmission obj,
SubmissionStepConfig config) throws Exception {
Expand All @@ -73,27 +81,27 @@ public void doPatchProcessing(Context context, HttpServletRequest currentRequest

String instance = null;
if ("remove".equals(op.getOp())) {
if (op.getPath().contains(UPLOAD_STEP_METADATA_PATH)) {
if (UPDATE_METADATA_PATTERN.matcher(op.getPath()).matches()) {
instance = UPLOAD_STEP_METADATA_OPERATION_ENTRY;
} else if (op.getPath().contains(UPLOAD_STEP_ACCESSCONDITIONS_OPERATION_ENTRY)) {
} else if (ACCESS_CONDITION_PATTERN.matcher(op.getPath()).matches()) {
instance = stepConf.getType() + "." + UPLOAD_STEP_ACCESSCONDITIONS_OPERATION_ENTRY;
} else if (op.getPath().contains(PRIMARY_FLAG_ENTRY)) {
} else if (PRIMARY_FLAG_PATTERN.matcher(op.getPath()).matches()) {
instance = PRIMARY_FLAG_ENTRY;
} else {
instance = UPLOAD_STEP_REMOVE_OPERATION_ENTRY;
}
} else if ("move".equals(op.getOp())) {
if (op.getPath().contains(UPLOAD_STEP_METADATA_PATH)) {
if (UPDATE_METADATA_PATTERN.matcher(op.getPath()).matches()) {
instance = UPLOAD_STEP_METADATA_OPERATION_ENTRY;
} else {
instance = UPLOAD_STEP_MOVE_OPERATION_ENTRY;
}
} else {
if (op.getPath().contains(UPLOAD_STEP_ACCESSCONDITIONS_OPERATION_ENTRY)) {
if (ACCESS_CONDITION_PATTERN.matcher(op.getPath()).matches()) {
instance = stepConf.getType() + "." + UPLOAD_STEP_ACCESSCONDITIONS_OPERATION_ENTRY;
} else if (op.getPath().contains(UPLOAD_STEP_METADATA_PATH)) {
} else if (UPDATE_METADATA_PATTERN.matcher(op.getPath()).matches()) {
instance = UPLOAD_STEP_METADATA_OPERATION_ENTRY;
} else if (op.getPath().contains(PRIMARY_FLAG_ENTRY)) {
} else if (PRIMARY_FLAG_PATTERN.matcher(op.getPath()).matches()) {
instance = PRIMARY_FLAG_ENTRY;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import static com.jayway.jsonpath.JsonPath.read;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
Expand Down Expand Up @@ -2218,4 +2219,49 @@ public void testWorkflowWithHiddenSections() throws Exception {

}

@Test
public void deleteBitstreamTest()
throws Exception {
context.turnOffAuthorisationSystem();
EPerson submitter = EPersonBuilder.createEPerson(context)
.withEmail("submitter@example.com")
.withPassword(password)
.build();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection collection1 = CollectionBuilder.createCollection(context, parentCommunity,
"123456789/collection-test-patch")
.withName("Collection 1")
.build();
Bitstream bitstream = null;
WorkspaceItem witem = null;
String bitstreamContent = "0123456789";
try (InputStream is = IOUtils.toInputStream(bitstreamContent, Charset.defaultCharset())) {
context.setCurrentUser(submitter);
witem = WorkspaceItemBuilder.createWorkspaceItem(context, collection1)
.withTitle("Test WorkspaceItem")
.withIssueDate("2019-10-01")
.grantLicense()
.build();
bitstream = BitstreamBuilder.createBitstream(context, witem.getItem(), is)
.withName("Test bitstream")
.withDescription("This is a bitstream to test range requests")
.withMimeType("text/plain")
.build();
}
context.restoreAuthSystemState();
String tokenSubmitter = getAuthToken(submitter.getEmail(), password);
List<Operation> deleteFile = new ArrayList<>();
deleteFile.add(new RemoveOperation("/sections/bitstream-metadata-publication/files/0/"));
getClient(tokenSubmitter).perform(patch("/api/submission/workspaceitems/" + witem.getID())
.content(getPatchContent(deleteFile))
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
.andExpect(status().isOk());
// verify that the patch removed bitstream
getClient(tokenSubmitter).perform(get("/api/submission/workspaceitems/" + witem.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.sections.bitstream-metadata-publication.files",hasSize(0)));
}

}

0 comments on commit f787e0b

Please sign in to comment.