Skip to content

Commit 92fd81a

Browse files
committed
Refactor integration test
1 parent 8160511 commit 92fd81a

File tree

1 file changed

+79
-52
lines changed

1 file changed

+79
-52
lines changed

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

Lines changed: 79 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");
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()
@@ -210,45 +227,55 @@ public void testUpdateShift() {
210227

211228
@Test
212229
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());
230+
try {
231+
// Create shift with both start and end time
232+
OffsetDateTime now = OffsetDateTime.now();
233+
CreateShiftResponse shiftResponse = client.labor()
234+
.shifts()
235+
.create(CreateShiftRequest.builder()
236+
.shift(Shift.builder()
237+
.locationId(locationId)
238+
.startAt(now.format(DateTimeFormatter.ISO_INSTANT))
239+
.teamMemberId(memberId)
240+
.endAt(now.plusHours(8).format(DateTimeFormatter.ISO_INSTANT)) // Add end time
241+
.wage(ShiftWage.builder()
242+
.title("Regular")
243+
.hourlyRate(Helpers.newTestMoney(1500))
244+
.build())
245+
.build())
246+
.idempotencyKey(UUID.randomUUID().toString())
247+
.build());
235248

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

249276
@Test
250277
public void testDeleteBreakType() {
251-
// create break type
278+
// Create break type
252279
CreateBreakTypeResponse breakResponse = client.labor()
253280
.breakTypes()
254281
.create(CreateBreakTypeRequest.builder()
@@ -268,11 +295,11 @@ public void testDeleteBreakType() {
268295
if (!breakType.get().getId().isPresent()) {
269296
throw new RuntimeException("Break ID is null.");
270297
}
271-
breakId = breakType.get().getId().get();
298+
String testBreakId = breakType.get().getId().get();
272299

273300
DeleteBreakTypeResponse response = client.labor()
274301
.breakTypes()
275-
.delete(DeleteBreakTypesRequest.builder().id(breakId).build());
302+
.delete(DeleteBreakTypesRequest.builder().id(testBreakId).build());
276303
Assertions.assertNotNull(response);
277304
}
278305

@@ -283,4 +310,4 @@ public void testListWorkweekConfigs() {
283310
.list(ListWorkweekConfigsRequest.builder().build());
284311
Assertions.assertFalse(response.getItems().isEmpty());
285312
}
286-
}
313+
}

0 commit comments

Comments
 (0)