/*
 *  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.rest.history;

import org.camunda.bpm.engine.history.HistoricDecisionInstanceStatistics;
import org.camunda.bpm.engine.history.HistoricDecisionInstanceStatisticsQuery;
import org.camunda.bpm.engine.rest.AbstractRestServiceTest;
import org.camunda.bpm.engine.rest.helper.MockProvider;
import org.camunda.bpm.engine.rest.util.container.TestContainerRule;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import javax.ws.rs.core.Response;
import java.util.List;

import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author Askar Akhmerov
 */
public class HistoricDecisionStatisticsRestServiceQueryTest extends AbstractRestServiceTest {

  @ClassRule
  public static TestContainerRule rule = new TestContainerRule();

  protected static final String HISTORY_URL = TEST_RESOURCE_ROOT_PATH + "/history";
  protected static final String HISTORIC_DECISION_STATISTICS_URL = HISTORY_URL + "/decision-requirements-definition/{id}/statistics";

  private HistoricDecisionInstanceStatisticsQuery historicDecisionInstanceStatisticsQuery;

  @Before
  public void setUpRuntimeData() {
    setupHistoricDecisionStatisticsMock();
  }

  private void setupHistoricDecisionStatisticsMock() {
    List<HistoricDecisionInstanceStatistics> mocks = MockProvider.createMockHistoricDecisionStatistics();

    historicDecisionInstanceStatisticsQuery = mock(HistoricDecisionInstanceStatisticsQuery.class);
    when(processEngine.getHistoryService()
        .createHistoricDecisionInstanceStatisticsQuery(eq(MockProvider.EXAMPLE_DECISION_REQUIREMENTS_DEFINITION_ID)))
    .thenReturn(historicDecisionInstanceStatisticsQuery);

    when(historicDecisionInstanceStatisticsQuery.decisionInstanceId(MockProvider.EXAMPLE_DECISION_INSTANCE_ID)).thenReturn(historicDecisionInstanceStatisticsQuery);
    when(historicDecisionInstanceStatisticsQuery.list()).thenReturn(mocks);
  }

  @Test
  public void testHistoricDefinitionInstanceStatisticsRetrieval() {
    given().pathParam("id", MockProvider.EXAMPLE_DECISION_REQUIREMENTS_DEFINITION_ID)
        .then().expect()
        .statusCode(Response.Status.OK.getStatusCode())
          .body("$.size()", is(2))
          .body("decisionDefinitionKey", hasItems(MockProvider.EXAMPLE_DECISION_DEFINITION_KEY, MockProvider.ANOTHER_DECISION_DEFINITION_KEY))
          .body("evaluations", hasItems(1, 2))
        .when().get(HISTORIC_DECISION_STATISTICS_URL);
  }

  @Test
  public void testHistoricDefinitionInstanceStatisticsRetrievalWithDefinitionInstance() {
    given().pathParam("id", MockProvider.EXAMPLE_DECISION_REQUIREMENTS_DEFINITION_ID)
        .queryParam("decisionInstanceId", MockProvider.EXAMPLE_DECISION_INSTANCE_ID)
        .then().expect()
        .statusCode(Response.Status.OK.getStatusCode())
          .body("$.size()", is(2))
          .body("decisionDefinitionKey", hasItems(MockProvider.EXAMPLE_DECISION_DEFINITION_KEY, MockProvider.ANOTHER_DECISION_DEFINITION_KEY))
          .body("evaluations", hasItems(1, 2))
        .when().get(HISTORIC_DECISION_STATISTICS_URL);
  }
}

@@ -949,24 +949,48 @@
 tainerRule;%0A
+import org.junit.After;%0A
 import org.j
@@ -1002,16 +1002,16 @@
 Before;%0A
-
 import o
@@ -1052,16 +1052,44 @@
 it.Test;
+%0Aimport org.mockito.Mockito;
 %0A%0Aimport
@@ -1361,16 +1361,58 @@
 o.mock;%0A
+import static org.mockito.Mockito.verify;%0A
 import s
@@ -2057,16 +2057,152 @@
 );%0A  %7D%0A%0A
+  @After%0A  public void tearDown() %7B%0A    Mockito.reset(processEngine.getHistoryService(), historicDecisionInstanceStatisticsQuery);%0A  %7D%0A%0A
   privat
@@ -3438,24 +3438,176 @@
 ISTICS_URL);
+%0A%0A    verify(processEngine.getHistoryService()).createHistoricDecisionInstanceStatisticsQuery(MockProvider.EXAMPLE_DECISION_REQUIREMENTS_DEFINITION_ID);
 %0A  %7D%0A%0A  @Tes
@@ -4167,32 +4167,32 @@
 hasItems(1, 2))%0A
-
         .when().
@@ -4229,15 +4229,282 @@
 CS_URL);
+%0A%0A    verify(processEngine.getHistoryService()).createHistoricDecisionInstanceStatisticsQuery(MockProvider.EXAMPLE_DECISION_REQUIREMENTS_DEFINITION_ID);%0A    verify(historicDecisionInstanceStatisticsQuery).decisionInstanceId(MockProvider.EXAMPLE_DECISION_INSTANCE_ID);
 %0A  %7D%0A%7D%0A

add verify on query