use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class TestCMIS method testContentDisposition_MNT_17477.
@Test
public void testContentDisposition_MNT_17477() throws Exception {
final TestNetwork network1 = getTestFixture().getRandomNetwork();
String username = "user" + System.currentTimeMillis();
PersonInfo personInfo = new PersonInfo(username, username, username, TEST_PASSWORD, null, null, null, null, null, null, null);
TestPerson person1 = network1.createUser(personInfo);
String person1Id = person1.getId();
publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
CmisSession cmisSession = publicApiClient.createPublicApiCMISSession(Binding.browser, CMIS_VERSION_11, AlfrescoObjectFactoryImpl.class.getName());
Folder folder = (Folder) cmisSession.getObjectByPath("/Shared");
//
// Upload test JPG document
//
String name = GUID.generate() + ".jpg";
Map<String, Object> properties = new HashMap<>();
{
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.put(PropertyIds.NAME, name);
}
ContentStreamImpl fileContent = new ContentStreamImpl();
{
fileContent.setMimeType(MimetypeMap.MIMETYPE_IMAGE_JPEG);
fileContent.setStream(this.getClass().getResourceAsStream("/test.jpg"));
}
Document doc = folder.createDocument(properties, fileContent, VersioningState.MAJOR);
String docId = doc.getId();
// note: Content-Disposition can be "inline or "attachment" for content types that are white-listed (eg. specific image types & pdf)
HttpResponse response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root/Shared/" + name, null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("inline"));
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root/Shared/" + name + "?download=inline", null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("inline"));
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root/Shared/" + name + "?download=attachment", null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("attachment"));
// note: AtomPub binding (via OpenCMIS) does not support "download" query parameter
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/atom/content?id=" + docId, null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("attachment"));
//
// Create test HTML document
//
name = GUID.generate() + ".html";
properties = new HashMap<>();
{
properties.put(PropertyIds.OBJECT_TYPE_ID, TYPE_CMIS_DOCUMENT);
properties.put(PropertyIds.NAME, name);
}
fileContent = new ContentStreamImpl();
{
ContentWriter writer = new FileContentWriter(TempFileProvider.createTempFile(GUID.generate(), ".html"));
writer.putContent("<html><script>alert(123);</script><body>Hello <b>world</b></body</html>");
ContentReader reader = writer.getReader();
fileContent.setMimeType(MimetypeMap.MIMETYPE_HTML);
fileContent.setStream(reader.getContentInputStream());
}
doc = folder.createDocument(properties, fileContent, VersioningState.MAJOR);
docId = doc.getId();
// note: Content-Disposition will always be "attachment" for content types that are not white-listed
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root/Shared/" + name, null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("attachment;"));
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root/Shared/" + name + "?download=inline", null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("attachment;"));
// note: AtomPub binding (via OpenCMIS) does not support "download" query parameter
response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/atom/content?id=" + docId, null);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().get("Content-Disposition").startsWith("attachment;"));
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class TestCMIS method testBrowserBindingRoot.
/**
* ACE-5753 / REPO-1815 Check the return from http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/browser/root
* @throws Exception
*/
@Test
public void testBrowserBindingRoot() throws Exception {
final TestNetwork network1 = getTestFixture().getRandomNetwork();
Iterator<String> personIt = network1.getPersonIds().iterator();
final String personId = personIt.next();
assertNotNull(personId);
Person person = repoService.getPerson(personId);
assertNotNull(person);
publicApiClient.setRequestContext(new RequestContext(network1.getId(), personId));
HttpResponse response = publicApiClient.get(network1.getId() + "/public/cmis/versions/1.1/browser/root", null);
assertEquals(200, response.getStatusCode());
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class TestCMIS method testGetIdParamsPropertiesAreUnique.
// The following property names were repeated: alfcmis:nodeRef, cmis:description, cmis:changeToken
// Not planing on fixing this at the moment as it will be having little effect.
@Ignore
@Test()
public void testGetIdParamsPropertiesAreUnique() throws Exception {
String cmisId = getTestFileIdWithTwoRenditionsOneSourceAndTwoTargetAssociations();
HashMap<String, String> reqParams = new HashMap<>();
reqParams.put("id", cmisId);
publicApiClient.setRequestContext(new RequestContext(testNetwork.getId(), testPersonId));
HttpResponse resp = publicApiClient.get(Binding.atom, CMIS_VERSION_11, "id", reqParams);
String xml = resp.getResponse();
System.out.println(xml);
String tmp1 = xml.split("cmis:properties")[1];
String tmp2 = tmp1.substring(0, tmp1.indexOf("<e1:aspects"));
String[] tmp3 = tmp2.split("queryName=\"");
List<String> names = new ArrayList<>();
for (int j = 1; j < tmp3.length; j += 2) {
String tmp4 = tmp3[j];
String name = tmp4.substring(0, tmp4.indexOf('"'));
names.add(name);
}
final Set<String> duplicates = new HashSet();
final Set<String> uniqueNames = new HashSet();
for (String name : names) {
if (!uniqueNames.add(name)) {
duplicates.add(name);
}
}
if (!duplicates.isEmpty()) {
StringJoiner sj = new StringJoiner((", "));
for (String duplicate : duplicates) {
sj.add(duplicate);
}
fail("The following property names were repeated: " + sj);
}
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class TestCustomConstraint method testCreateInlineConstraint.
@Test
public void testCreateInlineConstraint() throws Exception {
setRequestContext(customModelAdmin);
String modelName = "testModelInlineConstraint" + System.currentTimeMillis();
final Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
// Create the model as a Model Administrator
createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);
String regExConstraintName = "testInlineFileNameRegEx" + System.currentTimeMillis();
{
// Create RegEx constraint
CustomModelConstraint inlineRegExConstraint = new CustomModelConstraint();
inlineRegExConstraint.setName(regExConstraintName);
inlineRegExConstraint.setType("REGEX");
// Create the inline RegEx constraint's parameters
List<CustomModelNamedValue> parameters = new ArrayList<>(2);
parameters.add(buildNamedValue("expression", "(.*[\\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)"));
parameters.add(buildNamedValue("requiresMatch", "false"));
// Add the parameters into the constraint
inlineRegExConstraint.setParameters(parameters);
// Create aspect
String aspectName = "testAspect1" + System.currentTimeMillis();
createTypeAspect(CustomAspect.class, modelName, aspectName, "title", "desc", null);
// Update the Aspect by adding property
CustomAspect aspectPayload = new CustomAspect();
aspectPayload.setName(aspectName);
final String aspectPropName = "testAspect1Prop1" + System.currentTimeMillis();
CustomModelProperty aspectProp = new CustomModelProperty();
aspectProp.setName(aspectPropName);
aspectProp.setTitle("property title");
aspectProp.setDataType("d:text");
// Add inline constraint
aspectProp.setConstraints(Arrays.asList(inlineRegExConstraint));
List<CustomModelProperty> props = new ArrayList<>(1);
props.add(aspectProp);
aspectPayload.setProperties(props);
// Create the property
put("cmm/" + modelName + "/aspects", aspectName, RestApiUtil.toJsonAsString(aspectPayload), SELECT_PROPS_QS, 200);
// Retrieve all the model's constraints
Paging paging = getPaging(0, Integer.MAX_VALUE);
HttpResponse response = getAll("cmm/" + modelName + "/constraints", paging, 200);
List<CustomModelConstraint> constraints = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomModelConstraint.class);
assertEquals("Inline constraints should not be included with the model defined constraints.", 0, constraints.size());
// Retrieve the updated aspect
response = getSingle("cmm/" + modelName + "/aspects", aspectName, 200);
CustomAspect returnedAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
// Check the aspect's added property
assertEquals(1, returnedAspect.getProperties().size());
CustomModelProperty customModelProperty = returnedAspect.getProperties().get(0);
assertEquals(aspectPropName, customModelProperty.getName());
assertEquals(0, customModelProperty.getConstraintRefs().size());
List<CustomModelConstraint> inlineConstraints = customModelProperty.getConstraints();
assertEquals(1, inlineConstraints.size());
compareCustomModelConstraints(inlineRegExConstraint, inlineConstraints.get(0), "prefixedName");
}
// Create inline and referenced constraint
{
// Create RegEx constraint
CustomModelConstraint regExConstraint = new CustomModelConstraint();
// duplicate name
regExConstraint.setName(regExConstraintName);
regExConstraint.setType("REGEX");
regExConstraint.setTitle("test RegEx title");
// Create the RegEx constraint's parameters
List<CustomModelNamedValue> parameters = new ArrayList<>(2);
parameters.add(buildNamedValue("expression", "(.*[\\\"\\*\\\\\\>\\<\\?\\/\\:\\|]+.*)|(.*[\\.]?.*[\\.]+$)|(.*[ ]+$)"));
parameters.add(buildNamedValue("requiresMatch", "false"));
// Add the parameters into the constraint
regExConstraint.setParameters(parameters);
// Try to create constraint as a Model Administrator
// duplicate name
post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 409);
String newRegExConstraintName = "testFileNameRegEx" + System.currentTimeMillis();
regExConstraint.setName(newRegExConstraintName);
// Create constraint as a Model Administrator
post("cmm/" + modelName + "/constraints", RestApiUtil.toJsonAsString(regExConstraint), 201);
// Retrieve the created RegEx constraint
HttpResponse response = getSingle("cmm/" + modelName + "/constraints", newRegExConstraintName, 200);
CustomModelConstraint returnedRegExConstraint = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModelConstraint.class);
// Create inline anonymous LENGTH constraint
CustomModelConstraint inlineAnonymousLengthConstraint = new CustomModelConstraint();
inlineAnonymousLengthConstraint.setType("LENGTH");
inlineAnonymousLengthConstraint.setTitle("test Length title");
// Create the Length constraint's parameters
parameters = new ArrayList<>(2);
parameters.add(buildNamedValue("maxLength", "256"));
parameters.add(buildNamedValue("minLength", "0"));
// Add the parameters into the constraint
inlineAnonymousLengthConstraint.setParameters(parameters);
// Create type
String typeName = "testType1" + System.currentTimeMillis();
CustomType type = createTypeAspect(CustomType.class, modelName, typeName, "test type1 title", "test type1 Desc", "cm:content");
// Update the Type by adding property
CustomType typePayload = new CustomType();
typePayload.setName(typeName);
String typePropName = "testType1Prop1" + System.currentTimeMillis();
CustomModelProperty typeProp = new CustomModelProperty();
typeProp.setName(typePropName);
typeProp.setTitle("property title");
typeProp.setDataType("d:int");
// Constraint Ref
typeProp.setConstraintRefs(Arrays.asList(returnedRegExConstraint.getPrefixedName()));
// inline constraint
typeProp.setConstraints(Arrays.asList(inlineAnonymousLengthConstraint));
List<CustomModelProperty> props = new ArrayList<>(1);
props.add(typeProp);
typePayload.setProperties(props);
// Try to create the property - LENGTH constraint can only be used with textual data type
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 400);
typeProp.setDataType("d:double");
// CTry to create the property - LENGTH constraint can only be used with textual data type
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 400);
typeProp.setDataType("d:text");
put("cmm/" + modelName + "/types", typeName, RestApiUtil.toJsonAsString(typePayload), SELECT_PROPS_QS, 200);
// Retrieve the updated type
response = getSingle("cmm/" + modelName + "/types", type.getName(), 200);
CustomType returnedType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
// Check the type's added property
assertEquals(1, returnedType.getProperties().size());
CustomModelProperty customModelProperty = returnedType.getProperties().get(0);
assertEquals(typePropName, customModelProperty.getName());
assertEquals(1, customModelProperty.getConstraintRefs().size());
assertEquals(returnedRegExConstraint.getPrefixedName(), customModelProperty.getConstraintRefs().get(0));
assertEquals(1, customModelProperty.getConstraints().size());
// M2PropertyDefinition will add a name
assertNotNull(customModelProperty.getConstraints().get(0).getName());
compareCustomModelConstraints(inlineAnonymousLengthConstraint, customModelProperty.getConstraints().get(0), "prefixedName", "name");
}
}
use of org.alfresco.rest.api.tests.client.HttpResponse in project alfresco-remote-api by Alfresco.
the class TestCustomModel method testDeleteCustomModel.
@Test
public void testDeleteCustomModel() throws Exception {
setRequestContext(customModelAdmin);
String modelName = "testDeleteModel" + System.currentTimeMillis();
Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
// Create the model as a Model Administrator
CustomModel customModel = createCustomModel(modelName, namespacePair, ModelStatus.DRAFT, null, "Joe Bloggs");
// Retrieve the created model
HttpResponse response = getSingle("cmm", modelName, 200);
CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
compareCustomModels(customModel, returnedModel);
setRequestContext(nonAdminUserName);
// Try to delete the model as a non Admin user
delete("cmm", modelName, 403);
setRequestContext(customModelAdmin);
// Delete the model as a Model Administrator
delete("cmm", modelName, 204);
// Create the model again
post("cmm", RestApiUtil.toJsonAsString(customModel), 201);
// Activated the model
CustomModel updatePayload = new CustomModel();
updatePayload.setStatus(ModelStatus.ACTIVE);
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
// Try to delete the active model
delete("cmm", modelName, 409);
// Deactivate and then delete the model
updatePayload = new CustomModel();
updatePayload.setStatus(ModelStatus.DRAFT);
put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), SELECT_STATUS_QS, 200);
delete("cmm", modelName, 204);
}
Aggregations