Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect Asynchronous Events + More #878

Merged
merged 6 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,18 @@ public void reservationFlowTest() throws Exception {
List<String> extensionStream = IOUtils.readLines(new InputStreamReader(extensionInputStream, StandardCharsets.UTF_8));
String concatenation = String.join("\n", extensionStream);
extensionService.createOrUpdate(null, null, new Extension("-", "syncName", concatenation.replace("placeHolder", "false"), true));
extensionService.createOrUpdate(null, null, new Extension("-", "asyncName", concatenation.replace("placeHolder", "true"), true));
// System.out.println(extensionRepository.getScript("-", "asyncName"));
}
List<BasicEventInfo> body = eventApiV2Controller.listEvents().getBody();
assertNotNull(body);
assertTrue(body.isEmpty());
ensureConfiguration();

// check if EVENT_CREATED was logged
List<ExtensionLog> extLogs = extensionLogRepository.getPage(null, null, null, 100, 0);
assertEquals("Size of log", 2, extLogs.size());
assertEquals("EVENT_CREATED", extLogs.get(1).getDescription());
assertEventLogged(extLogs, "EVENT_CREATED", 4, 1);
assertEventLogged(extLogs, "EVENT_CREATED", 4, 3);


{
Expand Down Expand Up @@ -576,7 +579,14 @@ public void reservationFlowTest() throws Exception {
assertTrue(activePaymentMethods.isEmpty());

configurationRepository.deleteCategoryLevelByKey(ConfigurationKeys.PAYMENT_METHODS_BLACKLIST.name(), event.getId(), hiddenCategoryId);

// clear the extension_log table so that we can check the very next additions
// cannot have just one row in the log, every event adds EXACTLY two logs
// log expected: RESERVATION_CANCELLED
cleanupExtensionLog();
reservationApiV2Controller.cancelPendingReservation(event.getShortName(), res.getBody().getValue());
extLogs = extensionLogRepository.getPage(null, null, null, 100, 0);
assertEventLogged(extLogs, "RESERVATION_CANCELLED", 2, 1);

// this is run by a job, but given the fact that it's in another separate transaction, it cannot work in this test (WaitingQueueSubscriptionProcessor.handleWaitingTickets)
assertEquals(1, ticketReservationManager.revertTicketsToFreeIfAccessRestricted(event.getId()));
Expand Down Expand Up @@ -896,6 +906,7 @@ public void reservationFlowTest() throws Exception {

var handleRes = reservationApiV2Controller.confirmOverview(event.getShortName(), reservationId, "en", paymentForm, new BeanPropertyBindingResult(paymentForm, "paymentForm"),
new MockHttpServletRequest());

assertEquals(HttpStatus.OK, handleRes.getStatusCode());

checkStatus(reservationId, HttpStatus.OK, true, TicketReservation.TicketReservationStatus.OFFLINE_PAYMENT);
Expand All @@ -912,8 +923,16 @@ public void reservationFlowTest() throws Exception {
assertEquals("0.10", orderSummary.getTotalVAT());
assertEquals("1.00", orderSummary.getVatPercentage());

//clear the extension_log table so that we can check the expectation
cleanupExtensionLog();

validatePayment(event.getShortName(), reservationId);

extLogs = extensionLogRepository.getPage(null, null, null, 100, 0);
assertEventLogged(extLogs, "RESERVATION_CONFIRMED", 4, 1);
assertEventLogged(extLogs, "TICKET_ASSIGNED", 4, 3);


checkStatus(reservationId, HttpStatus.OK, true, TicketReservation.TicketReservationStatus.COMPLETE);

tStatus = reservationApiV2Controller.getTransactionStatus(event.getShortName(), reservationId, "BANK_TRANSFER");
Expand Down Expand Up @@ -1002,6 +1021,9 @@ public void reservationFlowTest() throws Exception {
//

{
//clear the extension_log table so that we can check the expectation
cleanupExtensionLog();

Principal principal = mock(Principal.class);
Mockito.when(principal.getName()).thenReturn(user);
String ticketIdentifier = fullTicketInfo.getUuid();
Expand All @@ -1017,6 +1039,10 @@ public void reservationFlowTest() throws Exception {
assertFalse(audits.isEmpty());
assertTrue(audits.stream().anyMatch(sa -> sa.getTicketUuid().equals(ticketIdentifier)));

extLogs = extensionLogRepository.getPage(null, null, null, 100, 0);
assertEventLogged(extLogs, "TICKET_CHECKED_IN", 2, 1);



TicketAndCheckInResult ticketAndCheckInResultOk = checkInApiController.findTicketWithUUID(event.getId(), ticketIdentifier, ticketCode);
assertEquals(CheckInStatus.ALREADY_CHECK_IN, ticketAndCheckInResultOk.getResult().getStatus());
Expand All @@ -1032,6 +1058,7 @@ public void reservationFlowTest() throws Exception {

//test revert check in
assertTrue(checkInApiController.revertCheckIn(event.getId(), ticketIdentifier, principal));

assertFalse(checkInApiController.revertCheckIn(event.getId(), ticketIdentifier, principal));
TicketAndCheckInResult ticketAndCheckInResult2 = checkInApiController.findTicketWithUUID(event.getId(), ticketIdentifier, ticketCode);
assertEquals(CheckInStatus.OK_READY_TO_BE_CHECKED_IN, ticketAndCheckInResult2.getResult().getStatus());
Expand All @@ -1056,14 +1083,16 @@ public void reservationFlowTest() throws Exception {
assertEquals(0, eventWithAdditionalInfo4.getCheckedInTickets());


cleanupExtensionLog();

CheckInApiController.TicketCode tc2 = new CheckInApiController.TicketCode();
tc2.setCode(ticketCode);
TicketAndCheckInResult ticketAndcheckInResult = checkInApiController.checkIn(event.getId(), ticketIdentifier, tc2, new TestingAuthenticationToken("ciccio", "ciccio"));
assertEquals(CheckInStatus.SUCCESS, ticketAndcheckInResult.getResult().getStatus());
//

extLogs = extensionLogRepository.getPage(null, null, null, 100, 0);
assertEventLogged(extLogs, "TICKET_CHECKED_IN", 2, 1);

//
var offlineIdentifiers = checkInApiController.getOfflineIdentifiers(event.getShortName(), 0L, new MockHttpServletResponse(), principal);
assertFalse("Alf.io-PI integration must be enabled by default", offlineIdentifiers.isEmpty());

Expand Down Expand Up @@ -1177,6 +1206,15 @@ public void reservationFlowTest() throws Exception {

}

private void cleanupExtensionLog() {
jdbcTemplate.update("delete from extension_log", Map.of());
}

private void assertEventLogged(List<ExtensionLog> extLog, String event, int logSize, int index){
assertEquals(logSize, extLog.size()); // each event logs exactly two logs
assertEquals(event, extLog.get(index).getDescription());
}

private void checkStatus(String reservationId, HttpStatus expectedHttpStatus, Boolean validated, TicketReservation.TicketReservationStatus reservationStatus) {
var statusRes = reservationApiV2Controller.getReservationStatus(event.getShortName(), reservationId);
assertEquals(expectedHttpStatus, statusRes.getStatusCode());
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function getScriptMetadata() {
'RESERVATION_CANCELLED', //fired when reservation(s) are cancelled
'TICKET_CANCELLED', //fired when ticket(s) (but not the entire reservation) are cancelled
'TICKET_ASSIGNED', //fired on ticket assignment. No results expected.
'TICKET_CHECKED_IN', //fired when a ticket has been checked in. No results expected.
'WAITING_QUEUE_SUBSCRIPTION', //fired on waiting queue subscription. No results expected.
'EVENT_CREATED', //fired when an event has been created. Return boolean for synchronous variant, no results expected for the asynchronous one.
'EVENT_STATUS_CHANGE', //fired when an event status has changed (normally, from DRAFT to PUBLIC). Return boolean for synchronous variant, no results expected for the asynchronous one.
Expand Down