/*
 * Copyright 2017 Google Inc.
 *
 * 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 com.google.cloud.tools.eclipse.appengine.deploy.flex;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.cloud.tools.eclipse.appengine.deploy.StagingDelegate;
import com.google.cloud.tools.eclipse.appengine.facets.AppEngineFlexWarFacet;
import com.google.cloud.tools.eclipse.test.util.project.TestProjectCreator;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jst.common.project.facet.core.JavaFacet;
import org.eclipse.jst.j2ee.web.project.facet.WebFacetUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class FlexWarStagingDelegateTest {

  @Rule public TestProjectCreator projectCreator = new TestProjectCreator().withFacets(
      JavaFacet.VERSION_1_7, WebFacetUtils.WEB_25, AppEngineFlexWarFacet.FACET_VERSION);

  private IProject project;
  private IPath safeWorkDirectory;
  private IPath stagingDirectory;
  private IPath appEngineDirectory;

  @Before
  public void setUp() {
    project = projectCreator.getProject();
    safeWorkDirectory = project.getFolder("safe-work-directory").getLocation();
    stagingDirectory = project.getFolder("staging-result").getLocation();
    appEngineDirectory = project.getFolder("src/main/appengine").getLocation();
  }

  @Test
  public void testStage() {
    StagingDelegate delegate = new FlexWarStagingDelegate(project, appEngineDirectory);
    IStatus status = delegate.stage(stagingDirectory, safeWorkDirectory,
        null, null, new NullProgressMonitor());

    assertTrue(stagingDirectory.append("app-to-deploy.war").toFile().exists());
    assertTrue(stagingDirectory.append("app.yaml").toFile().exists());
    assertTrue(status.isOK());
  }

  @Test
  public void testGetOptionalConfigurationFilesDirectory() {
    StagingDelegate delegate = new FlexWarStagingDelegate(project, appEngineDirectory);

    assertEquals(appEngineDirectory, delegate.getOptionalConfigurationFilesDirectory());
  }
}

@@ -963,24 +963,81 @@
 ectCreator;%0A
+import java.io.PrintWriter;%0Aimport java.io.StringWriter;%0A
 import org.e
@@ -2307,24 +2307,82 @@
 onitor());%0A%0A
+    assertTrue(getStatusAsString(status), status.isOK());%0A
     assertTr
@@ -2526,35 +2526,596 @@
 ;%0A  
-  assertTrue(status.isOK())
+%7D%0A%0A  private static String getStatusAsString(IStatus status) %7B%0A    String stringStatus = status.getSeverity() + %22: %22 + status.getMessage();%0A    if (status.getException() != null) %7B%0A      stringStatus += %22%5Cn==== start of IStatus exception stack trace ====%5Cn%22;%0A      StringWriter stringWriter = new StringWriter();%0A      PrintWriter printWriter = new PrintWriter(stringWriter, true);%0A      status.getException().printStackTrace(printWriter);%0A      stringStatus += stringWriter.toString();%0A      stringStatus += %22==== end of IStatus exception stack trace ====%22;%0A    %7D%0A    return stringStatus
 ;%0A  

Print diagnostic message for FlexWarStagingDelegateTest (#3108)