use of com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec 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.tools.clientgen.builderspec.RootBuilderSpec in project rest.li by linkedin.
the class TestRequestBuilderSpecGenerator method testSimpleResource.
@Test
public void testSimpleResource() throws Exception {
String idl = moduleDir + FS + IDLS_DIR + FS + "testSimple.restspec.json";
Set<BuilderSpec> builderSpecs = generateBuilderSpec(new String[] { idl });
Assert.assertNotNull(builderSpecs);
Assert.assertTrue(builderSpecs.size() == 6);
Map<String, String> methodMap = new HashMap<>();
methodMap.put("get", "Gets the greeting.");
methodMap.put("delete", "Deletes the greeting.");
methodMap.put("update", "Updates the greeting.");
for (BuilderSpec spec : builderSpecs) {
Assert.assertTrue(spec instanceof RootBuilderSpec || spec instanceof RestMethodBuilderSpec);
if (spec instanceof RootBuilderSpec) {
Assert.assertTrue(spec instanceof SimpleRootBuilderSpec);
SimpleRootBuilderSpec simpleSpec = (SimpleRootBuilderSpec) spec;
if (simpleSpec.getResourcePath().indexOf('/') >= 0) {
Assert.assertEquals(simpleSpec.getSourceIdlName(), idl);
Assert.assertEquals(simpleSpec.getResourcePath(), "testSimple/testSimpleSub");
Assert.assertNotNull(simpleSpec.getRestMethods());
Assert.assertTrue(simpleSpec.getRestMethods().size() == 1);
Assert.assertEquals("get", simpleSpec.getRestMethods().get(0).getName());
Assert.assertTrue(simpleSpec.getResourceActions().isEmpty());
Assert.assertTrue(simpleSpec.getSubresources().isEmpty());
} else {
Assert.assertTrue(simpleSpec.getResourceActions().isEmpty());
Assert.assertTrue(simpleSpec.getSubresources().size() == 1);
Assert.assertEquals(simpleSpec.getSourceIdlName(), idl);
Assert.assertEquals(simpleSpec.getResourcePath(), "testSimple");
Assert.assertNotNull(simpleSpec.getRestMethods());
Assert.assertTrue(simpleSpec.getRestMethods().size() == 3);
List<RootBuilderMethodSpec> restMethods = simpleSpec.getRestMethods();
for (RootBuilderMethodSpec method : restMethods) {
Assert.assertTrue(method.getReturn() instanceof RestMethodBuilderSpec);
Assert.assertEquals(method.getReturn().getRootBuilderMethod(), method);
Assert.assertTrue(methodMap.containsKey(method.getName()));
Assert.assertEquals(methodMap.get(method.getName()), method.getDoc());
}
}
} else if (spec instanceof RestMethodBuilderSpec) {
RestMethodBuilderSpec builderSpec = (RestMethodBuilderSpec) spec;
ResourceMethod method = builderSpec.getResourceMethod();
Assert.assertTrue(methodMap.containsKey(method.toString()));
Assert.assertFalse(builderSpec.hasBindingMethods());
}
}
}
use of com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec in project rest.li by linkedin.
the class RequestBuilderSpecGenerator method generateSubResources.
private List<RootBuilderSpec> generateSubResources(String sourceFile, RootBuilderSpec parentRootBuilder, ResourceSchemaArray subresources, Map<String, String> pathKeyTypes) throws IOException {
List<RootBuilderSpec> subSpecList = new ArrayList<RootBuilderSpec>();
if (subresources != null) {
for (ResourceSchema resource : subresources) {
RootBuilderSpec resourceSpec = generateRootRequestBuilder(parentRootBuilder, resource, sourceFile, pathKeyTypes);
subSpecList.add(resourceSpec);
}
}
return subSpecList;
}
use of com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec in project rest.li by linkedin.
the class TestRequestBuilderSpecGenerator method testCollectionResource.
@Test
public void testCollectionResource() throws Exception {
String idl = moduleDir + FS + IDLS_DIR + FS + "testCollection.restspec.json";
Set<BuilderSpec> builderSpecs = generateBuilderSpec(new String[] { idl });
Assert.assertNotNull(builderSpecs);
Assert.assertTrue(builderSpecs.size() == 15);
List<String> expectedMethods = Arrays.asList("actionAnotherAction", "actionSomeAction", "actionVoidAction", "batchGet", "create", "delete", "findBySearch", "get", "getAll", "partialUpdate", "update");
List<String> actualMethods = new ArrayList<>();
CollectionRootBuilderSpec rootBuilder = null;
CollectionRootBuilderSpec subRootBuilder = null;
FinderBuilderSpec finderBuilder = null;
List<ActionBuilderSpec> actionBuilders = new ArrayList<>();
List<RestMethodBuilderSpec> basicMethodBuilders = new ArrayList<>();
for (BuilderSpec spec : builderSpecs) {
if (spec instanceof RootBuilderSpec) {
Assert.assertTrue(spec instanceof CollectionRootBuilderSpec);
CollectionRootBuilderSpec collSpec = (CollectionRootBuilderSpec) spec;
if (collSpec.getResourcePath().indexOf('/') >= 0) {
subRootBuilder = collSpec;
} else {
rootBuilder = collSpec;
}
} else if (spec instanceof FinderBuilderSpec) {
finderBuilder = (FinderBuilderSpec) spec;
} else if (spec instanceof ActionBuilderSpec) {
actionBuilders.add((ActionBuilderSpec) spec);
} else if (spec instanceof RestMethodBuilderSpec) {
basicMethodBuilders.add((RestMethodBuilderSpec) spec);
} else {
Assert.fail("There should not be any other builder spec generated!");
}
}
// assert sub resource root builder spec
Assert.assertNotNull(subRootBuilder);
Assert.assertEquals(subRootBuilder.getSourceIdlName(), idl);
Assert.assertEquals(subRootBuilder.getResourcePath(), "testCollection/{testCollectionId}/testCollectionSub");
Assert.assertEquals(subRootBuilder.getParentRootBuilder(), rootBuilder);
Assert.assertNotNull(subRootBuilder.getRestMethods());
Assert.assertTrue(subRootBuilder.getRestMethods().size() == 2);
Assert.assertTrue(subRootBuilder.getFinders().isEmpty());
Assert.assertTrue(subRootBuilder.getResourceActions().isEmpty());
Assert.assertTrue(subRootBuilder.getEntityActions().isEmpty());
Assert.assertTrue(subRootBuilder.getSubresources().isEmpty());
// assert root builder spec
Assert.assertNotNull(rootBuilder);
Assert.assertEquals(rootBuilder.getSourceIdlName(), idl);
Assert.assertEquals(rootBuilder.getResourcePath(), "testCollection");
Assert.assertNotNull(rootBuilder.getRestMethods());
Assert.assertTrue(rootBuilder.getRestMethods().size() == 7);
for (RootBuilderMethodSpec method : rootBuilder.getRestMethods()) {
actualMethods.add(method.getName());
Assert.assertEquals(method.getReturn().getRootBuilderMethod(), method);
}
Assert.assertNotNull(rootBuilder.getFinders());
Assert.assertTrue(rootBuilder.getFinders().size() == 1);
actualMethods.add(rootBuilder.getFinders().get(0).getName());
Assert.assertNotNull(rootBuilder.getResourceActions());
Assert.assertTrue(rootBuilder.getResourceActions().size() == 1);
actualMethods.add(rootBuilder.getResourceActions().get(0).getName());
Assert.assertNotNull(rootBuilder.getEntityActions());
Assert.assertTrue(rootBuilder.getEntityActions().size() == 2);
actualMethods.add(rootBuilder.getEntityActions().get(0).getName());
actualMethods.add(rootBuilder.getEntityActions().get(1).getName());
Assert.assertNotNull(rootBuilder.getSubresources());
Assert.assertTrue(rootBuilder.getSubresources().size() == 1);
Collections.sort(actualMethods);
Assert.assertEquals(actualMethods, expectedMethods);
// assert finder builder spec
Assert.assertNotNull(finderBuilder);
Assert.assertEquals("search", finderBuilder.getFinderName());
Assert.assertNotNull(finderBuilder.getQueryParamMethods());
Assert.assertTrue(finderBuilder.hasBindingMethods());
Assert.assertEquals(finderBuilder.getMetadataType().getFullName(), "com.linkedin.restli.tools.test.TestRecord");
Assert.assertTrue(finderBuilder.getQueryParamMethods().size() == 1);
QueryParamBindingMethodSpec finderQuery = finderBuilder.getQueryParamMethods().get(0);
Assert.assertEquals(finderQuery.getParamName(), "tone");
Assert.assertEquals(finderQuery.getMethodName(), "toneParam");
Assert.assertEquals(finderQuery.getArgType().getFullName(), "com.linkedin.restli.tools.test.TestEnum");
Assert.assertFalse(finderQuery.isNeedAddParamMethod());
Assert.assertTrue(finderQuery.isOptional());
// assert action builder spec
Assert.assertNotNull(actionBuilders);
Assert.assertTrue(actionBuilders.size() == 3);
for (ActionBuilderSpec spec : actionBuilders) {
Assert.assertTrue(spec.getActionName().equals("someAction") || spec.getActionName().equals("anotherAction") || spec.getActionName().equals("voidAction"));
if (spec.getActionName().equals("voidAction")) {
Assert.assertFalse(spec.hasBindingMethods());
} else {
Assert.assertTrue(spec.hasBindingMethods());
}
}
// assert get method builder query method
Assert.assertNotNull(basicMethodBuilders);
// 7 for root resource, 2 for sub resource
Assert.assertTrue(basicMethodBuilders.size() == 9);
for (RestMethodBuilderSpec spec : basicMethodBuilders) {
if (spec.getResourceMethod() == ResourceMethod.GET) {
Assert.assertNotNull(spec.getQueryParamMethods());
Assert.assertTrue(spec.getQueryParamMethods().size() == 1);
Assert.assertTrue(spec.hasBindingMethods());
QueryParamBindingMethodSpec getQuery = spec.getQueryParamMethods().get(0);
Assert.assertEquals(getQuery.getParamName(), "message");
Assert.assertEquals(getQuery.getMethodName(), "messageParam");
Assert.assertEquals(getQuery.getArgType().getSchema().getType(), DataSchema.Type.STRING);
Assert.assertFalse(getQuery.isNeedAddParamMethod());
Assert.assertTrue(getQuery.isOptional());
if (spec.getResource().getName().equals("testCollection")) {
DataMap expected = new DataMap();
expected.put("someAnnotation", new DataMap());
Assert.assertEquals(spec.getAnnotations(), expected);
}
} else if (spec.getResourceMethod() == ResourceMethod.DELETE && spec.getClassName().startsWith("TestCollectionSub")) {
// sub resource delete method should have path keys
List<PathKeyBindingMethodSpec> pathKeys = spec.getPathKeyMethods();
Assert.assertNotNull(pathKeys);
Assert.assertTrue(pathKeys.size() == 1);
Assert.assertTrue(spec.hasBindingMethods());
PathKeyBindingMethodSpec pathKeyMethod = pathKeys.get(0);
Assert.assertEquals(pathKeyMethod.getPathKey(), "testCollectionId");
Assert.assertEquals(pathKeyMethod.getMethodName(), "testCollectionIdKey");
Assert.assertEquals(pathKeyMethod.getArgType().getSchema().getType(), DataSchema.Type.LONG);
} else if (spec.getResourceMethod() == ResourceMethod.CREATE) {
Assert.assertEquals(spec.getQueryParamMethods().size(), 1);
Assert.assertTrue(spec.hasBindingMethods());
QueryParamBindingMethodSpec queryParam = spec.getQueryParamMethods().get(0);
Assert.assertEquals(queryParam.getParamName(), "isNullId");
Assert.assertEquals(queryParam.isOptional(), true);
DataMap expected = new DataMap();
expected.put("someOtherAnnotation", new DataMap());
Assert.assertEquals(spec.getAnnotations(), expected);
}
}
}
use of com.linkedin.restli.tools.clientgen.builderspec.RootBuilderSpec in project rest.li by linkedin.
the class TestRequestBuilderSpecGenerator method testActionResource.
@Test
public void testActionResource() throws Exception {
String idl = moduleDir + FS + IDLS_DIR + FS + "testAction.restspec.json";
Set<BuilderSpec> builderSpecs = generateBuilderSpec(new String[] { idl });
Assert.assertNotNull(builderSpecs);
Assert.assertEquals(builderSpecs.size(), 27);
ActionSetRootBuilderSpec rootBuilder = null;
List<ActionBuilderSpec> actionBuilders = new ArrayList<>();
for (BuilderSpec spec : builderSpecs) {
if (spec instanceof RootBuilderSpec) {
Assert.assertTrue(spec instanceof ActionSetRootBuilderSpec);
rootBuilder = (ActionSetRootBuilderSpec) spec;
} else if (spec instanceof ActionBuilderSpec) {
actionBuilders.add((ActionBuilderSpec) spec);
} else {
Assert.fail("There should not be any other builder spec generated!");
}
}
Assert.assertNotNull(rootBuilder);
Assert.assertEquals(rootBuilder.getSourceIdlName(), idl);
Assert.assertEquals(rootBuilder.getResourcePath(), "testAction");
Assert.assertNotNull(rootBuilder.getResourceActions());
Assert.assertEquals(rootBuilder.getResourceActions().size(), 26);
for (RootBuilderMethodSpec methodSpec : rootBuilder.getMethods()) {
Assert.assertTrue(methodSpec.getReturn() instanceof ActionBuilderSpec);
Assert.assertEquals(methodSpec.getReturn().getRootBuilderMethod(), methodSpec);
}
Assert.assertNotNull(actionBuilders);
Assert.assertEquals(actionBuilders.size(), 26);
Set<String> actionNames = new HashSet<>(Arrays.asList("arrayPromise", "echo", "echoRecord", "echoRecordArray", "echoStringArray", "echoEnumArray", "failCallbackCall", "failCallbackThrow", "failPromiseCall", "failPromiseThrow", "failTaskCall", "failTaskThrow", "failThrowInTask", "get", "nullPromise", "nullTask", "parseq", "parseq3", "returnBool", "returnBoolOptionalParam", "returnInt", "returnIntOptionalParam", "returnVoid", "timeout", "timeoutCallback", "ultimateAnswer"));
for (ActionBuilderSpec spec : actionBuilders) {
Assert.assertTrue(actionNames.contains(spec.getActionName()));
actionNames.remove(spec.getActionName());
if (spec.getActionName().equals("parseq")) {
List<ActionParamBindingMethodSpec> params = spec.getActionParamMethods();
for (ActionParamBindingMethodSpec param : params) {
if (param.getParamName().equals("a")) {
Assert.assertEquals(param.getArgType().getSchema().getType(), DataSchema.Type.INT);
} else if (param.getParamName().equals("b")) {
Assert.assertEquals(param.getArgType().getSchema().getType(), DataSchema.Type.STRING);
} else if (param.getParamName().equals("c")) {
Assert.assertEquals(param.getArgType().getSchema().getType(), DataSchema.Type.BOOLEAN);
} else {
Assert.fail("There should not be any other param method spec generated!");
}
}
Assert.assertEquals(spec.getValueClass().getSchema().getType(), DataSchema.Type.STRING);
}
}
Assert.assertTrue(actionNames.isEmpty());
}
Aggregations