Skip to content

Commit 75bedb4

Browse files
committed
Refactor integration test
1 parent 8160511 commit 75bedb4

File tree

3 files changed

+192
-75
lines changed

3 files changed

+192
-75
lines changed

legacy-sdk/src/test/java/com/squareup/square/legacy/SanityTest.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,55 @@ public void setup() throws Exception {
107107

108108
@Test
109109
public void testFileUpload() throws ApiException, IOException {
110-
CatalogApi api = client.getCatalogApi();
110+
int maxRetries = 3;
111+
int retryDelayMs = 2000; // 2 seconds between retries
112+
Exception lastException = null;
111113

112-
CatalogImage imageData =
113-
new CatalogImage.Builder().caption("Image for File Upload Test").build();
114+
for (int attempt = 1; attempt <= maxRetries; attempt++) {
115+
try {
116+
CatalogApi api = client.getCatalogApi();
114117

115-
CatalogObject image = new CatalogObject.Builder("IMAGE", "#java_sdk_test")
116-
.imageData(imageData)
117-
.build();
118+
CatalogImage imageData =
119+
new CatalogImage.Builder().caption("Image for File Upload Test").build();
120+
121+
CatalogObject image = new CatalogObject.Builder("IMAGE", "#java_sdk_test")
122+
.imageData(imageData)
123+
.build();
124+
125+
String idempotencyKey = UUID.randomUUID().toString();
126+
CreateCatalogImageRequest request = new CreateCatalogImageRequest.Builder(idempotencyKey, image).build();
127+
128+
String imgPath = Paths.get(System.getProperty("user.dir").toString(), "legacy-sdk/src/test/resources/square.png")
129+
.toString();
130+
File imageFile = new File(imgPath);
131+
132+
if (!imageFile.exists()) {
133+
throw new IOException("Test image file not found at: " + imgPath);
134+
}
135+
136+
CreateCatalogImageResponse result = api.createCatalogImage(request, new FileWrapper(imageFile, "image/jpeg"));
118137

119-
String idempotencyKey = UUID.randomUUID().toString();
120-
CreateCatalogImageRequest request = new CreateCatalogImageRequest.Builder(idempotencyKey, image).build();
138+
assertNotNull(result.getImage().getImageData().getUrl());
121139

122-
String imgPath = Paths.get(System.getProperty("user.dir").toString(), "src/test/resources/square.png")
123-
.toString();
124-
File imageFile = new File(imgPath);
140+
// Remove the image and clean up
141+
api.deleteCatalogObject(result.getImage().getId());
125142

126-
CreateCatalogImageResponse result = api.createCatalogImage(request, new FileWrapper(imageFile, "image/jpeg"));
143+
// If we get here, the test succeeded
144+
return;
127145

128-
assertNotNull(result.getImage().getImageData().getUrl());
146+
} catch (Exception e) {
147+
lastException = e;
148+
if (attempt < maxRetries) {
149+
// Only log retries, not the final failure
150+
System.err.println("Attempt " + attempt + " failed, retrying in " + retryDelayMs + "ms: " + e.getMessage());
151+
Thread.sleep(retryDelayMs);
152+
}
153+
}
154+
}
129155

130-
// Remove the image and clean up
131-
api.deleteCatalogObject(result.getImage().getId());
156+
// If we get here, all attempts failed
157+
System.err.println("Test failed after " + maxRetries + " attempts");
158+
throw lastException;
132159
}
133160

134161
@Test

legacy-sdk/src/test/java/com/squareup/square/legacy/api/LocationsApiTest.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,35 @@ public static void tearDownClass() {
4646
*/
4747
@Test
4848
public void testListLocations() throws Exception {
49+
int maxRetries = 3;
50+
int retryDelayMs = 2000; // 2 seconds between retries
51+
Exception lastException = null;
4952

50-
// Set callback and perform API call
51-
try {
52-
controller.listLocations();
53-
} catch (ApiException e) {
54-
// Empty block
53+
for (int attempt = 1; attempt <= maxRetries; attempt++) {
54+
try {
55+
// Set callback and perform API call
56+
controller.listLocations();
57+
58+
// Test whether the response is null
59+
assertNotNull("Response is null", httpResponse.getResponse());
60+
// Test response code
61+
assertEquals("Status is not 200", 200, httpResponse.getResponse().getStatusCode());
62+
63+
// If we get here, the test succeeded
64+
return;
65+
66+
} catch (Exception e) {
67+
lastException = e;
68+
if (attempt < maxRetries) {
69+
// Only log retries, not the final failure
70+
System.err.println("Attempt " + attempt + " failed, retrying in " + retryDelayMs + "ms");
71+
Thread.sleep(retryDelayMs);
72+
}
73+
}
5574
}
5675

57-
// Test whether the response is null
58-
assertNotNull("Response is null", httpResponse.getResponse());
59-
// Test response code
60-
assertEquals("Status is not 200", 200, httpResponse.getResponse().getStatusCode());
76+
// If we get here, all attempts failed
77+
System.err.println("Test failed after " + maxRetries + " attempts");
78+
throw lastException;
6179
}
62-
}
80+
}

src/test/java/com/squareup/square/integration/LaborTest.java

Lines changed: 122 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
import com.squareup.square.labor.types.SearchShiftsRequest;
1313
import com.squareup.square.labor.types.UpdateBreakTypeRequest;
1414
import com.squareup.square.labor.types.UpdateShiftRequest;
15+
import com.squareup.square.types.Location;
16+
import com.squareup.square.types.SearchTeamMembersRequest;
17+
import com.squareup.square.types.SearchTeamMembersQuery;
18+
import com.squareup.square.types.SearchTeamMembersFilter;
19+
import com.squareup.square.types.TeamMemberStatus;
1520
import com.squareup.square.types.BreakType;
1621
import com.squareup.square.types.CreateBreakTypeResponse;
1722
import com.squareup.square.types.CreateShiftResponse;
18-
import com.squareup.square.types.CreateTeamMemberRequest;
19-
import com.squareup.square.types.CreateTeamMemberResponse;
2023
import com.squareup.square.types.DeleteBreakTypeResponse;
2124
import com.squareup.square.types.DeleteShiftResponse;
2225
import com.squareup.square.types.GetBreakTypeResponse;
@@ -28,8 +31,11 @@
2831
import com.squareup.square.types.UpdateBreakTypeResponse;
2932
import com.squareup.square.types.UpdateShiftResponse;
3033
import com.squareup.square.types.WorkweekConfig;
34+
3135
import java.time.OffsetDateTime;
3236
import java.time.format.DateTimeFormatter;
37+
import java.util.Collections;
38+
import java.util.List;
3339
import java.util.Optional;
3440
import java.util.UUID;
3541
import org.junit.jupiter.api.AfterEach;
@@ -47,21 +53,32 @@ public class LaborTest {
4753
@BeforeEach
4854
public void before() {
4955
client = TestClientFactory.create();
50-
locationId = Helpers.createLocation(client);
51-
52-
// Create team member for testing
53-
CreateTeamMemberResponse teamResponse = client.teamMembers()
54-
.create(CreateTeamMemberRequest.builder()
55-
.idempotencyKey(UUID.randomUUID().toString())
56-
.teamMember(TeamMember.builder()
57-
.givenName("Sherlock")
58-
.familyName("Holmes")
59-
.build())
60-
.build());
61-
if (!teamResponse.getTeamMember().get().getId().isPresent()) {
62-
throw new RuntimeException("Failed to create team member.");
56+
57+
// Get first available location
58+
List<Location> locations = client.locations().list().getLocations()
59+
.orElseThrow(() -> new RuntimeException("No locations available"));
60+
if (locations.isEmpty()) {
61+
throw new RuntimeException("No locations available for testing");
62+
}
63+
locationId = locations.get(0).getId().orElseThrow(() -> new RuntimeException("Location ID not present"));
64+
65+
// Get first available team member at this location
66+
List<TeamMember> teamMembers = client.teamMembers().search(
67+
SearchTeamMembersRequest.builder()
68+
.query(SearchTeamMembersQuery.builder()
69+
.filter(SearchTeamMembersFilter.builder()
70+
.locationIds(Collections.singletonList(locationId))
71+
.status(TeamMemberStatus.ACTIVE)
72+
.build())
73+
.build())
74+
.build()
75+
).getTeamMembers()
76+
.orElseThrow(() -> new RuntimeException("Failed to get team members"));
77+
78+
if (teamMembers.isEmpty()) {
79+
throw new RuntimeException("No team members available at location " + locationId);
6380
}
64-
memberId = teamResponse.getTeamMember().get().getId().get();
81+
memberId = teamMembers.get(0).getId().orElseThrow(() -> new RuntimeException("Team member ID not present"));
6582

6683
// Create break type for testing
6784
CreateBreakTypeResponse breakResponse = client.labor()
@@ -209,46 +226,101 @@ public void testUpdateShift() {
209226
}
210227

211228
@Test
212-
public void testDeleteShift() {
213-
// create team member
214-
CreateTeamMemberResponse teamMemberResponse = client.teamMembers()
215-
.create(CreateTeamMemberRequest.builder()
216-
.idempotencyKey(UUID.randomUUID().toString())
217-
.teamMember(TeamMember.builder()
218-
.givenName("Sherlock")
219-
.familyName("Holmes")
220-
.build())
221-
.build());
229+
public void testDeleteShift() throws Exception {
230+
int maxRetries = 3;
231+
int retryDelayMs = 2000; // 2 seconds between retries
232+
Exception lastException = null;
222233

223-
// create shift
224-
CreateShiftResponse shiftResponse = client.labor()
225-
.shifts()
226-
.create(CreateShiftRequest.builder()
227-
.shift(Shift.builder()
228-
.locationId(locationId)
229-
.startAt(OffsetDateTime.now().format(DateTimeFormatter.ISO_INSTANT))
230-
.teamMemberId(
231-
teamMemberResponse.getTeamMember().get().getId())
234+
for (int attempt = 1; attempt <= maxRetries; attempt++) {
235+
try {
236+
// First search for existing shifts for this team member using query filter
237+
SearchShiftsRequest searchRequest = SearchShiftsRequest.builder()
238+
.query(com.squareup.square.types.ShiftQuery.builder()
239+
.filter(com.squareup.square.types.ShiftFilter.builder()
240+
.teamMemberIds(Collections.singletonList(memberId))
241+
.build())
232242
.build())
233-
.idempotencyKey(UUID.randomUUID().toString())
234-
.build());
243+
.limit(100)
244+
.build();
235245

236-
if (!shiftResponse.getShift().isPresent()) {
237-
throw new RuntimeException("Failed to create shift.");
246+
SearchShiftsResponse existingShifts = client.labor()
247+
.shifts()
248+
.search(searchRequest);
249+
250+
// Delete any existing shifts
251+
if (existingShifts.getShifts().isPresent()) {
252+
for (Shift existingShift : existingShifts.getShifts().get()) {
253+
if (existingShift.getId().isPresent()) {
254+
client.labor()
255+
.shifts()
256+
.delete(DeleteShiftsRequest.builder().id(existingShift.getId().get()).build());
257+
}
258+
}
259+
}
260+
261+
// Add a small delay after cleanup
262+
Thread.sleep(1000);
263+
264+
// Start the shift 10 seconds from now and end it 20 seconds from now
265+
OffsetDateTime startTime = OffsetDateTime.now().plusSeconds(10);
266+
OffsetDateTime endTime = startTime.plusSeconds(10); // Very short shift for testing
267+
268+
// Create shift
269+
CreateShiftResponse shiftResponse = client.labor()
270+
.shifts()
271+
.create(CreateShiftRequest.builder()
272+
.shift(Shift.builder()
273+
.locationId(locationId)
274+
.startAt(startTime.format(DateTimeFormatter.ISO_INSTANT))
275+
.teamMemberId(memberId)
276+
.endAt(endTime.format(DateTimeFormatter.ISO_INSTANT))
277+
.build())
278+
.idempotencyKey(UUID.randomUUID().toString())
279+
.build());
280+
281+
if (!shiftResponse.getShift().isPresent()) {
282+
throw new RuntimeException("Failed to create shift: shift response is empty");
283+
}
284+
285+
Optional<Shift> shift = shiftResponse.getShift();
286+
if (!shift.get().getId().isPresent()) {
287+
throw new RuntimeException("Shift ID is null in response: " + shift);
288+
}
289+
290+
String testShiftId = shift.get().getId().get();
291+
292+
// Add a small delay to ensure the shift is fully created
293+
Thread.sleep(1000);
294+
295+
DeleteShiftResponse response = client.labor()
296+
.shifts()
297+
.delete(DeleteShiftsRequest.builder().id(testShiftId).build());
298+
Assertions.assertNotNull(response);
299+
300+
// If we get here, the test succeeded
301+
return;
302+
303+
} catch (Exception e) {
304+
lastException = e;
305+
if (attempt < maxRetries) {
306+
// Only log retries, not the final failure
307+
System.err.println("Attempt " + attempt + " failed, retrying in " + retryDelayMs + "ms");
308+
Thread.sleep(retryDelayMs);
309+
}
310+
}
238311
}
239-
if (!shiftResponse.getShift().get().getId().isPresent()) {
240-
throw new RuntimeException("Shift ID is null.");
312+
313+
// If we get here, all attempts failed
314+
System.err.println("Test failed after " + maxRetries + " attempts");
315+
if (lastException instanceof com.squareup.square.core.SquareApiException) {
316+
System.err.println("Final API Error details: " + lastException.getMessage());
241317
}
242-
shiftId = shiftResponse.getShift().get().getId().get();
243-
DeleteShiftResponse response = client.labor()
244-
.shifts()
245-
.delete(DeleteShiftsRequest.builder().id(shiftId).build());
246-
Assertions.assertNotNull(response);
318+
throw lastException;
247319
}
248320

249321
@Test
250322
public void testDeleteBreakType() {
251-
// create break type
323+
// Create break type
252324
CreateBreakTypeResponse breakResponse = client.labor()
253325
.breakTypes()
254326
.create(CreateBreakTypeRequest.builder()
@@ -268,11 +340,11 @@ public void testDeleteBreakType() {
268340
if (!breakType.get().getId().isPresent()) {
269341
throw new RuntimeException("Break ID is null.");
270342
}
271-
breakId = breakType.get().getId().get();
343+
String testBreakId = breakType.get().getId().get();
272344

273345
DeleteBreakTypeResponse response = client.labor()
274346
.breakTypes()
275-
.delete(DeleteBreakTypesRequest.builder().id(breakId).build());
347+
.delete(DeleteBreakTypesRequest.builder().id(testBreakId).build());
276348
Assertions.assertNotNull(response);
277349
}
278350

@@ -283,4 +355,4 @@ public void testListWorkweekConfigs() {
283355
.list(ListWorkweekConfigsRequest.builder().build());
284356
Assertions.assertFalse(response.getItems().isEmpty());
285357
}
286-
}
358+
}

0 commit comments

Comments
 (0)