Skip to content

Commit e820968

Browse files
authored
Refactor integration test (#151)
1 parent 8160511 commit e820968

File tree

3 files changed

+110
-57
lines changed

3 files changed

+110
-57
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,17 @@ public void testFileUpload() throws ApiException, IOException {
110110
CatalogApi api = client.getCatalogApi();
111111

112112
CatalogImage imageData =
113-
new CatalogImage.Builder().caption("Image for File Upload Test").build();
113+
new CatalogImage.Builder().caption("Image for File Upload Test").build();
114114

115115
CatalogObject image = new CatalogObject.Builder("IMAGE", "#java_sdk_test")
116-
.imageData(imageData)
117-
.build();
116+
.imageData(imageData)
117+
.build();
118118

119119
String idempotencyKey = UUID.randomUUID().toString();
120120
CreateCatalogImageRequest request = new CreateCatalogImageRequest.Builder(idempotencyKey, image).build();
121121

122122
String imgPath = Paths.get(System.getProperty("user.dir").toString(), "src/test/resources/square.png")
123-
.toString();
123+
.toString();
124124
File imageFile = new File(imgPath);
125125

126126
CreateCatalogImageResponse result = api.createCatalogImage(request, new FileWrapper(imageFile, "image/jpeg"));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ public void testListLocations() throws Exception {
5959
// Test response code
6060
assertEquals("Status is not 200", 200, httpResponse.getResponse().getStatusCode());
6161
}
62-
}
62+
}

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

Lines changed: 105 additions & 52 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");
6362
}
64-
memberId = teamResponse.getTeamMember().get().getId().get();
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);
80+
}
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,82 @@ 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+
try {
231+
// First search for existing shifts for this team member using query filter
232+
SearchShiftsRequest searchRequest = SearchShiftsRequest.builder()
233+
.query(com.squareup.square.types.ShiftQuery.builder()
234+
.filter(com.squareup.square.types.ShiftFilter.builder()
235+
.teamMemberIds(Collections.singletonList(memberId))
236+
.build())
237+
.build())
238+
.limit(100)
239+
.build();
222240

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())
232-
.build())
233-
.idempotencyKey(UUID.randomUUID().toString())
234-
.build());
241+
SearchShiftsResponse existingShifts = client.labor()
242+
.shifts()
243+
.search(searchRequest);
235244

236-
if (!shiftResponse.getShift().isPresent()) {
237-
throw new RuntimeException("Failed to create shift.");
238-
}
239-
if (!shiftResponse.getShift().get().getId().isPresent()) {
240-
throw new RuntimeException("Shift ID is null.");
245+
// Delete any existing shifts
246+
if (existingShifts.getShifts().isPresent()) {
247+
for (Shift existingShift : existingShifts.getShifts().get()) {
248+
if (existingShift.getId().isPresent()) {
249+
client.labor()
250+
.shifts()
251+
.delete(DeleteShiftsRequest.builder().id(existingShift.getId().get()).build());
252+
}
253+
}
254+
}
255+
256+
// Start the shift 10 seconds from now and end it 20 seconds from now
257+
OffsetDateTime startTime = OffsetDateTime.now().plusSeconds(10);
258+
OffsetDateTime endTime = startTime.plusSeconds(10); // Very short shift for testing
259+
260+
// Create shift
261+
CreateShiftResponse shiftResponse = client.labor()
262+
.shifts()
263+
.create(CreateShiftRequest.builder()
264+
.shift(Shift.builder()
265+
.locationId(locationId)
266+
.startAt(startTime.format(DateTimeFormatter.ISO_INSTANT))
267+
.teamMemberId(memberId)
268+
.endAt(endTime.format(DateTimeFormatter.ISO_INSTANT))
269+
.build())
270+
.idempotencyKey(UUID.randomUUID().toString())
271+
.build());
272+
273+
if (!shiftResponse.getShift().isPresent()) {
274+
throw new RuntimeException("Failed to create shift: shift response is empty");
275+
}
276+
277+
Optional<Shift> shift = shiftResponse.getShift();
278+
if (!shift.get().getId().isPresent()) {
279+
throw new RuntimeException("Shift ID is null in response: " + shift);
280+
}
281+
282+
String testShiftId = shift.get().getId().get();
283+
284+
// Add a small delay to ensure the shift is fully created
285+
Thread.sleep(1000);
286+
287+
DeleteShiftResponse response = client.labor()
288+
.shifts()
289+
.delete(DeleteShiftsRequest.builder().id(testShiftId).build());
290+
Assertions.assertNotNull(response);
291+
292+
} catch (Exception e) {
293+
System.err.println("Test failed with exception: " + e.getMessage());
294+
if (e instanceof com.squareup.square.core.SquareApiException) {
295+
System.err.println("API Error details: " + e.getMessage());
296+
}
297+
e.printStackTrace();
298+
throw e;
241299
}
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);
247300
}
248301

249302
@Test
250303
public void testDeleteBreakType() {
251-
// create break type
304+
// Create break type
252305
CreateBreakTypeResponse breakResponse = client.labor()
253306
.breakTypes()
254307
.create(CreateBreakTypeRequest.builder()
@@ -268,11 +321,11 @@ public void testDeleteBreakType() {
268321
if (!breakType.get().getId().isPresent()) {
269322
throw new RuntimeException("Break ID is null.");
270323
}
271-
breakId = breakType.get().getId().get();
324+
String testBreakId = breakType.get().getId().get();
272325

273326
DeleteBreakTypeResponse response = client.labor()
274327
.breakTypes()
275-
.delete(DeleteBreakTypesRequest.builder().id(breakId).build());
328+
.delete(DeleteBreakTypesRequest.builder().id(testBreakId).build());
276329
Assertions.assertNotNull(response);
277330
}
278331

@@ -283,4 +336,4 @@ public void testListWorkweekConfigs() {
283336
.list(ListWorkweekConfigsRequest.builder().build());
284337
Assertions.assertFalse(response.getItems().isEmpty());
285338
}
286-
}
339+
}

0 commit comments

Comments
 (0)