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.query.events.handlers;
  
  import static java.util.Arrays.asList;
  import static org.mockito.Mockito.verify;
  import static org.mockito.Mockito.when;
  
  import jakarta.persistence.EntityManager;
  import java.util.List;
  import org.activiti.cloud.api.model.shared.events.CloudRuntimeEvent;
  import org.activiti.cloud.api.process.model.impl.events.CloudProcessCreatedEventImpl;
  import org.activiti.cloud.api.process.model.impl.events.CloudProcessStartedEventImpl;
  import org.activiti.cloud.services.query.app.QueryConsumerChannelHandler;
  import org.junit.jupiter.api.Test;
  import org.junit.jupiter.api.extension.ExtendWith;
  import org.mockito.InjectMocks;
  import org.mockito.Mock;
  import org.mockito.junit.jupiter.MockitoExtension;
  import org.springframework.integration.transaction.PseudoTransactionManager;
  import org.springframework.transaction.support.TransactionTemplate;
  
  @ExtendWith(MockitoExtension.class)
  public class QueryConsumerChannelHandlerTest {
  
      @InjectMocks
      private QueryConsumerChannelHandler consumer;
  
      @Mock
      private QueryEventHandlerContext eventHandlerContext;
  
      @Mock
      private QueryEventHandlerContextOptimizer optimizer;
  
      @Mock
      private EntityManager entityManager;
  
      @Test
      public void receiveShouldHandleReceivedEvent() {
          //given
          CloudProcessCreatedEventImpl processCreatedEvent = new CloudProcessCreatedEventImpl();
          CloudProcessStartedEventImpl processStartedEvent = new CloudProcessStartedEventImpl();
  
          List<CloudRuntimeEvent<?, ?>> events = asList(processCreatedEvent, processStartedEvent);
  
          when(optimizer.optimize(events)).thenReturn(events);
  
          //when
          new TransactionTemplate(new PseudoTransactionManager()).executeWithoutResult(tx -> consumer.receive(events));
  
          //then
          verify(optimizer).optimize(events);
          verify(eventHandlerContext).handle(processCreatedEvent, processStartedEvent);
          verify(entityManager).clear();
      }
  }
  