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.events.converter;
  
  import static org.assertj.core.api.Assertions.assertThat;
  import static org.mockito.ArgumentMatchers.any;
  import static org.mockito.Mockito.verify;
  import static org.mockito.Mockito.when;
  
  import java.util.HashMap;
  import org.activiti.api.model.shared.model.VariableInstance;
  import org.activiti.api.runtime.event.impl.VariableCreatedEventImpl;
  import org.activiti.api.runtime.model.impl.VariableInstanceImpl;
  import org.activiti.cloud.api.model.shared.events.CloudVariableCreatedEvent;
  import org.activiti.cloud.api.model.shared.impl.events.CloudVariableCreatedEventImpl;
  import org.activiti.spring.process.ProcessExtensionService;
  import org.activiti.spring.process.model.Extension;
  import org.activiti.spring.process.model.VariableDefinition;
  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;
  
  @ExtendWith(MockitoExtension.class)
  class ToCloudVariableEventConverterTest {
  
      @InjectMocks
      private ToCloudVariableEventConverter toCloudVariableEventConverter;
  
      @Mock
      private RuntimeBundleInfoAppender runtimeBundleInfoAppender;
  
      @Mock
      private ProcessExtensionService processExtensionService;
  
      @Test
      void should_returnNull_whenPropertiesAreEmpty() {
          VariableInstance variableInstance = new VariableInstanceImpl<>(
              "variableName",
              "string",
              "example",
              "processInstanceId",
              null
          );
          VariableCreatedEventImpl event = new VariableCreatedEventImpl(variableInstance, "processDefinitionId", false);
  
          Extension extension = new Extension();
          VariableDefinition variableDefinition = new VariableDefinition();
          variableDefinition.setName("variableName");
          variableDefinition.setId("variableDefinitionId");
  
          when(processExtensionService.getExtensionsForId("processDefinitionId")).thenReturn(extension);
  
          CloudVariableCreatedEvent cloudVariableCreatedEvent = toCloudVariableEventConverter.from(event);
  
          assertThat(cloudVariableCreatedEvent.getVariableDefinitionId()).isNull();
          assertThat(cloudVariableCreatedEvent.isEphemeralVariable()).isFalse();
      }
  
      @Test
      void should_returnNull_whenProcessDefinitionIdIsNull() {
          VariableInstance variableInstance = new VariableInstanceImpl<>(
              "variableName",
              "string",
              "example",
              "processInstanceId",
              null
          );
          VariableCreatedEventImpl event = new VariableCreatedEventImpl(variableInstance, null, false);
  
          Extension extension = new Extension();
          VariableDefinition variableDefinition = new VariableDefinition();
          variableDefinition.setName("variableName");
          variableDefinition.setId("variableDefinitionId");
          HashMap<String, VariableDefinition> properties = new HashMap<>();
          properties.put("variableDefinitionId", variableDefinition);
          extension.setProperties(properties);
  
          CloudVariableCreatedEvent cloudVariableCreatedEvent = toCloudVariableEventConverter.from(event);
  
          assertThat(cloudVariableCreatedEvent.getVariableDefinitionId()).isNull();
          assertThat(cloudVariableCreatedEvent.isEphemeralVariable()).isFalse();
      }
  
      @Test
      void should_convertToCloudVariableCreatedEventWithVariableDefinitionId() {
          VariableInstance variableInstance = new VariableInstanceImpl<>(
              "variableName",
              "string",
              "example",
              "processInstanceId",
              null
          );
          VariableCreatedEventImpl event = new VariableCreatedEventImpl(variableInstance, "processDefinitionId", false);
  
          Extension extension = new Extension();
          HashMap<String, VariableDefinition> properties = new HashMap<>();
          VariableDefinition variableDefinition = new VariableDefinition();
          variableDefinition.setName("variableName");
          variableDefinition.setId("variableDefinitionId");
          properties.put("variableDefinitionId", variableDefinition);
          extension.setProperties(properties);
  
          when(processExtensionService.getExtensionsForId("processDefinitionId")).thenReturn(extension);
  
          CloudVariableCreatedEvent cloudVariableCreatedEvent = toCloudVariableEventConverter.from(event);
  
          assertThat(cloudVariableCreatedEvent.getVariableDefinitionId()).isEqualTo("variableDefinitionId");
          VariableInstance entity = cloudVariableCreatedEvent.getEntity();
          assertThat(entity.getName()).isEqualTo("variableName");
          assertThat(entity.getType()).isEqualTo("string");
          assertThat((String) entity.getValue()).isEqualTo("example");
          assertThat(entity.getProcessInstanceId()).isEqualTo("processInstanceId");
          verify(runtimeBundleInfoAppender).appendRuntimeBundleInfoTo(any(CloudVariableCreatedEventImpl.class));
      }
  
      @Test
      void should_setTheValueOf_propertyIsEphemeral_from_VariableCreatedEvents() {
          VariableInstance variableInstance = new VariableInstanceImpl<>(
              "variableName",
              "string",
              "example",
              "processInstanceId",
              null
          );
          VariableCreatedEventImpl event = new VariableCreatedEventImpl(variableInstance, "processDefinitionId", true);
  
          Extension extension = new Extension();
          HashMap<String, VariableDefinition> properties = new HashMap<>();
          VariableDefinition variableDefinition = new VariableDefinition();
          variableDefinition.setName("variableName");
          variableDefinition.setId("variableDefinitionId");
          properties.put("variableDefinitionId", variableDefinition);
          extension.setProperties(properties);
  
          when(processExtensionService.getExtensionsForId("processDefinitionId")).thenReturn(extension);
  
          CloudVariableCreatedEvent cloudVariableCreatedEvent = toCloudVariableEventConverter.from(event);
  
          assertThat(cloudVariableCreatedEvent.isEphemeralVariable()).isTrue();
          assertThat(cloudVariableCreatedEvent.getVariableDefinitionId()).isEqualTo("variableDefinitionId");
          VariableInstance entity = cloudVariableCreatedEvent.getEntity();
          assertThat(entity.getName()).isEqualTo("variableName");
          assertThat(entity.getType()).isEqualTo("string");
          assertThat((String) entity.getValue()).isEqualTo("example");
          assertThat(entity.getProcessInstanceId()).isEqualTo("processInstanceId");
          verify(runtimeBundleInfoAppender).appendRuntimeBundleInfoTo(any(CloudVariableCreatedEventImpl.class));
      }
  }
  