MNT-22036 : REST API always applies versioning

Add versionState form-data property to POST nodes/{nodeId}/children multipart upload.
   Add versionState to MultiPartBuilder.
   Add tests for multipart upload
This commit is contained in:
Epure Alexandru-Eusebiu
2020-11-12 18:17:02 +02:00
parent 54922aefbd
commit fbb6f422cf
3 changed files with 149 additions and 2 deletions

View File

@@ -2914,6 +2914,7 @@ public class NodesImpl implements Nodes
String versionComment = null; String versionComment = null;
String relativePath = null; String relativePath = null;
String renditionNames = null; String renditionNames = null;
String versioningState = null;
Map<String, Object> qnameStrProps = new HashMap<>(); Map<String, Object> qnameStrProps = new HashMap<>();
Map<QName, Serializable> properties = null; Map<QName, Serializable> properties = null;
@@ -2971,6 +2972,10 @@ public class NodesImpl implements Nodes
renditionNames = getStringOrNull(field.getValue()); renditionNames = getStringOrNull(field.getValue());
break; break;
case "versionstate":
versioningState = getStringOrNull(field.getValue());
break;
default: default:
{ {
final String propName = field.getName(); final String propName = field.getName();
@@ -3042,13 +3047,29 @@ public class NodesImpl implements Nodes
throw new ConstraintViolatedException(fileName + " already exists."); throw new ConstraintViolatedException(fileName + " already exists.");
} }
} }
// Note: pending REPO-159, we currently auto-enable versioning on new upload (but not when creating empty file) // Note: pending REPO-159, we currently auto-enable versioning on new upload (but not when creating empty file)
if (versionMajor == null) if (versionMajor == null)
{ {
versionMajor = true; versionMajor = true;
} }
// MNT-22036 add versioningState property for newly created nodes.
if (null != versioningState)
{
switch (versioningState)
{
case "none":
versionMajor = null;
break;
case "major":
versionMajor = true;
break;
default:
versionMajor = false;
}
}
// Create a new file. // Create a new file.
NodeRef nodeRef = createNewFile(parentNodeRef, fileName, nodeTypeQName, content, properties, assocTypeQName, parameters, versionMajor, versionComment); NodeRef nodeRef = createNewFile(parentNodeRef, fileName, nodeTypeQName, content, properties, assocTypeQName, parameters, versionMajor, versionComment);

View File

