package br.uem.din.architectureEvolution.helpers;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Collection;

import org.junit.BeforeClass;
import org.junit.Test;

import br.uem.din.architectureEvolution.helpers.ModelHelperFactory;
import br.uem.din.architectureEvolution.helpers.StereotypeHelper;

import com.sdmetrics.model.MetaModel;
import com.sdmetrics.model.ModelElement;

public class StereotypeHelperTest {

	private static final String metaModelURL = "test/br/uem/din/architectureEvolution/resources/metamodel_poseidon4.xml";
	private static MetaModel metaModel;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		metaModel = ModelHelperFactory.getModelHelper().getMetaModel(metaModelURL);
	}

	@Test
	public void isVariationPoint_withoutVariationPoint_returnFalse() {
		ModelElement modelElement = generateModelElementByType("stereotype");
		boolean result = StereotypeHelper.isVariationPoint(modelElement);
		assertEquals("isVariationPoint should return false", false, result);
	}
	
	@Test
	public void isVariationPoint_withVariationPoint_returnTrue() {
		ModelElement modelElement = generateModelElementByType("stereotype");
		modelElement.setAttribute("name", "variationPoint");
		boolean result = StereotypeHelper.isVariationPoint(modelElement);
		assertEquals("isVariationPoint should return true", true, result);
	}

	@Test
	public void isFeature_withoutFeature_returnFalse() {
		ModelElement modelElement = generateModelElementByType("stereotype");
		boolean result = StereotypeHelper.isFeature(modelElement);
		assertEquals("isFeature should return false", false, result);
	}
	
	@Test
	public void isFeature_withFeature_returnTrue() {
		ModelElement modelElement = createElementWithStereotype("feature");
		
		boolean result = StereotypeHelper.isFeature(modelElement);
		assertEquals("isFeature should return true", true, result);
	}

	@Test
	public void isVariability_withoutVariability_returnFalse() {
		ModelElement modelElement = generateModelElementByType("stereotype");
		boolean result = StereotypeHelper.isVariability(modelElement);
		assertEquals("isVariability should return false", false, result);
	}
	
	@Test
	public void isVariability_withVariability_returnTrue() {
		ModelElement modelElement = createElementWithStereotype("variability");
		
		boolean result = StereotypeHelper.isVariability(modelElement);
		assertEquals("isVariability should return true", true, result);
	}

	@Test
	public void isConcern_withoutConcern_returnFalse() {
		ModelElement modelElement = generateModelElementByType("stereotype");
		boolean result = StereotypeHelper.isConcern(modelElement);
		assertEquals("isConcern should return false", false, result);
	}
	
	@Test
	public void isConcern_withConcern_returnTrue() {
		ModelElement modelElement = createElementWithStereotype("concern");
		
		boolean result = StereotypeHelper.isConcern(modelElement);
		assertEquals("isConcern should return true", true, result);
	}
	
	private ModelElement createElementWithStereotype(String stereotypeName) {
		ModelElement modelElement = generateModelElementByType("stereotype");
		
		ModelElement stereotype = generateModelElementByType("stereotype");
		stereotype.setAttribute("name", stereotypeName);
		
		Collection<ModelElement> stereotypes = new ArrayList<ModelElement>();
		stereotypes.add(stereotype);
		modelElement.setSetAttribute("stereotypes", stereotypes);
		return modelElement;
	}
	
	private ModelElement generateModelElementByType(String typeName) {
		ModelElement modelElement = new ModelElement(metaModel.getType(typeName));
		return modelElement;
	}
}
