/* 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.camunda.bpm.engine.test.api.multitenancy.query.history;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.camunda.bpm.engine.exception.NullValueException;
import org.camunda.bpm.engine.history.HistoricDetail;
import org.camunda.bpm.engine.history.HistoricDetailQuery;
import org.camunda.bpm.engine.impl.test.PluggableProcessEngineTestCase;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.instance.ExtensionElements;
import org.camunda.bpm.model.bpmn.instance.UserTask;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaFormData;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaFormField;

public class MultiTenancyHistoricDetailFormPropertyQueryTest extends PluggableProcessEngineTestCase {

  protected final static String TENANT_ONE = "tenant1";
  protected final static String TENANT_TWO = "tenant2";

  @Override
  protected void setUp() throws IOException {
    BpmnModelInstance oneTaskProcess = Bpmn.createExecutableProcess("testProcess")
      .startEvent()
      .userTask("userTask")
      .endEvent()
    .done();

    ExtensionElements extensionElements = oneTaskProcess.newInstance(ExtensionElements.class);
    UserTask userTask = (UserTask) oneTaskProcess.getModelElementById("userTask");
    userTask.setExtensionElements(extensionElements);

    CamundaFormData camundaFormData = oneTaskProcess.newInstance(CamundaFormData.class);
    CamundaFormField camundaFormField = oneTaskProcess.newInstance(CamundaFormField.class);
    camundaFormField.setCamundaId("myFormField");
    camundaFormField.setCamundaType("string");
    camundaFormData.addChildElement(camundaFormField);
    userTask.getExtensionElements().addChildElement(camundaFormData);

    deploymentForTenant(TENANT_ONE, oneTaskProcess);
    deploymentForTenant(TENANT_TWO, oneTaskProcess);

    ProcessInstance processInstanceOne = startProcessInstanceForTenant(TENANT_ONE);
    ProcessInstance processInstanceTwo = startProcessInstanceForTenant(TENANT_TWO);

    completeUserTask(processInstanceOne);
    completeUserTask(processInstanceTwo);
  }

  public void testQueryWithoutTenantId() {
    HistoricDetailQuery query = historyService
        .createHistoricDetailQuery()
        .formFields();

    assertThat(query.count(), is(2L));
  }

  public void testQueryByTenantId() {
    HistoricDetailQuery query = historyService
        .createHistoricDetailQuery()
        .formFields()
        .tenantIdIn(TENANT_ONE);

    assertThat(query.count(), is(1L));

    query = historyService
        .createHistoricDetailQuery()
        .formFields()
        .tenantIdIn(TENANT_TWO);

    assertThat(query.count(), is(1L));
  }

  public void testQueryByTenantIds() {
    HistoricDetailQuery query = historyService
        .createHistoricDetailQuery()
        .formFields()
        .tenantIdIn(TENANT_ONE, TENANT_TWO);

    assertThat(query.count(), is(2L));
  }

  public void testQueryByNonExistingTenantId() {
    HistoricDetailQuery query = historyService
        .createHistoricDetailQuery()
        .formFields()
        .tenantIdIn("nonExisting");

    assertThat(query.count(), is(0L));
  }

  public void testFailQueryByTenantIdNull() {
    try {
      historyService.createHistoricDetailQuery()
        .formFields()
        .tenantIdIn((String) null);

      fail("expected exception");
    } catch (NullValueException e) {
    }
  }

  public void testQuerySortingAsc() {
    List<HistoricDetail> historicDetails = historyService.createHistoricDetailQuery()
        .formFields()
        .orderByTenantId()
        .asc()
        .list();

    assertThat(historicDetails.size(), is(2));
    assertThat(historicDetails.get(0).getTenantId(), is(TENANT_ONE));
    assertThat(historicDetails.get(1).getTenantId(), is(TENANT_TWO));
  }

  public void testQuerySortingDesc() {
    List<HistoricDetail> historicDetails = historyService.createHistoricDetailQuery()
        .formFields()
        .orderByTenantId()
        .desc()
        .list();

    assertThat(historicDetails.size(), is(2));
    assertThat(historicDetails.get(0).getTenantId(), is(TENANT_TWO));
    assertThat(historicDetails.get(1).getTenantId(), is(TENANT_ONE));
  }

  protected ProcessInstance startProcessInstanceForTenant(String tenant) {
    return runtimeService.createProcessInstanceByKey("testProcess")
        .processDefinitionTenantId(tenant)
        .execute();
  }

  protected void completeUserTask(ProcessInstance processInstance) {
    Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
    assertThat(task, is(notNullValue()));

    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("myFormField", "myFormFieldValue");
    formService.submitTaskForm(task.getId(), properties);
  }

}

@@ -1302,260 +1302,8 @@
 nce;
-%0Aimport org.camunda.bpm.model.bpmn.instance.ExtensionElements;%0Aimport org.camunda.bpm.model.bpmn.instance.UserTask;%0Aimport org.camunda.bpm.model.bpmn.instance.camunda.CamundaFormData;%0Aimport org.camunda.bpm.model.bpmn.instance.camunda.CamundaFormField;
 %0A%0Apu
@@ -1715,473 +1715,42 @@
     
-.endEvent()%0A    .done();%0A%0A    ExtensionElements extensionElements = oneTaskProcess.newInstance(ExtensionElements.class);%0A    UserTask userTask = (UserTask) oneTaskProcess.getModelElementById(%22userTask%22);%0A    userTask.setExtensionElements(extensionElements);%0A%0A    CamundaFormData camundaFormData = oneTaskProcess.newInstance(CamundaFormData.class);%0A    CamundaFormField camundaFormField = oneTaskProcess.newInstance(CamundaFormField.class);%0A    camundaFormField.setC
+  .camundaFormField()%0A          .c
 amun
@@ -1772,35 +1772,21 @@
 ld%22)
-;
 %0A    
-camundaFormField.setC
+      .c
 amun
@@ -1805,132 +1805,71 @@
 ng%22)
-;
 %0A    
-camundaFormData.addChildElement(camundaFormField);%0A    userTask.getExtensionElem
+      .camundaFormFieldDone()%0A      .endEv
 ent
-s
 ()
-.addChildElement(camundaFormData
+%0A    .done(
 );%0A%0A

create camunda form field fluently