@@ -5612,6 +5612,124 @@ public class NodeApiTest extends AbstractSingleNetworkSiteTest
assertTrue(((ArrayList) (propUpdateResponse.get("custom:locations"))).size() == 1); assertTrue(((ArrayList) (propUpdateResponse.get("custom:locations"))).size() == 1);
} }
@Test
public void versioningStatePropetyMultipartUploadTest() throws Exception
{
setRequestContext(user1);
String myNodeId = getMyNodeId();
// Scenario 1 majorVersion and versionState multipart values are not set.
String fileName = "myfile" + UUID.randomUUID() + ".txt";
File file = getResourceFile("quick-2.pdf");
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
MultiPartRequest reqBody = multiPartBuilder.build();
HttpResponse response = post(getNodeChildrenUrl(myNodeId), reqBody.getBody(), null, reqBody.getContentType(), 201);
Document documentResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
// Default behaviour, expect to be MAJOR Version 1.0
Map<String, Object> documentProperties = documentResponse.getProperties();
assertEquals(2, documentProperties.size());
assertEquals("MAJOR", documentProperties.get("cm:versionType"));
assertEquals("1.0", documentProperties.get("cm:versionLabel"));
// Scenario 2 majorVersion is not set versionState is set to none.
fileName = "myfile" + UUID.randomUUID() + ".txt";
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
multiPartBuilder.setVersioningState("none");
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(myNodeId), reqBody.getBody(), null, reqBody.getContentType(), 201);
documentResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
documentProperties = documentResponse.getProperties();
assertNull(documentProperties);
// Scenario 3 majorVersion is set to true and versionState is set to none.
fileName = "myfile" + UUID.randomUUID() + ".txt";
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
multiPartBuilder.setMajorVersion(true);
multiPartBuilder.setVersioningState("none");
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(myNodeId), reqBody.getBody(), null, reqBody.getContentType(), 201);
documentResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
documentProperties = documentResponse.getProperties();
assertNull(documentProperties);
// Scenario 4 majorVersion is set to false and versionState is set to none.
fileName = "myfile" + UUID.randomUUID() + ".txt";
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
multiPartBuilder.setMajorVersion(false);
multiPartBuilder.setVersioningState("none");
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(myNodeId), reqBody.getBody(), null, reqBody.getContentType(), 201);
documentResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
documentProperties = documentResponse.getProperties();
assertNull(documentProperties);
// Scenario 5 majorVersion is not set versionState is set to minor.
fileName = "myfile" + UUID.randomUUID() + ".txt";
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
multiPartBuilder.setVersioningState("minor");
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(myNodeId), reqBody.getBody(), null, reqBody.getContentType(), 201);
documentResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
documentProperties = documentResponse.getProperties();
assertEquals(2, documentProperties.size());
assertEquals("MINOR", documentProperties.get("cm:versionType"));
assertEquals("0.1", documentProperties.get("cm:versionLabel"));
// Scenario 6 majorVersion is set to true and versionState is set to minor.
fileName = "myfile" + UUID.randomUUID() + ".txt";
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
multiPartBuilder.setMajorVersion(true);
multiPartBuilder.setVersioningState("minor");
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(myNodeId), reqBody.getBody(), null, reqBody.getContentType(), 201);
documentResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
documentProperties = documentResponse.getProperties();
assertEquals(2, documentProperties.size());
assertEquals("MINOR", documentProperties.get("cm:versionType"));
assertEquals("0.1", documentProperties.get("cm:versionLabel"));
// Scenario 7 majorVersion is not set versionState is set to major.
fileName = "myfile" + UUID.randomUUID() + ".txt";
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
multiPartBuilder.setVersioningState("major");
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(myNodeId), reqBody.getBody(), null, reqBody.getContentType(), 201);
documentResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
documentProperties = documentResponse.getProperties();
assertEquals(2, documentProperties.size());
assertEquals("MAJOR", documentProperties.get("cm:versionType"));
assertEquals("1.0", documentProperties.get("cm:versionLabel"));
// Scenario 8 majorVersion is set to false and versionState is set to major.
fileName = "myfile" + UUID.randomUUID() + ".txt";
multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
multiPartBuilder.setMajorVersion(false);
multiPartBuilder.setVersioningState("major");
reqBody = multiPartBuilder.build();
response = post(getNodeChildrenUrl(myNodeId), reqBody.getBody(), null, reqBody.getContentType(), 201);
documentResponse = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
documentProperties = documentResponse.getProperties();
assertEquals(2, documentProperties.size());
assertEquals("MAJOR", documentProperties.get("cm:versionType"));
assertEquals("1.0", documentProperties.get("cm:versionLabel"));
}
@Test public void testAuditableProperties() throws Exception @Test public void testAuditableProperties() throws Exception
{ {
setRequestContext(user1); setRequestContext(user1);

View File

@@ -43,7 +43,6 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
/** /**
* <i><b>multipart/form-data</b></i> builder. * <i><b>multipart/form-data</b></i> builder.
@@ -57,6 +56,7 @@ public class MultiPartBuilder
private String updateNodeRef; private String updateNodeRef;
private String description; private String description;
private String contentTypeQNameStr; private String contentTypeQNameStr;
private String versioningState;
private List<String> aspects = Collections.emptyList(); private List<String> aspects = Collections.emptyList();
private Boolean majorVersion; private Boolean majorVersion;
private Boolean overwrite; private Boolean overwrite;
@@ -76,6 +76,7 @@ public class MultiPartBuilder
this.updateNodeRef = that.updateNodeRef; this.updateNodeRef = that.updateNodeRef;
this.description = that.description; this.description = that.description;
this.contentTypeQNameStr = that.contentTypeQNameStr; this.contentTypeQNameStr = that.contentTypeQNameStr;
this.versioningState = that.versioningState;
this.aspects = new ArrayList<>(that.aspects); this.aspects = new ArrayList<>(that.aspects);
this.majorVersion = that.majorVersion; this.majorVersion = that.majorVersion;
this.overwrite = that.overwrite; this.overwrite = that.overwrite;
@@ -125,6 +126,12 @@ public class MultiPartBuilder
return this; return this;
} }
public MultiPartBuilder setVersioningState(String versioningState)
{
this.versioningState = versioningState;
return this;
}
public MultiPartBuilder setAspects(List<String> aspects) public MultiPartBuilder setAspects(List<String> aspects)
{ {
this.aspects = aspects; this.aspects = aspects;
@@ -278,6 +285,7 @@ public class MultiPartBuilder
addPartIfNotNull(parts, "updatenoderef", updateNodeRef); addPartIfNotNull(parts, "updatenoderef", updateNodeRef);
addPartIfNotNull(parts, "description", description); addPartIfNotNull(parts, "description", description);
addPartIfNotNull(parts, "contenttype", contentTypeQNameStr); addPartIfNotNull(parts, "contenttype", contentTypeQNameStr);
addPartIfNotNull(parts, "versioningstate", versioningState);
addPartIfNotNull(parts, "aspects", getCommaSeparated(aspects)); addPartIfNotNull(parts, "aspects", getCommaSeparated(aspects));
addPartIfNotNull(parts, "majorversion", majorVersion); addPartIfNotNull(parts, "majorversion", majorVersion);
addPartIfNotNull(parts, "overwrite", overwrite); addPartIfNotNull(parts, "overwrite", overwrite);