use of com.google.api.generator.engine.writer.JavaWriterVisitor in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method validJavaClass.
@Test
public void validJavaClass() {
// Create outer class variableDecls.
// [code] private static final String serviceName = "LibraryServiceStub";
VariableExpr serviceName = createServiceNameVarExpr();
AssignmentExpr serviceNameDel = createAssignmentExpr(serviceName, ValueExpr.withValue(StringObjectValue.withValue("LibraryServiceStub")));
// [code] protected List<Shelf> shelfList;
VariableExpr shelfListExpr = createShelfListVarExpr();
// [code] public static HashMap<String, Shelf> shelfMap;
VariableExpr shelfMapExpr = createShelfMapVarExpr();
// Create the LibraryServiceStub constructor
MethodDefinition libraryServiceCtor = createLibServiceCtor();
// Create nested class Shelf.
ClassDefinition nestedClassShelf = createNestedClassShelf();
// Create nested abstract class Book.
ClassDefinition nestedClassBook = createNestedClassBook();
// Create nested class Novel.
ClassDefinition nestedClassNovel = createNestedClassNovel();
// Create method `addShelf`
MethodDefinition addShelfMethod = createAddShelfMethod();
// Create method `updateShelfMap`
MethodDefinition updateShelfMap = createUpdateShelfMap();
// Create method `printShelfListToFile`
MethodDefinition printShelfListToFile = createPrintShelfListToFile();
// Create method `addBooksContainsNovel`
MethodDefinition addBooksContainsNovel = createAddBooksContainsNovel();
// Create private method `addBookToShelf`
MethodDefinition addBookToShelf = createAddBookToShelf();
// Create outer class LibraryServiceStub
ClassDefinition libraryServiceStubClass = ClassDefinition.builder().setFileHeader(Arrays.asList(createFileHeader())).setHeaderCommentStatements(Arrays.asList(createOuterClassJavaDocComment())).setPackageString("com.google.example.library.core").setAnnotations(Arrays.asList(AnnotationNode.withSuppressWarnings("all"), AnnotationNode.DEPRECATED, AnnotationNode.OVERRIDE)).setImplementsTypes(Arrays.asList(TypeNode.withReference(libraryServiceRef))).setExtendsType(TypeNode.withReference(stubRef)).setScope(ScopeNode.PUBLIC).setStatements(Arrays.asList(ExprStatement.withExpr(serviceNameDel), ExprStatement.withExpr(shelfListExpr), ExprStatement.withExpr(shelfMapExpr))).setMethods(Arrays.asList(libraryServiceCtor, addShelfMethod, updateShelfMap, printShelfListToFile, addBooksContainsNovel, addBookToShelf)).setNestedClasses(Arrays.asList(nestedClassShelf, nestedClassBook, nestedClassNovel)).setName("LibraryServiceStub").build();
JavaWriterVisitor javaWriterVisitor = new JavaWriterVisitor();
libraryServiceStubClass.accept(javaWriterVisitor);
Utils.saveCodegenToFile(this.getClass(), "JavaCodeGeneratorTest.golden", javaWriterVisitor.write());
Path goldenFilePath = Paths.get(GOLDENFILES_DIRECTORY, "JavaCodeGeneratorTest.golden");
Assert.assertCodeEquals(goldenFilePath, javaWriterVisitor.write());
}
use of com.google.api.generator.engine.writer.JavaWriterVisitor in project gapic-generator-java by googleapis.
the class ComposerTest method gapicClass_addApacheLicense.
@Test
public void gapicClass_addApacheLicense() {
ClassDefinition classDef = ClassDefinition.builder().setPackageString("com.google.showcase.v1beta1.stub").setName("ComposerPostProcOnFooBar").setScope(ScopeNode.PUBLIC).build();
List<GapicClass> gapicClassWithHeaderList = Composer.addApacheLicense(Arrays.asList(GapicClass.create(Kind.TEST, classDef)));
JavaWriterVisitor visitor = new JavaWriterVisitor();
gapicClassWithHeaderList.get(0).classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "ComposerPostProcOnFooBar.golden", visitor.write());
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "ComposerPostProcOnFooBar.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
use of com.google.api.generator.engine.writer.JavaWriterVisitor in project gapic-generator-java by googleapis.
the class ServiceStubSettingsClassComposerTest method generateServiceClasses.
@Test
public void generateServiceClasses() {
GapicContext context = RestTestProtoLoader.instance().parseCompliance();
Service echoProtoService = context.services().get(0);
GapicClass clazz = ServiceStubSettingsClassComposer.instance().generate(context, echoProtoService);
JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "ComplianceStubSettings.golden", visitor.write());
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "ComplianceStubSettings.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
use of com.google.api.generator.engine.writer.JavaWriterVisitor in project gapic-generator-java by googleapis.
the class ServiceSettingsClassComposerTest method generateServiceClasses.
@Test
public void generateServiceClasses() {
GapicContext context = GrpcRestTestProtoLoader.instance().parseShowcaseEcho();
Service echoProtoService = context.services().get(0);
GapicClass clazz = ServiceSettingsClassComposer.instance().generate(context, echoProtoService);
JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "EchoSettings.golden", visitor.write());
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "EchoSettings.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
use of com.google.api.generator.engine.writer.JavaWriterVisitor in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposerTest method generateResourceNameClass_loggingOnePatternMultipleVariables.
@Test
public void generateResourceNameClass_loggingOnePatternMultipleVariables() {
FileDescriptor serviceFileDescriptor = LoggingConfigProto.getDescriptor();
ServiceDescriptor serviceDescriptor = serviceFileDescriptor.getServices().get(0);
assertEquals(serviceDescriptor.getName(), "ConfigServiceV2");
List<FileDescriptor> protoFiles = Arrays.asList(serviceFileDescriptor, LoggingProto.getDescriptor(), LogEntryProto.getDescriptor(), LoggingConfigProto.getDescriptor(), LoggingMetricsProto.getDescriptor());
Map<String, ResourceName> resourceNames = new HashMap<>();
Map<String, Message> messageTypes = new HashMap<>();
for (FileDescriptor fileDescriptor : protoFiles) {
resourceNames.putAll(Parser.parseResourceNames(fileDescriptor));
messageTypes.putAll(Parser.parseMessages(fileDescriptor));
}
// Additional resource names.
FileDescriptor commonResourcesFileDescriptor = CommonResources.getDescriptor();
resourceNames.putAll(Parser.parseResourceNames(commonResourcesFileDescriptor));
Set<ResourceName> outputResourceNames = new HashSet<>();
Parser.parseService(serviceFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
ResourceName billingAccountLocationResname = resourceNames.get("logging.googleapis.com/BillingAccountLocation");
assertThat(outputResourceNames).contains(billingAccountLocationResname);
GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(billingAccountLocationResname, TestProtoLoader.instance().parseLogging());
JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "BillingAccountLocationName.golden", visitor.write());
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "BillingAccountLocationName.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Aggregations