use of com.google.api.generator.gapic.model.Sample in project gapic-generator-java by googleapis.
the class Assert method assertGoldenSamples.
public static void assertGoldenSamples(Class<?> clazz, String sampleDirName, String packkage, List<Sample> samples) {
for (Sample sample : samples) {
String fileName = sample.name() + ".golden";
String goldenSampleDir = Utils.getGoldenDir(clazz) + "/samples/" + sampleDirName.toLowerCase() + "/";
Path goldenFilePath = Paths.get(goldenSampleDir, fileName);
sample = sample.withHeader(Arrays.asList(CommentComposer.APACHE_LICENSE_COMMENT)).withRegionTag(sample.regionTag().withApiShortName("goldenSample"));
String sampleString = SampleCodeWriter.writeExecutableSample(sample, packkage + ".samples");
Utils.saveSampleCodegenToFile(clazz, sampleDirName.toLowerCase(), fileName, sampleString);
assertCodeEquals(goldenFilePath, sampleString);
}
}
use of com.google.api.generator.gapic.model.Sample in project gapic-generator-java by googleapis.
the class ComposerTest method composeSamples_showcase.
@Test
public void composeSamples_showcase() {
for (Sample sample : samples) {
assertEquals("File header will be empty before composing samples", sample.fileHeader(), ImmutableList.of());
assertEquals("ApiShortName will be empty before composing samples", sample.regionTag().apiShortName(), "");
assertEquals("ApiVersion will be empty before composing samples", sample.regionTag().apiVersion(), "");
}
List<Sample> composedSamples = Composer.prepareExecutableSamples(clazzes, protoPackage).get(0).samples();
for (Sample sample : composedSamples) {
assertEquals("File header should be apache", sample.fileHeader(), Arrays.asList(CommentComposer.APACHE_LICENSE_COMMENT));
assertEquals("ApiShortName should be showcase", sample.regionTag().apiShortName(), "showcase");
assertEquals("ApiVersion should be v1beta1", sample.regionTag().apiVersion(), "v1beta1");
}
}
use of com.google.api.generator.gapic.model.Sample in project gapic-generator-java by googleapis.
the class SampleCodeWriterTest method writeExecutableSample.
@Test
public void writeExecutableSample() {
Sample sample = testingSample.withRegionTag(regionTag.withOverloadDisambiguation("ExecutableSample"));
String result = SampleCodeWriter.writeExecutableSample(sample, packageName);
String expected = LineFormatter.lines("/*\n", " * Apache License\n", " */\n", "\n", "package com.google.samples;\n", "\n", "// [START testing_v1_generated_samples_write_executablesample_sync]\n", "import com.google.api.gax.rpc.ClientSettings;\n", "\n", "public class SyncWriteExecutableSample {\n", "\n", " public static void main(String[] args) throws Exception {\n", " syncWriteExecutableSample();\n", " }\n", "\n", " public static void syncWriteExecutableSample() throws Exception {\n", " // This snippet has been automatically generated for illustrative purposes only.\n", " // It may require modifications to work in your environment.\n", " ClientSettings clientSettings = ClientSettings.newBuilder().build();\n", " try (boolean aBool = false) {\n", " int x = 3;\n", " }\n", " }\n", "}\n", "// [END testing_v1_generated_samples_write_executablesample_sync]\n");
Assert.assertEquals(expected, result);
}
use of com.google.api.generator.gapic.model.Sample in project gapic-generator-java by googleapis.
the class SampleComposerUtilTest method handleDuplicateSamples_distinctDuplicates.
@Test
public void handleDuplicateSamples_distinctDuplicates() {
VariableExpr echoClientVariableExprDiffVarName = VariableExpr.withVariable(Variable.builder().setName("echoClient2").setType(clientType).build());
Sample echoClientSampleDiffVarName = echoClientSample.toBuilder().setBody(Arrays.asList(ExprStatement.withExpr(SampleComposerUtil.assignClientVariableWithCreateMethodExpr(echoClientVariableExprDiffVarName)))).build();
List<Sample> samples = Arrays.asList(echoClientSample, echoClientSample, echoClientSampleDiffVarName);
assertEquals(samples.size(), 3);
assertEquals(samples.get(0).name(), "SyncCreate");
assertEquals(samples.get(1).name(), "SyncCreate");
assertEquals(samples.get(2).name(), "SyncCreate");
List<Sample> result = SampleComposerUtil.handleDuplicateSamples(samples);
assertEquals(result.size(), 2);
assertTrue(result.contains(echoClientSample));
assertTrue(result.stream().filter(s -> s.name().equals("SyncCreate1")).findFirst().isPresent());
}
use of com.google.api.generator.gapic.model.Sample in project gapic-generator-java by googleapis.
the class SampleComposerUtilTest method handleDuplicateSamples_notDuplicateName.
@Test
public void handleDuplicateSamples_notDuplicateName() {
Sample echoClientSampleDiffRpcName = echoClientSample.withRegionTag(echoClientSample.regionTag().toBuilder().setRpcName("createB").build());
List<Sample> samples = Arrays.asList(echoClientSample, echoClientSample, echoClientSampleDiffRpcName);
assertEquals(samples.size(), 3);
assertEquals(samples.get(0).name(), "SyncCreate");
assertEquals(samples.get(1).name(), "SyncCreate");
assertEquals(samples.get(2).name(), "SyncCreateB");
List<Sample> result = SampleComposerUtil.handleDuplicateSamples(samples);
assertEquals(result.size(), 2);
assertTrue(result.contains(echoClientSample));
assertTrue(result.contains(echoClientSampleDiffRpcName));
}
Aggregations