Skip to content

Commit 8a562ff

Browse files
committed
Refactor integration test
1 parent 8160511 commit 8a562ff

File tree

1 file changed

+70
-282
lines changed

1 file changed

+70
-282
lines changed
Lines changed: 70 additions & 282 deletions
Original file line numberDiff line numberDiff line change
@@ -1,286 +1,74 @@
1-
package com.squareup.square.integration;
2-
3-
import com.squareup.square.SquareClient;
4-
import com.squareup.square.core.SyncPagingIterable;
5-
import com.squareup.square.labor.types.CreateBreakTypeRequest;
6-
import com.squareup.square.labor.types.CreateShiftRequest;
7-
import com.squareup.square.labor.types.DeleteBreakTypesRequest;
8-
import com.squareup.square.labor.types.DeleteShiftsRequest;
9-
import com.squareup.square.labor.types.GetBreakTypesRequest;
10-
import com.squareup.square.labor.types.GetShiftsRequest;
11-
import com.squareup.square.labor.types.ListWorkweekConfigsRequest;
12-
import com.squareup.square.labor.types.SearchShiftsRequest;
13-
import com.squareup.square.labor.types.UpdateBreakTypeRequest;
14-
import com.squareup.square.labor.types.UpdateShiftRequest;
15-
import com.squareup.square.types.BreakType;
16-
import com.squareup.square.types.CreateBreakTypeResponse;
17-
import com.squareup.square.types.CreateShiftResponse;
18-
import com.squareup.square.types.CreateTeamMemberRequest;
19-
import com.squareup.square.types.CreateTeamMemberResponse;
20-
import com.squareup.square.types.DeleteBreakTypeResponse;
21-
import com.squareup.square.types.DeleteShiftResponse;
22-
import com.squareup.square.types.GetBreakTypeResponse;
23-
import com.squareup.square.types.GetShiftResponse;
24-
import com.squareup.square.types.SearchShiftsResponse;
25-
import com.squareup.square.types.Shift;
26-
import com.squareup.square.types.ShiftWage;
27-
import com.squareup.square.types.TeamMember;
28-
import com.squareup.square.types.UpdateBreakTypeResponse;
29-
import com.squareup.square.types.UpdateShiftResponse;
30-
import com.squareup.square.types.WorkweekConfig;
31-
import java.time.OffsetDateTime;
32-
import java.time.format.DateTimeFormatter;
33-
import java.util.Optional;
34-
import java.util.UUID;
35-
import org.junit.jupiter.api.AfterEach;
36-
import org.junit.jupiter.api.Assertions;
37-
import org.junit.jupiter.api.BeforeEach;
38-
import org.junit.jupiter.api.Test;
39-
40-
public class LaborTest {
41-
private SquareClient client;
42-
private String locationId;
43-
private String memberId;
44-
private String breakId;
45-
private String shiftId;
46-
47-
@BeforeEach
48-
public void before() {
49-
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-
memberId = teamResponse.getTeamMember().get().getId().get();
65-
66-
// Create break type for testing
67-
CreateBreakTypeResponse breakResponse = client.labor()
68-
.breakTypes()
69-
.create(CreateBreakTypeRequest.builder()
70-
.breakType(BreakType.builder()
71-
.locationId(locationId)
72-
.breakName("Lunch_" + UUID.randomUUID())
73-
.expectedDuration("PT0H30M0S")
74-
.isPaid(true)
75-
.build())
76-
.idempotencyKey(UUID.randomUUID().toString())
77-
.build());
78-
if (!breakResponse.getBreakType().get().getId().isPresent()) {
79-
throw new RuntimeException("Failed to create break type.");
80-
}
81-
breakId = breakResponse.getBreakType().get().getId().get();
82-
83-
// Create shift for testing
84-
CreateShiftResponse shiftResponse = client.labor()
85-
.shifts()
86-
.create(CreateShiftRequest.builder()
87-
.shift(Shift.builder()
88-
.locationId(locationId)
89-
.startAt(OffsetDateTime.now().format(DateTimeFormatter.ISO_INSTANT))
90-
.teamMemberId(memberId)
91-
.build())
92-
.idempotencyKey(UUID.randomUUID().toString())
93-
.build());
94-
if (!shiftResponse.getShift().get().getId().isPresent()) {
95-
throw new RuntimeException("Failed to create shift.");
96-
}
97-
shiftId = shiftResponse.getShift().get().getId().get();
98-
}
99-
100-
@AfterEach
101-
public void after() {
102-
// Clean up resources
1+
@Test
2+
public void testDeleteShift() throws Exception {
1033
try {
104-
client.labor()
4+
// First delete any existing shifts for this team member
5+
SearchShiftsResponse existingShifts = client.labor()
1056
.shifts()
106-
.delete(DeleteShiftsRequest.builder().id(shiftId).build());
107-
} catch (Exception e) {
108-
// Test may have already deleted the shift
109-
}
110-
111-
try {
112-
client.labor()
113-
.breakTypes()
114-
.delete(DeleteBreakTypesRequest.builder().id(breakId).build());
7+
.search(SearchShiftsRequest.builder()
8+
.query(ShiftQuery.builder()
9+
.filter(ShiftFilter.builder()
10+
.teamMemberIds(Collections.singletonList(memberId))
11+
.build())
12+
.build())
13+
.limit(100)
14+
.build());
15+
16+
// Delete any existing shifts
17+
if (existingShifts.getShifts().isPresent()) {
18+
for (Shift existingShift : existingShifts.getShifts().get()) {
19+
if (existingShift.getId().isPresent()) {
20+
client.labor()
21+
.shifts()
22+
.delete(DeleteShiftsRequest.builder().id(existingShift.getId().get()).build());
23+
System.out.println("Deleted existing shift: " + existingShift.getId().get());
24+
}
25+
}
26+
}
27+
28+
// Start the shift a week from now
29+
OffsetDateTime startTime = OffsetDateTime.now().plusDays(7).withHour(9).withMinute(0).withSecond(0);
30+
System.out.println("Creating new shift starting at: " + startTime);
31+
32+
// Create shift
33+
CreateShiftResponse shiftResponse = client.labor()
34+
.shifts()
35+
.create(CreateShiftRequest.builder()
36+
.shift(Shift.builder()
37+
.locationId(locationId)
38+
.startAt(startTime.format(DateTimeFormatter.ISO_INSTANT))
39+
.teamMemberId(memberId)
40+
.endAt(startTime.plusHours(8).format(DateTimeFormatter.ISO_INSTANT))
41+
.build())
42+
.idempotencyKey(UUID.randomUUID().toString())
43+
.build());
44+
45+
if (!shiftResponse.getShift().isPresent()) {
46+
throw new RuntimeException("Failed to create shift: shift response is empty");
47+
}
48+
49+
Optional<Shift> shift = shiftResponse.getShift();
50+
if (!shift.get().getId().isPresent()) {
51+
throw new RuntimeException("Shift ID is null in response: " + shift);
52+
}
53+
54+
String testShiftId = shift.get().getId().get();
55+
System.out.println("Successfully created shift with ID: " + testShiftId + " starting at " + startTime);
56+
57+
// Add a small delay to ensure the shift is fully created
58+
Thread.sleep(1000);
59+
60+
DeleteShiftResponse response = client.labor()
61+
.shifts()
62+
.delete(DeleteShiftsRequest.builder().id(testShiftId).build());
63+
Assertions.assertNotNull(response);
64+
System.out.println("Successfully deleted shift with ID: " + testShiftId);
65+
11566
} catch (Exception e) {
116-
// Test may have already deleted the break
67+
System.err.println("Test failed with exception: " + e.getMessage());
68+
if (e instanceof com.squareup.square.core.SquareApiException) {
69+
System.err.println("API Error details: " + ((com.squareup.square.core.SquareApiException) e).getBody());
70+
}
71+
e.printStackTrace();
72+
throw e;
11773
}
118-
}
119-
120-
@Test
121-
public void testListBreakTypes() {
122-
SyncPagingIterable<BreakType> response = client.labor().breakTypes().list();
123-
Assertions.assertFalse(response.getItems().isEmpty());
124-
}
125-
126-
@Test
127-
public void testGetBreakType() {
128-
GetBreakTypeResponse response = client.labor()
129-
.breakTypes()
130-
.get(GetBreakTypesRequest.builder().id(breakId).build());
131-
Assertions.assertTrue(response.getBreakType().isPresent());
132-
Assertions.assertEquals(breakId, response.getBreakType().get().getId().get());
133-
}
134-
135-
@Test
136-
public void testUpdateBreakType() {
137-
UpdateBreakTypeRequest updateRequest = UpdateBreakTypeRequest.builder()
138-
.id(breakId)
139-
.breakType(BreakType.builder()
140-
.locationId(locationId)
141-
.breakName("Lunch_" + UUID.randomUUID())
142-
.expectedDuration("PT1H0M0S")
143-
.isPaid(true)
144-
.build())
145-
.build();
146-
UpdateBreakTypeResponse response = client.labor().breakTypes().update(updateRequest);
147-
Assertions.assertTrue(response.getBreakType().isPresent());
148-
Assertions.assertEquals(breakId, response.getBreakType().get().getId().get());
149-
Assertions.assertEquals("PT1H", response.getBreakType().get().getExpectedDuration());
150-
}
151-
152-
@Test
153-
public void testSearchShifts() {
154-
SearchShiftsRequest searchRequest =
155-
SearchShiftsRequest.builder().limit(1).build();
156-
SearchShiftsResponse response = client.labor().shifts().search(searchRequest);
157-
Assertions.assertTrue(response.getShifts().isPresent());
158-
Assertions.assertFalse(response.getShifts().get().isEmpty());
159-
}
160-
161-
@Test
162-
public void testGetShift() {
163-
GetShiftResponse response = client.labor()
164-
.shifts()
165-
.get(GetShiftsRequest.builder().id(shiftId).build());
166-
Assertions.assertTrue(response.getShift().isPresent());
167-
Assertions.assertEquals(shiftId, response.getShift().get().getId().get());
168-
}
169-
170-
@Test
171-
public void testUpdateShift() {
172-
UpdateShiftRequest updateRequest = UpdateShiftRequest.builder()
173-
.id(shiftId)
174-
.shift(Shift.builder()
175-
.locationId(locationId)
176-
.startAt(OffsetDateTime.now().minusMinutes(1).format(DateTimeFormatter.ISO_INSTANT))
177-
.teamMemberId(memberId)
178-
.wage(ShiftWage.builder()
179-
.title("Manager")
180-
.hourlyRate(Helpers.newTestMoney(2500))
181-
.build())
182-
.build())
183-
.build();
184-
UpdateShiftResponse response = client.labor().shifts().update(updateRequest);
185-
Assertions.assertTrue(response.getShift().isPresent());
186-
Assertions.assertEquals(
187-
"Manager", response.getShift().get().getWage().get().getTitle().get());
188-
Assertions.assertEquals(
189-
2500,
190-
response.getShift()
191-
.get()
192-
.getWage()
193-
.get()
194-
.getHourlyRate()
195-
.get()
196-
.getAmount()
197-
.get());
198-
Assertions.assertEquals(
199-
"USD",
200-
response.getShift()
201-
.get()
202-
.getWage()
203-
.get()
204-
.getHourlyRate()
205-
.get()
206-
.getCurrency()
207-
.get()
208-
.toString());
209-
}
210-
211-
@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());
235-
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.");
241-
}
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);
247-
}
248-
249-
@Test
250-
public void testDeleteBreakType() {
251-
// create break type
252-
CreateBreakTypeResponse breakResponse = client.labor()
253-
.breakTypes()
254-
.create(CreateBreakTypeRequest.builder()
255-
.breakType(BreakType.builder()
256-
.locationId(locationId)
257-
.breakName("Lunch_" + UUID.randomUUID())
258-
.expectedDuration("PT0H30M0S")
259-
.isPaid(true)
260-
.build())
261-
.idempotencyKey(UUID.randomUUID().toString())
262-
.build());
263-
264-
Optional<BreakType> breakType = breakResponse.getBreakType();
265-
if (!breakType.isPresent()) {
266-
throw new RuntimeException("Failed to create break type.");
267-
}
268-
if (!breakType.get().getId().isPresent()) {
269-
throw new RuntimeException("Break ID is null.");
270-
}
271-
breakId = breakType.get().getId().get();
272-
273-
DeleteBreakTypeResponse response = client.labor()
274-
.breakTypes()
275-
.delete(DeleteBreakTypesRequest.builder().id(breakId).build());
276-
Assertions.assertNotNull(response);
277-
}
278-
279-
@Test
280-
public void testListWorkweekConfigs() {
281-
SyncPagingIterable<WorkweekConfig> response = client.labor()
282-
.workweekConfigs()
283-
.list(ListWorkweekConfigsRequest.builder().build());
284-
Assertions.assertFalse(response.getItems().isEmpty());
285-
}
286-
}
74+
}

0 commit comments

Comments
 (0)