/*
 * Copyright 2014 Effektif GmbH.
 *
 * 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.effektif.workflow.test.examples;

import junit.framework.TestCase;

import org.junit.Test;

import com.effektif.workflow.api.Configuration;
import com.effektif.workflow.api.activities.Call;
import com.effektif.workflow.api.activities.EmbeddedSubprocess;
import com.effektif.workflow.api.activities.EndEvent;
import com.effektif.workflow.api.activities.ExclusiveGateway;
import com.effektif.workflow.api.activities.HttpServiceTask;
import com.effektif.workflow.api.activities.JavaServiceTask;
import com.effektif.workflow.api.activities.NoneTask;
import com.effektif.workflow.api.activities.ParallelGateway;
import com.effektif.workflow.api.activities.ReceiveTask;
import com.effektif.workflow.api.activities.StartEvent;
import com.effektif.workflow.api.model.WorkflowId;
import com.effektif.workflow.api.workflow.Activity;
import com.effektif.workflow.api.workflow.ExecutableWorkflow;
import com.effektif.workflow.impl.json.JsonStreamMapper;
import com.effektif.workflow.impl.memory.TestConfiguration;

/**
 * Stub for a test of JSON and BPMN output, used to generate samples for documentation.
 *
 * @author Peter Hilton
 */
public class DocumentationExamplesTest extends TestCase {

  private static Configuration configuration;

  @Override
  public void setUp() throws Exception {
    if (configuration == null) {
      configuration = new TestConfiguration();
      configuration.get(JsonStreamMapper.class).pretty();
      configuration.start();
      configuration.getWorkflowEngine();
    }
  }

  @Test
  public void testCall() {
    Call activity = new Call()
      .id("runTests")
      .subWorkflowSource("Run tests")
      .subWorkflowId(new WorkflowId("releaseTests1"));
    activity.setSubWorkflowSource("releaseTests");
    print(activity);
  }

  @Test
  public void testEmbeddedSubprocess() {
    EmbeddedSubprocess activity = new EmbeddedSubprocess();
    activity.setId("phase1");
    print(activity);
  }

  @Test
  public void testEndEvent() {
    EndEvent activity = new EndEvent();
    activity.setId("releaseComplete");
    print(activity);
  }

  @Test
  public void testExclusiveGateway() {
    ExclusiveGateway activity = (ExclusiveGateway) new ExclusiveGateway()
      .id("ok?")
      .defaultTransitionId("proceed");
    print(activity);
  }

  @Test
  public void testHttpServiceTask() {
    HttpServiceTask activity = new HttpServiceTask();
    activity.setId("publishReleaseNotes");
    print(activity);
  }

  @Test
  public void testJavaServiceTask() {
    JavaServiceTask activity = new JavaServiceTask();
    activity.setId("profilePerformance");
    print(activity);
  }

  @Test
  public void testNoneTask() {
    NoneTask activity = new NoneTask();
    activity.setId("verifyRequirements");
    print(activity);
  }

  @Test
  public void testParallelGateway() {
    ParallelGateway activity = new ParallelGateway();
    activity.setId("fork");
    print(activity);
  }

  @Test
  public void testReceiveTask() {
    ReceiveTask activity = new ReceiveTask();
    activity.setId("buildComplete");
    print(activity);
  }

  @Test
  public void testStartEvent() {
    StartEvent activity = new StartEvent();
    activity.setId("codeComplete");
    print(activity);
  }

  private void printJson(Object o) {
    System.out.println("--- " + o.getClass().getSimpleName() + "----------");
    JsonStreamMapper jsonMapper = configuration.get(JsonStreamMapper.class);
    System.out.println(jsonMapper.write(o));
  }

  private void print(Activity activity) {
    printJson(activity);

    ExecutableWorkflow workflow = new ExecutableWorkflow().activity(activity);
    // System.out.println(BpmnWriter.writeBpmnDocumentString(workflow, configuration));
  }
}

@@ -2468,35 +2468,18 @@
 rocess()
-;%0A    activity.setI
+.i
 d(%22phase
@@ -2619,32 +2619,154 @@
 easeComplete%22);%0A
+    activity.setName(%22software released%22);%0A    activity.setDescription(%22Ends the process when the release is complete.%22);%0A
     print(activi
@@ -3076,35 +3076,18 @@
 ceTask()
-;%0A    activity.setI
+.i
 d(%22publi
@@ -3228,35 +3228,18 @@
 ceTask()
-;%0A    activity.setI
+.i
 d(%22profi
@@ -3358,35 +3358,18 @@
 neTask()
-;%0A    activity.setI
+.i
 d(%22verif
@@ -3509,35 +3509,18 @@
 ateway()
-;%0A    activity.setI
+.i
 d(%22fork%22
@@ -3634,35 +3634,18 @@
 veTask()
-;%0A    activity.setI
+.i
 d(%22build
@@ -3761,24 +3761,43 @@
 StartEvent()
+.id(%22codeComplete%22)
 ;%0A    activi
@@ -3806,17 +3806,20 @@
 .set
-Id
+Name
 (%22code
-C
+ c
 ompl
@@ -3817,32 +3817,118 @@
 ode complete%22);%0A
+    activity.setDescription(%22Starts the process when the code is ready to release.%22);%0A
     print(activi

Update API examples