use of com.linkedin.restli.restspec.FinderSchemaArray in project rest.li by linkedin.
the class FinderTest method test.
@Test
public void test() throws IOException {
final String findersFilename = IDLS_DIR + FINDERS_FILE;
final ResourceSchema findersIdl = _codec.readResourceSchema(new FileInputStream(findersFilename));
final FinderSchemaArray finders = findersIdl.getCollection().getFinders();
for (FinderSchema finder : finders) {
if ("searchWithoutMetadata".equals(finder.getName())) {
Assert.assertFalse(finder.hasMetadata());
} else if ("searchWithMetadata".equals(finder.getName())) {
Assert.assertEquals(finder.getMetadata().getType(), SearchMetadata.class.getName());
} else if ("basicSearch".equals(finder.getName())) {
Assert.assertFalse(finder.hasMetadata());
} else if ("predefinedSearch".equals(finder.getName())) {
Assert.assertFalse(finder.hasMetadata());
} else {
throw new RuntimeException("Unknown finder is added to com.linkedin.restli.examples.greetings.server.FindersResource");
}
}
}
use of com.linkedin.restli.restspec.FinderSchemaArray in project rest.li by linkedin.
the class TestResourceSchemaToResourceSpecTranslator method compareResourceSpecs.
public void compareResourceSpecs(ResourceSpec actual, ResourceSpec expected, RichResourceSchema resourceSchema) {
Assert.assertEquals(actual.getSupportedMethods(), expected.getSupportedMethods());
compareTypes(actual.getKeyType(), expected.getKeyType());
compareTypes(actual.getValueType(), expected.getValueType());
compareComplexKey(actual.getComplexKeyType(), expected.getComplexKeyType());
compareKeyParts(actual.getKeyParts(), expected.getKeyParts());
ActionSchemaArray actions = resourceSchema.getActions();
compareActions(actual, expected, actions);
ActionSchemaArray entityActions = resourceSchema.getEntityActions();
compareActions(actual, expected, entityActions);
FinderSchemaArray finders = resourceSchema.getFinders();
for (FinderSchema finder : finders) {
compareParameters(actual.getRequestMetadata(finder.getName()), expected.getRequestMetadata(finder.getName()));
}
}
use of com.linkedin.restli.restspec.FinderSchemaArray in project rest.li by linkedin.
the class RequestBuilderSpecGenerator method generateRootRequestBuilder.
private RootBuilderSpec generateRootRequestBuilder(RootBuilderSpec parentRootBuilder, ResourceSchema resource, String sourceFile, Map<String, String> pathKeyTypes) throws IOException {
ValidationResult validationResult = ValidateDataAgainstSchema.validate(resource.data(), resource.schema(), new ValidationOptions());
if (!validationResult.isValid()) {
throw new IllegalArgumentException(String.format("Resource validation error. Resource File '%s', Error Details '%s'", sourceFile, validationResult.toString()));
}
String packageName = resource.getNamespace();
String resourceName = CodeUtil.capitalize(resource.getName());
String className;
if (_version == RestliVersion.RESTLI_2_0_0) {
className = getBuilderClassNameByVersion(RestliVersion.RESTLI_2_0_0, null, resource.getName(), true);
} else {
className = getBuilderClassNameByVersion(RestliVersion.RESTLI_1_0_0, null, resource.getName(), true);
}
RootBuilderSpec rootBuilderSpec = null;
if (resource.hasCollection()) {
rootBuilderSpec = new CollectionRootBuilderSpec(resource);
} else if (resource.hasSimple()) {
rootBuilderSpec = new SimpleRootBuilderSpec(resource);
} else if (resource.hasActionsSet()) {
rootBuilderSpec = new ActionSetRootBuilderSpec(resource);
} else {
log.warn("Ignoring unsupported association resource: " + resourceName);
return null;
}
rootBuilderSpec.setNamespace(packageName);
rootBuilderSpec.setClassName(className);
if (_version == RestliVersion.RESTLI_2_0_0) {
rootBuilderSpec.setBaseClassName("BuilderBase");
}
rootBuilderSpec.setSourceIdlName(sourceFile);
String resourcePath = getResourcePath(resource.getPath());
rootBuilderSpec.setResourcePath(resourcePath);
List<String> pathKeys = getPathKeys(resourcePath);
rootBuilderSpec.setPathKeys(pathKeys);
rootBuilderSpec.setParentRootBuilder(parentRootBuilder);
StringArray supportsList = null;
RestMethodSchemaArray restMethods = null;
FinderSchemaArray finders = null;
ResourceSchemaArray subresources = null;
ActionSchemaArray resourceActions = null;
ActionSchemaArray entityActions = null;
String keyClass = null;
if (resource.getCollection() != null) {
CollectionSchema collection = resource.getCollection();
String keyName = collection.getIdentifier().getName();
// Complex key is not supported
keyClass = collection.getIdentifier().getType();
pathKeyTypes.put(keyName, collection.getIdentifier().getType());
supportsList = collection.getSupports();
restMethods = collection.getMethods();
finders = collection.getFinders();
subresources = collection.getEntity().getSubresources();
resourceActions = collection.getActions();
entityActions = collection.getEntity().getActions();
} else if (resource.getSimple() != null) {
SimpleSchema simpleSchema = resource.getSimple();
keyClass = "Void";
supportsList = simpleSchema.getSupports();
restMethods = simpleSchema.getMethods();
subresources = simpleSchema.getEntity().getSubresources();
resourceActions = simpleSchema.getActions();
} else if (resource.getActionsSet() != null) {
ActionsSetSchema actionsSet = resource.getActionsSet();
resourceActions = actionsSet.getActions();
}
Set<ResourceMethod> supportedMethods = getSupportedMethods(supportsList);
if (!supportedMethods.isEmpty()) {
for (ResourceMethod resourceMethod : supportedMethods) {
validateResourceMethod(resource, resourceName, resourceMethod);
}
}
List<RootBuilderMethodSpec> restMethodSpecs = new ArrayList<>();
List<RootBuilderMethodSpec> finderSpecs = new ArrayList<>();
List<RootBuilderMethodSpec> resourceActionSpecs = new ArrayList<>();
List<RootBuilderMethodSpec> entityActionSpecs = new ArrayList<>();
List<RootBuilderSpec> subresourceSpecs = new ArrayList<>();
String schemaClass = resource.getSchema();
if (restMethods != null) {
restMethodSpecs = generateBasicMethods(rootBuilderSpec, keyClass, schemaClass, supportedMethods, restMethods, resourceName, pathKeys, pathKeyTypes);
}
if (finders != null) {
finderSpecs = generateFinders(rootBuilderSpec, finders, keyClass, schemaClass, resourceName, pathKeys, pathKeyTypes);
}
if (resourceActions != null) {
resourceActionSpecs = generateActions(rootBuilderSpec, resourceActions, keyClass, resourceName, pathKeys, pathKeyTypes);
}
if (entityActions != null) {
entityActionSpecs = generateActions(rootBuilderSpec, entityActions, keyClass, resourceName, pathKeys, pathKeyTypes);
}
if (subresources != null) {
subresourceSpecs = generateSubResources(sourceFile, rootBuilderSpec, subresources, pathKeyTypes);
}
// assign to rootBuilderClass
if (rootBuilderSpec instanceof CollectionRootBuilderSpec) {
CollectionRootBuilderSpec rootBuilder = (CollectionRootBuilderSpec) rootBuilderSpec;
rootBuilder.setRestMethods(restMethodSpecs);
rootBuilder.setFinders(finderSpecs);
rootBuilder.setResourceActions(resourceActionSpecs);
rootBuilder.setEntityActions(entityActionSpecs);
rootBuilder.setSubresources(subresourceSpecs);
} else if (rootBuilderSpec instanceof SimpleRootBuilderSpec) {
SimpleRootBuilderSpec rootBuilder = (SimpleRootBuilderSpec) rootBuilderSpec;
rootBuilder.setRestMethods(restMethodSpecs);
rootBuilder.setResourceActions(resourceActionSpecs);
rootBuilder.setSubresources(subresourceSpecs);
} else if (rootBuilderSpec instanceof ActionSetRootBuilderSpec) {
ActionSetRootBuilderSpec rootBuilder = (ActionSetRootBuilderSpec) rootBuilderSpec;
rootBuilder.setResourceActions(resourceActionSpecs);
}
registerBuilderSpec(rootBuilderSpec);
return rootBuilderSpec;
}
use of com.linkedin.restli.restspec.FinderSchemaArray in project rest.li by linkedin.
the class TestExamplesGenerator method findFinder.
private static FinderSchema findFinder(ResourceSchema resourceSchema, String finderName) {
final CollectionSchema collectionSchema = resourceSchema.getCollection();
if (collectionSchema != null) {
final FinderSchemaArray finders = collectionSchema.getFinders();
if (finders != null) {
for (FinderSchema finderSchema : finders) {
if (finderSchema.getName().equals(finderName)) {
return finderSchema;
}
}
}
}
final AssociationSchema associationSchema = resourceSchema.getAssociation();
if (associationSchema != null) {
final FinderSchemaArray finders = associationSchema.getFinders();
if (finders != null) {
for (FinderSchema finderSchema : finders) {
if (finderSchema.getName().equals(finderName)) {
return finderSchema;
}
}
}
}
return null;
}
use of com.linkedin.restli.restspec.FinderSchemaArray in project rest.li by linkedin.
the class ResourceModelEncoder method createFinders.
private FinderSchemaArray createFinders(final ResourceModel resourceModel) {
FinderSchemaArray findersArray = new FinderSchemaArray();
List<ResourceMethodDescriptor> resourceMethodDescriptors = resourceModel.getResourceMethodDescriptors();
Collections.sort(resourceMethodDescriptors, RESOURCE_METHOD_COMPARATOR);
for (ResourceMethodDescriptor resourceMethodDescriptor : resourceMethodDescriptors) {
if (ResourceMethod.FINDER.equals(resourceMethodDescriptor.getType())) {
FinderSchema finder = new FinderSchema();
finder.setName(resourceMethodDescriptor.getFinderName());
String doc = _docsProvider.getMethodDoc(resourceMethodDescriptor.getMethod());
if (doc != null) {
finder.setDoc(doc);
}
ParameterSchemaArray parameters = createParameters(resourceMethodDescriptor);
if (parameters.size() > 0) {
finder.setParameters(parameters);
}
StringArray assocKeys = createAssocKeyParameters(resourceMethodDescriptor);
if (assocKeys.size() > 0) {
finder.setAssocKeys(assocKeys);
}
if (resourceMethodDescriptor.getFinderMetadataType() != null) {
Class<?> metadataType = resourceMethodDescriptor.getFinderMetadataType();
MetadataSchema metadataSchema = new MetadataSchema();
metadataSchema.setType(buildDataSchemaType(metadataType));
finder.setMetadata(metadataSchema);
}
final DataMap customAnnotation = resourceMethodDescriptor.getCustomAnnotationData();
String deprecatedDoc = _docsProvider.getMethodDeprecatedTag(resourceMethodDescriptor.getMethod());
if (deprecatedDoc != null) {
customAnnotation.put(DEPRECATED_ANNOTATION_NAME, deprecateDocToAnnotationMap(deprecatedDoc));
}
if (!customAnnotation.isEmpty()) {
finder.setAnnotations(new CustomAnnotationContentSchemaMap(customAnnotation));
}
if (resourceMethodDescriptor.isPagingSupported()) {
finder.setPagingSupported(true);
}
findersArray.add(finder);
}
}
return findersArray;
}
Aggregations