1010import com .squareup .square .labor .types .GetShiftsRequest ;
1111import com .squareup .square .labor .types .ListWorkweekConfigsRequest ;
1212import com .squareup .square .labor .types .SearchShiftsRequest ;
13+ import com .squareup .square .labor .types .ShiftQuery ;
14+ import com .squareup .square .labor .types .ShiftFilter ;
1315import com .squareup .square .labor .types .UpdateBreakTypeRequest ;
1416import 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 ;
1522import com .squareup .square .types .BreakType ;
1623import com .squareup .square .types .CreateBreakTypeResponse ;
1724import com .squareup .square .types .CreateShiftResponse ;
18- import com .squareup .square .types .CreateTeamMemberRequest ;
19- import com .squareup .square .types .CreateTeamMemberResponse ;
2025import com .squareup .square .types .DeleteBreakTypeResponse ;
2126import com .squareup .square .types .DeleteShiftResponse ;
2227import com .squareup .square .types .GetBreakTypeResponse ;
2833import com .squareup .square .types .UpdateBreakTypeResponse ;
2934import com .squareup .square .types .UpdateShiftResponse ;
3035import com .squareup .square .types .WorkweekConfig ;
36+
3137import java .time .OffsetDateTime ;
3238import java .time .format .DateTimeFormatter ;
39+ import java .util .Collections ;
40+ import java .util .List ;
3341import java .util .Optional ;
3442import java .util .UUID ;
3543import 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,83 @@ 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 ());
231+ public void testDeleteShift () throws Exception {
232+ try {
233+ // First delete any existing shifts for this team member
234+ SearchShiftsResponse existingShifts = client .labor ()
235+ .shifts ()
236+ .search (SearchShiftsRequest .builder ()
237+ .query (ShiftQuery .builder ()
238+ .filter (ShiftFilter .builder ()
239+ .teamMemberIds (Collections .singletonList (memberId ))
240+ .build ())
241+ .build ())
242+ .limit (100 )
243+ .build ());
222244
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 ());
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+ System .out .println ("Deleted existing shift: " + existingShift .getId ().get ());
253+ }
254+ }
255+ }
235256
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." );
257+ // Start the shift a week from now
258+ OffsetDateTime startTime = OffsetDateTime .now ().plusDays (7 ).withHour (9 ).withMinute (0 ).withSecond (0 );
259+ System .out .println ("Creating new shift starting at: " + startTime );
260+
261+ // Create shift
262+ CreateShiftResponse shiftResponse = client .labor ()
263+ .shifts ()
264+ .create (CreateShiftRequest .builder ()
265+ .shift (Shift .builder ()
266+ .locationId (locationId )
267+ .startAt (startTime .format (DateTimeFormatter .ISO_INSTANT ))
268+ .teamMemberId (memberId )
269+ .endAt (startTime .plusHours (8 ).format (DateTimeFormatter .ISO_INSTANT ))
270+ .build ())
271+ .idempotencyKey (UUID .randomUUID ().toString ())
272+ .build ());
273+
274+ if (!shiftResponse .getShift ().isPresent ()) {
275+ throw new RuntimeException ("Failed to create shift: shift response is empty" );
276+ }
277+
278+ Optional <Shift > shift = shiftResponse .getShift ();
279+ if (!shift .get ().getId ().isPresent ()) {
280+ throw new RuntimeException ("Shift ID is null in response: " + shift );
281+ }
282+
283+ String testShiftId = shift .get ().getId ().get ();
284+ System .out .println ("Successfully created shift with ID: " + testShiftId + " starting at " + startTime );
285+
286+ // Add a small delay to ensure the shift is fully created
287+ Thread .sleep (1000 );
288+
289+ DeleteShiftResponse response = client .labor ()
290+ .shifts ()
291+ .delete (DeleteShiftsRequest .builder ().id (testShiftId ).build ());
292+ Assertions .assertNotNull (response );
293+ System .out .println ("Successfully deleted shift with ID: " + testShiftId );
294+
295+ } catch (Exception e ) {
296+ System .err .println ("Test failed with exception: " + e .getMessage ());
297+ if (e instanceof com .squareup .square .core .SquareApiException ) {
298+ System .err .println ("API Error details: " + ((com .squareup .square .core .SquareApiException ) e ).getBody ());
299+ }
300+ e .printStackTrace ();
301+ throw e ;
241302 }
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 );
247303 }
248304
249305 @ Test
250306 public void testDeleteBreakType () {
251- // create break type
307+ // Create break type
252308 CreateBreakTypeResponse breakResponse = client .labor ()
253309 .breakTypes ()
254310 .create (CreateBreakTypeRequest .builder ()
@@ -268,11 +324,11 @@ public void testDeleteBreakType() {
268324 if (!breakType .get ().getId ().isPresent ()) {
269325 throw new RuntimeException ("Break ID is null." );
270326 }
271- breakId = breakType .get ().getId ().get ();
327+ String testBreakId = breakType .get ().getId ().get ();
272328
273329 DeleteBreakTypeResponse response = client .labor ()
274330 .breakTypes ()
275- .delete (DeleteBreakTypesRequest .builder ().id (breakId ).build ());
331+ .delete (DeleteBreakTypesRequest .builder ().id (testBreakId ).build ());
276332 Assertions .assertNotNull (response );
277333 }
278334
@@ -283,4 +339,4 @@ public void testListWorkweekConfigs() {
283339 .list (ListWorkweekConfigsRequest .builder ().build ());
284340 Assertions .assertFalse (response .getItems ().isEmpty ());
285341 }
286- }
342+ }
0 commit comments