use of com.google.api.server.spi.config.model.ApiConfig in project endpoints-java by cloudendpoints.
the class ApiConfigValidatorTest method testApiWideConfigWithInvalidApiName.
@Test
public void testApiWideConfigWithInvalidApiName() throws Exception {
@Api(name = "TestApi", version = "v1", resource = "bar")
final class Test {
}
ApiConfig config = configLoader.loadConfiguration(ServiceContext.create(), Test.class);
try {
validator.validate(config);
fail("Expected InvalidApiNameException.");
} catch (InvalidApiNameException expected) {
}
}
use of com.google.api.server.spi.config.model.ApiConfig in project endpoints-java by cloudendpoints.
the class ApiConfigValidatorTest method testValidateMethods_ignoredMethod.
@Test
public void testValidateMethods_ignoredMethod() throws Exception {
final class Bean {
}
@Api
final class Endpoint {
@ApiMethod(ignored = AnnotationBoolean.TRUE)
public void thisShouldBeIgnored(Bean resource1, Bean resource2) {
}
}
ApiConfig config = configLoader.loadConfiguration(ServiceContext.create(), Endpoint.class);
// If this passes validation, then no error will be thrown. Otherwise, the validator will
// complain that the method has two resources.
validator.validate(config);
}
use of com.google.api.server.spi.config.model.ApiConfig in project endpoints-java by cloudendpoints.
the class ApiConfigValidatorTest method testApiMethodConfigWithApiMethodNameContainingContinuousDots.
@Test
public void testApiMethodConfigWithApiMethodNameContainingContinuousDots() throws Exception {
@Api(name = "testApi", version = "v1", resource = "bar")
final class Test {
@ApiMethod(name = "TestApi..testMethod")
public void test() {
}
}
ApiConfig config = configLoader.loadConfiguration(ServiceContext.create(), Test.class);
try {
validator.validate(config);
fail("Expected InvalidMethodNameException.");
} catch (InvalidMethodNameException expected) {
}
}
use of com.google.api.server.spi.config.model.ApiConfig in project endpoints-java by cloudendpoints.
the class ApiConfigValidatorTest method testApiMethodConfigWithApiMethodNameContainingSpecialCharacter.
@Test
public void testApiMethodConfigWithApiMethodNameContainingSpecialCharacter() throws Exception {
@Api(name = "testApi", version = "v1", resource = "bar")
final class Test {
@ApiMethod(name = "Api.Test#Method")
public void test() {
}
}
ApiConfig config = configLoader.loadConfiguration(ServiceContext.create(), Test.class);
try {
validator.validate(config);
fail("Expected InvalidMethodNameException.");
} catch (InvalidMethodNameException expected) {
}
}
use of com.google.api.server.spi.config.model.ApiConfig in project endpoints-java by cloudendpoints.
the class GetDiscoveryDocAction method getDiscoveryDoc.
/**
* Generates a Java client library for an API. Combines the steps of generating API
* configuration, generating Discovery doc and generating client library into one.
* @param classPath Class path to load service classes and their dependencies
* @param outputDirPath Directory to write output files into
* @param serviceClassNames Array of service class names of the API
* @param hostname The hostname to use
* @param basePath The base path to use
* @param outputToDisk Whether or not to output discovery docs to disk
*/
public Map<String, String> getDiscoveryDoc(URL[] classPath, String outputDirPath, List<String> serviceClassNames, String hostname, String basePath, boolean outputToDisk) throws ClassNotFoundException, IOException, ApiConfigException {
File outputDir = new File(outputDirPath);
if (!outputDir.isDirectory()) {
throw new IllegalArgumentException(outputDirPath + " is not a directory");
}
ClassLoader classLoader = new URLClassLoader(classPath, getClass().getClassLoader());
ApiConfig.Factory configFactory = new ApiConfig.Factory();
TypeLoader typeLoader = new TypeLoader(classLoader);
SchemaRepository schemaRepository = new SchemaRepository(typeLoader);
ApiConfigValidator validator = new ApiConfigValidator(typeLoader, schemaRepository);
DiscoveryGenerator discoveryGenerator = new DiscoveryGenerator(typeLoader);
List<ApiConfig> apiConfigs = Lists.newArrayListWithCapacity(serviceClassNames.size());
ImmutableListMultimap<ApiKey, ApiConfig> configsByKey = Multimaps.index(apiConfigs, new Function<ApiConfig, ApiKey>() {
@Override
public ApiKey apply(ApiConfig input) {
return input.getApiKey();
}
});
for (ApiKey key : configsByKey.keys()) {
validator.validate(configsByKey.get(key));
}
ApiConfigLoader configLoader = new ApiConfigLoader(configFactory, typeLoader, new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes()));
ServiceContext serviceContext = ServiceContext.createFromHostname(hostname, ServiceContext.DEFAULT_API_NAME);
for (Class<?> serviceClass : loadClasses(classLoader, serviceClassNames)) {
apiConfigs.add(configLoader.loadConfiguration(serviceContext, serviceClass));
}
DiscoveryGenerator.Result result = discoveryGenerator.writeDiscovery(apiConfigs, new DiscoveryContext().setHostname(hostname).setBasePath(basePath), schemaRepository);
ObjectWriter writer = ObjectMapperUtil.createStandardObjectMapper().writer(new EndpointsPrettyPrinter());
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
for (Map.Entry<ApiKey, RestDescription> entry : result.discoveryDocs().entrySet()) {
ApiKey key = entry.getKey();
String discoveryDocFilePath = outputDir + "/" + key.getName() + "-" + key.getVersion() + "-rest.discovery";
String docString = writer.writeValueAsString(entry.getValue());
if (outputToDisk) {
Files.write(docString, new File(discoveryDocFilePath), UTF_8);
System.out.println("API Discovery Document written to " + discoveryDocFilePath);
}
builder.put(discoveryDocFilePath, docString);
}
return builder.build();
}
Aggregations