/* 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.model.xml.testmodel;

import org.camunda.bpm.model.xml.ModelInstance;
import org.camunda.bpm.model.xml.impl.ModelInstanceImpl;
import org.camunda.bpm.model.xml.instance.ModelElementInstance;
import org.camunda.bpm.model.xml.testmodel.instance.Animals;
import org.camunda.bpm.model.xml.testmodel.instance.Bird;
import org.junit.Test;

import static org.fest.assertions.Assertions.assertThat;

/**
 * @author Daniel Meyer
 *
 */
public class ModelElementInstanceTest {

  @Test
  public void shouldReturnTextContent() {
    ModelInstance modelInstance = new TestModelParser()
      .parseModelFromStream(getClass().getResourceAsStream("ModelElementInstanceTest.textContent.xml"));

    ModelElementInstance tweety = modelInstance.getModelElementById("tweety");
    assertThat(tweety.getTextContent()).isEqualTo("");

    ModelElementInstance donald = modelInstance.getModelElementById("donald");
    assertThat(donald.getTextContent()).isEqualTo("some text content");

    ModelElementInstance daisy = modelInstance.getModelElementById("daisy");
    assertThat(daisy.getTextContent()).isEqualTo("some text content with outer line breaks");

    ModelElementInstance hedwig = modelInstance.getModelElementById("hedwig");
    assertThat(hedwig.getTextContent()).isEqualTo("some text content with inner\n        line breaks");
  }

  @Test
  public void shouldReturnRawTextContent() {
    ModelInstance modelInstance = new TestModelParser()
      .parseModelFromStream(getClass().getResourceAsStream("ModelElementInstanceTest.textContent.xml"));

    ModelElementInstance tweety = modelInstance.getModelElementById("tweety");
    assertThat(tweety.getRawTextContent()).isEqualTo("");

    ModelElementInstance donald = modelInstance.getModelElementById("donald");
    assertThat(donald.getRawTextContent()).isEqualTo("some text content");

    ModelElementInstance daisy = modelInstance.getModelElementById("daisy");
    assertThat(daisy.getRawTextContent()).isEqualTo("\n        some text content with outer line breaks\n    ");

    ModelElementInstance hedwig = modelInstance.getModelElementById("hedwig");
    assertThat(hedwig.getRawTextContent()).isEqualTo("\n        some text content with inner\n        line breaks\n    ");
  }

  private static Bird createBird(ModelInstance modelInstance, String id, Gender gender) {
    Bird bird = modelInstance.newInstance(Bird.class);
    bird.setId(id);
    bird.setGender(gender);
    ((Animals) modelInstance.getDocumentElement()).getAnimals().add(bird);
    return bird;
  }

  @Test
  public void shouldUpdateReference() {
    TestModelParser modelParser = new TestModelParser();
    ModelInstance modelInstance = modelParser.getEmptyModel();
    Animals animals = modelInstance.newInstance(Animals.class);
    modelInstance.setDocumentElement(animals);

    // create some birds
    Bird tweety = createBird(modelInstance, "tweety", Gender.Female);
    Bird fiffy = createBird(modelInstance, "fiffy", Gender.Female);
    Bird daffy = createBird(modelInstance, "daffy", Gender.Male);
    Bird timmy = createBird(modelInstance, "timmy", Gender.Unknown);

    // set references
    assertThat(tweety.getMother()).isNull();
    tweety.setMother(fiffy);
    assertThat(tweety.getMother()).isNotNull();
    assertThat(tweety.getMother()).isEqualTo(fiffy);

    assertThat(tweety.getFather()).isNull();
    tweety.setFather(daffy);
    assertThat(tweety.getFather()).isNotNull();
    assertThat(tweety.getFather()).isEqualTo(daffy);

    // change ids
    fiffy.setId("mother");
    assertThat(tweety.getMother()).isEqualTo(fiffy);

    daffy.setId("father");
    assertThat(tweety.getFather()).isEqualTo(daffy);

    // replace birds
    fiffy.replaceWithElement(timmy);
    assertThat(tweety.getMother()).isEqualTo(timmy);
    timmy.replaceWithElement(fiffy);
    assertThat(tweety.getMother()).isEqualTo(fiffy);

    daffy.replaceWithElement(timmy);
    assertThat(tweety.getFather()).isEqualTo(timmy);
    timmy.replaceWithElement(daffy);
    assertThat(tweety.getFather()).isEqualTo(daffy);

    // remove id
    fiffy.removeAttribute("id");
    assertThat(tweety.getMother()).isNull();

    daffy.removeAttribute("id");
    assertThat(tweety.getFather()).isNull();

    // validate model
    modelParser.validateModel(((ModelInstanceImpl) modelInstance).getDocument());
  }

}

@@ -2814,2096 +2814,6 @@
  %7D%0A%0A
-  private static Bird createBird(ModelInstance modelInstance, String id, Gender gender) %7B%0A    Bird bird = modelInstance.newInstance(Bird.class);%0A    bird.setId(id);%0A    bird.setGender(gender);%0A    ((Animals) modelInstance.getDocumentElement()).getAnimals().add(bird);%0A    return bird;%0A  %7D%0A%0A  @Test%0A  public void shouldUpdateReference() %7B%0A    TestModelParser modelParser = new TestModelParser();%0A    ModelInstance modelInstance = modelParser.getEmptyModel();%0A    Animals animals = modelInstance.newInstance(Animals.class);%0A    modelInstance.setDocumentElement(animals);%0A%0A    // create some birds%0A    Bird tweety = createBird(modelInstance, %22tweety%22, Gender.Female);%0A    Bird fiffy = createBird(modelInstance, %22fiffy%22, Gender.Female);%0A    Bird daffy = createBird(modelInstance, %22daffy%22, Gender.Male);%0A    Bird timmy = createBird(modelInstance, %22timmy%22, Gender.Unknown);%0A%0A    // set references%0A    assertThat(tweety.getMother()).isNull();%0A    tweety.setMother(fiffy);%0A    assertThat(tweety.getMother()).isNotNull();%0A    assertThat(tweety.getMother()).isEqualTo(fiffy);%0A%0A    assertThat(tweety.getFather()).isNull();%0A    tweety.setFather(daffy);%0A    assertThat(tweety.getFather()).isNotNull();%0A    assertThat(tweety.getFather()).isEqualTo(daffy);%0A%0A    // change ids%0A    fiffy.setId(%22mother%22);%0A    assertThat(tweety.getMother()).isEqualTo(fiffy);%0A%0A    daffy.setId(%22father%22);%0A    assertThat(tweety.getFather()).isEqualTo(daffy);%0A%0A    // replace birds%0A    fiffy.replaceWithElement(timmy);%0A    assertThat(tweety.getMother()).isEqualTo(timmy);%0A    timmy.replaceWithElement(fiffy);%0A    assertThat(tweety.getMother()).isEqualTo(fiffy);%0A%0A    daffy.replaceWithElement(timmy);%0A    assertThat(tweety.getFather()).isEqualTo(timmy);%0A    timmy.replaceWithElement(daffy);%0A    assertThat(tweety.getFather()).isEqualTo(daffy);%0A%0A    // remove id%0A    fiffy.removeAttribute(%22id%22);%0A    assertThat(tweety.getMother()).isNull();%0A%0A    daffy.removeAttribute(%22id%22);%0A    assertThat(tweety.getFather()).isNull();%0A%0A    // validate model%0A    modelParser.validateModel(((ModelInstanceImpl) modelInstance).getDocument());%0A  %7D%0A%0A
 %7D%0A

remove obsolete test