content: /*
   * Copyright 2017-2025 Hyland Software, Inc. and its affiliates.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.activiti.cloud.services.audit.jpa.controller;
  
  import static org.assertj.core.api.Assertions.assertThat;
  import static org.mockito.BDDMockito.given;
  import static org.mockito.Mockito.verify;
  import static org.mockito.Mockito.when;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  
  import java.util.ArrayList;
  import java.util.List;
  import org.activiti.api.process.model.events.ProcessRuntimeEvent;
  import org.activiti.api.runtime.model.impl.ProcessInstanceImpl;
  import org.activiti.api.runtime.shared.identity.UserGroupManager;
  import org.activiti.api.runtime.shared.security.SecurityManager;
  import org.activiti.cloud.alfresco.config.AlfrescoWebAutoConfiguration;
  import org.activiti.cloud.services.audit.api.config.AuditAPIAutoConfiguration;
  import org.activiti.cloud.services.audit.api.resources.EventsLinkRelationProvider;
  import org.activiti.cloud.services.audit.jpa.assembler.config.EventRepresentationModelAssemblerConfiguration;
  import org.activiti.cloud.services.audit.jpa.conf.AuditJPAAutoConfiguration;
  import org.activiti.cloud.services.audit.jpa.controllers.AuditEventsDeleteController;
  import org.activiti.cloud.services.audit.jpa.events.AuditEventEntity;
  import org.activiti.cloud.services.audit.jpa.events.ProcessStartedAuditEventEntity;
  import org.activiti.cloud.services.audit.jpa.repository.EventsRepository;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
  import org.springframework.context.annotation.Import;
  import org.springframework.data.web.config.EnableSpringDataWebSupport;
  import org.springframework.http.MediaType;
  import org.springframework.test.context.TestPropertySource;
  import org.springframework.test.context.bean.override.mockito.MockitoBean;
  import org.springframework.test.web.servlet.MockMvc;
  
  @TestPropertySource(properties = "activiti.rest.enable-deletion=true")
  @WebMvcTest(AuditEventsDeleteController.class)
  @EnableSpringDataWebSupport
  @AutoConfigureMockMvc
  @Import(
      {
          EventRepresentationModelAssemblerConfiguration.class,
          AuditAPIAutoConfiguration.class,
          AuditJPAAutoConfiguration.class,
          AlfrescoWebAutoConfiguration.class,
      }
  )
  public class AuditEventDeleteControllerIT {
  
      private static final String DOCUMENTATION_ALFRESCO_IDENTIFIER = "events-alfresco";
  
      @MockitoBean
      private EventsRepository eventsRepository;
  
      @Autowired
      private MockMvc mockMvc;
  
      @MockitoBean
      private SecurityManager securityManager;
  
      @MockitoBean
      private UserGroupManager userGroupManager;
  
      @BeforeEach
      public void setUp() {
          when(securityManager.getAuthenticatedUserId()).thenReturn("admin");
          assertThat(userGroupManager).isNotNull();
      }
  
      @Test
      public void deleteEventsShouldReturnAllEventsAndDeleteThem() throws Exception {
          //given
          List<AuditEventEntity> list = buildEventsData(1);
          given(eventsRepository.findAll()).willReturn(list);
  
          //when
          mockMvc
              .perform(
                  delete("/admin/v1/" + EventsLinkRelationProvider.COLLECTION_RESOURCE_REL)
                      .accept(MediaType.APPLICATION_JSON)
              )
              //then
              .andExpect(status().isOk());
  
          verify(eventsRepository).deleteAll(list);
      }
  
      private List<AuditEventEntity> buildEventsData(int recordsNumber) {
          List<AuditEventEntity> eventsList = new ArrayList<>();
  
          for (long i = 0; i < recordsNumber; i++) {
              //would like to mock this but jackson and mockito not happy together
              AuditEventEntity eventEntity = buildAuditEventEntity(i);
              eventsList.add(eventEntity);
          }
  
          return eventsList;
      }
  
      private AuditEventEntity buildAuditEventEntity(long id) {
          ProcessStartedAuditEventEntity eventEntity = new ProcessStartedAuditEventEntity();
          eventEntity.setEventId("eventId");
          eventEntity.setTimestamp(System.currentTimeMillis());
          eventEntity.setId(id);
          ProcessInstanceImpl processInstance = new ProcessInstanceImpl();
          processInstance.setId("10");
          processInstance.setProcessDefinitionId("1");
          eventEntity.setProcessInstance(processInstance);
          eventEntity.setServiceName("rb-my-app");
          eventEntity.setEventType(ProcessRuntimeEvent.ProcessEvents.PROCESS_STARTED.name());
          eventEntity.setProcessDefinitionId("1");
          eventEntity.setProcessInstanceId("10");
          eventEntity.setTimestamp(System.currentTimeMillis());
          return eventEntity;
      }
  }
  