|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.api.generator.gapic.composer.grpcrest; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | +import static org.junit.Assert.assertTrue; |
| 19 | + |
| 20 | +import com.google.api.generator.gapic.composer.common.TestProtoLoader; |
| 21 | +import com.google.api.generator.gapic.model.GapicContext; |
| 22 | +import com.google.api.generator.gapic.model.GapicServiceConfig; |
| 23 | +import com.google.api.generator.gapic.model.Message; |
| 24 | +import com.google.api.generator.gapic.model.ResourceName; |
| 25 | +import com.google.api.generator.gapic.model.Service; |
| 26 | +import com.google.api.generator.gapic.model.Transport; |
| 27 | +import com.google.api.generator.gapic.protoparser.Parser; |
| 28 | +import com.google.api.generator.gapic.protoparser.ServiceConfigParser; |
| 29 | +import com.google.longrunning.OperationsProto; |
| 30 | +import com.google.protobuf.Descriptors.FileDescriptor; |
| 31 | +import com.google.protobuf.Descriptors.ServiceDescriptor; |
| 32 | +import com.google.showcase.grpcrest.v1beta1.EchoGrpcrest; |
| 33 | +import java.nio.file.Path; |
| 34 | +import java.nio.file.Paths; |
| 35 | +import java.util.HashSet; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Optional; |
| 39 | +import java.util.Set; |
| 40 | + |
| 41 | +public class GrpcRestTestProtoLoader extends TestProtoLoader { |
| 42 | + private static final GrpcRestTestProtoLoader INSTANCE = new GrpcRestTestProtoLoader(); |
| 43 | + |
| 44 | + protected GrpcRestTestProtoLoader() { |
| 45 | + super(Transport.GRPC_REST, "src/test/resources/"); |
| 46 | + } |
| 47 | + |
| 48 | + public static GrpcRestTestProtoLoader instance() { |
| 49 | + return INSTANCE; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public GapicContext parseShowcaseEcho() { |
| 54 | + FileDescriptor echoFileDescriptor = EchoGrpcrest.getDescriptor(); |
| 55 | + |
| 56 | + ServiceDescriptor echoServiceDescriptor = echoFileDescriptor.getServices().get(0); |
| 57 | + assertEquals("Echo", echoServiceDescriptor.getName()); |
| 58 | + |
| 59 | + Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor); |
| 60 | + Map<String, Message> operationMessageTypes = |
| 61 | + Parser.parseMessages(OperationsProto.getDescriptor()); |
| 62 | + messageTypes.putAll(operationMessageTypes); |
| 63 | + Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor); |
| 64 | + Set<ResourceName> outputResourceNames = new HashSet<>(); |
| 65 | + List<Service> services = |
| 66 | + Parser.parseService( |
| 67 | + echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); |
| 68 | + |
| 69 | + String jsonFilename = "showcase_grpc_service_config.json"; |
| 70 | + Path jsonPath = Paths.get(getTestFilesDirectory(), jsonFilename); |
| 71 | + Optional<GapicServiceConfig> configOpt = ServiceConfigParser.parse(jsonPath.toString()); |
| 72 | + assertTrue(configOpt.isPresent()); |
| 73 | + GapicServiceConfig config = configOpt.get(); |
| 74 | + |
| 75 | + return GapicContext.builder() |
| 76 | + .setMessages(messageTypes) |
| 77 | + .setResourceNames(resourceNames) |
| 78 | + .setServices(services) |
| 79 | + .setServiceConfig(config) |
| 80 | + .setHelperResourceNames(outputResourceNames) |
| 81 | + .setTransport(getTransport()) |
| 82 | + .build(); |
| 83 | + } |
| 84 | +} |
0 commit comments