Search in sources :

Example 1 with Ticket

use of io.gravitee.repository.management.model.Ticket in project gravitee-management-rest-api by gravitee-io.

the class TicketServiceTest method shouldFindById.

@Test
public void shouldFindById() throws TechnicalException {
    Ticket ticket = new Ticket();
    ticket.setId("ticket1");
    ticket.setApi(API_ID);
    ticket.setApplication(APPLICATION_ID);
    ticket.setSubject(EMAIL_SUBJECT);
    ticket.setContent(EMAIL_CONTENT);
    ticket.setCreatedAt(new Date());
    ticket.setFromUser(USERNAME);
    ApiEntity apiEntity = new ApiEntity();
    apiEntity.setName("apiName");
    ApplicationEntity appEntity = new ApplicationEntity();
    appEntity.setName("appName");
    when(ticketRepository.findById("ticket1")).thenReturn(Optional.of(ticket));
    when(apiService.findById(API_ID)).thenReturn(apiEntity);
    when(applicationService.findById(APPLICATION_ID)).thenReturn(appEntity);
    TicketEntity ticketEntity = ticketService.findById("ticket1");
    assertEquals("ticket1", ticketEntity.getId());
    assertEquals("apiName", ticketEntity.getApi());
    assertEquals("appName", ticketEntity.getApplication());
}
Also used : Ticket(io.gravitee.repository.management.model.Ticket) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Test(org.junit.Test)

Example 2 with Ticket

use of io.gravitee.repository.management.model.Ticket in project gravitee-management-rest-api by gravitee-io.

the class TicketServiceTest method shouldSearchForTicketsFromUser.

@Test
public void shouldSearchForTicketsFromUser() throws TechnicalException {
    Ticket ticket = new Ticket();
    ticket.setId("generatedId");
    ticket.setApi(API_ID);
    ticket.setApplication(APPLICATION_ID);
    ticket.setSubject(EMAIL_SUBJECT);
    ticket.setContent(EMAIL_CONTENT);
    ticket.setCreatedAt(new Date());
    ticket.setFromUser(USERNAME);
    ApiEntity apiEntity = new ApiEntity();
    ApplicationEntity appEntity = new ApplicationEntity();
    List<Ticket> ticketList = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        Ticket t = new Ticket(ticket);
        t.setId("ticket" + i);
        ticketList.add(t);
    }
    when(ticketRepository.search(any(TicketCriteria.class), any(Sortable.class), any(Pageable.class))).thenReturn(new Page<>(ticketList, 0, 20, 20));
    when(apiService.findById(API_ID)).thenReturn(apiEntity);
    when(applicationService.findById(APPLICATION_ID)).thenReturn(appEntity);
    TicketQuery query = new TicketQuery();
    query.setFromUser("fromUser");
    Page<TicketEntity> searchResult = ticketService.search(query, new SortableImpl("subject", true), new PageableImpl(1, Integer.MAX_VALUE));
    assertEquals(searchResult.getContent().size(), 20);
    assertEquals(searchResult.getPageNumber(), 1);
    assertEquals(searchResult.getTotalElements(), 20);
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) Ticket(io.gravitee.repository.management.model.Ticket) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TicketQuery(io.gravitee.rest.api.model.api.TicketQuery) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) Pageable(io.gravitee.repository.management.api.search.Pageable) Sortable(io.gravitee.repository.management.api.search.Sortable) TicketCriteria(io.gravitee.repository.management.api.search.TicketCriteria) Test(org.junit.Test)

Example 3 with Ticket

use of io.gravitee.repository.management.model.Ticket in project gravitee-management-rest-api by gravitee-io.

the class TicketServiceImpl method create.

