Skip to content

Commit 1f091a6

Browse files
committed
Refactor integration test
1 parent 8160511 commit 1f091a6

File tree

1 file changed

+77
-53
lines changed

1 file changed

+77
-53
lines changed

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

Lines changed: 77 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010
import com.squareup.square.labor.types.GetShiftsRequest;
1111
import com.squareup.square.labor.types.ListWorkweekConfigsRequest;
1212
import com.squareup.square.labor.types.SearchShiftsRequest;
13+
import com.squareup.square.labor.types.SearchShiftsQuery;
14+
import com.squareup.square.labor.types.ShiftFilter;
1315
import com.squareup.square.labor.types.UpdateBreakTypeRequest;
1416
import com.squareup.square.labor.types.UpdateShiftRequest;
17+
import com.squareup.square.types.Location;
18+
import com.squareup.square.types.SearchTeamMembersRequest;
19+
import com.squareup.square.types.SearchTeamMembersQuery;
20+
import com.squareup.square.types.SearchTeamMembersFilter;
21+
import com.squareup.square.types.TeamMemberStatus;
1522
import com.squareup.square.types.BreakType;
1623
import com.squareup.square.types.CreateBreakTypeResponse;
1724
import com.squareup.square.types.CreateShiftResponse;
18-
import com.squareup.square.types.CreateTeamMemberRequest;
19-
import com.squareup.square.types.CreateTeamMemberResponse;
2025
import com.squareup.square.types.DeleteBreakTypeResponse;
2126
import com.squareup.square.types.DeleteShiftResponse;
2227
import com.squareup.square.types.GetBreakTypeResponse;
@@ -28,8 +33,11 @@
2833
import com.squareup.square.types.UpdateBreakTypeResponse;
2934
import com.squareup.square.types.UpdateShiftResponse;
3035
import com.squareup.square.types.WorkweekConfig;
36+
3137
import java.time.OffsetDateTime;
3238
import java.time.format.DateTimeFormatter;
39+
import java.util.Collections;
40+
import java.util.List;
3341
import java.util.Optional;
3442
import java.util.UUID;
3543
import org.junit.jupiter.api.AfterEach;
@@ -47,21 +55,32 @@ public class LaborTest {
4755
@BeforeEach
4856
public void before() {
4957
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.");
58+
59+
// Get first available location
60+
List<Location> locations = client.locations().list().getLocations()
61+
.orElseThrow(() -> new RuntimeException("No locations available"));
62+
if (locations.isEmpty()) {
63+
throw new RuntimeException("No locations available for testing");
64+
}
65+
locationId = locations.get(0).getId().orElseThrow(() -> new RuntimeException("Location ID not present"));
66+
67+
// Get first available team member at this location
68+
List<TeamMember> teamMembers = client.teamMembers().search(
69+
SearchTeamMembersRequest.builder()
70+
.query(SearchTeamMembersQuery.builder()
71+
.filter(SearchTeamMembersFilter.builder()
72+
.locationIds(Collections.singletonList(locationId))
73+
.status(TeamMemberStatus.ACTIVE)
74+
.build())
75+
.build())
76+
.build()
77+
).getTeamMembers()
78+
.orElseThrow(() -> new RuntimeException("Failed to get team members"));
79+
80+
if (teamMembers.isEmpty()) {
81+
throw new RuntimeException("No team members available at location " + locationId);
6382
}
64-
memberId = teamResponse.getTeamMember().get().getId().get();
83+
memberId = teamMembers.get(0).getId().orElseThrow(() -> new RuntimeException("Team member ID not present"));
6584

6685
// Create break type for testing
6786
CreateBreakTypeResponse breakResponse = client.labor()
@@ -209,46 +228,51 @@ public void testUpdateShift() {
209228
}
210229

211230
@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());
222-
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());
231+
public void testDeleteShift() throws Exception {
232+
try {
233+
// Create shift starting tomorrow to avoid conflicts with existing shifts
234+
OffsetDateTime startTime = OffsetDateTime.now().plusDays(1).withHour(9).withMinute(0).withSecond(0);
235+
CreateShiftResponse shiftResponse = client.labor()
236+
.shifts()
237+
.create(CreateShiftRequest.builder()
238+
.shift(Shift.builder()
239+
.locationId(locationId)
240+
.startAt(startTime.format(DateTimeFormatter.ISO_INSTANT))
241+
.teamMemberId(memberId)
242+
.build())
243+
.idempotencyKey(UUID.randomUUID().toString())
244+
.build());
235245

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.");
246+
if (!shiftResponse.getShift().isPresent()) {
247+
throw new RuntimeException("Failed to create shift: shift response is empty");
248+
}
249+
250+
Optional<Shift> shift = shiftResponse.getShift();
251+
if (!shift.get().getId().isPresent()) {
252+
throw new RuntimeException("Shift ID is null in response: " + shift);
253+
}
254+
255+
String testShiftId = shift.get().getId().get();
256+
System.out.println("Created shift with ID: " + testShiftId);
257+
258+
// Add a small delay to ensure the shift is fully created
259+
Thread.sleep(1000);
260+
261+
DeleteShiftResponse response = client.labor()
262+
.shifts()
263+
.delete(DeleteShiftsRequest.builder().id(testShiftId).build());
264+
Assertions.assertNotNull(response);
265+
266+
} catch (Exception e) {
267+
System.err.println("Test failed with exception: " + e.getMessage());
268+
e.printStackTrace();
269+
throw e;
241270
}
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);
247271
}
248272

249273
@Test
250274
public void testDeleteBreakType() {
251-
// create break type
275+
// Create break type
252276
CreateBreakTypeResponse breakResponse = client.labor()
253277
.breakTypes()
254278
.create(CreateBreakTypeRequest.builder()
@@ -268,11 +292,11 @@ public void testDeleteBreakType() {
268292
if (!breakType.get().getId().isPresent()) {
269293
throw new RuntimeException("Break ID is null.");
270294
}
271-
breakId = breakType.get().getId().get();
295+
String testBreakId = breakType.get().getId().get();
272296

273297
DeleteBreakTypeResponse response = client.labor()
274298
.breakTypes()
275-
.delete(DeleteBreakTypesRequest.builder().id(breakId).build());
299+
.delete(DeleteBreakTypesRequest.builder().id(testBreakId).build());
276300
Assertions.assertNotNull(response);
277301
}
278302

@@ -283,4 +307,4 @@ public void testListWorkweekConfigs() {
283307
.list(ListWorkweekConfigsRequest.builder().build());
284308
Assertions.assertFalse(response.getItems().isEmpty());
285309
}
286-
}
310+
}

0 commit comments

Comments
 (0)