use of org.alfresco.rest.framework.core.ResourceMetadata in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testPostExtractor.
@SuppressWarnings("unchecked")
@Test
public void testPostExtractor() throws IOException {
// Put together the stubs
ResourceWebScriptPost extractor = new ResourceWebScriptPost();
extractor.setAssistant(assistant);
extractor.setLocator(locator);
Map<String, String> templateVars = new HashMap<String, String>();
Content content = mock(Content.class);
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
when(request.getContent()).thenReturn(content);
Params params = extractor.extractParams(mockEntity(), request);
assertNotNull(params);
assertNotNull(params.getFilter());
assertTrue("Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass()));
Object passed = params.getPassedIn();
assertNotNull(passed);
assertTrue(List.class.isAssignableFrom(passed.getClass()));
List<Object> passedObjs = (List<Object>) passed;
assertTrue(passedObjs.size() == 1);
assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
// No entity id for POST
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
try {
params = extractor.extractParams(mockEntity(), request);
fail("Should not get here. No entity id for POST");
} catch (UnsupportedResourceOperationException uoe) {
// Must throw this exception
assertNotNull(uoe);
}
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
params = extractor.extractParams(mockRelationship(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
passed = params.getPassedIn();
assertNotNull(passed);
passedObjs = (List<Object>) passed;
assertTrue(passedObjs.size() == 1);
assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
try {
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "45678");
params = extractor.extractParams(mockRelationship(), request);
fail("Should not get here.");
} catch (UnsupportedResourceOperationException iae) {
// Must throw this exception
assertNotNull("POSTING to a relationship collection by id is not correct.", iae);
}
templateVars.clear();
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "codfish");
try {
// POST does not support addressed parameters.
params = extractor.extractParams(mockEntity(), request);
fail("Should not get here.");
} catch (UnsupportedResourceOperationException uoe) {
// Must throw this exception
assertNotNull(uoe);
}
testExtractOperationParams(templateVars, request, extractor);
templateVars.clear();
Method aMethod = ResourceInspector.findMethod(EntityResourceAction.Create.class, GrassEntityResource.class);
ResourceOperation op = ResourceInspector.inspectOperation(GrassEntityResource.class, aMethod, HttpMethod.POST);
List<ResourceMetadata> metainfo = ResourceInspector.inspect(GrassEntityResource.class);
assertNotNull(op);
assertTrue("Create method should have two params", op.getParameters().size() == 2);
ResourceParameter singleParam = op.getParameters().get(0);
assertTrue(ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(singleParam.getParamType()));
assertFalse("Create grass does not support multiple grass creations", singleParam.isAllowMultiple());
assertFalse(singleParam.isRequired());
// Test context when the request body is null and 'required' webApiParam is false
when(request.getHeader("content-length")).thenReturn("0");
params = extractor.extractParams(metainfo.get(0), request);
assertNotNull(params);
// Test context when the request body is provided and 'required' property is false
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.GRASS_JSON));
params = extractor.extractParams(metainfo.get(0), request);
assertNotNull(params);
}
use of org.alfresco.rest.framework.core.ResourceMetadata in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method mockOperation.
/**
* Mocks an operation
* @return ResourceMetadata a Entity
*/
private static ResourceMetadata mockOperation() {
ResourceMetadata resourceMock = mock(ResourceMetadata.class);
when(resourceMock.getType()).thenReturn(ResourceMetadata.RESOURCE_TYPE.OPERATION);
return resourceMock;
}
use of org.alfresco.rest.framework.core.ResourceMetadata in project alfresco-remote-api by Alfresco.
the class InspectorTests method testWebDeletedAnnotationOnClass.
@Test
public void testWebDeletedAnnotationOnClass() {
List<ResourceMetadata> metainfo = ResourceInspector.inspect(GrassEntityResourceNowDeleted.class);
assertTrue("Must be at least one ResourceMetadata", metainfo.size() > 0);
ResourceMetadata metaData = metainfo.get(0);
assertNotNull(metaData);
assertNull("GrassEntityResourceNowDeleted all methods deleted", metaData.getOperation(HttpMethod.GET));
assertNull("GrassEntityResourceNowDeleted all methods deleted", metaData.getOperation(HttpMethod.PUT));
assertNull("GrassEntityResourceNowDeleted all methods deleted", metaData.getOperation(HttpMethod.DELETE));
assertNull("GrassEntityResourceNowDeleted all methods deleted", metaData.getOperation(HttpMethod.POST));
metainfo = ResourceInspector.inspect(SheepBlackSheepResourceIsNoMore.class);
assertTrue("Must be at least one ResourceMetadata", metainfo.size() > 0);
metaData = metainfo.get(0);
assertNotNull(metaData);
assertNull("SheepBlackSheepResourceIsNoMore all methods deleted", metaData.getOperation(HttpMethod.GET));
assertNull("SheepBlackSheepResourceIsNoMore all methods deleted", metaData.getOperation(HttpMethod.PUT));
assertNull("SheepBlackSheepResourceIsNoMore all methods deleted", metaData.getOperation(HttpMethod.DELETE));
assertNull("SheepBlackSheepResourceIsNoMore all methods deleted", metaData.getOperation(HttpMethod.POST));
}
use of org.alfresco.rest.framework.core.ResourceMetadata in project alfresco-remote-api by Alfresco.
the class InspectorTests method testInspectAddressedProperties.
@Test
public void testInspectAddressedProperties() {
Api api = Api.valueOf("alfrescomock", "private", "1");
List<ResourceMetadata> metainfo = new ArrayList<ResourceMetadata>();
ResourceInspector.inspectAddressedProperties(api, FlockEntityResource.class, "myroot", metainfo);
assertTrue(metainfo.size() == 1);
ResourceMetadata metaData = metainfo.get(0);
assertEquals("/myroot/{id}/photo", metaData.getUniqueId());
assertTrue(metaData.getOperations().size() == 3);
assertNotNull("FlockEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
assertNotNull("FlockEntityResource supports PUT", metaData.getOperation(HttpMethod.PUT));
assertNotNull("FlockEntityResource supports DELETE", metaData.getOperation(HttpMethod.DELETE));
metainfo.clear();
ResourceInspector.inspectAddressedProperties(api, FlocketEntityResource.class, "myroot", metainfo);
assertTrue(metainfo.size() == 3);
for (ResourceMetadata resourceMetadata : metainfo) {
if ("/myroot/{id}/photo".equals(resourceMetadata.getUniqueId())) {
assertNotNull("FlocketEntityResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
assertNotNull("FlocketEntityResource supports PUT", resourceMetadata.getOperation(HttpMethod.PUT));
assertNotNull("FlocketEntityResource supports DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
} else {
if ("/myroot/{id}/album".equals(resourceMetadata.getUniqueId())) {
assertNotNull("FlocketEntityResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
assertNotNull("FlocketEntityResource supports PUT", resourceMetadata.getOperation(HttpMethod.PUT));
assertNull("FlocketEntityResource does not support DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
} else {
if ("/myroot/{id}/madeUpProp".equals(resourceMetadata.getUniqueId())) {
assertNotNull("FlocketEntityResource supports GET", resourceMetadata.getOperation(HttpMethod.GET));
assertNull("FlocketEntityResource does not supports PUT", resourceMetadata.getOperation(HttpMethod.PUT));
assertNull("FlocketEntityResource does not support DELETE", resourceMetadata.getOperation(HttpMethod.DELETE));
} else {
fail("Invalid address property information.");
}
}
}
}
}
use of org.alfresco.rest.framework.core.ResourceMetadata in project alfresco-remote-api by Alfresco.
the class InspectorTests method testInspectEntity.
@Test
public void testInspectEntity() {
List<ResourceMetadata> metainfo = ResourceInspector.inspect(SheepEntityResource.class);
assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
ResourceMetadata metaData = metainfo.get(0);
assertNotNull(metaData);
assertNotNull("SheepEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
assertNotNull("SheepEntityResource supports PUT", metaData.getOperation(HttpMethod.PUT));
assertNotNull("SheepEntityResource supports DELETE", metaData.getOperation(HttpMethod.DELETE));
assertNull("SheepEntityResource does not support POST", metaData.getOperation(HttpMethod.POST));
ResourceOperation op = metaData.getOperation(HttpMethod.GET);
assertEquals("Sheep ReadALL should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
op = metaData.getOperation(HttpMethod.PUT);
assertTrue("SheepEntityResource must support Sheep", Sheep.class.equals(metaData.getObjectType(op)));
metainfo = ResourceInspector.inspect(SheepNoActionEntityResource.class);
assertTrue("SheepNoActionEntityResource has no actions.", metainfo.isEmpty());
metainfo = ResourceInspector.inspect(GoatEntityResource.class);
assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
metaData = metainfo.get(0);
assertNotNull(metaData);
assertNotNull("GoatEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
op = metaData.getOperation(HttpMethod.GET);
List<ResourceParameter> params = op.getParameters();
assertTrue("readById method should have 1 url param", params.size() == 1);
metainfo = ResourceInspector.inspect(FlockEntityResource.class);
assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
metaData = metainfo.get(0);
assertNotNull(metaData);
assertNotNull("FlockEntityResource supports GET", metaData.getOperation(HttpMethod.GET));
assertNotNull("FlockEntityResource supports PUT", metaData.getOperation(HttpMethod.PUT));
assertNotNull("FlockEntityResource supports DELETE", metaData.getOperation(HttpMethod.DELETE));
assertNull("FlockEntityResource does not support POST", metaData.getOperation(HttpMethod.POST));
metainfo = ResourceInspector.inspect(MultiPartTestEntityResource.class);
assertTrue("Must be one ResourceMetadata", metainfo.size() == 1);
metaData = metainfo.get(0);
assertNotNull(metaData);
assertNotNull("MultiPartTestEntityResource support POST", metaData.getOperation(HttpMethod.POST));
assertNull("MultiPartTestEntityResource does not supports GET", metaData.getOperation(HttpMethod.GET));
assertNull("MultiPartTestEntityResource does not supports PUT", metaData.getOperation(HttpMethod.PUT));
assertNull("MultiPartTestEntityResource does not supports DELETE", metaData.getOperation(HttpMethod.DELETE));
op = metaData.getOperation(HttpMethod.POST);
assertTrue("MultiPartTestEntityResource must support MultiPartTestResponse", MultiPartTestResponse.class.equals(metaData.getObjectType(op)));
assertEquals("MultiPartTestEntityResource should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
}
Aggregations