Skip to content

Commit 0119eea

Browse files
committed
Refactor integration test
1 parent 8160511 commit 0119eea

File tree

1 file changed

+82
-53
lines changed

1 file changed

+82
-53
lines changed

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

Lines changed: 82 additions & 53 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,18 @@
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;
39+
import java.util.Optional;
40+
import java.util.UUID;
41+
import org.junit.jupiter.api.AfterEach;
42+
import org.junit.jupiter.api.Assertions;
43+
import org.junit.jupiter.api.BeforeEach;
44+
import org.junit.jupiter.api.Test;
45+
import java.util.List;
3346
import java.util.Optional;
3447
import java.util.UUID;
3548
import org.junit.jupiter.api.AfterEach;
@@ -47,21 +60,32 @@ public class LaborTest {
4760
@BeforeEach
4861
public void before() {
4962
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.");
63+
64+
// Get first available location
65+
List<Location> locations = client.locations().list().getLocations()
66+
.orElseThrow(() -> new RuntimeException("No locations available"));
67+
if (locations.isEmpty()) {
68+
throw new RuntimeException("No locations available for testing");
6369
}
64-
memberId = teamResponse.getTeamMember().get().getId().get();
70+
locationId = locations.get(0).getId().orElseThrow(() -> new RuntimeException("Location ID not present"));
71+
72+
// Get first available team member at this location
73+
List<TeamMember> teamMembers = client.teamMembers().search(
74+
SearchTeamMembersRequest.builder()
75+
.query(SearchTeamMembersQuery.builder()
76+
.filter(SearchTeamMembersFilter.builder()
77+
.locationIds(Collections.singletonList(locationId))
78+
.status(TeamMemberStatus.ACTIVE)
79+
.build())
80+
.build())
81+
.build()
82+
).getTeamMembers()
83+
.orElseThrow(() -> new RuntimeException("Failed to get team members"));
84+
85+
if (teamMembers.isEmpty()) {
86+
throw new RuntimeException("No team members available at location " + locationId);
87+
}
88+
memberId = teamMembers.get(0).getId().orElseThrow(() -> new RuntimeException("Team member ID not present"));
6589

6690
// Create break type for testing
6791
CreateBreakTypeResponse breakResponse = client.labor()
@@ -209,46 +233,51 @@ public void testUpdateShift() {
209233
}
210234

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

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

249278
@Test
250279
public void testDeleteBreakType() {
251-
// create break type
280+
// Create break type
252281
CreateBreakTypeResponse breakResponse = client.labor()
253282
.breakTypes()
254283
.create(CreateBreakTypeRequest.builder()
@@ -268,11 +297,11 @@ public void testDeleteBreakType() {
268297
if (!breakType.get().getId().isPresent()) {
269298
throw new RuntimeException("Break ID is null.");
270299
}
271-
breakId = breakType.get().getId().get();
300+
String testBreakId = breakType.get().getId().get();
272301

273302
DeleteBreakTypeResponse response = client.labor()
274303
.breakTypes()
275-
.delete(DeleteBreakTypesRequest.builder().id(breakId).build());
304+
.delete(DeleteBreakTypesRequest.builder().id(testBreakId).build());
276305
Assertions.assertNotNull(response);
277306
}
278307

@@ -283,4 +312,4 @@ public void testListWorkweekConfigs() {
283312
.list(ListWorkweekConfigsRequest.builder().build());
284313
Assertions.assertFalse(response.getItems().isEmpty());
285314
}
286-
}
315+
}

0 commit comments

Comments
 (0)