Search in sources :

Example 1 with JavaWriterVisitor

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());
}
Also used : Path(java.nio.file.Path) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) Test(org.junit.Test)

Example 2 with JavaWriterVisitor

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());
}
Also used : Path(java.nio.file.Path) GapicClass(com.google.api.generator.gapic.model.GapicClass) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) Test(org.junit.Test)

Example 3 with JavaWriterVisitor

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());
}
Also used : Path(java.nio.file.Path) GapicClass(com.google.api.generator.gapic.model.GapicClass) Service(com.google.api.generator.gapic.model.Service) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) GapicContext(com.google.api.generator.gapic.model.GapicContext) Test(org.junit.Test)

Example 4 with JavaWriterVisitor

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());
}
Also used : Path(java.nio.file.Path) GapicClass(com.google.api.generator.gapic.model.GapicClass) Service(com.google.api.generator.gapic.model.Service) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) GapicContext(com.google.api.generator.gapic.model.GapicContext) Test(org.junit.Test)

Example 5 with JavaWriterVisitor

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());
}
Also used : Path(java.nio.file.Path) Message(com.google.api.generator.gapic.model.Message) HashMap(java.util.HashMap) ResourceName(com.google.api.generator.gapic.model.ResourceName) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) GapicClass(com.google.api.generator.gapic.model.GapicClass) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

JavaWriterVisitor (com.google.api.generator.engine.writer.JavaWriterVisitor)25 Path (java.nio.file.Path)22 GapicClass (com.google.api.generator.gapic.model.GapicClass)21 Test (org.junit.Test)21 GapicContext (com.google.api.generator.gapic.model.GapicContext)15 Service (com.google.api.generator.gapic.model.Service)14 ResourceName (com.google.api.generator.gapic.model.ResourceName)5 Message (com.google.api.generator.gapic.model.Message)4 HashSet (java.util.HashSet)4 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)3 ServiceDescriptor (com.google.protobuf.Descriptors.ServiceDescriptor)3 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)1 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)1 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)1 Statement (com.google.api.generator.engine.ast.Statement)1 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)1 ByteString (com.google.protobuf.ByteString)1 CodeGeneratorResponse (com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse)1