|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package io.clientcore.annotation.processor.test; |
| 5 | + |
| 6 | +import io.clientcore.annotation.processor.test.implementation.TestInterfaceClientService; |
| 7 | +import io.clientcore.annotation.processor.test.implementation.models.Foo; |
| 8 | +import io.clientcore.annotation.processor.test.implementation.models.FooListResult; |
| 9 | +import io.clientcore.core.http.models.HttpHeaderName; |
| 10 | +import io.clientcore.core.http.models.HttpHeaders; |
| 11 | +import io.clientcore.core.http.models.HttpMethod; |
| 12 | +import io.clientcore.core.http.models.HttpRequest; |
| 13 | +import io.clientcore.core.http.models.PagedIterable; |
| 14 | +import io.clientcore.core.http.models.PagedResponse; |
| 15 | +import io.clientcore.core.http.models.RequestOptions; |
| 16 | +import io.clientcore.core.http.models.Response; |
| 17 | +import io.clientcore.core.http.models.ResponseBodyMode; |
| 18 | +import io.clientcore.core.http.pipeline.HttpPipeline; |
| 19 | +import io.clientcore.core.http.pipeline.HttpPipelineBuilder; |
| 20 | +import io.clientcore.core.models.binarydata.BinaryData; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import org.junit.jupiter.api.Disabled; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 30 | + |
| 31 | +/** |
| 32 | + * Test class for verifying the deserialization of paged responses and their handling in code generation. |
| 33 | + */ |
| 34 | +public class PagingOperationTests { |
| 35 | + private static final BinaryData FIRST_PAGE_RESPONSE = BinaryData.fromString("[{\"bar\":\"hello.world\",\"baz\":[\"hello\",\"hello.world\"],\"qux\":{\"a.b\":\"c.d\",\"bar.a\":\"ttyy\",\"bar.b\":\"uuzz\",\"hello\":\"world\"}}]"); |
| 36 | + private static final BinaryData NEXTLINK_RESPONSE = BinaryData.fromString("[{\"bar\":\"hello.world2\",\"additionalProperties\":{\"bar\":\"baz\",\"a.b\":\"c.d\",\"properties.bar\":\"barbar\"}}]"); |
| 37 | + |
| 38 | + /** |
| 39 | + * Verifies that a response containing a List is correctly handled when returning a List<Foo>. |
| 40 | + */ |
| 41 | + @Test |
| 42 | + public void testListFoo() { |
| 43 | + String uri = "https://example.com"; |
| 44 | + String firstPageUri = uri + "/foos"; |
| 45 | + String nextLinkUri = uri + "/foos?page=2"; |
| 46 | + RequestOptions requestOptions = new RequestOptions().setResponseBodyMode(ResponseBodyMode.DESERIALIZE); |
| 47 | + HttpPipeline pipeline = new HttpPipelineBuilder() |
| 48 | + .httpClient(request -> { |
| 49 | + String requestUri = request.getUri().toString(); |
| 50 | + request.setRequestOptions(requestOptions); |
| 51 | + if (firstPageUri.equals(requestUri)) { |
| 52 | + return createMockResponse(request, 200, FIRST_PAGE_RESPONSE, nextLinkUri); |
| 53 | + } else if (nextLinkUri.equals(requestUri)) { |
| 54 | + return createMockResponse(request, 200, NEXTLINK_RESPONSE, null); |
| 55 | + } |
| 56 | + |
| 57 | + return new MockHttpResponse(request, 404); |
| 58 | + }) |
| 59 | + .build(); |
| 60 | + TestInterfaceClientService testInterface = TestInterfaceClientService.getNewInstance(pipeline, null); |
| 61 | + |
| 62 | + // Retrieve initial response |
| 63 | + Response<List<Foo>> initialResponse = testInterface.listFoo(uri, RequestOptions.none()); |
| 64 | + |
| 65 | + List<Foo> fooFirstPageResponse = initialResponse.getValue(); |
| 66 | + assertNotNull(fooFirstPageResponse); |
| 67 | + assertNotNull(fooFirstPageResponse.get(0).bar()); |
| 68 | + |
| 69 | + // Convert List<Foo> response to PagedResponse<Foo> |
| 70 | + PagedResponse<Foo> firstPage = toPagedResponse(initialResponse, null); |
| 71 | + |
| 72 | + PagedIterable<Foo> pagedIterable = new PagedIterable<>( |
| 73 | + pagingOptions -> firstPage, // First page |
| 74 | + (pagingOptions, nextLink) -> { |
| 75 | + Response<List<Foo>> nextResponse = testInterface.listNextFoo(nextLink, RequestOptions.none()); |
| 76 | + return toPagedResponse(nextResponse, nextLink); |
| 77 | + } |
| 78 | + ); |
| 79 | + |
| 80 | + assertNotNull(pagedIterable); |
| 81 | + Set<Foo> allItems = pagedIterable.stream().collect(Collectors.toSet()); |
| 82 | + assertEquals(1, allItems.size()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testListFooListResult() { |
| 87 | + String uri = "https://example.com"; |
| 88 | + String firstPageUri = uri + "/foos"; |
| 89 | + String nextLinkUri = uri + "/foos?page=2"; |
| 90 | + RequestOptions requestOptions = new RequestOptions().setResponseBodyMode(ResponseBodyMode.DESERIALIZE); |
| 91 | + HttpPipeline pipeline = new HttpPipelineBuilder() |
| 92 | + .httpClient(request -> { |
| 93 | + String requestUri = request.getUri().toString(); |
| 94 | + request.setRequestOptions(requestOptions); |
| 95 | + if (firstPageUri.equals(requestUri)) { |
| 96 | + return createMockResponse(request, 200, BinaryData.fromString( |
| 97 | + "{\"items\":[{\"bar\":\"hello.world\",\"baz\":[\"hello\",\"hello.world\"],\"qux\":{\"a" + |
| 98 | + ".b\":\"c.d\"," + |
| 99 | + "\"bar.a\":\"ttyy\",\"bar.b\":\"uuzz\",\"hello\":\"world\"}}], \"nextLink\":\"" + nextLinkUri + "\"}"), |
| 100 | + nextLinkUri); |
| 101 | + } else if (nextLinkUri.equals(requestUri)) { |
| 102 | + return createMockResponse(request, 200, BinaryData.fromString( |
| 103 | + "{\"items\":[{\"bar\":\"hello.world2\",\"additionalProperties\":{\"bar\":\"baz\",\"a" + |
| 104 | + ".b\":\"c.d\",\"properties.bar\":\"barbar\"}}]"), |
| 105 | + null); |
| 106 | + } |
| 107 | + return new MockHttpResponse(request, 404); |
| 108 | + }) |
| 109 | + .build(); |
| 110 | + |
| 111 | + TestInterfaceClientService testInterface = TestInterfaceClientService.getNewInstance(pipeline, null); |
| 112 | + |
| 113 | + // Fetch the first page |
| 114 | + PagedIterable<Foo> pagedIterable = new PagedIterable<>( |
| 115 | + pagingOptions -> toPagedResponse(testInterface.listFooListResult(uri, RequestOptions.none()), nextLinkUri), |
| 116 | + (pagingOptions, nextLink) -> toPagedResponse(testInterface.listNextFooListResult(nextLink, RequestOptions.none()), null) |
| 117 | + ); |
| 118 | + |
| 119 | + assertNotNull(pagedIterable); |
| 120 | + Set<Foo> allItems = pagedIterable.stream().collect(Collectors.toSet()); |
| 121 | + assertEquals(1, allItems.size()); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Creates a mock HTTP response with JSON body and optional nextLink header. |
| 126 | + */ |
| 127 | + private MockHttpResponse createMockResponse(HttpRequest request, int statusCode, BinaryData jsonBody, String nextLink) { |
| 128 | + HttpHeaders headers = new HttpHeaders(); |
| 129 | + if (nextLink != null) { |
| 130 | + headers.set(HttpHeaderName.fromString("nextLink"), nextLink); |
| 131 | + } |
| 132 | + |
| 133 | + return new MockHttpResponse(request, statusCode, headers, jsonBody); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Converts a Response<T> to a PagedResponse<Foo>. |
| 138 | + * Supports both Response<FooListResult> and Response<List<Foo>>. |
| 139 | + */ |
| 140 | + @SuppressWarnings({ "unchecked", "cast" }) |
| 141 | + private <T> PagedResponse<Foo> toPagedResponse(Response<T> response, String nextLink) { |
| 142 | + if (response == null || response.getValue() == null) { |
| 143 | + return new PagedResponse<>( |
| 144 | + response != null ? response.getRequest() : new HttpRequest().setMethod(HttpMethod.GET).setUri("https://example.com"), |
| 145 | + 200, |
| 146 | + response != null ? response.getHeaders() : new HttpHeaders(), |
| 147 | + response != null ? response.getBody() : null, |
| 148 | + Collections.emptyList() // Return an empty list when null |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + List<Foo> items; |
| 153 | + if (response.getValue() instanceof FooListResult) { |
| 154 | + items = ((FooListResult) response.getValue()).getItems(); // Extract list from FooListResult |
| 155 | + nextLink = ((FooListResult) response.getValue()).getNextLink(); |
| 156 | + } else if (response.getValue() instanceof List) { |
| 157 | + items = (List<Foo>) response.getValue(); // Directly use the List<Foo> |
| 158 | + } else { |
| 159 | + throw new IllegalArgumentException("Unsupported response type: " + response.getValue().getClass().getName()); |
| 160 | + } |
| 161 | + |
| 162 | + return new PagedResponse<>( |
| 163 | + response.getRequest(), |
| 164 | + response.getStatusCode(), |
| 165 | + response.getHeaders(), |
| 166 | + response.getBody(), |
| 167 | + items, |
| 168 | + nextLink, |
| 169 | + null, |
| 170 | + null, |
| 171 | + null, |
| 172 | + null |
| 173 | + ); |
| 174 | + } |
| 175 | +} |
0 commit comments