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.converters;
  
  import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
  import static org.activiti.test.Assertions.assertThat;
  
  import java.io.Serializable;
  import org.activiti.api.model.shared.model.VariableInstance;
  import org.activiti.api.runtime.model.impl.VariableInstanceImpl;
  import org.activiti.cloud.services.audit.jpa.converters.json.VariableJpaJsonConverter;
  import org.junit.jupiter.api.Test;
  
  public class VariablesJpaJsonConverterTest {
  
      private VariableJpaJsonConverter converter = new VariableJpaJsonConverter();
  
      @Test
      public void convertToDatabaseColumnShouldReturnTheEntityJsonRepresentation() throws Exception {
          //given
          VariableInstanceImpl<String> variable = new VariableInstanceImpl<>(
              "var-name",
              "String",
              "my string value",
              "proc-inst-id",
              "task-id"
          );
  
          //when
          String jsonRepresentation = converter.convertToDatabaseColumn(variable);
  
          //then
          assertThatJson(jsonRepresentation).inPath("name").isEqualTo("var-name");
          assertThatJson(jsonRepresentation).inPath("type").isEqualTo("String");
          assertThatJson(jsonRepresentation).inPath("value").isEqualTo("my string value");
          assertThatJson(jsonRepresentation).inPath("taskId").isEqualTo("task-id");
          assertThatJson(jsonRepresentation).inPath("processInstanceId").isEqualTo("proc-inst-id");
      }
  
      @Test
      public void convertToEntityAttributeShouldCreateAProcessInstanceWithFieldsSet() throws Exception {
          //given
          String jsonRepresentation =
              "{\"name\":\"var-name\"," +
              "\"type\":\"String\"," +
              "\"value\":\"my string value\"," +
              "\"taskId\":\"task-id\"," +
              "\"processInstanceId\":\"proc-inst-id\"}";
  
          //when
          VariableInstance variableInstance = converter.convertToEntityAttribute(jsonRepresentation);
  
          //then
          assertThat(variableInstance)
              .isNotNull()
              .hasType("String")
              .hasName("var-name")
              .hasValue("my string value")
              .hasProcessInstanceId("proc-inst-id")
              .hasTaskId("task-id");
      }
  
      @Test
      public void converterShouldDealWithDifferentTypes() throws Exception {
          Invoice invoice = new Invoice("inv-id", "customer");
          //given
          VariableInstanceImpl variable = new VariableInstanceImpl(
              "var-name",
              "Invoice",
              invoice,
              "proc-inst-id",
              "task-id"
          );
  
          //when
          String jsonRepresentation = converter.convertToDatabaseColumn(variable);
  
          //then
          assertThatJson(jsonRepresentation).inPath("name").isEqualTo("var-name");
          assertThatJson(jsonRepresentation).inPath("type").isEqualTo("Invoice");
          assertThatJson(jsonRepresentation).inPath("value").isEqualTo(invoice);
          assertThatJson(jsonRepresentation).inPath("taskId").isEqualTo("task-id");
          assertThatJson(jsonRepresentation).inPath("processInstanceId").isEqualTo("proc-inst-id");
      }
  
      private class Invoice implements Serializable {
  
          private String id;
          private String customer;
  
          public Invoice() {}
  
          public Invoice(String id, String customer) {
              this.id = id;
              this.customer = customer;
          }
  
          public String getId() {
              return id;
          }
  
          public void setId(String id) {
              this.id = id;
          }
  
          public String getCustomer() {
              return customer;
          }
  
          public void setCustomer(String customer) {
              this.customer = customer;
          }
      }
  }
  