package org.camunda.bpm.spring.boot.starter.example.dmn.rest;

import static org.assertj.core.api.Assertions.assertThat;

import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.impl.util.json.JSONArray;
import org.camunda.bpm.engine.repository.DecisionDefinition;
import org.camunda.bpm.spring.boot.starter.CamundaBpmProperties;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@Ignore("org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?")
// FIXME did bot run with sb1.4
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { DmnRestApplication.class })
@WebAppConfiguration
@IntegrationTest({ "server.port=0" })
@DirtiesContext
public class DmnRestApplicationTest {

  private static final String CHECK_ORDER = "checkOrder";

  @Value("${local.server.port}")
  private int port;

  @Autowired
  private CamundaBpmProperties camundaBpmProperties;

  @Autowired
  private RepositoryService repositoryService;

  @Test
  public void deploys_orderDiscount_dmn() {
    final DecisionDefinition definition = repositoryService.createDecisionDefinitionQuery().decisionDefinitionKey(CHECK_ORDER).singleResult();
    assertThat(definition).isNotNull();
  }

  @Test
  public void evaluate_checkOrder() throws InterruptedException {
    String url = String.format("http://localhost:%d/rest/engine/%s/decision-definition/key/%s/evaluate", port, camundaBpmProperties.getProcessEngineName(),
        CHECK_ORDER);

    String JSONInput = "{\n" + "  \"variables\" : {\n" + "    \"status\" : { \"value\" : \"silver\", \"type\" : \"String\" },\n"
        + "    \"sum\" : { \"value\" : 900, \"type\" : \"Integer\" }\n" + "  }\n" + "}\n";

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<String> request = new HttpEntity<String>(JSONInput, headers);

    final String check = new TestRestTemplate().postForObject(url, request, String.class);

    assertThat(new JSONArray(check).getJSONObject(0).getJSONObject("result").getString("value")).isEqualTo("ok");

  }

}

@@ -350,33 +350,8 @@
 es;%0A
-import org.junit.Ignore;%0A
 impo
@@ -561,27 +561,34 @@
 ot.test.
-Integration
+context.SpringBoot
 Test;%0Aim
@@ -626,38 +626,45 @@
 est.
-SpringApplicationConfiguration
+context.SpringBootTest.WebEnvironment
 ;%0Aim
@@ -975,333 +975,16 @@
 ring
-JUnit4ClassRunner;%0Aimport org.springframework.test.context.web.WebAppConfiguration;%0A%0A@Ignore(%22org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?%22)%0A// FIXME did bot run with sb1.4
+Runner;%0A
 %0A@Ru
@@ -999,19 +999,8 @@
 ring
-JUnit4Class
 Runn
@@ -1020,32 +1020,16 @@
 ring
-ApplicationConfiguration
+BootTest
 (cla
@@ -1067,67 +1067,53 @@
 ss %7D
-)%0A@WebAppConfiguration%0A@IntegrationTest(%7B %22server.port=0%22 %7D
+, webEnvironment = WebEnvironment.RANDOM_PORT
 )%0A@D

fix DmnRestApplicationTest #117