use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class NodeVersionsApiTest method testDeleteVersion.
/**
* Test delete version
*
* Note: see also VersionServiceImplTest.testDeleteLastVersion
*
* @throws Exception
*/
@Test
public void testDeleteVersion() throws Exception {
setRequestContext(user1);
String sharedFolderNodeId = getSharedNodeId();
// create folder
String f1Id = null;
try {
f1Id = createFolder(sharedFolderNodeId, "testDeleteVersion-f1").getId();
String textContentSuffix = "Amazingly few discotheques provide jukeboxes ";
String contentName = "content-1";
int cnt = 4;
Pair<String, String> pair = uploadTextFileVersions(user1, f1Id, contentName, cnt, textContentSuffix, 0, null, null);
String versionLabel = pair.getFirst();
String docId = pair.getSecond();
assertEquals("1.3", versionLabel);
// check version history count
HttpResponse response = getAll(getNodeVersionsUrl(docId), null, null, 200);
List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(cnt, nodes.size());
// -ve tests:
{
setRequestContext(null);
// -ve test - unauthenticated - belts-and-braces ;-)
delete(getNodeVersionsUrl(docId), "1.0", null, 401);
setRequestContext(user1);
// -ve test - unknown nodeId
delete(getNodeVersionsUrl("dummy"), "1.0", null, 404);
// -ve test - unknown versionId
delete(getNodeVersionsUrl(docId), "15.0", null, 404);
setRequestContext(user2);
// -ve test - permission denied (on version other than most recent)
delete(getNodeVersionsUrl(docId), "1.0", null, 403);
// -ve test - permission denied (on most recent version)
delete(getNodeVersionsUrl(docId), "1.3", null, 403);
}
setRequestContext(user1);
delete(getNodeVersionsUrl(docId), "1.0", null, 204);
response = getAll(getNodeVersionsUrl(docId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(cnt - 1, nodes.size());
// check live node (does not change)
response = getSingle(URL_NODES, docId, 200);
Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertEquals("1.3", nodeResp.getProperties().get("cm:versionLabel"));
response = getSingle(getNodeContentUrl(docId), null, 200);
assertEquals(textContentSuffix + "4", response.getResponse());
// delete current (most recent) version
delete(getNodeVersionsUrl(docId), "1.3", null, 204);
response = getAll(getNodeVersionsUrl(docId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(cnt - 2, nodes.size());
// check live node (changes)
response = getSingle(URL_NODES, docId, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertEquals("1.2", nodeResp.getProperties().get("cm:versionLabel"));
response = getSingle(getNodeContentUrl(docId), null, 200);
assertEquals(textContentSuffix + "3", response.getResponse());
// delete another version
delete(getNodeVersionsUrl(docId), "1.1", null, 204);
response = getAll(getNodeVersionsUrl(docId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(cnt - 3, nodes.size());
// check live node (does not change)
response = getSingle(URL_NODES, docId, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertEquals("1.2", nodeResp.getProperties().get("cm:versionLabel"));
response = getSingle(getNodeContentUrl(docId), null, 200);
assertEquals(textContentSuffix + "3", response.getResponse());
// -ve test - cannot delete last version (via delete version api call) (see REPO-835 & REPO-834)
delete(getNodeVersionsUrl(docId), "1.2", null, 422);
/* note: currently we cannot delete last version so this is not applicable
// check live node - removing last version does not (currently) remove versionable aspect
response = getSingle(URL_NODES, docId, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertTrue(nodeResp.getAspectNames().contains("cm:versionable"));
Map<String, Object> props = nodeResp.getProperties();
if (props != null)
{
assertNull(props.get("cm:versionLabel"));
assertNull(props.get("cm:versionType")); // note: see special fix in delete version API (at least for now)
}
*/
response = getAll(getNodeVersionsUrl(docId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals("1.2", nodes.get(0).getId());
// Update again ..
String textContent = "more changes 0";
ByteArrayInputStream inputStream = new ByteArrayInputStream(textContent.getBytes());
File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
PublicApiHttpClient.BinaryPayload payload = new PublicApiHttpClient.BinaryPayload(txtFile);
response = putBinary(getNodeContentUrl(docId), payload, null, null, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertTrue(nodeResp.getAspectNames().contains("cm:versionable"));
assertEquals("1.3", nodeResp.getProperties().get("cm:versionLabel"));
assertEquals("MINOR", nodeResp.getProperties().get("cm:versionType"));
// remove versionable aspect (this will clear the history and disable versioning)
response = getSingle(URL_NODES, docId, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
List<String> aspectNamesToKeep = new ArrayList<>();
aspectNamesToKeep.addAll(nodeResp.getAspectNames());
aspectNamesToKeep.remove("cm:versionable");
Node nUpdate = new Node();
nUpdate.setAspectNames(aspectNamesToKeep);
response = put(URL_NODES, docId, toJsonAsStringNonNull(nUpdate), null, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertFalse(nodeResp.getAspectNames().contains("cm:versionable"));
Map<String, Object> props = nodeResp.getProperties();
if (props != null) {
assertNull(props.get("cm:versionLabel"));
assertNull(props.get("cm:versionType"));
}
response = getAll(getNodeVersionsUrl(docId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
// Update again ..
textContent = "more changes 1";
inputStream = new ByteArrayInputStream(textContent.getBytes());
txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
payload = new PublicApiHttpClient.BinaryPayload(txtFile);
response = putBinary(getNodeContentUrl(docId), payload, null, null, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertFalse(nodeResp.getAspectNames().contains("cm:versionable"));
// re-enable versioning (default model properties should cause initial version to be created as 1.0)
response = getSingle(URL_NODES, docId, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
aspectNamesToKeep = new ArrayList<>();
aspectNamesToKeep.addAll(nodeResp.getAspectNames());
aspectNamesToKeep.add("cm:versionable");
nUpdate = new Node();
nUpdate.setAspectNames(aspectNamesToKeep);
response = put(URL_NODES, docId, toJsonAsStringNonNull(nUpdate), null, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertTrue(nodeResp.getAspectNames().contains("cm:versionable"));
props = nodeResp.getProperties();
assertEquals("1.0", props.get("cm:versionLabel"));
assertEquals("MAJOR", props.get("cm:versionType"));
// check content
response = getSingle(getNodeVersionsUrl(docId), "1.0/content", null, 200);
assertEquals(textContent, response.getResponse());
// Update again ..
textContent = "more changes 2";
inputStream = new ByteArrayInputStream(textContent.getBytes());
txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
payload = new PublicApiHttpClient.BinaryPayload(txtFile);
response = putBinary(getNodeContentUrl(docId), payload, null, null, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertTrue(nodeResp.getAspectNames().contains("cm:versionable"));
props = nodeResp.getProperties();
assertEquals("1.1", props.get("cm:versionLabel"));
assertEquals("MINOR", props.get("cm:versionType"));
// check content
response = getSingle(getNodeVersionsUrl(docId), "1.1/content", null, 200);
assertEquals(textContent, response.getResponse());
} finally {
if (f1Id != null) {
// some cleanup
setRequestContext(user1);
deleteNode(f1Id, true, 204);
}
}
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class NodeVersionsApiTest method uploadTextFileVersions.
/**
* This test helper method uses "overwrite=true" to create one or more new versions, including the initial create if needed.
*
* If the file does not already exist (currentVersionLabel should be null) and majorVersionIn is also null
* then the first version is created as MAJOR (1.0) and subsequent versions are created as MINOR.
*
* @param userId
* @param parentFolderNodeId - parent folder
* @param fileName - file name
* @param cnt - number of new versions (>= 1)
* @param textContentPrefix - prefix for text content
* @param currentVersionCounter - overall version counter, used as a suffix in text content and version comment
* @param majorVersionIn - if null then false, if true then create MAJOR versions else if false create MINOR versions
* @param currentVersionLabel - the current version label (if file already exists)
* @return
* @throws Exception
*/
private Pair<String, String> uploadTextFileVersions(String userId, String parentFolderNodeId, String fileName, int cnt, String textContentPrefix, int currentVersionCounter, final Boolean majorVersionIn, String currentVersionLabel) throws Exception {
Map<String, String> params = new HashMap<>();
params.put(Nodes.PARAM_OVERWRITE, "true");
if (majorVersionIn != null) {
params.put(Nodes.PARAM_VERSION_MAJOR, majorVersionIn.toString());
}
String docId = null;
for (int i = 1; i <= cnt; i++) {
boolean expectedMajorVersion = (majorVersionIn != null ? majorVersionIn : false);
if (currentVersionLabel == null) {
currentVersionLabel = "0.0";
// special case - 1st version is major (if not specified otherwise)
if (majorVersionIn == null) {
expectedMajorVersion = true;
}
}
String[] parts = currentVersionLabel.split("\\.");
int majorVer = new Integer(parts[0]).intValue();
int minorVer = new Integer(parts[1]).intValue();
if (expectedMajorVersion) {
majorVer++;
minorVer = 0;
} else {
minorVer++;
}
currentVersionLabel = majorVer + "." + minorVer;
currentVersionCounter++;
params.put("comment", "my version " + currentVersionCounter);
String textContent = textContentPrefix + currentVersionCounter;
// uses upload with overwrite here ...
Document documentResp = createTextFile(parentFolderNodeId, fileName, textContent, "UTF-8", params);
docId = documentResp.getId();
assertTrue(documentResp.getAspectNames().contains("cm:versionable"));
assertNotNull(documentResp.getProperties());
assertEquals(currentVersionLabel, documentResp.getProperties().get("cm:versionLabel"));
// double-check - get version node info
HttpResponse response = getSingle(getNodeVersionsUrl(docId), currentVersionLabel, null, 200);
Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertEquals(currentVersionLabel, nodeResp.getProperties().get("cm:versionLabel"));
assertEquals((expectedMajorVersion ? "MAJOR" : "MINOR"), nodeResp.getProperties().get("cm:versionType"));
}
return new Pair<String, String>(currentVersionLabel, docId);
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class QueriesNodesApiTest method testLiveSearchNodes_Tags.
@Test
@Category(RedundantTests.class)
public void testLiveSearchNodes_Tags() throws Exception {
setRequestContext(user1);
PublicApiClient.Nodes nodesProxy = publicApiClient.nodes();
int f1Count = 5;
List<String> f1NodeIds = new ArrayList<>(f1Count);
int f2Count = 3;
List<String> f2NodeIds = new ArrayList<>(f2Count);
int totalCount = f1Count + f2Count;
List<String> allIds = new ArrayList<>(totalCount);
String testTag = "ghi789tag";
String testFileTag = "ghi789file";
String testFolderTag = "ghi789folder";
try {
// As user 1 ...
Paging paging = getPaging(0, 100);
String f1Id = createFolder(Nodes.PATH_MY, "folder tag 1").getId();
String f2Id = createFolder(Nodes.PATH_MY, "folder tag 2").getId();
String name = "name";
for (int i = 1; i <= f1Count; i++) {
// create doc - in folder 1
String contentText = "f1 test document " + user1 + " document " + i;
String docName = name + i;
Document doc = createTextFile(f1Id, docName, contentText, "UTF-8", null);
publicApiClient.setRequestContext(new RequestContext("", user1));
// ignore result
nodesProxy.createNodeTag(doc.getId(), new Tag(testTag));
// ignore result
nodesProxy.createNodeTag(doc.getId(), new Tag(testFileTag));
f1NodeIds.add(doc.getId());
}
for (int i = 1; i <= f2Count; i++) {
// create folder - in folder 2
String folderName = name + i;
Folder folder = createFolder(f2Id, folderName, null);
publicApiClient.setRequestContext(new RequestContext("", user1));
// ignore result
nodesProxy.createNodeTag(folder.getId(), new Tag(testTag));
// ignore result
nodesProxy.createNodeTag(folder.getId(), new Tag(testFolderTag));
f2NodeIds.add(folder.getId());
}
allIds.addAll(f1NodeIds);
allIds.addAll(f2NodeIds);
// Search hits based on tag
Map<String, String> params = new HashMap<>(1);
params.put(Queries.PARAM_TERM, testTag);
HttpResponse response = getAll(URL_QUERIES_LSN, paging, params, 200);
List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
checkNodeIds(nodes, allIds, null);
params = new HashMap<>(1);
params.put(Queries.PARAM_TERM, testFileTag);
response = getAll(URL_QUERIES_LSN, paging, params, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
checkNodeIds(nodes, f1NodeIds, null);
params = new HashMap<>(1);
params.put(Queries.PARAM_TERM, testFolderTag);
response = getAll(URL_QUERIES_LSN, paging, params, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
checkNodeIds(nodes, f2NodeIds, null);
} finally {
// some cleanup
setRequestContext(user1);
for (String nodeId : allIds) {
deleteNode(nodeId, true, 204);
}
}
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class QueriesNodesApiTest method checkApiCall.
private List<Node> checkApiCall(String pathRegex, String queryForm, String term, String nodeType, String rootNodeId, String include, String orderBy, Paging paging, int expectedStatus, Boolean checkNodeOrderAsc, Boolean propertyNullCheck, List<String> ids) throws Exception {
Map<String, String> params = new HashMap<>(1);
params.put(Queries.PARAM_TERM, term);
if (include != null) {
params.put(Queries.PARAM_INCLUDE, include);
}
if (nodeType != null) {
params.put(Queries.PARAM_NODE_TYPE, nodeType);
}
if (rootNodeId != null) {
params.put(Queries.PARAM_ROOT_NODE_ID, rootNodeId);
}
if (orderBy != null) {
params.put(Queries.PARAM_ORDERBY, orderBy);
}
// Create the list of NodeRefs returned from the dummy search
dummySearchServiceQueryNodeRefs.clear();
for (String id : ids) {
NodeRef nodeRef = getNodeRef(id);
dummySearchServiceQueryNodeRefs.add(nodeRef);
}
// Mix up the NodeRefs returned from the dummy search as the client side code is going to be doing the sorting.
if (orderBy != null) {
Collections.shuffle(dummySearchServiceQueryNodeRefs);
}
HttpResponse response = getAll(URL_QUERIES_LSN, paging, params, 200);
List<Node> nodes = null;
if (expectedStatus == 200) {
String termWithEscapedAsterisks = term.replaceAll("\\*", "\\\\*").replaceAll("\"", "\\\\\"");
String expectedQuery = DEAFULT_QUERY.equals(queryForm) ? String.format(DEAFULT_QUERY, termWithEscapedAsterisks) : NODE_TYPE_QUERY.equals(queryForm) ? String.format(NODE_TYPE_QUERY, termWithEscapedAsterisks, nodeType) : ROOT_NODE_QUERY_SUFFIX.equals(queryForm) ? String.format(ROOT_NODE_QUERY_SUFFIX, termWithEscapedAsterisks) : "TODO";
ArgumentCaptor<SearchParameters> searchParametersCaptor = ArgumentCaptor.forClass(SearchParameters.class);
verify(mockSearchService, times(++callCountToMockSearchService)).query(searchParametersCaptor.capture());
SearchParameters parameters = searchParametersCaptor.getValue();
String query = parameters.getQuery();
if (ROOT_NODE_QUERY_SUFFIX.equals(queryForm)) {
assertNotNull(query);
assertTrue("Query should have started with " + ROOT_NODE_QUERY_PREFIX + " but was " + query, query.startsWith(ROOT_NODE_QUERY_PREFIX));
assertTrue("Query should have ended with " + expectedQuery + " but was " + query, query.endsWith(expectedQuery));
String path = query.substring(ROOT_NODE_QUERY_PREFIX.length(), query.length() - expectedQuery.length());
assertTrue("Query path should match " + pathRegex + " but was " + path, Pattern.matches(pathRegex, path));
} else {
assertEquals("Query", expectedQuery, query);
}
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
checkNodeIds(nodes, ids, checkNodeOrderAsc);
if (propertyNullCheck != null) {
for (Node node : nodes) {
if (propertyNullCheck) {
assertNull(node.getAspectNames());
assertNull(node.getProperties());
assertNull(node.getPath());
assertNull(node.getIsLink());
} else {
assertNotNull(node.getAspectNames());
assertNotNull(node.getProperties());
assertNotNull(node.getPath());
assertNotNull(node.getIsLink());
}
}
}
}
return nodes;
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class QueriesSitesApiTest method checkApiCall.
// Note expectedIds defaults to ids
private void checkApiCall(String term, String orderBy, Paging paging, int expectedStatus, String[] expectedIds, String... ids) throws Exception {
Map<String, String> params = new HashMap<>(1);
params.put(Queries.PARAM_TERM, "\"" + term + "\"");
if (orderBy != null) {
params.put(Queries.PARAM_ORDERBY, orderBy);
}
dummySearchServiceQueryNodeRefs.clear();
for (String id : ids) {
NodeRef nodeRef = getNodeRef(id);
dummySearchServiceQueryNodeRefs.add(nodeRef);
}
expectedIds = expectedIds != null ? expectedIds : ids;
HttpResponse response = getAll(URL_QUERIES_LSS, paging, params, 200);
if (expectedStatus == 200) {
String termWithEscapedAsterisks = term.replaceAll("\\*", "\\\\*");
String expectedQuery = "TYPE:\"{http://www.alfresco.org/model/site/1.0}site\" AND (\"*" + termWithEscapedAsterisks + "*\")";
ArgumentCaptor<SearchParameters> searchParametersCaptor = ArgumentCaptor.forClass(SearchParameters.class);
verify(mockSearchService, times(++callCountToMockSearchService)).query(searchParametersCaptor.capture());
SearchParameters parameters = searchParametersCaptor.getValue();
assertEquals("Query", expectedQuery, parameters.getQuery());
List<Site> sites = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Site.class);
assertEquals(expectedIds.length, sites.size());
if (orderBy != null) {
for (int i = 0; i < expectedIds.length; i++) {
String id = expectedIds[i];
String actualId = sites.get(i).getId();
assertEquals("Order " + i + ":", id, actualId);
}
}
}
}
Aggregations