diff --git a/src/main/resources/com/google/api/codegen/java/main.snip b/src/main/resources/com/google/api/codegen/java/main.snip index e8ec777819..311cc20f33 100644 --- a/src/main/resources/com/google/api/codegen/java/main.snip +++ b/src/main/resources/com/google/api/codegen/java/main.snip @@ -357,7 +357,7 @@ // AUTO-GENERATED DOCUMENTATION AND METHOD /** @join commentLine : apiMethodDoc.mainDocLines - {@""} * {@commentLine} + {@""} * {@commentLine} @end * * Sample code: @@ -368,7 +368,7 @@ * @if apiMethodDoc.paramDocs {@""} * - @join doc : apiMethodDoc.paramDocs + @join doc : apiMethodDoc.paramDocs {@paramDoc(doc)} @end @end @@ -406,5 +406,13 @@ @end @private pathTemplateCheckLine(pathTemplateCheck) - {@pathTemplateCheck.pathTemplateName}.validate({@pathTemplateCheck.paramName}, "{@pathTemplateCheck.validationMessageContext}"); + @if pathTemplateCheck.allowEmptyString + if (!{@pathTemplateCheck.paramName}.isEmpty()) { + {@pathTemplateCheck.pathTemplateName}.validate({@pathTemplateCheck.paramName}, \ + "{@pathTemplateCheck.validationMessageContext}"); + } + @else + {@pathTemplateCheck.pathTemplateName}.validate({@pathTemplateCheck.paramName}, \ + "{@pathTemplateCheck.validationMessageContext}"); + @end @end diff --git a/src/main/resources/com/google/api/codegen/java/mock_service.snip b/src/main/resources/com/google/api/codegen/java/mock_service.snip index c53170de95..9bb43d7f1f 100644 --- a/src/main/resources/com/google/api/codegen/java/mock_service.snip +++ b/src/main/resources/com/google/api/codegen/java/mock_service.snip @@ -22,6 +22,11 @@ return serviceImpl.getRequests(); } + @@Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + @@Override public ServerServiceDefinition getServiceDefinition() { return {@mockService.grpcContainerName}.bindService(serviceImpl); diff --git a/src/main/resources/com/google/api/codegen/java/mock_service_impl.snip b/src/main/resources/com/google/api/codegen/java/mock_service_impl.snip index 0785a078b2..0e9ae65266 100644 --- a/src/main/resources/com/google/api/codegen/java/mock_service_impl.snip +++ b/src/main/resources/com/google/api/codegen/java/mock_service_impl.snip @@ -11,42 +11,41 @@ @@javax.annotation.Generated("by GAPIC") public class {@mockServiceImpl.name} implements {@mockServiceImpl.grpcClassName} { - private ArrayList requests; + private ArrayList requests; + private Queue responses; - public {@mockServiceImpl.name}() { - requests = new ArrayList<>(); - } + public {@mockServiceImpl.name}() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } - public List getRequests() { - return requests; - } + public List getRequests() { + return requests; + } - public void reset() { - requests = new ArrayList<>(); - } + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } - @join method : mockServiceImpl.grpcMethods - {@grpcMethod(method)} + @join method : mockServiceImpl.grpcMethods + {@grpcMethod(method)} - @end + @end } @end @private grpcMethod(method) @@Override - @if method.isStreaming - public StreamObserver<{@method.requestTypeName}> {@method.name}( - StreamObserver<{@method.responseTypeName}> responseObserver) { - System.err.println("Streaming method is not supported."); - return null; - } - @else - public void {@method.name}({@method.requestTypeName} request, - StreamObserver<{@method.responseTypeName}> responseObserver) { - {@method.responseTypeName} response = ({@method.responseTypeName}) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - @end + public void {@method.name}({@method.requestTypeName} request, + StreamObserver<{@method.responseTypeName}> responseObserver) { + {@method.responseTypeName} response = ({@method.responseTypeName}) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } @end diff --git a/src/main/resources/com/google/api/codegen/java/test.snip b/src/main/resources/com/google/api/codegen/java/test.snip index e44f4a44b9..80425dfba9 100644 --- a/src/main/resources/com/google/api/codegen/java/test.snip +++ b/src/main/resources/com/google/api/codegen/java/test.snip @@ -52,31 +52,48 @@ @@Test @@SuppressWarnings("all") public void {@test.name}() { + {@setResponse(test.mockResponse)} + {@initCode(test.initCode)} {@methodCall(test)} - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - {@test.requestTypeName} request = ({@test.requestTypeName})requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + {@test.requestTypeName} actualRequest = ({@test.requestTypeName})actualRequests.get(0); @join assert : test.asserts - Assert.assertEquals(request.{@assert.actualValueGetter}(), {@assert.expectedValueIdentifier}); + Assert.assertEquals(actualRequest.{@assert.actualValueGetter}(), {@assert.expectedValueIdentifier}); @end } @end +@private setResponse(response) + {@initCode(response.initCode)} + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); +@end + @private methodCall(test) @switch test.methodType @case "PagedFlattenedMethod" - PageAccessor<{@test.resourceTypeName}> response = api.{@test.surfaceMethodName}(\ + PageAccessor<{@test.resourceTypeName}> pageAccessor = api.{@test.surfaceMethodName}(\ {@sampleMethodCallArgList(test.initCode.fieldSettings)}); // PageAccessor will not make actual request until it is being used. // Add all the pages here in order to make grpc requests. - List<{@test.resourceTypeName}> resources = Lists.newArrayList(response.getPageValues()); + List<{@test.resourceTypeName}> resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(0, resources.size()); @case "FlattenedMethod" - api.{@test.surfaceMethodName}(\ - {@sampleMethodCallArgList(test.initCode.fieldSettings)}); + @if test.hasReturnValue + {@test.responseTypeName} actualResponse = + api.{@test.surfaceMethodName}(\ + {@sampleMethodCallArgList(test.initCode.fieldSettings)}); + Assert.assertEquals(expectedResponse, actualResponse); + @else + api.{@test.surfaceMethodName}(\ + {@sampleMethodCallArgList(test.initCode.fieldSettings)}); + @end @default $unhandledCase: {@test.type}$ @end diff --git a/src/test/java/com/google/api/codegen/testdata/java_mock_service_impl_library.baseline b/src/test/java/com/google/api/codegen/testdata/java_mock_service_impl_library.baseline index c4f07836e4..6555f980d2 100644 --- a/src/test/java/com/google/api/codegen/testdata/java_mock_service_impl_library.baseline +++ b/src/test/java/com/google/api/codegen/testdata/java_mock_service_impl_library.baseline @@ -51,178 +51,187 @@ import java.util.Queue; @javax.annotation.Generated("by GAPIC") public class MockLibraryServiceImpl implements LibraryService { - private ArrayList requests; - - public MockLibraryServiceImpl() { - requests = new ArrayList<>(); - } - - public List getRequests() { - return requests; - } - - public void reset() { - requests = new ArrayList<>(); - } - - @Override - public void createShelf(CreateShelfRequest request, - StreamObserver responseObserver) { - Shelf response = (Shelf) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void getShelf(GetShelfRequest request, - StreamObserver responseObserver) { - Shelf response = (Shelf) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void listShelves(ListShelvesRequest request, - StreamObserver responseObserver) { - ListShelvesResponse response = (ListShelvesResponse) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void deleteShelf(DeleteShelfRequest request, - StreamObserver responseObserver) { - Empty response = (Empty) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void mergeShelves(MergeShelvesRequest request, - StreamObserver responseObserver) { - Shelf response = (Shelf) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void createBook(CreateBookRequest request, - StreamObserver responseObserver) { - Book response = (Book) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void publishSeries(PublishSeriesRequest request, - StreamObserver responseObserver) { - PublishSeriesResponse response = (PublishSeriesResponse) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void getBook(GetBookRequest request, - StreamObserver responseObserver) { - Book response = (Book) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void listBooks(ListBooksRequest request, - StreamObserver responseObserver) { - ListBooksResponse response = (ListBooksResponse) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void deleteBook(DeleteBookRequest request, - StreamObserver responseObserver) { - Empty response = (Empty) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void updateBook(UpdateBookRequest request, - StreamObserver responseObserver) { - Book response = (Book) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void moveBook(MoveBookRequest request, - StreamObserver responseObserver) { - Book response = (Book) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void listStrings(ListStringsRequest request, - StreamObserver responseObserver) { - ListStringsResponse response = (ListStringsResponse) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void addComments(AddCommentsRequest request, - StreamObserver responseObserver) { - Empty response = (Empty) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void getBookFromArchive(GetBookFromArchiveRequest request, - StreamObserver responseObserver) { - Book response = (Book) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public void updateBookIndex(UpdateBookIndexRequest request, - StreamObserver responseObserver) { - Empty response = (Empty) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } - - @Override - public StreamObserver streamShelves( - StreamObserver responseObserver) { - System.err.println("Streaming method is not supported."); - return null; - } - - @Override - public void addTag(AddTagRequest request, - StreamObserver responseObserver) { - AddTagResponse response = (AddTagResponse) responses.remove(); - requests.add(request); - responseObserver.onNext(response); - responseObserver.onCompleted(); - } + private ArrayList requests; + private Queue responses; + + public MockLibraryServiceImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void createShelf(CreateShelfRequest request, + StreamObserver responseObserver) { + Shelf response = (Shelf) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void getShelf(GetShelfRequest request, + StreamObserver responseObserver) { + Shelf response = (Shelf) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void listShelves(ListShelvesRequest request, + StreamObserver responseObserver) { + ListShelvesResponse response = (ListShelvesResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void deleteShelf(DeleteShelfRequest request, + StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void mergeShelves(MergeShelvesRequest request, + StreamObserver responseObserver) { + Shelf response = (Shelf) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void createBook(CreateBookRequest request, + StreamObserver responseObserver) { + Book response = (Book) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void publishSeries(PublishSeriesRequest request, + StreamObserver responseObserver) { + PublishSeriesResponse response = (PublishSeriesResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void getBook(GetBookRequest request, + StreamObserver responseObserver) { + Book response = (Book) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void listBooks(ListBooksRequest request, + StreamObserver responseObserver) { + ListBooksResponse response = (ListBooksResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void deleteBook(DeleteBookRequest request, + StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void updateBook(UpdateBookRequest request, + StreamObserver responseObserver) { + Book response = (Book) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void moveBook(MoveBookRequest request, + StreamObserver responseObserver) { + Book response = (Book) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void listStrings(ListStringsRequest request, + StreamObserver responseObserver) { + ListStringsResponse response = (ListStringsResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void addComments(AddCommentsRequest request, + StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void getBookFromArchive(GetBookFromArchiveRequest request, + StreamObserver responseObserver) { + Book response = (Book) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void updateBookIndex(UpdateBookIndexRequest request, + StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void streamShelves(ListShelvesRequest request, + StreamObserver responseObserver) { + ListShelvesResponse response = (ListShelvesResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void addTag(AddTagRequest request, + StreamObserver responseObserver) { + AddTagResponse response = (AddTagResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } } diff --git a/src/test/java/com/google/api/codegen/testdata/java_mock_service_library.baseline b/src/test/java/com/google/api/codegen/testdata/java_mock_service_library.baseline index b711297e1a..b0b2052e37 100644 --- a/src/test/java/com/google/api/codegen/testdata/java_mock_service_library.baseline +++ b/src/test/java/com/google/api/codegen/testdata/java_mock_service_library.baseline @@ -34,6 +34,11 @@ public class MockLibraryService implements MockGrpcService { return serviceImpl.getRequests(); } + @Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + @Override public ServerServiceDefinition getServiceDefinition() { return LibraryServiceGrpc.bindService(serviceImpl); diff --git a/src/test/java/com/google/api/codegen/testdata/java_test_library.baseline b/src/test/java/com/google/api/codegen/testdata/java_test_library.baseline index 881ba1dbb5..2ed3f28d56 100644 --- a/src/test/java/com/google/api/codegen/testdata/java_test_library.baseline +++ b/src/test/java/com/google/api/codegen/testdata/java_test_library.baseline @@ -94,250 +94,377 @@ public class LibraryServiceTest { @Test @SuppressWarnings("all") public void createShelfTest() { + Shelf expectedResponse = Shelf.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + Shelf shelf = Shelf.newBuilder().build(); - api.createShelf(shelf); + Shelf actualResponse = + api.createShelf(shelf); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - CreateShelfRequest request = (CreateShelfRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + CreateShelfRequest actualRequest = (CreateShelfRequest)actualRequests.get(0); - Assert.assertEquals(request.getShelf(), shelf); + Assert.assertEquals(actualRequest.getShelf(), shelf); } @Test @SuppressWarnings("all") public void getShelfTest() { + Shelf expectedResponse = Shelf.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatShelfName("[SHELF]"); - api.getShelf(formattedName); + Shelf actualResponse = + api.getShelf(formattedName); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - GetShelfRequest request = (GetShelfRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetShelfRequest actualRequest = (GetShelfRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); + Assert.assertEquals(actualRequest.getName(), formattedName); } @Test @SuppressWarnings("all") public void getShelfTest2() { + Shelf expectedResponse = Shelf.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatShelfName("[SHELF]"); SomeMessage message = SomeMessage.newBuilder().build(); - api.getShelf(formattedName, message); + Shelf actualResponse = + api.getShelf(formattedName, message); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - GetShelfRequest request = (GetShelfRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetShelfRequest actualRequest = (GetShelfRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); - Assert.assertEquals(request.getMessage(), message); + Assert.assertEquals(actualRequest.getName(), formattedName); + Assert.assertEquals(actualRequest.getMessage(), message); } @Test @SuppressWarnings("all") public void getShelfTest3() { + Shelf expectedResponse = Shelf.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatShelfName("[SHELF]"); SomeMessage message = SomeMessage.newBuilder().build(); com.google.example.library.v1.StringBuilder stringBuilder = com.google.example.library.v1.StringBuilder.newBuilder().build(); - api.getShelf(formattedName, message, stringBuilder); + Shelf actualResponse = + api.getShelf(formattedName, message, stringBuilder); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - GetShelfRequest request = (GetShelfRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetShelfRequest actualRequest = (GetShelfRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); - Assert.assertEquals(request.getMessage(), message); - Assert.assertEquals(request.getStringBuilder(), stringBuilder); + Assert.assertEquals(actualRequest.getName(), formattedName); + Assert.assertEquals(actualRequest.getMessage(), message); + Assert.assertEquals(actualRequest.getStringBuilder(), stringBuilder); } @Test @SuppressWarnings("all") public void listShelvesTest() { + ListShelvesResponse expectedResponse = ListShelvesResponse.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); - PageAccessor response = api.listShelves(); + + PageAccessor pageAccessor = api.listShelves(); // PageAccessor will not make actual request until it is being used. // Add all the pages here in order to make grpc requests. - List resources = Lists.newArrayList(response.getPageValues()); + List resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(0, resources.size()); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - ListShelvesRequest request = (ListShelvesRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListShelvesRequest actualRequest = (ListShelvesRequest)actualRequests.get(0); } @Test @SuppressWarnings("all") public void deleteShelfTest() { + Empty expectedResponse = Empty.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatShelfName("[SHELF]"); api.deleteShelf(formattedName); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - DeleteShelfRequest request = (DeleteShelfRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + DeleteShelfRequest actualRequest = (DeleteShelfRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); + Assert.assertEquals(actualRequest.getName(), formattedName); } @Test @SuppressWarnings("all") public void mergeShelvesTest() { + Shelf expectedResponse = Shelf.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatShelfName("[SHELF]"); String formattedOtherShelfName = LibraryServiceApi.formatShelfName("[SHELF]"); - api.mergeShelves(formattedName, formattedOtherShelfName); + Shelf actualResponse = + api.mergeShelves(formattedName, formattedOtherShelfName); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - MergeShelvesRequest request = (MergeShelvesRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + MergeShelvesRequest actualRequest = (MergeShelvesRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); - Assert.assertEquals(request.getOtherShelfName(), formattedOtherShelfName); + Assert.assertEquals(actualRequest.getName(), formattedName); + Assert.assertEquals(actualRequest.getOtherShelfName(), formattedOtherShelfName); } @Test @SuppressWarnings("all") public void createBookTest() { + Book expectedResponse = Book.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatShelfName("[SHELF]"); Book book = Book.newBuilder().build(); - api.createBook(formattedName, book); + Book actualResponse = + api.createBook(formattedName, book); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - CreateBookRequest request = (CreateBookRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + CreateBookRequest actualRequest = (CreateBookRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); - Assert.assertEquals(request.getBook(), book); + Assert.assertEquals(actualRequest.getName(), formattedName); + Assert.assertEquals(actualRequest.getBook(), book); } @Test @SuppressWarnings("all") public void publishSeriesTest() { + PublishSeriesResponse expectedResponse = PublishSeriesResponse.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + Shelf shelf = Shelf.newBuilder().build(); List books = new ArrayList<>(); int edition = 0; - api.publishSeries(shelf, books, edition); + PublishSeriesResponse actualResponse = + api.publishSeries(shelf, books, edition); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - PublishSeriesRequest request = (PublishSeriesRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + PublishSeriesRequest actualRequest = (PublishSeriesRequest)actualRequests.get(0); - Assert.assertEquals(request.getShelf(), shelf); - Assert.assertEquals(request.getBooksList(), books); - Assert.assertEquals(request.getEdition(), edition); + Assert.assertEquals(actualRequest.getShelf(), shelf); + Assert.assertEquals(actualRequest.getBooksList(), books); + Assert.assertEquals(actualRequest.getEdition(), edition); } @Test @SuppressWarnings("all") public void getBookTest() { + Book expectedResponse = Book.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatBookName("[SHELF]", "[BOOK]"); - api.getBook(formattedName); + Book actualResponse = + api.getBook(formattedName); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - GetBookRequest request = (GetBookRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetBookRequest actualRequest = (GetBookRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); + Assert.assertEquals(actualRequest.getName(), formattedName); } @Test @SuppressWarnings("all") public void listBooksTest() { + ListBooksResponse expectedResponse = ListBooksResponse.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatShelfName("[SHELF]"); String filter = ""; - PageAccessor response = api.listBooks(formattedName, filter); + PageAccessor pageAccessor = api.listBooks(formattedName, filter); // PageAccessor will not make actual request until it is being used. // Add all the pages here in order to make grpc requests. - List resources = Lists.newArrayList(response.getPageValues()); + List resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(0, resources.size()); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - ListBooksRequest request = (ListBooksRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListBooksRequest actualRequest = (ListBooksRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); - Assert.assertEquals(request.getFilter(), filter); + Assert.assertEquals(actualRequest.getName(), formattedName); + Assert.assertEquals(actualRequest.getFilter(), filter); } @Test @SuppressWarnings("all") public void deleteBookTest() { + Empty expectedResponse = Empty.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatBookName("[SHELF]", "[BOOK]"); api.deleteBook(formattedName); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - DeleteBookRequest request = (DeleteBookRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + DeleteBookRequest actualRequest = (DeleteBookRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); + Assert.assertEquals(actualRequest.getName(), formattedName); } @Test @SuppressWarnings("all") public void updateBookTest() { + Book expectedResponse = Book.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatBookName("[SHELF]", "[BOOK]"); Book book = Book.newBuilder().build(); - api.updateBook(formattedName, book); + Book actualResponse = + api.updateBook(formattedName, book); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - UpdateBookRequest request = (UpdateBookRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + UpdateBookRequest actualRequest = (UpdateBookRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); - Assert.assertEquals(request.getBook(), book); + Assert.assertEquals(actualRequest.getName(), formattedName); + Assert.assertEquals(actualRequest.getBook(), book); } @Test @SuppressWarnings("all") public void updateBookTest2() { + Book expectedResponse = Book.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatBookName("[SHELF]", "[BOOK]"); Book book = Book.newBuilder().build(); FieldMask updateMask = FieldMask.newBuilder().build(); com.google.example.library.v1.FieldMask physicalMask = com.google.example.library.v1.FieldMask.newBuilder().build(); - api.updateBook(formattedName, book, updateMask, physicalMask); - - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - UpdateBookRequest request = (UpdateBookRequest)requests.get(0); - - Assert.assertEquals(request.getName(), formattedName); - Assert.assertEquals(request.getBook(), book); - Assert.assertEquals(request.getUpdateMask(), updateMask); - Assert.assertEquals(request.getPhysicalMask(), physicalMask); + Book actualResponse = + api.updateBook(formattedName, book, updateMask, physicalMask); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + UpdateBookRequest actualRequest = (UpdateBookRequest)actualRequests.get(0); + + Assert.assertEquals(actualRequest.getName(), formattedName); + Assert.assertEquals(actualRequest.getBook(), book); + Assert.assertEquals(actualRequest.getUpdateMask(), updateMask); + Assert.assertEquals(actualRequest.getPhysicalMask(), physicalMask); } @Test @SuppressWarnings("all") public void moveBookTest() { + Book expectedResponse = Book.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatBookName("[SHELF]", "[BOOK]"); String formattedOtherShelfName = LibraryServiceApi.formatShelfName("[SHELF]"); - api.moveBook(formattedName, formattedOtherShelfName); + Book actualResponse = + api.moveBook(formattedName, formattedOtherShelfName); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - MoveBookRequest request = (MoveBookRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + MoveBookRequest actualRequest = (MoveBookRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); - Assert.assertEquals(request.getOtherShelfName(), formattedOtherShelfName); + Assert.assertEquals(actualRequest.getName(), formattedName); + Assert.assertEquals(actualRequest.getOtherShelfName(), formattedOtherShelfName); } @Test @SuppressWarnings("all") public void listStringsTest() { + ListStringsResponse expectedResponse = ListStringsResponse.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + - PageAccessor response = api.listStrings(); + PageAccessor pageAccessor = api.listStrings(); // PageAccessor will not make actual request until it is being used. // Add all the pages here in order to make grpc requests. - List resources = Lists.newArrayList(response.getPageValues()); + List resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(0, resources.size()); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - ListStringsRequest request = (ListStringsRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListStringsRequest actualRequest = (ListStringsRequest)actualRequests.get(0); } @Test @SuppressWarnings("all") public void addCommentsTest() { + Empty expectedResponse = Empty.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatBookName("[SHELF]", "[BOOK]"); ByteString comment = ByteString.copyFromUtf8(""); Stage stage = Stage.UNSET; @@ -348,30 +475,44 @@ public class LibraryServiceTest { List comments = Arrays.asList(commentsElement); api.addComments(formattedName, comments); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - AddCommentsRequest request = (AddCommentsRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + AddCommentsRequest actualRequest = (AddCommentsRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); - Assert.assertEquals(request.getCommentsList(), comments); + Assert.assertEquals(actualRequest.getName(), formattedName); + Assert.assertEquals(actualRequest.getCommentsList(), comments); } @Test @SuppressWarnings("all") public void getBookFromArchiveTest() { + Book expectedResponse = Book.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatArchivedBookName("[ARCHIVE_PATH]", "[BOOK]"); - api.getBookFromArchive(formattedName); + Book actualResponse = + api.getBookFromArchive(formattedName); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - GetBookFromArchiveRequest request = (GetBookFromArchiveRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetBookFromArchiveRequest actualRequest = (GetBookFromArchiveRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); + Assert.assertEquals(actualRequest.getName(), formattedName); } @Test @SuppressWarnings("all") public void updateBookIndexTest() { + Empty expectedResponse = Empty.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedName = LibraryServiceApi.formatBookName("[SHELF]", "[BOOK]"); String indexName = "default index"; String indexMapItem = ""; @@ -379,28 +520,36 @@ public class LibraryServiceTest { indexMap.put("default_key", indexMapItem); api.updateBookIndex(formattedName, indexName, indexMap); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - UpdateBookIndexRequest request = (UpdateBookIndexRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + UpdateBookIndexRequest actualRequest = (UpdateBookIndexRequest)actualRequests.get(0); - Assert.assertEquals(request.getName(), formattedName); - Assert.assertEquals(request.getIndexName(), indexName); - Assert.assertEquals(request.getIndexMap(), indexMap); + Assert.assertEquals(actualRequest.getName(), formattedName); + Assert.assertEquals(actualRequest.getIndexName(), indexName); + Assert.assertEquals(actualRequest.getIndexMap(), indexMap); } @Test @SuppressWarnings("all") public void addTagTest() { + AddTagResponse expectedResponse = AddTagResponse.newBuilder() + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + serviceHelper.getService().setResponses(expectedResponses); + String formattedResource = LibraryServiceApi.formatBookName("[SHELF]", "[BOOK]"); String tag = ""; - api.addTag(formattedResource, tag); + AddTagResponse actualResponse = + api.addTag(formattedResource, tag); + Assert.assertEquals(expectedResponse, actualResponse); - List requests = serviceHelper.getService().getRequests(); - Assert.assertEquals(1, requests.size()); - AddTagRequest request = (AddTagRequest)requests.get(0); + List actualRequests = serviceHelper.getService().getRequests(); + Assert.assertEquals(1, actualRequests.size()); + AddTagRequest actualRequest = (AddTagRequest)actualRequests.get(0); - Assert.assertEquals(request.getResource(), formattedResource); - Assert.assertEquals(request.getTag(), tag); + Assert.assertEquals(actualRequest.getResource(), formattedResource); + Assert.assertEquals(actualRequest.getTag(), tag); } }