Skip to content

Commit

Permalink
#110 - insert event + additional services
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Jun 8, 2016
1 parent fea07d7 commit 12d0036
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.controller.api.admin;

import alfio.model.modification.EventModification;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ public Event getSingleEventById(@PathVariable("eventId") int eventId, Principal
@RequestMapping(value = "/events/check", method = POST)
public ValidationResult validateEvent(@RequestBody EventModification eventModification, Errors errors) {
ValidationResult base = validateEventHeader(Optional.<Event>empty(), eventModification, errors)
.or(validateEventPrices(Optional.<Event>empty(), eventModification, errors));
.or(validateEventPrices(Optional.<Event>empty(), eventModification, errors))
.or(eventModification.getAdditionalServices().stream().map(as -> validateAdditionalService(as, eventModification, errors)).reduce(ValidationResult::or).orElse(ValidationResult.success()));
AtomicInteger counter = new AtomicInteger();
return base.or(eventModification.getTicketCategories().stream()
.map(c -> validateCategory(c, errors, "ticketCategories[" + counter.getAndIncrement() + "]."))
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/alfio/manager/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import alfio.model.user.Organization;
import alfio.repository.*;
import alfio.util.Json;
import alfio.util.MonetaryUtil;
import ch.digitalfondue.npjt.AffectedRowCountAndKey;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
Expand Down Expand Up @@ -85,6 +86,7 @@ public class EventManager {
private final PluginManager pluginManager;
private final TicketFieldRepository ticketFieldRepository;
private final EventDeleterRepository eventDeleterRepository;
private final AdditionalServiceRepository additionalServiceRepository;

@Autowired
public EventManager(UserManager userManager,
Expand All @@ -101,7 +103,8 @@ public EventManager(UserManager userManager,
ConfigurationManager configurationManager,
PluginManager pluginManager,
TicketFieldRepository ticketFieldRepository,
EventDeleterRepository eventDeleterRepository) {
EventDeleterRepository eventDeleterRepository,
AdditionalServiceRepository additionalServiceRepository) {
this.userManager = userManager;
this.eventRepository = eventRepository;
this.eventDescriptionRepository = eventDescriptionRepository;
Expand All @@ -117,6 +120,7 @@ public EventManager(UserManager userManager,
this.pluginManager = pluginManager;
this.ticketFieldRepository = ticketFieldRepository;
this.eventDeleterRepository = eventDeleterRepository;
this.additionalServiceRepository = additionalServiceRepository;
}

public Event getSingleEvent(String eventName, String username) {
Expand Down Expand Up @@ -167,9 +171,15 @@ public void createEvent(EventModification em) {
createAdditionalFields(eventId, em);
createCategoriesForEvent(em, event);
createAllTicketsForEvent(eventId, event);
createAllAdditionalServices(eventId, em.getAdditionalServices(), event.getZoneId());
initPlugins(event);
}

private void createAllAdditionalServices(int eventId, List<EventModification.AdditionalService> additionalServices, ZoneId zoneId) {
Optional.ofNullable(additionalServices)
.ifPresent(list -> list.forEach(as -> additionalServiceRepository.insert(eventId, Optional.ofNullable(as.getPrice()).map(MonetaryUtil::unitToCents).orElse(0), as.isFixPrice(), as.getOrdinal(), as.getAvailableQuantity(), as.getMaxQtyPerOrder(), as.getInception().toZonedDateTime(zoneId), as.getExpiration().toZonedDateTime(zoneId), as.getVat(), as.getVatType())));
}

private void initPlugins(Event event) {
pluginManager.installPlugins(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

self.onEditComplete = function(item) {
if(!_.find(self.list, function(i) { return i === item })) {
item.ordinal = self.list.length;
self.list.push(item);
}
editComplete();
Expand Down
15 changes: 9 additions & 6 deletions src/test/java/alfio/manager/EventManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import alfio.manager.plugin.PluginManager;
import alfio.manager.user.UserManager;
import alfio.model.*;
import alfio.model.Event;
import alfio.model.SpecialPrice;
import alfio.model.Ticket;
import alfio.model.TicketCategory;
import alfio.model.modification.TicketCategoryWithStatistic;
import alfio.model.modification.TicketWithStatistic;
import alfio.model.user.Organization;
Expand Down Expand Up @@ -57,7 +60,7 @@ public class EventManagerTest {{
TicketCategory updated = mock(TicketCategory.class);
TicketRepository ticketRepository = it.usesMock(TicketRepository.class);
NamedParameterJdbcTemplate jdbc = it.usesMock(NamedParameterJdbcTemplate.class);
EventManager eventManager = new EventManager(null, null, null, null, null, null, ticketRepository, null, null, null, jdbc, null, pluginManager, null, null);
EventManager eventManager = new EventManager(null, null, null, null, null, null, ticketRepository, null, null, null, jdbc, null, pluginManager, null, null, null);
when(original.getId()).thenReturn(20);
when(updated.getId()).thenReturn(30);
when(original.getPriceInCents()).thenReturn(1000);
Expand Down Expand Up @@ -94,7 +97,7 @@ public class EventManagerTest {{

describe("handlePriceChange", it -> {
TicketRepository ticketRepository = it.usesMock(TicketRepository.class);
EventManager eventManager = new EventManager(null, null, null, null, null, null, ticketRepository, null, null, null, null, null, pluginManager, null, null);
EventManager eventManager = new EventManager(null, null, null, null, null, null, ticketRepository, null, null, null, null, null, pluginManager, null, null, null);
TicketCategory original = mock(TicketCategory.class);
TicketCategory updated = mock(TicketCategory.class);

Expand Down Expand Up @@ -130,7 +133,7 @@ public class EventManagerTest {{
describe("handleTokenModification", it -> {
SpecialPriceRepository specialPriceRepository = it.usesMock(SpecialPriceRepository.class);
NamedParameterJdbcTemplate jdbc = it.usesMock(NamedParameterJdbcTemplate.class);
EventManager eventManager = new EventManager(null, null, null, null, null, null, null, specialPriceRepository, null, null, jdbc, null, pluginManager, null, null);
EventManager eventManager = new EventManager(null, null, null, null, null, null, null, specialPriceRepository, null, null, jdbc, null, pluginManager, null, null, null);
TicketCategory original = mock(TicketCategory.class);
TicketCategory updated = mock(TicketCategory.class);

Expand Down Expand Up @@ -193,7 +196,7 @@ public class EventManagerTest {{
int eventId = 0;
TicketCategoryRepository ticketCategoryRepository = it.usesMock(TicketCategoryRepository.class);
TicketCategoryDescriptionRepository ticketCategoryDescriptionRepository = it.usesMock(TicketCategoryDescriptionRepository.class);
EventManager eventManager = new EventManager(null, null, null, null, ticketCategoryRepository, ticketCategoryDescriptionRepository, null, null, null, null, null, null, pluginManager, null, null);
EventManager eventManager = new EventManager(null, null, null, null, ticketCategoryRepository, ticketCategoryDescriptionRepository, null, null, null, null, null, null, pluginManager, null, null, null);
Event event = mock(Event.class);
int availableSeats = 20;
when(event.getAvailableSeats()).thenReturn(availableSeats);
Expand Down Expand Up @@ -247,7 +250,7 @@ public class EventManagerTest {{
TicketCategoryWithStatistic tc = new TicketCategoryWithStatistic(ticketCategory, Collections.<TicketWithStatistic>emptyList(), Collections.<SpecialPrice>emptyList(), ZoneId.systemDefault(), categoryPriceCalculator(event), desc);
when(esm.loadTicketCategoryWithStats(eq(categoryId), eq(event))).thenReturn(tc);

EventManager eventManager = new EventManager(userManager, eventRepository, eventDescriptionRepository, esm, ticketCategoryRepository, ticketCategoryDescriptionRepository, ticketRepository, specialPriceRepository, null, null, null, null, pluginManager, null, null);
EventManager eventManager = new EventManager(userManager, eventRepository, eventDescriptionRepository, esm, ticketCategoryRepository, ticketCategoryDescriptionRepository, ticketRepository, specialPriceRepository, null, null, null, null, pluginManager, null, null, null);
when(event.getId()).thenReturn(eventId);
when(event.getOrganizationId()).thenReturn(organizationId);
Organization organization = mock(Organization.class);
Expand Down
125 changes: 125 additions & 0 deletions src/test/java/alfio/util/ValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.util;

import alfio.model.AdditionalService;
import alfio.model.AdditionalServiceDescription;
import alfio.model.ContentLanguage;
import alfio.model.modification.DateTimeModification;
import alfio.model.modification.EventModification;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.validation.Errors;
import org.springframework.validation.MapBindingResult;

import java.math.BigDecimal;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.stream.Stream;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ValidatorTest {

public static final DateTimeModification VALID_EXPIRATION = DateTimeModification.fromZonedDateTime(ZonedDateTime.now().plusHours(1L));
public static final DateTimeModification VALID_INCEPTION = DateTimeModification.fromZonedDateTime(ZonedDateTime.now().minusDays(1L));
@Mock
private EventModification eventModification;
private Errors errors;
private EventModification.AdditionalServiceDescription title = new EventModification.AdditionalServiceDescription("it", "titolo", AdditionalServiceDescription.AdditionalServiceDescriptionType.TITLE);
private EventModification.AdditionalServiceDescription description = new EventModification.AdditionalServiceDescription("it", "descrizione", AdditionalServiceDescription.AdditionalServiceDescriptionType.DESCRIPTION);

@Before
public void init() {
errors = new MapBindingResult(new HashMap<>(), "test");
}

@Test
public void testValidationSuccess() {
EventModification.AdditionalService valid1 = new EventModification.AdditionalService(BigDecimal.ZERO, false, 0, -1, 1, VALID_INCEPTION, VALID_EXPIRATION, null, AdditionalService.VatType.NONE, Collections.emptyList(), singletonList(title), singletonList(description));
EventModification.AdditionalService valid2 = new EventModification.AdditionalService(BigDecimal.ONE, true, 1, 100, 1, VALID_INCEPTION, VALID_EXPIRATION, BigDecimal.TEN, AdditionalService.VatType.INHERITED, Collections.emptyList(), singletonList(title), singletonList(description));
assertTrue(Stream.of(valid1, valid2).map(as -> Validator.validateAdditionalService(as, errors)).allMatch(ValidationResult::isSuccess));
assertFalse(errors.hasFieldErrors());
}

@Test
public void testValidationErrorExpirationBeforeInception() {
EventModification.AdditionalService invalid = new EventModification.AdditionalService(BigDecimal.ZERO, false, 0, -1, 1, VALID_EXPIRATION, VALID_INCEPTION, null, AdditionalService.VatType.NONE, Collections.emptyList(), singletonList(title), singletonList(description));
assertFalse(Validator.validateAdditionalService(invalid, errors).isSuccess());
assertTrue(errors.hasFieldErrors());
assertNotNull(errors.getFieldError("inception"));
assertNotNull(errors.getFieldError("expiration"));
}

@Test
public void testValidationErrorInceptionNull() {
EventModification.AdditionalService invalid = new EventModification.AdditionalService(BigDecimal.ONE, true, 1, 100, 1, null, VALID_EXPIRATION, BigDecimal.TEN, AdditionalService.VatType.INHERITED, Collections.emptyList(), singletonList(title), singletonList(description));
assertFalse(Validator.validateAdditionalService(invalid, errors).isSuccess());
assertTrue(errors.hasFieldErrors());
assertNotNull(errors.getFieldError("inception"));
}

@Test
public void testValidationExpirationNull() {
EventModification.AdditionalService invalid = new EventModification.AdditionalService(BigDecimal.ONE, true, 1, 100, 1, VALID_INCEPTION, null, BigDecimal.TEN, AdditionalService.VatType.INHERITED, Collections.emptyList(), singletonList(title), singletonList(description));
assertFalse(Validator.validateAdditionalService(invalid, errors).isSuccess());
assertTrue(errors.hasFieldErrors());
assertNotNull(errors.getFieldError("expiration"));
}

@Test
public void testValidationInceptionExpirationNull() {
EventModification.AdditionalService invalid = new EventModification.AdditionalService(BigDecimal.ONE, true, 1, 100, 1, null, null, BigDecimal.TEN, AdditionalService.VatType.INHERITED, Collections.emptyList(), singletonList(title), singletonList(description));
assertFalse(Validator.validateAdditionalService(invalid, errors).isSuccess());
assertTrue(errors.hasFieldErrors());
assertNotNull(errors.getFieldError("inception"));
assertNotNull(errors.getFieldError("expiration"));
}

@Test
public void testValidationFailedDescriptionsDontMatchTitles() {
EventModification.AdditionalService invalid = new EventModification.AdditionalService(BigDecimal.ZERO, false, 0, -1, 1, VALID_INCEPTION, VALID_EXPIRATION, null, AdditionalService.VatType.NONE, Collections.emptyList(), emptyList(), singletonList(description));
EventModification.AdditionalService valid = new EventModification.AdditionalService(BigDecimal.ONE, true, 1, 100, 1, VALID_INCEPTION, VALID_EXPIRATION, BigDecimal.TEN, AdditionalService.VatType.INHERITED, Collections.emptyList(), singletonList(title), singletonList(description));
assertTrue(Validator.validateAdditionalService(valid, errors).isSuccess());
assertFalse(Validator.validateAdditionalService(invalid, errors).isSuccess());
assertTrue(errors.hasFieldErrors());
assertNotNull(errors.getFieldError("title"));
assertNotNull(errors.getFieldError("description"));
}

@Test
public void testValidationFailedDescription() {
when(eventModification.getLocales()).thenReturn(ContentLanguage.ENGLISH.getValue());
EventModification.AdditionalService invalid1 = new EventModification.AdditionalService(BigDecimal.ZERO, false, 0, -1, 1, VALID_INCEPTION, VALID_EXPIRATION, null, AdditionalService.VatType.NONE, Collections.emptyList(), emptyList(), singletonList(description));//English is required here
EventModification.AdditionalService invalid2 = new EventModification.AdditionalService(BigDecimal.ONE, true, 1, 100, 1, VALID_INCEPTION, VALID_EXPIRATION, BigDecimal.TEN, AdditionalService.VatType.INHERITED, Collections.emptyList(), singletonList(title), singletonList(new EventModification.AdditionalServiceDescription("en", "", AdditionalServiceDescription.AdditionalServiceDescriptionType.DESCRIPTION)));
assertFalse(Validator.validateAdditionalService(invalid1, errors).isSuccess());
assertTrue(errors.hasFieldErrors());
assertNotNull(errors.getFieldError("description"));

assertFalse(Validator.validateAdditionalService(invalid2, errors).isSuccess());
assertTrue(errors.hasFieldErrors());
assertNotNull(errors.getFieldError("description"));
}
}

0 comments on commit 12d0036

Please sign in to comment.