package ch.ivyteam.ivy.server.newengine;

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

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import ch.ivyteam.ivy.reporting.internal.BirtRuntimeManagerExternal;
import ch.ivyteam.ivy.server.AbstractProjectBasedServerTest;
import ch.ivyteam.util.Pair;
import mockit.Mock;
import mockit.MockUp;

public class TestPi extends AbstractProjectBasedServerTest
{
  private static final File TEST_PROJECT = new File("testProjects/testNewEngine");
  private String designPathFromMock;
  
  @BeforeClass
  public static void setUpBeforeClass() throws Exception
  {
    AbstractProjectBasedServerTest.setUp(TEST_PROJECT);
  }
  
  @Before
  @SuppressWarnings("unused")
  public void beforeTest()
  {
    new BirtServerMock();
  }

  @Test
  /*
   * Test the provided Birt extension {@link ch.ivyteam.ivy.reporting.extension.BirtReportGeneratorPI}
   * Mocked on server
   */
  public void testProvidedBirtExtension() throws Exception
  {
    String designName = "TechnicalReport.rptdesign";
    File designPath = new File(TEST_PROJECT, "/webContent/customFiles/" + designName);
    startProcessInNewEngineOverHttpWithBirtPathsProvided("providedBirtExtension", createUrlPath(designPath.toURI()));
    assertThat(designPathFromMock).endsWith(designName);
  }
  
  private String createUrlPath(URI uri)
  {
    return StringUtils.removeStart(uri.getPath(), "/");
  }
  
  private String startProcessInNewEngineOverHttpWithBirtPathsProvided(String processName, String birtDesignPath) throws Exception, IOException
  {
    @SuppressWarnings("unchecked")
    Pair<String, String>[] startParameters = new Pair[]{new Pair<String, String>("designPath", birtDesignPath)};
    URI processStartUri = processStartHelper.getProcessRequestUri(getTestProcessModelVersion(), processName, startParameters);
    return processStartHelper.executeHttpRequestAndGetContent(processStartUri);
  }
  
  public class BirtServerMock extends MockUp<BirtRuntimeManagerExternal>
  {
    @SuppressWarnings("unused")
    @Mock
    public void createSimpleReport(String designPath, List<String> formats, File reportPath, String reportName, Map<String, Object> parameterMap)
    {
      designPathFromMock = designPath;
    }
  }

}

@@ -318,24 +318,50 @@
 foreClass;%0D%0A
+import org.junit.Ignore;%0D%0A
 import org.j
@@ -582,16 +582,120 @@
 kUp;%0D%0A%0D%0A
+@Ignore(%22to much ivy test environment involved -%3E easier and more effective to test in eclipse RCPTT%22)%0D%0A
 public c

make build pass