@Override
public TicketEntity create(final String userId, final NewTicketEntity ticketEntity, final String referenceId, final ParameterReferenceType referenceType) {
    try {
        if (!isEnabled(referenceId, referenceType)) {
            throw new SupportUnavailableException();
        }
        LOGGER.info("Creating a support ticket: {}", ticketEntity);
        final Map<String, Object> parameters = new HashMap<>();
        final UserEntity user = userService.findById(userId);
        if (user.getEmail() == null) {
            throw new EmailRequiredException(userId);
        }
        parameters.put("user", user);
        final String emailTo;
        final ApiModelEntity api;
        final ApplicationEntity applicationEntity;
        if (ticketEntity.getApi() == null || ticketEntity.getApi().isEmpty()) {
            api = null;
            final MetadataEntity emailMetadata = metadataService.findDefaultByKey(DefaultMetadataUpgrader.METADATA_EMAIL_SUPPORT_KEY);
            if (emailMetadata == null) {
                throw new IllegalStateException("The support email metadata has not been found");
            }
            emailTo = emailMetadata.getValue();
        } else {
            api = apiService.findByIdForTemplates(ticketEntity.getApi(), true);
            final String apiMetadataEmailSupport = api.getMetadata().get(DefaultMetadataUpgrader.METADATA_EMAIL_SUPPORT_KEY);
            if (apiMetadataEmailSupport == null) {
                throw new IllegalStateException("The support email API metadata has not been found");
            }
            emailTo = apiMetadataEmailSupport;
            parameters.put("api", api);
        }
        if (DefaultMetadataUpgrader.DEFAULT_METADATA_EMAIL_SUPPORT.equals(emailTo)) {
            throw new IllegalStateException("The support email API metadata has not been changed");
        }
        if (ticketEntity.getApplication() != null && !ticketEntity.getApplication().isEmpty()) {
            applicationEntity = applicationService.findById(ticketEntity.getApplication());
            parameters.put("application", applicationEntity);
        } else {
            applicationEntity = null;
        }
        parameters.put("content", ticketEntity.getContent().replaceAll("(\r\n|\n)", "<br />"));
        parameters.put("ticketSubject", ticketEntity.getSubject());
        final String fromName = user.getFirstname() == null ? user.getEmail() : user.getFirstname() + ' ' + user.getLastname();
        emailService.sendEmailNotification(new EmailNotificationBuilder().replyTo(user.getEmail()).fromName(fromName).to(emailTo).copyToSender(ticketEntity.isCopyToSender()).template(TEMPLATES_FOR_ACTION_SUPPORT_TICKET).params(parameters).build());
        sendUserNotification(user, api, applicationEntity);
        Ticket ticket = convert(ticketEntity);
        ticket.setId(UuidString.generateRandom());
        ticket.setCreatedAt(new Date());
        ticket.setFromUser(userId);
        Ticket createdTicket = ticketRepository.create(ticket);
        return convert(createdTicket);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create a ticket {}", ticketEntity, ex);
        throw new TechnicalManagementException("An error occurs while trying to create a ticket " + ticketEntity, ex);
    }
}
Also used : Ticket(io.gravitee.repository.management.model.Ticket) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) HashMap(java.util.HashMap) EmailRequiredException(io.gravitee.rest.api.service.exceptions.EmailRequiredException) UuidString(io.gravitee.rest.api.service.common.UuidString) EmailNotificationBuilder(io.gravitee.rest.api.service.builder.EmailNotificationBuilder) Date(java.util.Date) SupportUnavailableException(io.gravitee.rest.api.service.exceptions.SupportUnavailableException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with Ticket

use of io.gravitee.repository.management.model.Ticket in project gravitee-management-rest-api by gravitee-io.

the class TicketServiceImpl method search.

@Override
public Page<TicketEntity> search(TicketQuery query, Sortable sortable, Pageable pageable) {
    try {
        LOGGER.debug("search tickets");
        TicketCriteria criteria = queryToCriteriaBuilder(query).build();
        Page<Ticket> tickets = ticketRepository.search(criteria, buildSortable(sortable), new PageableBuilder().pageNumber(pageable.getPageNumber() - 1).pageSize(pageable.getPageSize()).build());
        List<TicketEntity> entities = tickets.getContent().stream().map(this::getApiNameAndApplicationName).map(this::convert).collect(Collectors.toList());
        LOGGER.debug("search tickets - Done with {} elements", entities.size());
        return new Page<>(entities, tickets.getPageNumber() + 1, (int) tickets.getPageElements(), tickets.getTotalElements());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to search tickets", ex);
        throw new TechnicalManagementException("An error occurs while trying to search tickets", ex);
    }
}
Also used : Ticket(io.gravitee.repository.management.model.Ticket) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TicketCriteria(io.gravitee.repository.management.api.search.TicketCriteria) Page(io.gravitee.common.data.domain.Page) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 5 with Ticket

use of io.gravitee.repository.management.model.Ticket in project gravitee-management-rest-api by gravitee-io.

the class TicketServiceImpl method convert.

private Ticket convert(NewTicketEntity ticketEntity) {
    if (ticketEntity == null) {
        return null;
    }
    Ticket ticket = new Ticket();
    ticket.setApi(ticketEntity.getApi());
    ticket.setApplication(ticketEntity.getApplication());
    ticket.setSubject(ticketEntity.getSubject());
    ticket.setContent(ticketEntity.getContent());
    return ticket;
}
Also used : Ticket(io.gravitee.repository.management.model.Ticket)

Aggregations

Ticket (io.gravitee.repository.management.model.Ticket)8 Test (org.junit.Test)5 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 TicketCriteria (io.gravitee.repository.management.api.search.TicketCriteria)3 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)3 Pageable (io.gravitee.repository.management.api.search.Pageable)2 Sortable (io.gravitee.repository.management.api.search.Sortable)2 TicketQuery (io.gravitee.rest.api.model.api.TicketQuery)2 PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)2 SortableImpl (io.gravitee.rest.api.model.common.SortableImpl)2 EmailNotificationBuilder (io.gravitee.rest.api.service.builder.EmailNotificationBuilder)2 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)2 Page (io.gravitee.common.data.domain.Page)1 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 EmailRequiredException (io.gravitee.rest.api.service.exceptions.EmailRequiredException)1 SupportUnavailableException (io.gravitee.rest.api.service.exceptions.SupportUnavailableException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1