1212import com .squareup .square .labor .types .SearchShiftsRequest ;
1313import com .squareup .square .labor .types .UpdateBreakTypeRequest ;
1414import 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 ;
1520import com .squareup .square .types .BreakType ;
1621import com .squareup .square .types .CreateBreakTypeResponse ;
1722import com .squareup .square .types .CreateShiftResponse ;
18- import com .squareup .square .types .CreateTeamMemberRequest ;
19- import com .squareup .square .types .CreateTeamMemberResponse ;
2023import com .squareup .square .types .DeleteBreakTypeResponse ;
2124import com .squareup .square .types .DeleteShiftResponse ;
2225import com .squareup .square .types .GetBreakTypeResponse ;
2831import com .squareup .square .types .UpdateBreakTypeResponse ;
2932import com .squareup .square .types .UpdateShiftResponse ;
3033import com .squareup .square .types .WorkweekConfig ;
34+
3135import java .time .OffsetDateTime ;
3236import 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 ;
3346import java .util .Optional ;
3447import java .util .UUID ;
3548import 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" );
69+ }
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 );
6387 }
64- memberId = teamResponse . getTeamMember (). get ().getId ().get ( );
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,76 @@ 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 ());
236+ public void testDeleteShift () throws Exception {
237+ try {
238+ // First search for existing shifts for this team member
239+ SearchShiftsResponse existingShifts = client .labor ()
240+ .shifts ()
241+ .search (SearchShiftsRequest .builder ()
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+ // Find a start time after any existing shifts
246+ OffsetDateTime startTime = OffsetDateTime .now ().plusDays (1 ).withHour (9 ).withMinute (0 ).withSecond (0 );
247+
248+ // If there are existing shifts, make sure we start after them
249+ if (existingShifts .getShifts ().isPresent () && !existingShifts .getShifts ().get ().isEmpty ()) {
250+ Optional <OffsetDateTime > lastShiftEnd = existingShifts .getShifts ().get ().stream ()
251+ .filter (s -> s .getEndAt ().isPresent ())
252+ .map (s -> OffsetDateTime .parse (s .getEndAt ().get ()))
253+ .max (OffsetDateTime ::compareTo );
254+
255+ if (lastShiftEnd .isPresent () && lastShiftEnd .get ().isAfter (startTime )) {
256+ startTime = lastShiftEnd .get ().plusDays (1 ).withHour (9 ).withMinute (0 ).withSecond (0 );
257+ }
258+ }
235259
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." );
260+ // Create shift
261+ CreateShiftResponse shiftResponse = client .labor ()
262+ .shifts ()
263+ .create (CreateShiftRequest .builder ()
264+ .shift (Shift .builder ()
265+ .locationId (locationId )
266+ .startAt (startTime .format (DateTimeFormatter .ISO_INSTANT ))
267+ .teamMemberId (memberId )
268+ .endAt (startTime .plusHours (8 ).format (DateTimeFormatter .ISO_INSTANT ))
269+ .build ())
270+ .idempotencyKey (UUID .randomUUID ().toString ())
271+ .build ());
272+
273+ if (!shiftResponse .getShift ().isPresent ()) {
274+ throw new RuntimeException ("Failed to create shift: shift response is empty" );
275+ }
276+
277+ Optional <Shift > shift = shiftResponse .getShift ();
278+ if (!shift .get ().getId ().isPresent ()) {
279+ throw new RuntimeException ("Shift ID is null in response: " + shift );
280+ }
281+
282+ String testShiftId = shift .get ().getId ().get ();
283+ System .out .println ("Created shift with ID: " + testShiftId + " starting at " + startTime );
284+
285+ // Add a small delay to ensure the shift is fully created
286+ Thread .sleep (1000 );
287+
288+ DeleteShiftResponse response = client .labor ()
289+ .shifts ()
290+ .delete (DeleteShiftsRequest .builder ().id (testShiftId ).build ());
291+ Assertions .assertNotNull (response );
292+
293+ } catch (Exception e ) {
294+ System .err .println ("Test failed with exception: " + e .getMessage ());
295+ if (e instanceof com .squareup .square .core .SquareApiException ) {
296+ System .err .println ("API Error details: " + ((com .squareup .square .core .SquareApiException ) e ).getBody ());
297+ }
298+ e .printStackTrace ();
299+ throw e ;
241300 }
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 );
247301 }
248302
249303 @ Test
250304 public void testDeleteBreakType () {
251- // create break type
305+ // Create break type
252306 CreateBreakTypeResponse breakResponse = client .labor ()
253307 .breakTypes ()
254308 .create (CreateBreakTypeRequest .builder ()
@@ -268,11 +322,11 @@ public void testDeleteBreakType() {
268322 if (!breakType .get ().getId ().isPresent ()) {
269323 throw new RuntimeException ("Break ID is null." );
270324 }
271- breakId = breakType .get ().getId ().get ();
325+ String testBreakId = breakType .get ().getId ().get ();
272326
273327 DeleteBreakTypeResponse response = client .labor ()
274328 .breakTypes ()
275- .delete (DeleteBreakTypesRequest .builder ().id (breakId ).build ());
329+ .delete (DeleteBreakTypesRequest .builder ().id (testBreakId ).build ());
276330 Assertions .assertNotNull (response );
277331 }
278332
@@ -283,4 +337,4 @@ public void testListWorkweekConfigs() {
283337 .list (ListWorkweekConfigsRequest .builder ().build ());
284338 Assertions .assertFalse (response .getItems ().isEmpty ());
285339 }
286- }
340+ }
0 commit comments