Skip to content

Commit f097450

Browse files
committed
Refactor integration test
1 parent 8160511 commit f097450

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

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

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
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;
1519
import com.squareup.square.types.BreakType;
1620
import com.squareup.square.types.CreateBreakTypeResponse;
1721
import com.squareup.square.types.CreateShiftResponse;
18-
import com.squareup.square.types.CreateTeamMemberRequest;
19-
import com.squareup.square.types.CreateTeamMemberResponse;
2022
import com.squareup.square.types.DeleteBreakTypeResponse;
2123
import com.squareup.square.types.DeleteShiftResponse;
2224
import com.squareup.square.types.GetBreakTypeResponse;
@@ -28,8 +30,11 @@
2830
import com.squareup.square.types.UpdateBreakTypeResponse;
2931
import com.squareup.square.types.UpdateShiftResponse;
3032
import com.squareup.square.types.WorkweekConfig;
33+
3134
import java.time.OffsetDateTime;
3235
import java.time.format.DateTimeFormatter;
36+
import java.util.Collections;
37+
import java.util.List;
3338
import java.util.Optional;
3439
import java.util.UUID;
3540
import org.junit.jupiter.api.AfterEach;
@@ -47,21 +52,30 @@ public class LaborTest {
4752
@BeforeEach
4853
public void before() {
4954
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.");
55+
56+
// Get first available location
57+
List<Location> locations = client.locations().list().getLocations();
58+
if (locations == null || locations.isEmpty()) {
59+
throw new RuntimeException("No locations available for testing");
60+
}
61+
locationId = locations.get(0).getId().orElseThrow(() -> new RuntimeException("Location ID not present"));
62+
63+
// Get first available team member at this location
64+
List<TeamMember> teamMembers = client.teamMembers().search(
65+
SearchTeamMembersRequest.builder()
66+
.query(SearchTeamMembersQuery.builder()
67+
.filter(SearchTeamMembersFilter.builder()
68+
.locationIds(Collections.singletonList(locationId))
69+
.status("ACTIVE")
70+
.build())
71+
.build())
72+
.build()
73+
).getTeamMembers();
74+
75+
if (teamMembers == null || teamMembers.isEmpty()) {
76+
throw new RuntimeException("No team members available at location " + locationId);
6377
}
64-
memberId = teamResponse.getTeamMember().get().getId().get();
78+
memberId = teamMembers.get(0).getId().orElseThrow(() -> new RuntimeException("Team member ID not present"));
6579

6680
// Create break type for testing
6781
CreateBreakTypeResponse breakResponse = client.labor()
@@ -210,25 +224,14 @@ public void testUpdateShift() {
210224

211225
@Test
212226
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
227+
// Create shift
224228
CreateShiftResponse shiftResponse = client.labor()
225229
.shifts()
226230
.create(CreateShiftRequest.builder()
227231
.shift(Shift.builder()
228232
.locationId(locationId)
229233
.startAt(OffsetDateTime.now().format(DateTimeFormatter.ISO_INSTANT))
230-
.teamMemberId(
231-
teamMemberResponse.getTeamMember().get().getId())
234+
.teamMemberId(memberId)
232235
.build())
233236
.idempotencyKey(UUID.randomUUID().toString())
234237
.build());
@@ -239,16 +242,16 @@ public void testDeleteShift() {
239242
if (!shiftResponse.getShift().get().getId().isPresent()) {
240243
throw new RuntimeException("Shift ID is null.");
241244
}
242-
shiftId = shiftResponse.getShift().get().getId().get();
245+
String testShiftId = shiftResponse.getShift().get().getId().get();
243246
DeleteShiftResponse response = client.labor()
244247
.shifts()
245-
.delete(DeleteShiftsRequest.builder().id(shiftId).build());
248+
.delete(DeleteShiftsRequest.builder().id(testShiftId).build());
246249
Assertions.assertNotNull(response);
247250
}
248251

249252
@Test
250253
public void testDeleteBreakType() {
251-
// create break type
254+
// Create break type
252255
CreateBreakTypeResponse breakResponse = client.labor()
253256
.breakTypes()
254257
.create(CreateBreakTypeRequest.builder()
@@ -268,11 +271,11 @@ public void testDeleteBreakType() {
268271
if (!breakType.get().getId().isPresent()) {
269272
throw new RuntimeException("Break ID is null.");
270273
}
271-
breakId = breakType.get().getId().get();
274+
String testBreakId = breakType.get().getId().get();
272275

273276
DeleteBreakTypeResponse response = client.labor()
274277
.breakTypes()
275-
.delete(DeleteBreakTypesRequest.builder().id(breakId).build());
278+
.delete(DeleteBreakTypesRequest.builder().id(testBreakId).build());
276279
Assertions.assertNotNull(response);
277280
}
278281

@@ -283,4 +286,4 @@ public void testListWorkweekConfigs() {
283286
.list(ListWorkweekConfigsRequest.builder().build());
284287
Assertions.assertFalse(response.getItems().isEmpty());
285288
}
286-
}
289+
}

0 commit comments

Comments
 (0)