Skip to content

Commit 4546089

Browse files
Add digital twins test for pagination of event routes (#15869)
1 parent af97f53 commit 4546089

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed

sdk/digitaltwins/azure-digitaltwins-core/src/test/java/com/azure/digitaltwins/core/EventRoutesAsyncTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package com.azure.digitaltwins.core;
22

33
import com.azure.core.http.HttpClient;
4+
import com.azure.core.http.rest.PagedResponse;
45
import com.azure.core.util.logging.ClientLogger;
56
import com.azure.digitaltwins.core.models.EventRoute;
7+
import com.azure.digitaltwins.core.models.EventRoutesListOptions;
68
import org.junit.jupiter.params.ParameterizedTest;
79
import org.junit.jupiter.params.provider.MethodSource;
10+
import reactor.core.publisher.Flux;
811
import reactor.test.StepVerifier;
912

1013
import java.net.HttpURLConnection;
14+
import java.util.concurrent.atomic.AtomicBoolean;
15+
import java.util.concurrent.atomic.AtomicInteger;
1116

1217
import static com.azure.digitaltwins.core.TestHelper.DISPLAY_NAME_WITH_ARGUMENTS;
1318
import static com.azure.digitaltwins.core.TestHelper.assertRestException;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
1421

1522
/**
1623
* Tests for the async client's event route APIs
@@ -81,4 +88,40 @@ public void createEventRouteThrowsIfFilterIsMalformed(HttpClient httpClient, Dig
8188
StepVerifier.create(asyncClient.createEventRoute(eventRouteId, eventRouteToCreate))
8289
.verifyErrorSatisfies(ex -> assertRestException(ex, HttpURLConnection.HTTP_BAD_REQUEST));
8390
}
91+
92+
@ParameterizedTest(name = DISPLAY_NAME_WITH_ARGUMENTS)
93+
@MethodSource("com.azure.digitaltwins.core.TestHelper#getTestParameters")
94+
@Override
95+
public void listEventRoutesPaginationWorks(HttpClient httpClient, DigitalTwinsServiceVersion serviceVersion) {
96+
DigitalTwinsAsyncClient asyncClient = getAsyncClient(httpClient, serviceVersion);
97+
final int eventRouteCountToCreate = 5;
98+
final int expectedPageSize = 2;
99+
100+
// create enough event routes so that the list API can have multiple pages
101+
for (int i = 0; i < eventRouteCountToCreate; i++) {
102+
String eventRouteId = testResourceNamer.randomUuid();
103+
EventRoute eventRouteToCreate = new EventRoute(EVENT_ROUTE_ENDPOINT_NAME);
104+
eventRouteToCreate.setFilter(FILTER);
105+
StepVerifier.create(asyncClient.createEventRoute(eventRouteId, eventRouteToCreate))
106+
.verifyComplete();
107+
}
108+
109+
// list event routes by page, make sure that all non-final pages have the expected page size
110+
AtomicInteger pageCount = new AtomicInteger(0);
111+
EventRoutesListOptions eventRoutesListOptions = (new EventRoutesListOptions()).setMaxItemCount(expectedPageSize);
112+
StepVerifier.create(asyncClient.listEventRoutes(eventRoutesListOptions).byPage())
113+
.thenConsumeWhile(
114+
(pagedResponseOfEventRoute) -> pagedResponseOfEventRoute != null,
115+
(pagedResponseOfEventRoute) -> {
116+
pageCount.incrementAndGet();
117+
118+
// Any page of results with a continuation token should be a non-final page, and should have the exact page size that we specified above
119+
if (pagedResponseOfEventRoute.getContinuationToken() != null) {
120+
assertEquals(expectedPageSize, pagedResponseOfEventRoute.getValue().size());
121+
}
122+
})
123+
.verifyComplete();
124+
125+
assertTrue(pageCount.get() >= 3, "At least three pages should have been returned.");
126+
}
84127
}

sdk/digitaltwins/azure-digitaltwins-core/src/test/java/com/azure/digitaltwins/core/EventRoutesTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
import com.azure.core.http.HttpClient;
44
import com.azure.core.http.rest.PagedIterable;
5+
import com.azure.core.http.rest.PagedResponse;
6+
import com.azure.core.util.Context;
57
import com.azure.core.util.logging.ClientLogger;
68
import com.azure.digitaltwins.core.models.EventRoute;
9+
import com.azure.digitaltwins.core.models.EventRoutesListOptions;
710
import org.junit.jupiter.params.ParameterizedTest;
811
import org.junit.jupiter.params.provider.MethodSource;
912

1013
import java.net.HttpURLConnection;
1114

1215
import static com.azure.digitaltwins.core.TestHelper.DISPLAY_NAME_WITH_ARGUMENTS;
1316
import static com.azure.digitaltwins.core.TestHelper.assertRestException;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
1419

1520
/**
1621
* Tests for the sync client's event route APIs
@@ -75,4 +80,37 @@ public void createEventRouteThrowsIfFilterIsMalformed(HttpClient httpClient, Dig
7580

7681
assertRestException(() -> client.createEventRoute(eventRouteId, eventRouteToCreate), HttpURLConnection.HTTP_BAD_REQUEST);
7782
}
83+
84+
@ParameterizedTest(name = DISPLAY_NAME_WITH_ARGUMENTS)
85+
@MethodSource("com.azure.digitaltwins.core.TestHelper#getTestParameters")
86+
@Override
87+
public void listEventRoutesPaginationWorks(HttpClient httpClient, DigitalTwinsServiceVersion serviceVersion) {
88+
DigitalTwinsClient client = getClient(httpClient, serviceVersion);
89+
final int eventRouteCountToCreate = 5;
90+
final int expectedPageSize = 2;
91+
92+
// create enough event routes so that the list API can have multiple pages
93+
for (int i = 0; i < eventRouteCountToCreate; i++) {
94+
String eventRouteId = testResourceNamer.randomUuid();
95+
EventRoute eventRouteToCreate = new EventRoute(EVENT_ROUTE_ENDPOINT_NAME);
96+
eventRouteToCreate.setFilter(FILTER);
97+
client.createEventRoute(eventRouteId, eventRouteToCreate);
98+
}
99+
100+
// list event routes by page, make sure that all non-final pages have the expected page size
101+
EventRoutesListOptions eventRoutesListOptions = (new EventRoutesListOptions()).setMaxItemCount(expectedPageSize);
102+
PagedIterable<EventRoute> eventRoutes = client.listEventRoutes(eventRoutesListOptions, Context.NONE);
103+
Iterable<PagedResponse<EventRoute>> eventRoutePages = eventRoutes.iterableByPage();
104+
int pageCount = 0;
105+
for (PagedResponse<EventRoute> eventRoutePagedResponse : eventRoutePages) {
106+
pageCount++;
107+
108+
// Any page of results with a continuation token should be a non-final page, and should have the exact page size that we specified above
109+
if (eventRoutePagedResponse.getContinuationToken() != null) {
110+
assertEquals(expectedPageSize, eventRoutePagedResponse.getValue().size());
111+
}
112+
}
113+
114+
assertTrue(pageCount >= 3, "At least three pages should have been returned.");
115+
}
78116
}

sdk/digitaltwins/azure-digitaltwins-core/src/test/java/com/azure/digitaltwins/core/EventRoutesTestBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public abstract class EventRoutesTestBase extends DigitalTwinsTestBase {
3232
@Test
3333
public abstract void createEventRouteThrowsIfFilterIsMalformed(HttpClient httpClient, DigitalTwinsServiceVersion serviceVersion);
3434

35+
@Test
36+
public abstract void listEventRoutesPaginationWorks(HttpClient httpClient, DigitalTwinsServiceVersion serviceVersion);
37+
3538
// Azure Digital Twins instances have a low cap on the number of event routes allowed, so we need to delete the existing
3639
// event routes before each test to make sure that we can add an event route in each test.
3740
@BeforeEach
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{
2+
"networkCallRecords" : [ {
3+
"Method" : "GET",
4+
"Uri" : "https://REDACTED.api.wus2.digitaltwins.azure.net/eventroutes?api-version=2020-05-31-preview",
5+
"Headers" : {
6+
"User-Agent" : "azsdk-java-azure-digitaltwins-core/1.0.0-beta.3 (11.0.8; Windows 10; 10.0)",
7+
"x-ms-client-request-id" : "fc4f1981-f140-4a5a-a9bf-0f6cdbcdd515"
8+
},
9+
"Response" : {
10+
"Strict-Transport-Security" : "max-age=2592000",
11+
"retry-after" : "0",
12+
"Content-Length" : "28",
13+
"StatusCode" : "200",
14+
"Body" : "{\"value\":[],\"nextLink\":null}",
15+
"Date" : "Thu, 01 Oct 2020 21:29:28 GMT",
16+
"Content-Type" : "application/json; charset=utf-8"
17+
},
18+
"Exception" : null
19+
}, {
20+
"Method" : "PUT",
21+
"Uri" : "https://REDACTED.api.wus2.digitaltwins.azure.net/eventroutes/860ec2a5-ade0-4d0c-9545-0bb533b91eb0?api-version=2020-05-31-preview",
22+
"Headers" : {
23+
"User-Agent" : "azsdk-java-azure-digitaltwins-core/1.0.0-beta.3 (11.0.8; Windows 10; 10.0)",
24+
"x-ms-client-request-id" : "66026f8b-b1fc-4278-a452-82387d6b90a4",
25+
"Content-Type" : "application/json"
26+
},
27+
"Response" : {
28+
"Strict-Transport-Security" : "max-age=2592000",
29+
"retry-after" : "0",
30+
"Content-Length" : "0",
31+
"StatusCode" : "204",
32+
"Date" : "Thu, 01 Oct 2020 21:29:29 GMT"
33+
},
34+
"Exception" : null
35+
}, {
36+
"Method" : "PUT",
37+
"Uri" : "https://REDACTED.api.wus2.digitaltwins.azure.net/eventroutes/0ca3d236-73fb-4de1-964a-361bc37d8682?api-version=2020-05-31-preview",
38+
"Headers" : {
39+
"User-Agent" : "azsdk-java-azure-digitaltwins-core/1.0.0-beta.3 (11.0.8; Windows 10; 10.0)",
40+
"x-ms-client-request-id" : "76394e83-387e-4d09-84c8-41667a94681e",
41+
"Content-Type" : "application/json"
42+
},
43+
"Response" : {
44+
"Strict-Transport-Security" : "max-age=2592000",
45+
"retry-after" : "0",
46+
"Content-Length" : "0",
47+
"StatusCode" : "204",
48+
"Date" : "Thu, 01 Oct 2020 21:29:29 GMT"
49+
},
50+
"Exception" : null
51+
}, {
52+
"Method" : "PUT",
53+
"Uri" : "https://REDACTED.api.wus2.digitaltwins.azure.net/eventroutes/245740f0-9508-4fb6-90a3-2dc1acadcc4f?api-version=2020-05-31-preview",
54+
"Headers" : {
55+
"User-Agent" : "azsdk-java-azure-digitaltwins-core/1.0.0-beta.3 (11.0.8; Windows 10; 10.0)",
56+
"x-ms-client-request-id" : "4414bd90-7e81-414b-87d0-597cf8d16ddf",
57+
"Content-Type" : "application/json"
58+
},
59+
"Response" : {
60+
"Strict-Transport-Security" : "max-age=2592000",
61+
"retry-after" : "0",
62+
"Content-Length" : "0",
63+
"StatusCode" : "204",
64+
"Date" : "Thu, 01 Oct 2020 21:29:29 GMT"
65+
},
66+
"Exception" : null
67+
}, {
68+
"Method" : "PUT",
69+
"Uri" : "https://REDACTED.api.wus2.digitaltwins.azure.net/eventroutes/2579f7f0-3716-47ba-aafe-bfbd69798bd1?api-version=2020-05-31-preview",
70+
"Headers" : {
71+
"User-Agent" : "azsdk-java-azure-digitaltwins-core/1.0.0-beta.3 (11.0.8; Windows 10; 10.0)",
72+
"x-ms-client-request-id" : "6597899a-60f7-440e-a538-896f1dd7ddd6",
73+
"Content-Type" : "application/json"
74+
},
75+
"Response" : {
76+
"Strict-Transport-Security" : "max-age=2592000",
77+
"retry-after" : "0",
78+
"Content-Length" : "0",
79+
"StatusCode" : "204",
80+
"Date" : "Thu, 01 Oct 2020 21:29:29 GMT"
81+
},
82+
"Exception" : null
83+
}, {
84+
"Method" : "PUT",
85+
"Uri" : "https://REDACTED.api.wus2.digitaltwins.azure.net/eventroutes/1a106057-b192-494c-a993-b888812ed3b7?api-version=2020-05-31-preview",
86+
"Headers" : {
87+
"User-Agent" : "azsdk-java-azure-digitaltwins-core/1.0.0-beta.3 (11.0.8; Windows 10; 10.0)",
88+
"x-ms-client-request-id" : "5a36401c-6801-4297-9ea7-518f1cc89870",
89+
"Content-Type" : "application/json"
90+
},
91+
"Response" : {
92+
"Strict-Transport-Security" : "max-age=2592000",
93+
"retry-after" : "0",
94+
"Content-Length" : "0",
95+
"StatusCode" : "204",
96+
"Date" : "Thu, 01 Oct 2020 21:29:28 GMT"
97+
},
98+
"Exception" : null
99+
}, {
100+
"Method" : "GET",
101+
"Uri" : "https://REDACTED.api.wus2.digitaltwins.azure.net/eventroutes?api-version=2020-05-31-preview",
102+
"Headers" : {
103+
"User-Agent" : "azsdk-java-azure-digitaltwins-core/1.0.0-beta.3 (11.0.8; Windows 10; 10.0)",
104+
"x-ms-client-request-id" : "0feeec4b-ef03-4363-9845-4beaeb9e4f19"
105+
},
106+
"Response" : {
107+
"Strict-Transport-Security" : "max-age=2592000",
108+
"retry-after" : "0",
109+
"Content-Length" : "640",
110+
"StatusCode" : "200",
111+
"Body" : "{\"value\":[{\"id\":\"860ec2a5-ade0-4d0c-9545-0bb533b91eb0\",\"endpointName\":\"someEventHubEndpoint\",\"filter\":\"$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'\"},{\"id\":\"0ca3d236-73fb-4de1-964a-361bc37d8682\",\"endpointName\":\"someEventHubEndpoint\",\"filter\":\"$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'\"}],\"nextLink\":\"/EventRoutes?continuationToken=%5B%7B%22token%22%3A%22%2BRID%3A~9cpWAPHTRta0uwEAAAAAAA%3D%3D%23RT%3A1%23TRC%3A2%23ISV%3A2%23IEO%3A65551%22,%22range%22%3A%7B%22min%22%3A%22%22,%22max%22%3A%22FF%22%7D%7D%5D&api-version=2020-05-31-preview\"}",
112+
"Date" : "Thu, 01 Oct 2020 21:29:29 GMT",
113+
"Content-Type" : "application/json; charset=utf-8"
114+
},
115+
"Exception" : null
116+
}, {
117+
"Method" : "GET",
118+
"Uri" : "https://REDACTED.api.wus2.digitaltwins.azure.net/EventRoutes?continuationToken=%5B%7B%22token%22%3A%22%2BRID%3A~9cpWAPHTRta0uwEAAAAAAA%3D%3D%23RT%3A1%23TRC%3A2%23ISV%3A2%23IEO%3A65551%22,%22range%22%3A%7B%22min%22%3A%22%22,%22max%22%3A%22FF%22%7D%7D%5D&api-version=2020-05-31-preview",
119+
"Headers" : {
120+
"User-Agent" : "azsdk-java-azure-digitaltwins-core/1.0.0-beta.3 (11.0.8; Windows 10; 10.0)",
121+
"x-ms-client-request-id" : "c6e67624-0be6-4ad9-8438-19f679194140"
122+
},
123+
"Response" : {
124+
"Strict-Transport-Security" : "max-age=2592000",
125+
"retry-after" : "0",
126+
"Content-Length" : "640",
127+
"StatusCode" : "200",
128+
"Body" : "{\"value\":[{\"id\":\"245740f0-9508-4fb6-90a3-2dc1acadcc4f\",\"endpointName\":\"someEventHubEndpoint\",\"filter\":\"$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'\"},{\"id\":\"2579f7f0-3716-47ba-aafe-bfbd69798bd1\",\"endpointName\":\"someEventHubEndpoint\",\"filter\":\"$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'\"}],\"nextLink\":\"/EventRoutes?continuationToken=%5B%7B%22token%22%3A%22%2BRID%3A~9cpWAPHTRta2uwEAAAAAAA%3D%3D%23RT%3A2%23TRC%3A4%23ISV%3A2%23IEO%3A65551%22,%22range%22%3A%7B%22min%22%3A%22%22,%22max%22%3A%22FF%22%7D%7D%5D&api-version=2020-05-31-preview\"}",
129+
"Date" : "Thu, 01 Oct 2020 21:29:29 GMT",
130+
"Content-Type" : "application/json; charset=utf-8"
131+
},
132+
"Exception" : null
133+
}, {
134+
"Method" : "GET",
135+
"Uri" : "https://REDACTED.api.wus2.digitaltwins.azure.net/EventRoutes?continuationToken=%5B%7B%22token%22%3A%22%2BRID%3A~9cpWAPHTRta2uwEAAAAAAA%3D%3D%23RT%3A2%23TRC%3A4%23ISV%3A2%23IEO%3A65551%22,%22range%22%3A%7B%22min%22%3A%22%22,%22max%22%3A%22FF%22%7D%7D%5D&api-version=2020-05-31-preview",
136+
"Headers" : {
137+
"User-Agent" : "azsdk-java-azure-digitaltwins-core/1.0.0-beta.3 (11.0.8; Windows 10; 10.0)",
138+
"x-ms-client-request-id" : "e7e55d2e-2d85-41c3-9147-4f4676f7c5db"
139+
},
140+
"Response" : {
141+
"Strict-Transport-Security" : "max-age=2592000",
142+
"retry-after" : "0",
143+
"Content-Length" : "217",
144+
"StatusCode" : "200",
145+
"Body" : "{\"value\":[{\"id\":\"1a106057-b192-494c-a993-b888812ed3b7\",\"endpointName\":\"someEventHubEndpoint\",\"filter\":\"$eventType = 'DigitalTwinTelemetryMessages' or $eventType = 'DigitalTwinLifecycleNotification'\"}],\"nextLink\":null}",
146+
"Date" : "Thu, 01 Oct 2020 21:29:29 GMT",
147+
"Content-Type" : "application/json; charset=utf-8"
148+
},
149+
"Exception" : null
150+
} ],
151+
"variables" : [ "860ec2a5-ade0-4d0c-9545-0bb533b91eb0", "0ca3d236-73fb-4de1-964a-361bc37d8682", "245740f0-9508-4fb6-90a3-2dc1acadcc4f", "2579f7f0-3716-47ba-aafe-bfbd69798bd1", "1a106057-b192-494c-a993-b888812ed3b7" ]
152+
}

0 commit comments

Comments
 (0)